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
24 changes: 24 additions & 0 deletions src/Select.tsx
Expand Up @@ -470,6 +470,30 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
}
};

// ================ Options change ============
React.useEffect(() => {
// When it's a non-controllable component and options change.
if (
defaultValue === undefined &&
value === undefined &&
rawLabeledValues.length &&
valueOptions.size
) {
// check whether if match new label.
const findNotMatchLabel = rawLabeledValues.find((item) => {
const findedOption = valueOptions.get(item.value);
return findedOption ? findedOption.label !== item.label : false;
});
// update values to match the new label.
if (findNotMatchLabel) {
// 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]);

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