react-query-select
v1.0.1
Published
Downloads
5
Readme
QuerySelect Component Documentation
State Management
isCategorySearch
: Tracks whether the user is searching within a category.optionArray
: Holds the list of options to display in the dropdown (Default will be category list on category selection it will list option inside that category).inputSearch
: Stores the user's current input query.
Custom Multi-Value Rendering
The MultiValue
function is used to customize how the selected items (chips) are rendered. It uses the CustomMultiValue
component and passes updateSelection
and enableEditInput
functions, allowing users to edit the values of the chips.
When enableEditInput
is true we will replace chip with a input.
useMultiValueHandlers Hook
const { selection, setSelection, updateSelection, enableEditInput } =
useMultiValueHandlers(onSelectionChangeInternal);
This custom hook (useMultiValueHandlers
) manages the state of the selected items (chips). It provides:
selection
: The current selection of free text and category options.setSelection
: A function to update the selection state.updateSelection
: A function to update a specific chip value based on index. For free text chips.enableEditInput
: Allows the user to edit a selected chip.
Key Functions
a) onSelectionChangeInternal()
const onSelectionChangeInternal = (updatedSelection: OptionType[]) => {
onSelectionChange(
updatedSelection
.filter((option) => option.optionType === "freeText")
.map((option) => option.label)
);
};
This function is called when the selection changes. It filters out the free text chips and sends their labels to the onSelectionChange
callback.
b) createCategoryOptionList
const createCategoryOptionList = (list: string[] | typeof categoryList): OptionType[] => {
return list.map((category) => ({
value: `Category-Chip-${category}`,
label: category,
optionType: "category" as ChipType,
}));
};
This function converts a list of category names into an array of OptionType
objects, each representing a category chip. It’s used when displaying available categories in the dropdown.
c) handleBlur
const handleBlur = () => {
const lastOption = getLastSelectedItem();
if (
inputSearch.length &&
lastOption?.optionType !== "category" &&
!selection.find((value) => value.label === inputSearch)
) {
const newOption = {
label: inputSearch,
optionType: "freeText" as ChipType,
value: `FreeText-Chip-${inputSearch}`,
};
onSelectionChangeInternal([...selection, newOption]);
setSelection((prev) => [...prev, newOption]);
setInputSearch("");
}
};
This function is triggered when the input loses focus or when user click 'Enter'. It creates a new free-text chip if the user has typed something that hasn’t been selected already (We are disabling creation of duplicate free text chip).
d) makeLastChipEditable
const makeLastChipEditable = (e: React.KeyboardEvent<HTMLDivElement>) => {
if (inputSearch || selection.length === 0) {
return;
}
const lastIndex = selection.length - 1;
const lastOption = selection[lastIndex];
const isFreeTextChange = lastOption?.optionType === "freeText";
const isCategoryTypeChange =
lastOption && lastOption.optionType === "category-option";
setCategorySearch(false);
if (isFreeTextChange || isCategoryTypeChange) {
e.preventDefault();
setInputSearch(lastOption?.label || "");
setSelection(selection.slice(0, lastIndex));
if (isCategoryTypeChange) {
setCategorySearch(true);
setOptionArray(categorySearchResults);
selectCategoryOption(undefined);
} else {
onSelectionChangeInternal(selection.slice(0, lastIndex));
}
}
};
This function is triggered when the user presses backspace. If the input is empty and there’s a previous selection, it makes the last chip (category option or free text) editable by moving it back into the input field. If last chip is category type then it will be removed.
e) onSelection
const onSelection = (
newValue: OptionType[],
actionMeta: ActionMeta<OptionType>
) => {
const { action, removedValue, option } = actionMeta;
const currentOption = option || removedValue;
const isFreeTextChange = currentOption?.optionType === "freeText";
const isCategoryChange = currentOption?.optionType === "category";
const isCategoryTypeChange =
currentOption && currentOption.optionType === "category-option";
setSelection(newValue);
setCategorySearch(false);
if (isFreeTextChange) {
onSelectionChangeInternal(newValue);
} else if (isCategoryChange && option) {
setCategorySearch(true);
selectCategory(currentOption.label as T);
setOptionArray(categorySearchResults);
} else if (isCategoryTypeChange && option) {
setOptionArray([]);
selectCategoryOption(option?.value);
}
};
This function is called whenever a selection (chip) is added or removed. It updates the selection state, handles free-text changes, and triggers category-specific behaviors like showing category options.
f) debouncedQueryChange
const debouncedQueryChange = useCallback(() => {
const lastOption = getLastSelectedItem();
onQueryChange?.(
inputSearch,
lastOption?.optionType === "category" ? lastOption.label as T : undefined
);
}, [inputSearch]);
This function debounces the query change event. It sends the latest input value and selected category (if applicable) to the onQueryChange
callback. This reduces the number of times the function is called while typing.