Skip to content

Issue with boolean filterOptions #200

@alexggordon

Description

@alexggordon

We've run into an issue with the filterOption prop. The issue is in the filterMixin.js in the filterOption function.

  filterOption(input, child, defaultFilter = defaultFilterFn) {
    if (!input) {
      return true;
    }
    const filterOption = ('filterOption' in this.props) ?
      this.props.filterOption : defaultFilter;
    if (!filterOption) {
      return true;
    } else if (child.props.disabled) {
      return false;
    } else if (typeof filterOption === 'function') {
      return filterOption.call(this, input, child);
    }
    return true;
  }

Here, if this.props.filterOption is true, then it returns true (no if/else statements handle this), however, if this.props.filterOption is false, then true is also returned (handled by the first if (!filterOption) { statement. So when you set the filterOption to true (as is the "default") it will not actually filter anything as expected.

We believe the desired behavior is when setting filterOption to true, use the defaultFilterFn, however, when setting filterOption to false, to not filter the options. Then obviously if filterOption is a function, call that function.

We've amended the filterOption function to do this with this code.

  filterOption(input, child, defaultFilter = defaultFilterFn) {
    if (!input) {
      return true;
    }

    if (typeof this.props.filterOption === 'boolean' && this.props.filterOption === false) {
      return true;
    }

    if (child.props.disabled) {
      return false;
    }

    const filterOption = typeof this.props.filterOption === 'function' ?
      this.props.filterOption : defaultFilter;

    return filterOption.call(this, input, child);
  }

However, this breaks the filterOption could be true as described in default value spec. After looking at this spec, we're not sure what it should be testing. If our assumptions above are correct, we believe this test is incorrect. Can you explain or comment on this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions