Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: non-controllable component's options change cause wrong label #1007

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
18 changes: 18 additions & 0 deletions src/Select.tsx
Expand Up @@ -466,6 +466,24 @@ const Select = React.forwardRef(
}
};

// ================ Options change ============

React.useEffect(() => {
// When it's a non-controllable component and options change, update values to match the new label.
if (defaultValue === undefined && value === undefined && rawLabeledValues.length && valueOptions.size && rawLabeledValues.find(item => {
const findedOption = valueOptions.get(item.value);
if (findedOption) {
return findedOption.label !== item.label
}
return false
})) {
bbb169 marked this conversation as resolved.
Show resolved Hide resolved
// should not use triggerChange directly to cause `onChange` event
const values = rawLabeledValues.map(item => valueOptions.get(item.value));
const labeledValues = convert2LabelValues(values);
setInternalValue(labeledValues);
}
}, [valueOptions])
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved

// ======================= Accessibility ========================
const [activeValue, setActiveValue] = React.useState<string>(null);
const [accessibilityIndex, setAccessibilityIndex] = React.useState(0);
Expand Down