|
1 | | -# react-native-google-places-textinput |
| 1 | +# React Native Google Places TextInput |
2 | 2 |
|
3 | | -A customizable React Native component for Google Places Autocomplete TextInput using Places API (New) |
| 3 | +A customizable React Native TextInput component for Google Places Autocomplete using the Places API (New) |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🎨 Fully customizable UI |
| 8 | +- 🌍 RTL support |
| 9 | +- ⌨️ Debounced search |
| 10 | +- 🔄 Loading indicators |
| 11 | +- 📱 Keyboard-aware |
| 12 | +- 🎯 TypeScript support |
| 13 | +- 🔍 Custom place types filtering |
| 14 | +- 🌐 Multi-language support |
4 | 15 |
|
5 | 16 | ## Installation |
6 | 17 |
|
7 | | -```sh |
| 18 | +```bash |
8 | 19 | npm install react-native-google-places-textinput |
| 20 | +# or |
| 21 | +yarn add react-native-google-places-textinput |
9 | 22 | ``` |
10 | 23 |
|
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +- Get a Google Places API key from the [Google Cloud Console](https://console.cloud.google.com/) |
| 27 | +- Enable Places API (New) in your Google Cloud Project |
| 28 | + |
11 | 29 | ## Usage |
12 | 30 |
|
| 31 | +```javascript |
| 32 | +import GooglePlacesTextInput from 'react-native-google-places-textinput'; |
13 | 33 |
|
14 | | -```js |
15 | | -import { multiply } from 'react-native-google-places-textinput'; |
| 34 | +const YourComponent = () => { |
| 35 | + const handlePlaceSelect = (place) => { |
| 36 | + if (place) { |
| 37 | + console.log('Selected place:', place); |
| 38 | + } |
| 39 | + }; |
16 | 40 |
|
17 | | -// ... |
| 41 | + // Example with custom styles |
| 42 | + const customStyles = { |
| 43 | + container: { |
| 44 | + width: '100%', |
| 45 | + marginHorizontal: 0, |
| 46 | + }, |
| 47 | + input: { |
| 48 | + height: 45, |
| 49 | + borderColor: '#ccc', |
| 50 | + borderRadius: 8, |
| 51 | + }, |
| 52 | + suggestionsContainer: { |
| 53 | + backgroundColor: '#ffffff', |
| 54 | + maxHeight: 250, |
| 55 | + }, |
| 56 | + suggestionItem: { |
| 57 | + padding: 15, |
| 58 | + }, |
| 59 | + suggestionText: { |
| 60 | + main: { |
| 61 | + fontSize: 16, |
| 62 | + color: '#333', |
| 63 | + }, |
| 64 | + secondary: { |
| 65 | + fontSize: 14, |
| 66 | + color: '#666', |
| 67 | + } |
| 68 | + }, |
| 69 | + loadingIndicator: { |
| 70 | + color: '#999', |
| 71 | + }, |
| 72 | + placeholder: { |
| 73 | + color: '#999', |
| 74 | + } |
| 75 | + }; |
18 | 76 |
|
19 | | -const result = await multiply(3, 7); |
| 77 | + return ( |
| 78 | + <GooglePlacesTextInput |
| 79 | + apiKey="YOUR_GOOGLE_PLACES_API_KEY" |
| 80 | + placeHolderText="Search for a place" |
| 81 | + onPlaceSelect={handlePlaceSelect} |
| 82 | + languageCode="en" |
| 83 | + style={customStyles} |
| 84 | + /> |
| 85 | + ); |
| 86 | +}; |
20 | 87 | ``` |
21 | 88 |
|
| 89 | +## Props |
| 90 | + |
| 91 | +| Prop | Type | Required | Default | Description | |
| 92 | +|------|------|----------|---------|-------------| |
| 93 | +| apiKey | string | Yes | - | Your Google Places API key | |
| 94 | +| value | string | No | '' | Initial input value | |
| 95 | +| placeHolderText | string | No | - | Placeholder text for input | |
| 96 | +| languageCode | string | No | - | Language code (e.g., 'en', 'fr') | |
| 97 | +| includedRegionCodes | string[] | No | - | Array of region codes to filter results | |
| 98 | +| types | string[] | No | [] | Array of place types to filter | |
| 99 | +| biasPrefixText | string | No | - | Text to prepend to search query | |
| 100 | +| minCharsToFetch | number | No | 1 | Minimum characters before fetching | |
| 101 | +| onPlaceSelect | (place: Place \| null) => void | Yes | - | Callback when place is selected | |
| 102 | +| debounceDelay | number | No | 200 | Delay before triggering search | |
| 103 | +| showLoadingIndicator | boolean | No | true | Show/hide loading indicator | |
| 104 | +| style | StyleProp | No | {} | Custom styles object | |
| 105 | + |
| 106 | +## Methods |
| 107 | + |
| 108 | +The component exposes the following methods through refs: |
| 109 | + |
| 110 | +- `clear()`: Clears the input and suggestions |
| 111 | +- `focus()`: Focuses the input field |
| 112 | + |
| 113 | +```javascript |
| 114 | +const inputRef = useRef(null); |
| 115 | + |
| 116 | +// Usage |
| 117 | +inputRef.current?.clear(); |
| 118 | +inputRef.current?.focus(); |
| 119 | +``` |
| 120 | +
|
| 121 | +## Styling |
| 122 | +
|
| 123 | +The component accepts a `style` prop with the following structure: |
| 124 | +
|
| 125 | +```typescript |
| 126 | +type Styles = { |
| 127 | + container?: ViewStyle; |
| 128 | + input?: TextStyle; |
| 129 | + suggestionsContainer?: ViewStyle; |
| 130 | + suggestionItem?: ViewStyle; |
| 131 | + suggestionText?: { |
| 132 | + main?: TextStyle; |
| 133 | + secondary?: TextStyle; |
| 134 | + }; |
| 135 | + loadingIndicator?: { |
| 136 | + color?: string; |
| 137 | + }; |
| 138 | + placeholder?: { |
| 139 | + color?: string; |
| 140 | + }; |
| 141 | +} |
| 142 | +``` |
22 | 143 |
|
23 | 144 | ## Contributing |
24 | 145 |
|
|
0 commit comments