tbleckert / react-select-search

⚡️ Lightweight select component for React
https://react-select-search.com
MIT License
676 stars 147 forks source link

default value does not set properly. #62

Closed ning-liu-uk closed 4 years ago

ning-liu-uk commented 4 years ago

A really cool project, love it. does exactly what I need.

Might find a defect, my test case: first init with empty value/options, then pass in valid value/options, after re-render, default value would not show properly.

Potential fix as following marked part, should use options from newly passed in props check if new value is valid.

Ning.

componentWillReceiveProps(nextProps) { const nextState = {}; const { defaultOptions, value } = this.state;

    if (nextProps.options !== defaultOptions) {
        const flattenedOptions = FlattenOptions(nextProps.options);

        nextState.options = flattenedOptions;
        nextState.defaultOptions = flattenedOptions;
    }

     if (nextProps.value !== value) {

const option = this.findByValue(nextState.defaultOptions, nextProps.value);

        if (option) {
            nextState.value = nextProps.value;
            nextState.search = option.name;
        } else {
            nextState.value = [];
            nextState.search = '';
        }
    }

    this.setState(nextState);
}
tbleckert commented 4 years ago

@ning-liu-uk Thanks for the kind words and for reporting. This issue is solved in v1 along with lots of other improvements.