I'm using Semantic React UI Search to filter results of a data table component in React. The table should display all data if search is empty, and display no data or the matching results if search is not empty. My issue is there's always a quick flash of "No data" while you're doing a search.
The original Search code displayed the results as a dropdown, but I modified it to modify the table. Code is below.
class Page extends Component {
resetComponent = () => this.setState({ isLoading: false, results: [], value: '' })
handleSearchChange = (e, { value }) => {
this.setState({ isLoading: true, value })
setTimeout(() => {
if (this.state.value.length < 1) return this.resetComponent()
const re = new RegExp(_.escapeRegExp(this.state.value), 'i')
const isMatch = result => re.test(result.name)
this.setState({
isLoading: false,
results: _.filter(this.props.users, isMatch),
})
}, 200)
}
render() {
const { users } = this.props
const { value, results } = this.state
const dataToShow = _.isEmpty(results) && !value ? users : results
return (
<Container>
<Search
open={false}
loading={isLoading}
onSearchChange={_.debounce(this.handleSearchChange, 500, { leading: true })}
value={value}
{...this.props}
/>
<Table data={dataToShow} />
</Container>
)
}
}
I think the const dataToShow = _.isEmpty(results) && !value ? users : results
line is what causes it to flash, but I don't know how else to display no results if no match, or all results if empty.
How can I get this timeout/debounce to work properly on the table?
If I do <Table data={results} />
the debounce works, but the table does not display all data on initial load.