|
| 1 | +# Styling Guide for GooglePlacesTextInput |
| 2 | + |
| 3 | +This guide explains how to customize the appearance of the GooglePlacesTextInput component to match your app's design. |
| 4 | + |
| 5 | +## Styling Structure |
| 6 | + |
| 7 | +The component accepts a `style` prop with the following structure: |
| 8 | + |
| 9 | +```typescript |
| 10 | +type Styles = { |
| 11 | + container?: ViewStyle; |
| 12 | + input?: TextStyle; |
| 13 | + suggestionsContainer?: ViewStyle; |
| 14 | + suggestionsList?: ViewStyle; |
| 15 | + suggestionItem?: ViewStyle; |
| 16 | + suggestionText?: { |
| 17 | + main?: TextStyle; |
| 18 | + secondary?: TextStyle; |
| 19 | + }; |
| 20 | + loadingIndicator?: { |
| 21 | + color?: string; |
| 22 | + }; |
| 23 | + placeholder?: { |
| 24 | + color?: string; |
| 25 | + }; |
| 26 | +} |
| 27 | +``` |
| 28 | +
|
| 29 | +## Component Layout Structure |
| 30 | +
|
| 31 | +Understanding the component's structure helps with styling. Here's how the component is organized: |
| 32 | +
|
| 33 | +```javascript |
| 34 | +<View style={[styles.container, style.container]}> |
| 35 | + <View> |
| 36 | + <TextInput style={[styles.input, style.input, ...]} /> |
| 37 | + <TouchableOpacity> (Clear button) </TouchableOpacity> |
| 38 | + <ActivityIndicator /> (Loading indicator) |
| 39 | + </View> |
| 40 | + <View style={[styles.suggestionsContainer, style.suggestionsContainer]}> |
| 41 | + <FlatList |
| 42 | + style={style.suggestionsList} |
| 43 | + renderItem={({item}) => ( |
| 44 | + <TouchableOpacity style={[styles.suggestionItem, style.suggestionItem]}> |
| 45 | + <Text style={[styles.mainText, style.suggestionText?.main]}> |
| 46 | + {mainText} |
| 47 | + </Text> |
| 48 | + <Text style={[styles.secondaryText, style.suggestionText?.secondary]}> |
| 49 | + {secondaryText} |
| 50 | + </Text> |
| 51 | + </TouchableOpacity> |
| 52 | + )} |
| 53 | + /> |
| 54 | + </View> |
| 55 | +</View> |
| 56 | +``` |
| 57 | +
|
| 58 | +## Styling Examples |
| 59 | +
|
| 60 | +### Basic Input Styling |
| 61 | +
|
| 62 | +```javascript |
| 63 | +const styles = { |
| 64 | + container: { |
| 65 | + width: '100%', |
| 66 | + marginHorizontal: 16, |
| 67 | + }, |
| 68 | + input: { |
| 69 | + height: 50, |
| 70 | + borderRadius: 8, |
| 71 | + borderWidth: 1, |
| 72 | + borderColor: '#CCCCCC', |
| 73 | + paddingHorizontal: 12, |
| 74 | + fontSize: 16, |
| 75 | + }, |
| 76 | + placeholder: { |
| 77 | + color: '#888888', |
| 78 | + } |
| 79 | +}; |
| 80 | +``` |
| 81 | + |
| 82 | +### Material Design Style |
| 83 | + |
| 84 | +```javascript |
| 85 | +const materialStyles = { |
| 86 | + container: { |
| 87 | + width: '100%', |
| 88 | + marginHorizontal: 16, |
| 89 | + }, |
| 90 | + input: { |
| 91 | + height: 56, |
| 92 | + borderWidth: 0, |
| 93 | + borderRadius: 4, |
| 94 | + fontSize: 16, |
| 95 | + paddingHorizontal: 12, |
| 96 | + backgroundColor: '#F5F5F5', |
| 97 | + elevation: 2, |
| 98 | + shadowColor: '#000', |
| 99 | + shadowOffset: { width: 0, height: 1 }, |
| 100 | + shadowOpacity: 0.2, |
| 101 | + shadowRadius: 1, |
| 102 | + }, |
| 103 | + suggestionsContainer: { |
| 104 | + backgroundColor: '#FFFFFF', |
| 105 | + borderRadius: 4, |
| 106 | + marginTop: 4, |
| 107 | + elevation: 3, |
| 108 | + shadowColor: '#000', |
| 109 | + shadowOffset: { width: 0, height: 2 }, |
| 110 | + shadowOpacity: 0.2, |
| 111 | + shadowRadius: 2, |
| 112 | + }, |
| 113 | + suggestionItem: { |
| 114 | + paddingVertical: 12, |
| 115 | + paddingHorizontal: 16, |
| 116 | + }, |
| 117 | + suggestionText: { |
| 118 | + main: { |
| 119 | + fontSize: 16, |
| 120 | + color: '#212121', |
| 121 | + }, |
| 122 | + secondary: { |
| 123 | + fontSize: 14, |
| 124 | + color: '#757575', |
| 125 | + } |
| 126 | + }, |
| 127 | + loadingIndicator: { |
| 128 | + color: '#6200EE', |
| 129 | + }, |
| 130 | + placeholder: { |
| 131 | + color: '#9E9E9E', |
| 132 | + } |
| 133 | +}; |
| 134 | +``` |
| 135 | + |
| 136 | +### iOS Style |
| 137 | + |
| 138 | +```javascript |
| 139 | +const iosStyles = { |
| 140 | + container: { |
| 141 | + width: '100%', |
| 142 | + paddingHorizontal: 16, |
| 143 | + }, |
| 144 | + input: { |
| 145 | + height: 44, |
| 146 | + borderRadius: 10, |
| 147 | + backgroundColor: '#E9E9EB', |
| 148 | + paddingHorizontal: 15, |
| 149 | + fontSize: 17, |
| 150 | + fontWeight: '400', |
| 151 | + }, |
| 152 | + suggestionsContainer: { |
| 153 | + backgroundColor: '#FFFFFF', |
| 154 | + borderRadius: 10, |
| 155 | + marginTop: 8, |
| 156 | + shadowColor: '#000', |
| 157 | + shadowOffset: { width: 0, height: 2 }, |
| 158 | + shadowOpacity: 0.1, |
| 159 | + shadowRadius: 8, |
| 160 | + }, |
| 161 | + suggestionItem: { |
| 162 | + paddingVertical: 12, |
| 163 | + paddingHorizontal: 15, |
| 164 | + borderBottomWidth: 0.5, |
| 165 | + borderBottomColor: '#C8C7CC', |
| 166 | + }, |
| 167 | + suggestionText: { |
| 168 | + main: { |
| 169 | + fontSize: 17, |
| 170 | + color: '#000000', |
| 171 | + fontWeight: '400', |
| 172 | + }, |
| 173 | + secondary: { |
| 174 | + fontSize: 15, |
| 175 | + color: '#8E8E93', |
| 176 | + } |
| 177 | + }, |
| 178 | + loadingIndicator: { |
| 179 | + color: '#007AFF', |
| 180 | + }, |
| 181 | + placeholder: { |
| 182 | + color: '#8E8E93', |
| 183 | + } |
| 184 | +}; |
| 185 | +``` |
| 186 | + |
| 187 | +## Styling the Suggestions List |
| 188 | + |
| 189 | +The suggestions list is implemented as a FlatList with customizable height: |
| 190 | + |
| 191 | +```javascript |
| 192 | +const styles = { |
| 193 | + suggestionsContainer: { |
| 194 | + maxHeight: 250, // Set the maximum height |
| 195 | + backgroundColor: '#FFFFFF', |
| 196 | + borderBottomLeftRadius: 8, |
| 197 | + borderBottomRightRadius: 8, |
| 198 | + borderWidth: 1, |
| 199 | + borderColor: '#EEEEEE', |
| 200 | + borderTopWidth: 0, |
| 201 | + }, |
| 202 | + // Make individual items stand out with dividers |
| 203 | + suggestionItem: { |
| 204 | + paddingVertical: 12, |
| 205 | + paddingHorizontal: 16, |
| 206 | + borderBottomWidth: 1, |
| 207 | + borderBottomColor: '#F0F0F0', |
| 208 | + } |
| 209 | +}; |
| 210 | +``` |
| 211 | + |
| 212 | +## Loading Indicator and Clear Button |
| 213 | + |
| 214 | +You can customize the color of the loading indicator: |
| 215 | + |
| 216 | +```javascript |
| 217 | +const styles = { |
| 218 | + loadingIndicator: { |
| 219 | + color: '#FF5722', // orange color |
| 220 | + } |
| 221 | +}; |
| 222 | +``` |
| 223 | + |
| 224 | +The clear button is automatically styled based on platform (iOS or Android) but you can hide it with the `showClearButton` prop: |
| 225 | + |
| 226 | +```javascript |
| 227 | +<GooglePlacesTextInput |
| 228 | + apiKey="YOUR_KEY" |
| 229 | + showClearButton={false} |
| 230 | + // ...other props |
| 231 | +/> |
| 232 | +``` |
| 233 | + |
| 234 | +## RTL Support |
| 235 | + |
| 236 | +The component automatically handles RTL layouts based on the text direction. You can also force RTL with the `forceRTL` prop: |
| 237 | + |
| 238 | +```javascript |
| 239 | +<GooglePlacesTextInput |
| 240 | + apiKey="YOUR_KEY" |
| 241 | + forceRTL={true} |
| 242 | + // ...other props |
| 243 | +/> |
| 244 | +``` |
| 245 | + |
| 246 | +## Combining with Other Style Systems |
| 247 | + |
| 248 | +If you're using a styling library like styled-components or Tailwind, you can still use this component by generating the style object: |
| 249 | + |
| 250 | +```javascript |
| 251 | +// Example with StyleSheet.create |
| 252 | +import { StyleSheet } from 'react-native'; |
| 253 | + |
| 254 | +const styles = StyleSheet.create({ |
| 255 | + container: { |
| 256 | + width: '100%', |
| 257 | + }, |
| 258 | + input: { |
| 259 | + height: 50, |
| 260 | + borderWidth: 1, |
| 261 | + }, |
| 262 | + // ...other styles |
| 263 | +}); |
| 264 | + |
| 265 | +// Pass the styles to the component |
| 266 | +<GooglePlacesTextInput |
| 267 | + style={{ |
| 268 | + container: styles.container, |
| 269 | + input: styles.input, |
| 270 | + // ...other styles |
| 271 | + }} |
| 272 | +/> |
| 273 | +``` |
0 commit comments