Skip to content

Commit ded9aa1

Browse files
authored
Add clear button to main screen search (#1286)
* Add clear button to main screen search * Fix issue when filtering screens by search too soon
1 parent 665a304 commit ded9aa1

File tree

1 file changed

+52
-18
lines changed

1 file changed

+52
-18
lines changed

demo/src/screens/MainScreen.js

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,17 @@ import PropTypes from 'prop-types';
55
import {StyleSheet, FlatList, ViewPropTypes} from 'react-native';
66
import {Navigation} from 'react-native-navigation';
77
import {gestureHandlerRootHOC} from 'react-native-gesture-handler';
8-
import {Assets, Colors, View, Text, TouchableOpacity, TextField, Image, TabController} from 'react-native-ui-lib'; //eslint-disable-line
8+
import {
9+
Assets,
10+
Colors,
11+
View,
12+
Text,
13+
TouchableOpacity,
14+
TextField,
15+
Image,
16+
Button,
17+
TabController
18+
} from 'react-native-ui-lib'; //eslint-disable-line
919
import {navigationData} from './MenuStructure';
1020

1121
const settingsIcon = require('../assets/icons/settings.png');
@@ -15,7 +25,7 @@ class MainScreen extends Component {
1525
static propTypes = {
1626
containerStyle: ViewPropTypes.style,
1727
renderItem: PropTypes.func,
18-
pageStyle: ViewPropTypes.style
28+
pageStyle: ViewPropTypes.style
1929
};
2030

2131
settingsScreenName = 'unicorn.Settings';
@@ -52,7 +62,6 @@ class MainScreen extends Component {
5262

5363
onSearchBoxBlur = () => {
5464
this.closeSearchBox();
55-
// this.filterExplorerScreens('');
5665
};
5766

5867
getMenuData = () => {
@@ -67,8 +76,8 @@ class MainScreen extends Component {
6776
this.pushScreen({
6877
name: this.settingsScreenName,
6978
passProps: {
70-
navigationData: data,
71-
playground: this.props.playground,
79+
navigationData: data,
80+
playground: this.props.playground,
7281
extraSettingsUI: this.props.extraSettingsUI
7382
}
7483
});
@@ -105,12 +114,22 @@ class MainScreen extends Component {
105114
this.closeSearchBox();
106115

107116
setTimeout(() => {
108-
// this.filterExplorerScreens('');
109117
this.pushScreen(row);
110118
}, 0);
111119
};
112120

113-
filterExplorerScreens = filterText => {
121+
updateSearch = filterText => {
122+
this.setState({filterText}, () => {
123+
this.filterExplorerScreens();
124+
});
125+
};
126+
127+
clearSearch = () => {
128+
this.updateSearch('');
129+
};
130+
131+
filterExplorerScreens = () => {
132+
const {filterText} = this.state;
114133
let filteredNavigationData = {};
115134
const data = this.getMenuData();
116135

@@ -134,32 +153,35 @@ class MainScreen extends Component {
134153
}
135154

136155
this.setState({
137-
filterText,
138156
filteredNavigationData
139157
});
140158
};
141159

142160
/** Renders */
143161
renderSearch = () => {
162+
const {filterText} = this.state;
144163
return (
145164
<TextField
165+
migrate
146166
ref={r => (this.input = r)}
147-
value={this.state.filterText}
167+
value={filterText}
148168
testID="uilib.search_for_component"
149169
placeholder="Search for your component..."
150-
onChangeText={this.filterExplorerScreens}
170+
onChangeText={this.updateSearch}
151171
onBlur={this.onSearchBoxBlur}
152-
containerStyle={{padding: 16, paddingBottom: 0}}
153-
style={{
154-
padding: 12,
155-
backgroundColor: Colors.dark80,
156-
borderRadius: 8
157-
}}
172+
containerStyle={styles.searchContainer}
173+
fieldStyle={styles.searchField}
158174
enableErrors={false}
159175
hideUnderline
160176
floatingPlaceholder={false}
161177
text70
162-
rightButtonProps={{iconSource: Assets.icons.search, style: {marginRight: 12, alignSelf: 'center'}}}
178+
trailingAccessory={
179+
filterText ? (
180+
<Button link iconSource={Assets.icons.demo.close} grey10 onPress={this.clearSearch}/>
181+
) : (
182+
<Image source={Assets.icons.demo.search}/>
183+
)
184+
}
163185
/>
164186
);
165187
};
@@ -251,7 +273,10 @@ class MainScreen extends Component {
251273
{showResults && this.renderSearchResults(filteredNavigationData)}
252274

253275
{showCarousel && (
254-
<TabController asCarousel items={_.map(data, section => ({label: section.title, testID: `section.${section.title}`}))}>
276+
<TabController
277+
asCarousel
278+
items={_.map(data, section => ({label: section.title, testID: `section.${section.title}`}))}
279+
>
255280
<TabController.TabBar testID={'mainScreenTabBar'}/>
256281
{this.renderPages(data)}
257282
</TabController>
@@ -271,6 +296,15 @@ class MainScreen extends Component {
271296
const styles = StyleSheet.create({
272297
entryTextDeprecated: {
273298
textDecorationLine: 'line-through'
299+
},
300+
searchContainer: {
301+
padding: 16,
302+
paddingBottom: 0
303+
},
304+
searchField: {
305+
padding: 12,
306+
backgroundColor: Colors.dark80,
307+
borderRadius: 8
274308
}
275309
});
276310

0 commit comments

Comments
 (0)