Skip to content

Commit f14389c

Browse files
committed
feat: add forceRTL prop and improve RTL handling
1 parent dfcd53a commit f14389c

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ A customizable React Native TextInput component for Google Places Autocomplete u
55
## Features
66

77
- 🎨 Fully customizable UI
8-
- 🌍 RTL support
98
- ⌨️ Debounced search
10-
- 🔄 Loading indicators
9+
- 🗑️ Clear button support
10+
- 🔄 Loading indicator
1111
- 📱 Keyboard-aware
12-
- 🎯 TypeScript support
1312
- 🔍 Custom place types filtering
13+
- 🌍 RTL support
1414
- 🌐 Multi-language support
15+
- 🎯 TypeScript support
1516

1617
## Preview
1718

@@ -165,6 +166,7 @@ const StyledExample = () => {
165166
| style | StyleProp | No | {} | Custom styles object |
166167
| showLoadingIndicator | boolean | No | true | Show/hide loading indicator |
167168
| showClearButton | boolean | No | true | Show/hide the input clear button |
169+
| forceRTL | boolean | No | undefined | Force RTL layout direction |
168170
| **Event Handlers** |
169171
| onPlaceSelect | (place: Place \| null) => void | Yes | - | Callback when place is selected |
170172
| onTextChange | (text: string) => void | No | - | Callback triggered on text input changes |

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"type": "git",
6565
"url": "git+https://github.com/amitpdev/react-native-google-places-textinput.git"
6666
},
67-
"author": "Amit Palomo <[email protected]> (https://github.com/amitpdev)",
67+
"author": "Amit Palomo <[email protected]> (https://github.com/amitpdev)",
6868
"license": "MIT",
6969
"bugs": {
7070
"url": "https://github.com/amitpdev/react-native-google-places-textinput/issues"

src/GooglePlacesTextInput.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020

2121
const DEFAULT_GOOGLE_API_URL =
2222
'https://places.googleapis.com/v1/places:autocomplete';
23+
2324
const GooglePlacesTextInput = forwardRef(
2425
(
2526
{
@@ -37,6 +38,7 @@ const GooglePlacesTextInput = forwardRef(
3738
debounceDelay = 200,
3839
showLoadingIndicator = true,
3940
showClearButton = true,
41+
forceRTL = undefined,
4042
style = {},
4143
},
4244
ref
@@ -154,8 +156,11 @@ const GooglePlacesTextInput = forwardRef(
154156
}
155157
};
156158

157-
// Update text alignment based on language
158-
const isRTL = I18nManager.isRTL;
159+
// RTL detection logic
160+
const isRTL =
161+
forceRTL !== undefined
162+
? forceRTL
163+
: isRTLText(placeHolderText) || I18nManager.isRTL;
159164

160165
const renderSuggestion = ({ item }) => {
161166
const { mainText, secondaryText } = item.placePrediction.structuredFormat;
@@ -177,7 +182,7 @@ const GooglePlacesTextInput = forwardRef(
177182
style={[
178183
styles.mainText,
179184
style.suggestionText?.main,
180-
isRTL && styles.rtlText,
185+
{ textAlign: isRTL ? 'right' : 'left' },
181186
]}
182187
>
183188
{mainText.text}
@@ -187,7 +192,7 @@ const GooglePlacesTextInput = forwardRef(
187192
style={[
188193
styles.secondaryText,
189194
style.suggestionText?.secondary,
190-
isRTL && styles.rtlText,
195+
{ textAlign: isRTL ? 'right' : 'left' },
191196
]}
192197
>
193198
{secondaryText.text}
@@ -224,7 +229,13 @@ const GooglePlacesTextInput = forwardRef(
224229
style={[
225230
styles.input,
226231
style.input,
227-
{ paddingRight: showClearButton ? 75 : 45 }, // Adjust padding based on clear button visibility
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+
},
228239
]}
229240
placeholder={placeHolderText}
230241
placeholderTextColor={style.placeholder?.color || '#666666'}
@@ -386,4 +397,14 @@ const styles = StyleSheet.create({
386397
},
387398
});
388399

400+
const isRTLText = (text) => {
401+
if (!text) return false;
402+
// Hebrew: \u0590-\u05FF
403+
// Arabic: \u0600-\u06FF, \u0750-\u077F (Arabic Supplement), \u0870-\u089F (Arabic Extended-B)
404+
// Arabic Presentation Forms: \uFB50-\uFDFF, \uFE70-\uFEFF
405+
const rtlRegex =
406+
/[\u0590-\u05FF\u0600-\u06FF\u0750-\u077F\u0870-\u089F\uFB50-\uFDFF\uFE70-\uFEFF]/;
407+
return rtlRegex.test(text);
408+
};
409+
389410
export default GooglePlacesTextInput;

0 commit comments

Comments
 (0)