forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchbar.tsx
264 lines (250 loc) · 6.21 KB
/
Searchbar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import * as React from 'react';
import {
StyleSheet,
StyleProp,
TextInput,
I18nManager,
ViewStyle,
TextStyle,
} from 'react-native';
import color from 'color';
import IconButton from './IconButton';
import Surface from './Surface';
import { withTheme } from '../core/theming';
import { Theme } from '../types';
import { IconSource } from './Icon';
import MaterialCommunityIcon from './MaterialCommunityIcon';
type Props = React.ComponentPropsWithRef<typeof TextInput> & {
/**
* Accessibility label for the button. This is read by the screen reader when the user taps the button.
*/
clearAccessibilityLabel?: string;
/**
* Accessibility label for the button. This is read by the screen reader when the user taps the button.
*/
searchAccessibilityLabel?: string;
/**
* Hint text shown when the input is empty.
*/
placeholder?: string;
/**
* The value of the text input.
*/
value: string;
/**
* Icon name for the left icon button (see `onIconPress`).
*/
icon?: IconSource;
/**
* Callback that is called when the text input's text changes.
*/
onChangeText?: (query: string) => void;
/**
* Callback to execute if we want the left icon to act as button.
*/
onIconPress?: () => void;
/**
* Set style of the TextInput component inside the searchbar
*/
inputStyle?: StyleProp<TextStyle>;
style?: StyleProp<ViewStyle>;
/**
* @optional
*/
theme: Theme;
/**
* Custom color for icon, default will be derived from theme
*/
iconColor?: string;
/**
* Custom icon for clear button, default will be icon close
*/
clearIcon?: IconSource;
};
/**
* Searchbar is a simple input box where users can type search queries.
*
* <div class="screenshots">
* <img class="medium" src="screenshots/searchbar.png" />
* </div>
*
* ## Usage
* ```js
* import * as React from 'react';
* import { Searchbar } from 'react-native-paper';
*
* export default class MyComponent extends React.Component {
* state = {
* searchQuery: '',
* };
*
* _onChangeSearch = query => this.setState({ searchQuery: query });
*
* render() {
* const { searchQuery } = this.state;
* return (
* <Searchbar
* placeholder="Search"
* onChangeText={this._onChangeSearch}
* value={searchQuery}
* />
* );
* }
* }
* ```
*/
class Searchbar extends React.Component<Props> {
static defaultProps = {
searchAccessibilityLabel: 'search',
clearAccessibilityLabel: 'clear',
};
private handleClearPress = () => {
this.clear();
this.props.onChangeText && this.props.onChangeText('');
};
private root: TextInput | undefined | null;
/**
* @internal
*/ setNativeProps(args: Object) {
return this.root && this.root.setNativeProps(args);
}
/**
* Returns `true` if the input is currently focused, `false` otherwise.
*/
isFocused() {
return this.root && this.root.isFocused();
}
/**
* Removes all text from the TextInput.
*/
clear() {
return this.root && this.root.clear();
}
/**
* Focuses the input.
*/
focus() {
return this.root && this.root.focus();
}
/**
* Removes focus from the input.
*/
blur() {
return this.root && this.root.blur();
}
render() {
const {
clearAccessibilityLabel,
clearIcon,
icon,
iconColor: customIconColor,
inputStyle,
onIconPress,
placeholder,
searchAccessibilityLabel,
style,
theme,
value,
...rest
} = this.props;
const { colors, roundness, dark, fonts } = theme;
const textColor = colors.text;
const font = fonts.regular;
const iconColor =
customIconColor ||
(dark
? textColor
: color(textColor)
.alpha(0.54)
.rgb()
.string());
const rippleColor = color(textColor)
.alpha(0.32)
.rgb()
.string();
return (
<Surface
style={[
{ borderRadius: roundness, elevation: 4 },
styles.container,
style,
]}
>
<IconButton
accessibilityTraits="button"
accessibilityComponentType="button"
accessibilityRole="button"
borderless
rippleColor={rippleColor}
onPress={onIconPress}
color={iconColor}
icon={
icon ||
(({ size, color }) => (
<MaterialCommunityIcon
name="magnify"
color={color}
size={size}
direction={I18nManager.isRTL ? 'rtl' : 'ltr'}
/>
))
}
accessibilityLabel={searchAccessibilityLabel}
/>
<TextInput
style={[styles.input, { color: textColor, ...font }, inputStyle]}
placeholder={placeholder || ''}
placeholderTextColor={colors.placeholder}
selectionColor={colors.primary}
underlineColorAndroid="transparent"
returnKeyType="search"
keyboardAppearance={dark ? 'dark' : 'light'}
accessibilityTraits="search"
accessibilityRole="search"
ref={c => {
this.root = c;
}}
value={value}
{...rest}
/>
<IconButton
borderless
disabled={!value}
accessibilityLabel={clearAccessibilityLabel}
color={value ? iconColor : 'rgba(255, 255, 255, 0)'}
rippleColor={rippleColor}
onPress={this.handleClearPress}
icon={
clearIcon ||
(({ size, color }) => (
<MaterialCommunityIcon
name="close"
color={color}
size={size}
direction={I18nManager.isRTL ? 'rtl' : 'ltr'}
/>
))
}
accessibilityTraits="button"
accessibilityComponentType="button"
accessibilityRole="button"
/>
</Surface>
);
}
}
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
},
input: {
flex: 1,
fontSize: 18,
paddingLeft: 8,
alignSelf: 'stretch',
textAlign: I18nManager.isRTL ? 'right' : 'left',
minWidth: 0,
},
});
export default withTheme(Searchbar);