npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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.