Add default search term in Searchable
Summary: Add a way to provide default search term; similar to default filters Reviewed By: mweststrate Differential Revision: D19449561 fbshipit-source-id: 6845ae5d2e058fe68974ac7aa8fd7b45814c2002
This commit is contained in:
committed by
Facebook Github Bot
parent
f94446310d
commit
ff26deedd7
@@ -113,6 +113,7 @@ type Props = {
|
|||||||
columns?: TableColumns;
|
columns?: TableColumns;
|
||||||
onFilterChange: (filters: Array<Filter>) => void;
|
onFilterChange: (filters: Array<Filter>) => void;
|
||||||
defaultFilters: Array<Filter>;
|
defaultFilters: Array<Filter>;
|
||||||
|
defaultSearchTerm: string;
|
||||||
allowRegexSearch: boolean;
|
allowRegexSearch: boolean;
|
||||||
allowBodySearch: boolean;
|
allowBodySearch: boolean;
|
||||||
};
|
};
|
||||||
@@ -148,7 +149,7 @@ const Searchable = (
|
|||||||
state: State = {
|
state: State = {
|
||||||
filters: this.props.defaultFilters || [],
|
filters: this.props.defaultFilters || [],
|
||||||
focusedToken: -1,
|
focusedToken: -1,
|
||||||
searchTerm: '',
|
searchTerm: this.props.defaultSearchTerm ?? '',
|
||||||
hasFocus: false,
|
hasFocus: false,
|
||||||
regexEnabled: false,
|
regexEnabled: false,
|
||||||
bodySearchEnabled: false,
|
bodySearchEnabled: false,
|
||||||
@@ -235,8 +236,10 @@ const Searchable = (
|
|||||||
if (this.props.onFilterChange != null) {
|
if (this.props.onFilterChange != null) {
|
||||||
this.props.onFilterChange(this.state.filters);
|
this.props.onFilterChange(this.state.filters);
|
||||||
}
|
}
|
||||||
} else if (prevProps.defaultFilters !== this.props.defaultFilters) {
|
} else {
|
||||||
const mergedFilters = [...this.state.filters];
|
let mergedFilters = this.state.filters;
|
||||||
|
if (prevProps.defaultFilters !== this.props.defaultFilters) {
|
||||||
|
mergedFilters = [...this.state.filters];
|
||||||
this.props.defaultFilters.forEach((defaultFilter: Filter) => {
|
this.props.defaultFilters.forEach((defaultFilter: Filter) => {
|
||||||
const filterIndex = mergedFilters.findIndex(
|
const filterIndex = mergedFilters.findIndex(
|
||||||
(f: Filter) => f.key === defaultFilter.key,
|
(f: Filter) => f.key === defaultFilter.key,
|
||||||
@@ -247,8 +250,17 @@ const Searchable = (
|
|||||||
mergedFilters.push(defaultFilter);
|
mergedFilters.push(defaultFilter);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
let newSearchTerm = this.state.searchTerm;
|
||||||
|
if (
|
||||||
|
prevProps.defaultSearchTerm !== this.props.defaultSearchTerm ||
|
||||||
|
prevProps.defaultFilters !== this.props.defaultFilters
|
||||||
|
) {
|
||||||
|
newSearchTerm = this.props.defaultSearchTerm;
|
||||||
|
}
|
||||||
this.setState({
|
this.setState({
|
||||||
filters: mergedFilters,
|
filters: mergedFilters,
|
||||||
|
searchTerm: newSearchTerm,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user