Skip to content

Commit

Permalink
AdHocFiltersCombobox - Improve filter editing behaviour when pre-fill…
Browse files Browse the repository at this point in the history
…ing value on edit (#992)
  • Loading branch information
Sergej-Vlasov authored Dec 5, 2024
1 parent 9910f1a commit 56a6661
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const AdHocCombobox = forwardRef(function AdHocCombobox(
const [inputValue, setInputValue] = useState('');
const [activeIndex, setActiveIndex] = useState<number | null>(null);
const [filterInputType, setInputType] = useState<AdHocInputType>(!isAlwaysWip ? 'value' : 'key');
const [preventFiltering, setPreventFiltering] = useState<boolean>(!isAlwaysWip && filterInputType === 'value');
const styles = useStyles2(getStyles);
// control multi values with local state in order to commit all values at once and avoid _wip reset mid creation
const [filterMultiValues, setFilterMultiValues] = useState<Array<SelectableValue<string>>>([]);
Expand Down Expand Up @@ -191,6 +192,9 @@ export const AdHocCombobox = forwardRef(function AdHocCombobox(
const value = event.target.value;
setInputValue(value);
setActiveIndex(0);
if (preventFiltering) {
setPreventFiltering(false);
}
}

const handleRemoveMultiValue = useCallback(
Expand All @@ -203,7 +207,9 @@ export const AdHocCombobox = forwardRef(function AdHocCombobox(

// operation order on fetched options:
// fuzzy search -> extract into groups -> flatten group labels and options
const filteredDropDownItems = flattenOptionGroups(handleOptionGroups(optionsSearcher(inputValue, filterInputType)));
const filteredDropDownItems = flattenOptionGroups(
handleOptionGroups(optionsSearcher(preventFiltering ? '' : inputValue, filterInputType))
);

// adding custom option this way so that virtualiser is aware of it and can scroll to
if (allowCustomValue && filterInputType !== 'operator' && inputValue) {
Expand Down Expand Up @@ -413,8 +419,12 @@ export const AdHocCombobox = forwardRef(function AdHocCombobox(
(value: SelectableValue<string>) => {
const valueLabel = value.label || value.value!;
setFilterMultiValues((prev) => prev.filter((item) => item.value !== value.value));
setPreventFiltering(true);
setInputValue(valueLabel);
refs.domReference.current?.focus();
setTimeout(() => {
refs.domReference.current?.select();
});
},
[refs.domReference]
);
Expand Down Expand Up @@ -454,6 +464,9 @@ export const AdHocCombobox = forwardRef(function AdHocCombobox(
// this avoids populating input during delete with backspace
if (!hasMultiValueOperator && populateInputOnEdit) {
setInputValue(filter?.value || '');
setTimeout(() => {
refs.domReference.current?.select();
});
}

refs.domReference.current?.focus();
Expand Down

0 comments on commit 56a6661

Please sign in to comment.