MultiSelect: improuve setters

This commit is contained in:
Paul Chavard 2021-05-20 13:05:36 +02:00
parent 868ec214ce
commit e0d8d096f2

View file

@ -100,12 +100,15 @@ function ComboMultipleDropdownList({
}
};
const saveSelection = (selections) => {
setSelections(selections);
if (hiddenField) {
hiddenField.setAttribute('value', JSON.stringify(selections));
fire(hiddenField, 'autosave:trigger');
}
const saveSelection = (fn) => {
setSelections((selections) => {
selections = fn(selections);
if (hiddenField) {
hiddenField.setAttribute('value', JSON.stringify(selections));
fire(hiddenField, 'autosave:trigger');
}
return selections;
});
};
const onSelect = (value) => {
@ -113,15 +116,15 @@ function ComboMultipleDropdownList({
([val]) => val == value
);
const selectedValue = maybeValue && maybeValue[1];
if (value) {
if (selectedValue) {
if (
acceptNewValues &&
extraOptions[0] &&
extraOptions[0][0] == selectedValue
) {
setNewValues([...newValues, selectedValue]);
setNewValues((newValues) => [...newValues, selectedValue]);
}
saveSelection([...selections, selectedValue]);
saveSelection((selections) => [...selections, selectedValue]);
}
setTerm('');
};
@ -129,8 +132,12 @@ function ComboMultipleDropdownList({
const onRemove = (label) => {
const optionValue = optionValueByLabel(label);
if (optionValue) {
saveSelection(selections.filter((value) => value != optionValue));
setNewValues(newValues.filter((value) => value != optionValue));
saveSelection((selections) =>
selections.filter((value) => value != optionValue)
);
setNewValues((newValues) =>
newValues.filter((value) => value != optionValue)
);
}
inputRef.current.focus();
};