gregnb / mui-datatables

Datatables for React using Material-UI
MIT License
2.71k stars 934 forks source link

Empty strings don't work in Dropdown filters #880

Closed patorjk closed 5 years ago

patorjk commented 5 years ago

In my data there can be fields with empty strings. I noticed that the dropdown filtering doesn't work for them - it treats them the same as the "All" keyword, which leads to odd results when selecting them. You can see it in action here:

https://codesandbox.io/s/muidatatables-custom-toolbar-sisef

The problem stems from the handleDropdownChange method in TableFilter.js:

  handleDropdownChange = (event, index, column) => {
    const labelFilterAll = this.props.options.textLabels.filter.all;
    const value = event.target.value === labelFilterAll ? '' : event.target.value;
    this.props.onFilterUpdate(index, value, column, 'dropdown');
  };

It treats an empty string as well as the "All" keyword as the same, and then passes an empty string to the onFilterUpdate method (filterUpdate in MUIDataTable.js). filterUpdate hits the "default" in the switch statement for dropdowns and removes the filters on an empty string:

  filterUpdate = (index, value, column, type) => {
        ...
        switch (type) {
          case 'checkbox':
            filterPos >= 0 ? filterList[index].splice(filterPos, 1) : filterList[index].push(value);
            break;
          case 'multiselect':
            filterList[index] = value === '' ? [] : value;
            break;
          case 'custom':
            filterList[index] = value;
            break;
          default:
            filterList[index] = filterPos >= 0 || value === '' ? [] : [value];
        }

Expected Behavior

Empty strings should be valid filter options.

Current Behavior

Empty strings, as well as the All keyword, are treated the same.

Steps to Reproduce (for bugs)

  1. https://codesandbox.io/s/muidatatables-custom-toolbar-sisef
  2. Select the Filter icon
  3. Select the empty string option

Your Environment

Tech Version
Material-UI 2.9.0
MUI-datatables 3
React 16.4
browser Chrome
gabrielliwerant commented 5 years ago

The fix should now be in 2.11.0!