Skip to content

Add clear button to main screen search #1286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 52 additions & 18 deletions demo/src/screens/MainScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ import PropTypes from 'prop-types';
import {StyleSheet, FlatList, ViewPropTypes} from 'react-native';
import {Navigation} from 'react-native-navigation';
import {gestureHandlerRootHOC} from 'react-native-gesture-handler';
import {Assets, Colors, View, Text, TouchableOpacity, TextField, Image, TabController} from 'react-native-ui-lib'; //eslint-disable-line
import {
Assets,
Colors,
View,
Text,
TouchableOpacity,
TextField,
Image,
Button,
TabController
} from 'react-native-ui-lib'; //eslint-disable-line
import {navigationData} from './MenuStructure';

const settingsIcon = require('../assets/icons/settings.png');
Expand All @@ -15,7 +25,7 @@ class MainScreen extends Component {
static propTypes = {
containerStyle: ViewPropTypes.style,
renderItem: PropTypes.func,
pageStyle: ViewPropTypes.style
pageStyle: ViewPropTypes.style
};

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

onSearchBoxBlur = () => {
this.closeSearchBox();
// this.filterExplorerScreens('');
};

getMenuData = () => {
Expand All @@ -67,8 +76,8 @@ class MainScreen extends Component {
this.pushScreen({
name: this.settingsScreenName,
passProps: {
navigationData: data,
playground: this.props.playground,
navigationData: data,
playground: this.props.playground,
extraSettingsUI: this.props.extraSettingsUI
}
});
Expand Down Expand Up @@ -105,12 +114,22 @@ class MainScreen extends Component {
this.closeSearchBox();

setTimeout(() => {
// this.filterExplorerScreens('');
this.pushScreen(row);
}, 0);
};

filterExplorerScreens = filterText => {
updateSearch = filterText => {
this.setState({filterText}, () => {
this.filterExplorerScreens();
});
};

clearSearch = () => {
this.updateSearch('');
};

filterExplorerScreens = () => {
const {filterText} = this.state;
let filteredNavigationData = {};
const data = this.getMenuData();

Expand All @@ -134,32 +153,35 @@ class MainScreen extends Component {
}

this.setState({
filterText,
filteredNavigationData
});
};

/** Renders */
renderSearch = () => {
const {filterText} = this.state;
return (
<TextField
migrate
ref={r => (this.input = r)}
value={this.state.filterText}
value={filterText}
testID="uilib.search_for_component"
placeholder="Search for your component..."
onChangeText={this.filterExplorerScreens}
onChangeText={this.updateSearch}
onBlur={this.onSearchBoxBlur}
containerStyle={{padding: 16, paddingBottom: 0}}
style={{
padding: 12,
backgroundColor: Colors.dark80,
borderRadius: 8
}}
containerStyle={styles.searchContainer}
fieldStyle={styles.searchField}
enableErrors={false}
hideUnderline
floatingPlaceholder={false}
text70
rightButtonProps={{iconSource: Assets.icons.search, style: {marginRight: 12, alignSelf: 'center'}}}
trailingAccessory={
filterText ? (
<Button link iconSource={Assets.icons.demo.close} grey10 onPress={this.clearSearch}/>
) : (
<Image source={Assets.icons.demo.search}/>
)
}
/>
);
};
Expand Down Expand Up @@ -251,7 +273,10 @@ class MainScreen extends Component {
{showResults && this.renderSearchResults(filteredNavigationData)}

{showCarousel && (
<TabController asCarousel items={_.map(data, section => ({label: section.title, testID: `section.${section.title}`}))}>
<TabController
asCarousel
items={_.map(data, section => ({label: section.title, testID: `section.${section.title}`}))}
>
<TabController.TabBar testID={'mainScreenTabBar'}/>
{this.renderPages(data)}
</TabController>
Expand All @@ -271,6 +296,15 @@ class MainScreen extends Component {
const styles = StyleSheet.create({
entryTextDeprecated: {
textDecorationLine: 'line-through'
},
searchContainer: {
padding: 16,
paddingBottom: 0
},
searchField: {
padding: 12,
backgroundColor: Colors.dark80,
borderRadius: 8
}
});

Expand Down