Skip to content

Commit 4167d5d

Browse files
committed
fix: enhance RTL support with improved text alignment and padding
1 parent 84a0223 commit 4167d5d

File tree

1 file changed

+43
-53
lines changed

1 file changed

+43
-53
lines changed

src/GooglePlacesTextInput.js

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ const GooglePlacesTextInput = forwardRef(
158158

159159
// RTL detection logic
160160
const isRTL =
161-
forceRTL !== undefined
162-
? forceRTL
163-
: isRTLText(placeHolderText) || I18nManager.isRTL;
161+
forceRTL !== undefined ? forceRTL : isRTLText(placeHolderText);
164162

165163
const renderSuggestion = ({ item }) => {
166164
const { mainText, secondaryText } = item.placePrediction.structuredFormat;
@@ -182,7 +180,7 @@ const GooglePlacesTextInput = forwardRef(
182180
style={[
183181
styles.mainText,
184182
style.suggestionText?.main,
185-
{ textAlign: isRTL ? 'right' : 'left' },
183+
getTextAlign(),
186184
]}
187185
>
188186
{mainText.text}
@@ -192,7 +190,7 @@ const GooglePlacesTextInput = forwardRef(
192190
style={[
193191
styles.secondaryText,
194192
style.suggestionText?.secondary,
195-
{ textAlign: isRTL ? 'right' : 'left' },
193+
getTextAlign(),
196194
]}
197195
>
198196
{secondaryText.text}
@@ -221,6 +219,40 @@ const GooglePlacesTextInput = forwardRef(
221219
);
222220
};
223221

222+
const getPadding = () => {
223+
const physicalRTL = I18nManager.isRTL;
224+
const clearButtonPadding = showClearButton ? 75 : 45;
225+
if (isRTL !== physicalRTL) {
226+
return {
227+
paddingStart: clearButtonPadding,
228+
paddingEnd: 15,
229+
};
230+
}
231+
return {
232+
paddingStart: 15,
233+
paddingEnd: clearButtonPadding,
234+
};
235+
};
236+
237+
const getTextAlign = () => {
238+
const isDeviceRTL = I18nManager.isRTL;
239+
if (isDeviceRTL) {
240+
// Device is RTL, so "left" and "right" are swapped
241+
return { textAlign: isRTL ? 'left' : 'right' };
242+
} else {
243+
// Device is LTR, normal behavior
244+
return { textAlign: isRTL ? 'right' : 'left' };
245+
}
246+
};
247+
248+
const getIconPosition = (paddingValue) => {
249+
const physicalRTL = I18nManager.isRTL;
250+
if (isRTL !== physicalRTL) {
251+
return { start: paddingValue };
252+
}
253+
return { end: paddingValue };
254+
};
255+
224256
return (
225257
<View style={[styles.container, style.container]}>
226258
<View>
@@ -229,13 +261,8 @@ const GooglePlacesTextInput = forwardRef(
229261
style={[
230262
styles.input,
231263
style.input,
232-
{
233-
// Icons are on the left when RTL, so add more padding on left
234-
paddingLeft: isRTL ? (showClearButton ? 75 : 45) : 15,
235-
// Icons are on the right when LTR, so add more padding on right
236-
paddingRight: isRTL ? 15 : showClearButton ? 75 : 45,
237-
textAlign: isRTL ? 'right' : 'left',
238-
},
264+
getPadding(),
265+
{ textAlign: isRTL ? 'right' : 'left' },
239266
]}
240267
placeholder={placeHolderText}
241268
placeholderTextColor={style.placeholder?.color || '#666666'}
@@ -249,7 +276,7 @@ const GooglePlacesTextInput = forwardRef(
249276
{/* Clear button - shown only if showClearButton is true */}
250277
{showClearButton && inputText !== '' && (
251278
<TouchableOpacity
252-
style={[isRTL ? styles.leftIcon : styles.rightIcon]}
279+
style={[styles.clearButton, getIconPosition(12)]}
253280
onPress={() => {
254281
setInputText('');
255282
setPredictions([]);
@@ -273,12 +300,7 @@ const GooglePlacesTextInput = forwardRef(
273300
{/* Loading indicator - position adjusts based on showClearButton */}
274301
{loading && showLoadingIndicator && (
275302
<ActivityIndicator
276-
style={[
277-
isRTL ? styles.leftLoadingIcon : styles.rightLoadingIcon,
278-
!showClearButton &&
279-
(isRTL ? styles.leftEdge : styles.rightEdge),
280-
styles.loadingIndicator,
281-
]}
303+
style={[styles.loadingIndicator, getIconPosition(45)]}
282304
size={'small'}
283305
color={style.loadingIndicator?.color || '#000000'}
284306
/>
@@ -303,14 +325,6 @@ const styles = StyleSheet.create({
303325
backgroundColor: 'white',
304326
fontSize: 16,
305327
},
306-
loadingIndicator: {
307-
position: 'absolute',
308-
top: '50%',
309-
transform: [{ translateY: -10 }],
310-
},
311-
rtlText: {
312-
writingDirection: 'rtl',
313-
},
314328
suggestionsContainer: {
315329
backgroundColor: '#efeff1', // default background
316330
borderRadius: 6,
@@ -334,40 +348,16 @@ const styles = StyleSheet.create({
334348
marginTop: 2,
335349
textAlign: 'left',
336350
},
337-
rightAligned: {
338-
right: 15,
339-
},
340-
rightIcon: {
351+
clearButton: {
341352
position: 'absolute',
342353
top: '50%',
343354
transform: [{ translateY: -13 }],
344-
right: 12,
345355
padding: 0,
346356
},
347-
leftIcon: {
348-
position: 'absolute',
349-
top: '50%',
350-
transform: [{ translateY: -13 }],
351-
left: 12,
352-
padding: 0,
353-
},
354-
rightLoadingIcon: {
355-
position: 'absolute',
356-
top: '50%',
357-
transform: [{ translateY: -10 }],
358-
right: 45,
359-
},
360-
leftLoadingIcon: {
357+
loadingIndicator: {
361358
position: 'absolute',
362359
top: '50%',
363360
transform: [{ translateY: -10 }],
364-
left: 45,
365-
},
366-
rightEdge: {
367-
right: 12,
368-
},
369-
leftEdge: {
370-
left: 12,
371361
},
372362
iOSclearButton: {
373363
fontSize: 18,

0 commit comments

Comments
 (0)