mirror of
https://gitlab.com/wheres-the-tp/ui-mobile.git
synced 2026-01-25 07:54:57 -06:00
always show search bar
This commit is contained in:
parent
93662810fa
commit
50ad4c5ee8
3 changed files with 53 additions and 44 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
// @flow
|
// @flow
|
||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
import { Toolbar } from 'react-native-material-ui';
|
import { View, TextInput, TouchableOpacity } from 'react-native';
|
||||||
|
import { Toolbar, Icon } from 'react-native-material-ui';
|
||||||
import { type RoutingContextFlowTypes, routingContextPropTypes } from '../routes';
|
import { type RoutingContextFlowTypes, routingContextPropTypes } from '../routes';
|
||||||
import { path } from 'ramda';
|
import { path } from 'ramda';
|
||||||
import queryString from 'query-string';
|
import queryString from 'query-string';
|
||||||
|
|
@ -8,7 +9,8 @@ import { getSearch } from '../helpers/RouteHelpers';
|
||||||
import FoodItemSaveBtn from '../components/FoodItemSaveBtn';
|
import FoodItemSaveBtn from '../components/FoodItemSaveBtn';
|
||||||
import { withFilter } from '../enhancers/filterEnhancers';
|
import { withFilter } from '../enhancers/filterEnhancers';
|
||||||
import FilterRecord from '../records/FilterRecord';
|
import FilterRecord from '../records/FilterRecord';
|
||||||
import debounce from '../helpers/debounce';
|
|
||||||
|
import { palette } from '../ui-theme';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
title: ?string,
|
title: ?string,
|
||||||
|
|
@ -27,10 +29,6 @@ class TopToolbar extends Component {
|
||||||
|
|
||||||
context: RoutingContextFlowTypes;
|
context: RoutingContextFlowTypes;
|
||||||
|
|
||||||
state = {
|
|
||||||
search: '',
|
|
||||||
};
|
|
||||||
|
|
||||||
onRightPress = ({ action }: { action: string }) => {
|
onRightPress = ({ action }: { action: string }) => {
|
||||||
const {
|
const {
|
||||||
router: { history },
|
router: { history },
|
||||||
|
|
@ -45,22 +43,15 @@ class TopToolbar extends Component {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
onSubmitEditing = () => {
|
onSearchCleared = () => {
|
||||||
const { search } = this.state;
|
|
||||||
const { setFilter, filter } = this.props;
|
|
||||||
setFilter(filter.update(prevFilter => prevFilter.set('search', search)));
|
|
||||||
};
|
|
||||||
|
|
||||||
onSearchClosed = () => {
|
|
||||||
const { setFilter, filter } = this.props;
|
const { setFilter, filter } = this.props;
|
||||||
setFilter(filter.update(prevFilter => prevFilter.set('search', '')));
|
setFilter(filter.update(prevFilter => prevFilter.set('search', '')));
|
||||||
};
|
};
|
||||||
|
|
||||||
onChangeText = debounce(search => {
|
onChangeText = search => {
|
||||||
const { setFilter, filter } = this.props;
|
const { setFilter, filter } = this.props;
|
||||||
setFilter(filter.update(prevFilter => prevFilter.set('search', search)));
|
setFilter(filter.update(prevFilter => prevFilter.set('search', search)));
|
||||||
this.setState({ search });
|
};
|
||||||
}, 100);
|
|
||||||
|
|
||||||
getRightElement = () => {
|
getRightElement = () => {
|
||||||
const { viewMode } = getSearch(this.context);
|
const { viewMode } = getSearch(this.context);
|
||||||
|
|
@ -79,39 +70,55 @@ class TopToolbar extends Component {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
getSearchable = () => {
|
|
||||||
const pathname = path(['router', 'route', 'location', 'pathname'], this.context);
|
|
||||||
|
|
||||||
if (!/list/.test(pathname)) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
searchable: {
|
|
||||||
autoFocus: true,
|
|
||||||
onSubmitEditing: this.onSubmitEditing,
|
|
||||||
onSearchClosed: this.onSearchClosed,
|
|
||||||
onChangeText: this.onChangeText,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { toggleSideMenu } = this.props;
|
const { toggleSideMenu, filter } = this.props;
|
||||||
const history = path(['router', 'history'], this.context);
|
const history = path(['router', 'history'], this.context);
|
||||||
const search = getSearch(this.context);
|
const route = path(['router', 'route', 'location', 'pathname'], this.context);
|
||||||
const title = search.routeTitle || this.props.title;
|
const routeSearch = getSearch(this.context);
|
||||||
|
const title = routeSearch.routeTitle || this.props.title;
|
||||||
const isBasePage = history.index === 0;
|
const isBasePage = history.index === 0;
|
||||||
|
|
||||||
|
const showSearch = /^\/list/.test(route);
|
||||||
|
const showSearchClear = filter.search.length > 0;
|
||||||
|
const searchPlaceholder = /\/food/.test(route) ? 'Search Food...' : 'Search Places...';
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Toolbar
|
<View style={{ backgroundColor: palette.primaryColor }}>
|
||||||
leftElement={isBasePage ? 'menu' : 'arrow-back'}
|
<Toolbar
|
||||||
onLeftElementPress={isBasePage ? toggleSideMenu : history.goBack}
|
leftElement={isBasePage ? 'menu' : 'arrow-back'}
|
||||||
centerElement={title}
|
onLeftElementPress={isBasePage ? toggleSideMenu : history.goBack}
|
||||||
rightElement={this.getRightElement()}
|
centerElement={title}
|
||||||
onRightElementPress={this.onRightPress}
|
rightElement={this.getRightElement()}
|
||||||
{...this.getSearchable()}
|
onRightElementPress={this.onRightPress}
|
||||||
/>
|
/>
|
||||||
|
{showSearch && (
|
||||||
|
<View
|
||||||
|
style={{
|
||||||
|
flexDirection: 'row',
|
||||||
|
alignItems: 'center',
|
||||||
|
margin: 10,
|
||||||
|
backgroundColor: 'white',
|
||||||
|
borderRadius: 3,
|
||||||
|
}}>
|
||||||
|
<Icon name="search" style={{ margin: 10 }} />
|
||||||
|
<TextInput
|
||||||
|
value={filter.search}
|
||||||
|
onChangeText={this.onChangeText}
|
||||||
|
placeholder={searchPlaceholder}
|
||||||
|
underlineColorAndroid="transparent"
|
||||||
|
style={{
|
||||||
|
flex: 1,
|
||||||
|
fontSize: 18,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
{showSearchClear && (
|
||||||
|
<TouchableOpacity onPress={this.onSearchCleared}>
|
||||||
|
<Icon name="close" style={{ margin: 10 }} />
|
||||||
|
</TouchableOpacity>
|
||||||
|
)}
|
||||||
|
</View>
|
||||||
|
)}
|
||||||
|
</View>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ const manualUpdate$ = foodItemSubject.scan(
|
||||||
);
|
);
|
||||||
|
|
||||||
const fetchedFoodItems$ = Filter$.combineLatest(location$)
|
const fetchedFoodItems$ = Filter$.combineLatest(location$)
|
||||||
|
.debounceTime(200)
|
||||||
.mergeMap(([filter, loc]) => {
|
.mergeMap(([filter, loc]) => {
|
||||||
if (!loc) {
|
if (!loc) {
|
||||||
return Promise.resolve({});
|
return Promise.resolve({});
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,7 @@ foodItems$
|
||||||
|
|
||||||
location$
|
location$
|
||||||
.combineLatest(filter$)
|
.combineLatest(filter$)
|
||||||
|
.debounceTime(200)
|
||||||
.mergeMap(([location, filter]: [?Position, FilterRecord]) => {
|
.mergeMap(([location, filter]: [?Position, FilterRecord]) => {
|
||||||
return safeFindNearbyPlaces({
|
return safeFindNearbyPlaces({
|
||||||
location,
|
location,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue