Files
flipper/src/plugins/navigation/components/SearchBar.js
Benjamin Elo ea8a6546c9 Added providers to the auto complete sheet
Summary:
Okay so the main changes here are integrating the providers into the auto complete sheet and getting the search bar to work with it also.

For instance, in the search bar, I want to update the value string to whatever the user has highlighted in the auto complete sheet, without executing a new query. So thus, I had to create a new state variable in the search bar component for this.

I've also moved the custom hook into its own file to keep the component short in size. It had to be mainly rewritten to support providers instead of only bookmarks. Same goes for the entire AutoCompleteSheet component.

The bookmarksProvider is stored in the persisted state as to not regenerate every-time on render. It is only updated if the bookmarks are updated which are also now stored in the persistedState for the same reason.

Lastly, a DefaultProvider object was also made for the initial persisted state object.

Reviewed By: danielbuechele

Differential Revision: D16581644

fbshipit-source-id: 88723a4081d96250f723a4cd7b1ade101bf3e8f3
2019-08-02 01:58:02 -07:00

158 lines
4.0 KiB
JavaScript

/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
* @flow strict-local
*/
import {
Component,
styled,
SearchBox,
SearchInput,
Toolbar,
Glyph,
} from 'flipper';
import {AutoCompleteSheet, IconButton, FavoriteButton} from './';
import type {AutoCompleteProvider, Bookmark} from '../flow-types';
type Props = {|
onFavorite: (query: string) => void,
onNavigate: (query: string) => void,
bookmarks: Map<string, Bookmark>,
providers: Array<AutoCompleteProvider>,
|};
type State = {|
query: string,
inputFocused: boolean,
autoCompleteSheetOpen: boolean,
searchInputValue: string,
|};
const IconContainer = styled('div')({
display: 'inline-flex',
height: '16px',
alignItems: 'center',
'': {
marginLeft: 10,
'.icon-button': {
height: 16,
},
'img,div': {
verticalAlign: 'top',
alignItems: 'none',
},
},
});
const ToolbarContainer = styled('div')({
'.drop-shadow': {
boxShadow: '0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24)',
},
});
const SearchInputContainer = styled('div')({
height: '100%',
width: '100%',
marginLeft: 5,
marginRight: 9,
position: 'relative',
});
class SearchBar extends Component<Props, State> {
state = {
inputFocused: false,
autoCompleteSheetOpen: false,
query: '',
searchInputValue: '',
};
favorite = (searchInputValue: string) => {
this.props.onFavorite(searchInputValue);
};
navigateTo = (searchInputValue: string) => {
this.setState({query: searchInputValue, searchInputValue});
this.props.onNavigate(searchInputValue);
};
queryInputChanged = (event: SyntheticInputEvent<>) => {
const value = event.target.value;
this.setState({query: value, searchInputValue: value});
};
render = () => {
const {bookmarks, providers} = this.props;
const {
autoCompleteSheetOpen,
inputFocused,
searchInputValue,
query,
} = this.state;
return (
<ToolbarContainer>
<Toolbar>
<SearchBox className={inputFocused ? 'drop-shadow' : null}>
<SearchInputContainer>
<SearchInput
value={searchInputValue}
onBlur={() =>
this.setState({
autoCompleteSheetOpen: false,
inputFocused: false,
})
}
onFocus={event => {
event.target.select();
this.setState({
autoCompleteSheetOpen: true,
inputFocused: true,
});
}}
onChange={this.queryInputChanged}
onKeyPress={e => {
if (e.key === 'Enter') {
this.navigateTo(this.state.searchInputValue);
e.target.blur();
}
}}
placeholder="Navigate To..."
/>
{autoCompleteSheetOpen && query.length > 0 ? (
<AutoCompleteSheet
providers={providers}
onNavigate={this.navigateTo}
onHighlighted={newInputValue =>
this.setState({searchInputValue: newInputValue})
}
query={query}
/>
) : null}
</SearchInputContainer>
</SearchBox>
{searchInputValue.length > 0 ? (
<IconContainer>
<IconButton
icon="send"
size={16}
outline={true}
onClick={() => this.navigateTo(searchInputValue)}
/>
<FavoriteButton
size={16}
highlighted={bookmarks.has(searchInputValue)}
onClick={() => this.favorite(searchInputValue)}
/>
</IconContainer>
) : null}
</Toolbar>
</ToolbarContainer>
);
};
}
export default SearchBar;