UNPKG

6.22 kBJavaScriptView Raw
1var __rest = (this && this.__rest) || function (s, e) {
2 var t = {};
3 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4 t[p] = s[p];
5 if (s != null && typeof Object.getOwnPropertySymbols === "function")
6 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8 t[p[i]] = s[p[i]];
9 }
10 return t;
11};
12import React from 'react';
13import { ActivityIndicator, StyleSheet, View, } from 'react-native';
14import { renderNode } from '../helpers';
15import Input from '../input/Input';
16import Icon from '../icons/Icon';
17const defaultSearchIcon = (theme) => {
18 var _a;
19 return ({
20 type: 'material',
21 size: 18,
22 name: 'search',
23 color: (_a = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _a === void 0 ? void 0 : _a.grey3,
24 });
25};
26const defaultClearIcon = (theme) => {
27 var _a;
28 return ({
29 type: 'material',
30 size: 18,
31 name: 'clear',
32 color: (_a = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _a === void 0 ? void 0 : _a.grey3,
33 });
34};
35class SearchBar extends React.Component {
36 constructor(props) {
37 super(props);
38 this.focus = () => {
39 this.input.focus();
40 };
41 this.blur = () => {
42 this.input.blur();
43 };
44 this.clear = () => {
45 this.input.clear();
46 this.onChangeText('');
47 this.props.onClear();
48 };
49 this.onFocus = (event) => {
50 this.props.onFocus(event);
51 this.setState({ isEmpty: this.props.value === '' });
52 };
53 this.onBlur = (event) => {
54 this.props.onBlur(event);
55 };
56 this.onChangeText = (text) => {
57 this.props.onChangeText(text);
58 this.setState({ isEmpty: text === '' });
59 };
60 const { value } = props;
61 this.state = {
62 isEmpty: value ? value === '' : true,
63 };
64 }
65 render() {
66 var _a, _b, _c, _d, _e, _f;
67 const _g = this.props, { theme } = _g, rest = __rest(_g, ["theme"]);
68 const { lightTheme, round, clearIcon = defaultClearIcon(theme), containerStyle, searchIcon = defaultSearchIcon(theme), leftIconContainerStyle, rightIconContainerStyle, inputContainerStyle, inputStyle, showLoading, loadingProps, placeholderTextColor = (_a = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _a === void 0 ? void 0 : _a.grey3 } = rest, attributes = __rest(rest, ["lightTheme", "round", "clearIcon", "containerStyle", "searchIcon", "leftIconContainerStyle", "rightIconContainerStyle", "inputContainerStyle", "inputStyle", "showLoading", "loadingProps", "placeholderTextColor"]);
69 const { isEmpty } = this.state;
70 const { style: loadingStyle } = loadingProps, otherLoadingProps = __rest(loadingProps, ["style"]);
71 return (<View style={StyleSheet.flatten([
72 {
73 borderTopWidth: 1,
74 borderBottomWidth: 1,
75 borderBottomColor: '#000',
76 borderTopColor: '#000',
77 padding: 8,
78 backgroundColor: (_b = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _b === void 0 ? void 0 : _b.grey0,
79 },
80 lightTheme && {
81 borderTopColor: '#e1e1e1',
82 borderBottomColor: '#e1e1e1',
83 backgroundColor: (_c = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _c === void 0 ? void 0 : _c.grey5,
84 },
85 containerStyle,
86 ])}>
87 <Input testID="searchInput" renderErrorMessage={false} {...attributes} onFocus={this.onFocus} onBlur={this.onBlur} onChangeText={this.onChangeText} ref={(input) => {
88 this.input = input;
89 }} placeholderTextColor={placeholderTextColor} inputStyle={StyleSheet.flatten([
90 {
91 color: (_d = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _d === void 0 ? void 0 : _d.grey3,
92 marginLeft: 10,
93 },
94 inputStyle,
95 ])} inputContainerStyle={StyleSheet.flatten([
96 {
97 borderBottomWidth: 0,
98 borderRadius: 3,
99 overflow: 'hidden',
100 minHeight: 30,
101 backgroundColor: (_e = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _e === void 0 ? void 0 : _e.searchBg,
102 },
103 lightTheme && {
104 backgroundColor: (_f = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _f === void 0 ? void 0 : _f.grey4,
105 },
106 round && styles.round,
107 inputContainerStyle,
108 ])} containerStyle={styles.inputContainer} leftIcon={renderNode(Icon, searchIcon, defaultSearchIcon(theme))} leftIconContainerStyle={StyleSheet.flatten([
109 styles.leftIconContainerStyle,
110 leftIconContainerStyle,
111 ])} rightIcon={<View style={{ flexDirection: 'row' }}>
112 {showLoading && (<ActivityIndicator key="loading" style={StyleSheet.flatten([{ marginRight: 5 }, loadingStyle])} {...otherLoadingProps}/>)}
113
114 {!isEmpty &&
115 renderNode(Icon, clearIcon, Object.assign(Object.assign({}, defaultClearIcon(theme)), { key: 'cancel', onPress: this.clear }))}
116 </View>} rightIconContainerStyle={StyleSheet.flatten([
117 styles.rightIconContainerStyle,
118 rightIconContainerStyle,
119 ])}/>
120 </View>);
121 }
122}
123SearchBar.defaultProps = {
124 value: '',
125 loadingProps: {},
126 showLoading: false,
127 lightTheme: false,
128 round: false,
129 onClear: () => null,
130 onFocus: () => null,
131 onBlur: () => null,
132 onChangeText: () => null,
133};
134const styles = StyleSheet.create({
135 rightIconContainerStyle: {
136 marginRight: 8,
137 },
138 leftIconContainerStyle: {
139 marginLeft: 8,
140 },
141 inputContainer: {
142 paddingHorizontal: 0,
143 },
144 round: {
145 borderRadius: 15,
146 },
147});
148export default SearchBar;