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