Auto focus search bar when clicking the search icon

This commit is contained in:
Yohan Simard 2019-11-15 18:41:25 +01:00
parent d614e8a1ed
commit fd75675681
3 changed files with 53 additions and 10 deletions

View file

@ -13,6 +13,7 @@ type Props = {
hasBackButton: boolean, hasBackButton: boolean,
hasSearchField: boolean, hasSearchField: boolean,
searchCallback: Function, searchCallback: Function,
shouldFocusSearchBar: boolean,
leftButton: React.Node, leftButton: React.Node,
rightButton: React.Node, rightButton: React.Node,
title: string, title: string,
@ -34,12 +35,20 @@ export default class CustomHeader extends React.Component<Props> {
hasBackButton: false, hasBackButton: false,
hasSearchField: false, hasSearchField: false,
searchCallback: () => null, searchCallback: () => null,
shouldFocusSearchBar: false,
title: '', title: '',
leftButton: <View/>, leftButton: <View/>,
rightButton: <View/>, rightButton: <View/>,
hasTabs: false, hasTabs: false,
}; };
searchBarRef: Input;
componentDidMount() {
if (this.searchBarRef !== undefined && this.props.shouldFocusSearchBar)
this.searchBarRef.focus();
}
getSearchBar() { getSearchBar() {
return ( return (
<Item <Item
@ -51,6 +60,7 @@ export default class CustomHeader extends React.Component<Props> {
icon={'magnify'} icon={'magnify'}
color={ThemeManager.getCurrentThemeVariables().toolbarBtnColor}/> color={ThemeManager.getCurrentThemeVariables().toolbarBtnColor}/>
<Input <Input
ref={(ref) => this.searchBarRef = ref}
placeholder={i18n.t('proximoScreen.search')} placeholder={i18n.t('proximoScreen.search')}
placeholderTextColor={ThemeManager.getCurrentThemeVariables().toolbarPlaceholderColor} placeholderTextColor={ThemeManager.getCurrentThemeVariables().toolbarPlaceholderColor}
onChangeText={(text) => this.props.searchCallback(text)}/> onChangeText={(text) => this.props.searchCallback(text)}/>

View file

@ -61,15 +61,18 @@ export default class ProximoListScreen extends React.Component<Props, State> {
modalRef: { current: null | Modalize }; modalRef: { current: null | Modalize };
originalData: Array<Object>; originalData: Array<Object>;
navData = this.props.navigation.getParam('data', []);
shouldFocusSearchBar = this.navData['shouldFocusSearchBar'];
constructor(props: any) { constructor(props: any) {
super(props); super(props);
this.modalRef = React.createRef(); this.modalRef = React.createRef();
this.originalData = this.props.navigation.getParam('data', []); this.originalData = this.navData['data'];
} }
state = { state = {
currentlyDisplayedData: this.props.navigation.getParam('data', []).sort(sortPrice), currentlyDisplayedData: this.navData['data'].sort(sortPrice),
currentSortMode: sortMode.price, currentSortMode: sortMode.price,
isSortReversed: false, isSortReversed: false,
sortPriceIcon: '', sortPriceIcon: '',
@ -310,6 +313,7 @@ export default class ProximoListScreen extends React.Component<Props, State> {
navigation={nav} navigation={nav}
hasSearchField={true} hasSearchField={true}
searchCallback={(text) => this.search(text)} searchCallback={(text) => this.search(text)}
shouldFocusSearchBar={this.shouldFocusSearchBar}
rightButton={this.getSortMenu()}/> rightButton={this.getSortMenu()}/>
<Content> <Content>

View file

@ -101,7 +101,31 @@ export default class ProximoMainScreen extends FetchedDataSectionList {
} }
getRightButton() { getRightButton() {
let searchScreenData = {
shouldFocusSearchBar: true,
data: {
type: {
id: "0",
name: i18n.t('proximoScreen.all'),
icon: 'star'
},
data: this.getAvailableArticles(this.state.fetchedData.articles, undefined)
},
};
return ( return (
<View
style={{
flexDirection: 'row'
}}>
<Touchable
style={{padding: 6}}
onPress={() => this.props.navigation.navigate('ProximoListScreen', searchScreenData)}>
<CustomMaterialIcon
color={Platform.OS === 'ios' ? ThemeManager.getCurrentThemeVariables().brandPrimary : "#fff"}
icon="magnify"/>
</Touchable>
<Touchable <Touchable
style={{padding: 6}} style={{padding: 6}}
onPress={() => this.props.navigation.navigate('ProximoAboutScreen')}> onPress={() => this.props.navigation.navigate('ProximoAboutScreen')}>
@ -109,17 +133,22 @@ export default class ProximoMainScreen extends FetchedDataSectionList {
color={Platform.OS === 'ios' ? ThemeManager.getCurrentThemeVariables().brandPrimary : "#fff"} color={Platform.OS === 'ios' ? ThemeManager.getCurrentThemeVariables().brandPrimary : "#fff"}
icon="information"/> icon="information"/>
</Touchable> </Touchable>
</View>
); );
} }
getRenderItem(item: Object, section: Object, data: Object) { getRenderItem(item: Object, section: Object, data: Object) {
let dataToSend = {
shouldFocusSearchBar: false,
data: item,
};
if (item.data.length > 0) { if (item.data.length > 0) {
return ( return (
<ListItem <ListItem
button button
thumbnail thumbnail
onPress={() => { onPress={() => {
this.props.navigation.navigate('ProximoListScreen', item); this.props.navigation.navigate('ProximoListScreen', dataToSend);
}} }}
> >
<Left> <Left>