table icon indicating copy to clipboard operation
table copied to clipboard

setAllFilters doesn't update Filter UI's JSX

Open chuckrea opened this issue 6 years ago • 1 comments

Using v7?

I am using v7

Describe the bug Calling setAllFilters[] will reset the filters in state but will not update that column's Filter UI. For example, I have a dropdown selector for filtering a column. Once a selection is made, the table will filter. Calling setAllFilters[] will return the table to its original, unfiltered state, but the dropdown selector will remain in the state as if the filter was still selected.

This seems to be specifically related to <select> filters. Click toggles and input search filters seem to work as intended

function DropdownFilter({ column: { Header, filterValue, setFilter, preFilteredRows, id } }) {
	// Calculate the options for filtering
	// using the preFilteredRows
	// Also, de-dupe them
	const options = React.useMemo(() => {
		const options = [];
		preFilteredRows.forEach(row => {
			options.push(row.values[id])
		});
		return options.filter((option, i) => options.indexOf(option) === i);
	}, [id, preFilteredRows])

	// Render a multi-select box
	return (
		<select
			value={filterValue}
			onChange={e => {
				setFilter(e.target.value || undefined)
			}}
		>
		<option value="">{Header}</option>
			{options.map((option, i) => (
				<option key={i} value={option}>
					{option}
				</option>
			))}
		</select>
	)
}

To Reproduce Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior The Filter UI should update as well to correctly reflect state

Codesandbox! Use a new react-table codesandbox to reproduce the issue.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context Add any other context about the problem here.

chuckrea avatar Mar 09 '20 17:03 chuckrea

setting the value of the select tag to

value={filterValue || ''}

did the trick

chuckrea avatar Mar 09 '20 18:03 chuckrea