Make clicked search selections render their sections and scroll to item. Search box should no longer delete some inputs while typing.

This commit is contained in:
Chris Bojemski 2020-01-14 13:22:50 -08:00
parent 8d982ae27d
commit 124377fc2c
3 changed files with 29 additions and 11 deletions

View File

@ -62,7 +62,7 @@ export class Redoc extends React.Component<RedocProps, AppState> {
marker={marker} marker={marker}
getItemById={menu.getItemById} getItemById={menu.getItemById}
onActivate={menu.activateAndScroll} onActivate={menu.activateAndScroll}
setActiveSelection={() => this.setActiveSelection} setActiveSelection={this.setActiveSelection}
/> />
)) || )) ||
null} null}

View File

@ -15,16 +15,16 @@ import {
SearchResultsBox, SearchResultsBox,
SearchWrap, SearchWrap,
} from './styled.elements'; } from './styled.elements';
import { bind, debounce } from 'decko';
export interface SearchBoxProps { export interface SearchBoxProps {
search: SearchStore<string>; search: SearchStore<string>;
marker: MarkerService; marker: MarkerService;
getItemById: (id: string) => IMenuItem | undefined; getItemById: (id: string) => IMenuItem | undefined;
onActivate: (item: IMenuItem) => void; onActivate: (item: IMenuItem) => void;
setActiveSelection: (item: IMenuItem) => void;
className?: string; className?: string;
setActiveSelection: () => void;
} }
export interface SearchBoxState { export interface SearchBoxState {
@ -101,6 +101,14 @@ export class SearchBox extends React.PureComponent<SearchBoxProps, SearchBoxStat
this.props.marker.mark(term); this.props.marker.mark(term);
} }
@bind
@debounce(600)
searchCallback(searchTerm: string) {
this.props.search.search(searchTerm).then(res => {
this.setResults(res, searchTerm);
});
}
search = (event: React.ChangeEvent<HTMLInputElement>) => { search = (event: React.ChangeEvent<HTMLInputElement>) => {
const q = event.target.value; const q = event.target.value;
if (q.length < 3) { if (q.length < 3) {
@ -108,13 +116,12 @@ export class SearchBox extends React.PureComponent<SearchBoxProps, SearchBoxStat
return; return;
} }
this.setState({ this.setState(
{
term: q, term: q,
}); },
() => this.searchCallback(this.state.term),
this.props.search.search(event.target.value).then(res => { );
this.setResults(res, q);
});
}; };
render() { render() {
@ -155,7 +162,8 @@ export class SearchBox extends React.PureComponent<SearchBoxProps, SearchBoxStat
withoutChildren={true} withoutChildren={true}
key={res.item.id} key={res.item.id}
data-role="search:result" data-role="search:result"
setActiveSelection={() => this.props.setActiveSelection} setActiveSelection={this.props.setActiveSelection}
isSearchItem={true}
/> />
))} ))}
</SearchResultsBox> </SearchResultsBox>

View File

@ -10,6 +10,7 @@ import { MenuItemLabel, MenuItemLi, MenuItemTitle, OperationBadge } from './styl
export interface MenuItemProps { export interface MenuItemProps {
item: IMenuItem; item: IMenuItem;
isSearchItem?: boolean;
onActivate?: (item: IMenuItem) => void; onActivate?: (item: IMenuItem) => void;
setActiveSelection: (item: IMenuItem) => void; setActiveSelection: (item: IMenuItem) => void;
withoutChildren?: boolean; withoutChildren?: boolean;
@ -20,6 +21,9 @@ export class MenuItem extends React.Component<MenuItemProps> {
ref = React.createRef<HTMLLabelElement>(); ref = React.createRef<HTMLLabelElement>();
activate = (evt: React.MouseEvent<HTMLElement>) => { activate = (evt: React.MouseEvent<HTMLElement>) => {
if (this.props.isSearchItem) {
this.searchItemClick(this.props.item);
}
this.props.setActiveSelection(this.props.item); this.props.setActiveSelection(this.props.item);
this.props.onActivate!(this.props.item); this.props.onActivate!(this.props.item);
evt.stopPropagation(); evt.stopPropagation();
@ -39,6 +43,12 @@ export class MenuItem extends React.Component<MenuItemProps> {
} }
} }
searchItemClick(item: IMenuItem) {
if (item.parent) {
this.props.setActiveSelection(item.parent);
}
}
render() { render() {
const { item, withoutChildren } = this.props; const { item, withoutChildren } = this.props;
return ( return (