3.1.1 • Published 4 months ago

chakra-combobox v3.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Chakra Combobox

chakra-combobox is a library based on Chakra UI that provides an asynchronous Combobox component with support for option virtualization and dynamic data loading.

Installation

Before using chakra-combobox, install the necessary dependencies:

pnpm add @chakra-ui/react @emotion/react

If using npm or yarn:

npm install @chakra-ui/react @emotion/react

or

yarn add @chakra-ui/react @emotion/react

Basic Usage with React Query

import { AsyncCombobox } from "chakra-combobox";
import { useState } from "react";
import { useInfiniteQuery } from "@tanstack/react-query";
import { getDogBreeds } from "../api/dogs";

const initialPage = 1;

export const AsyncCombobox = () => {
  const [search, setSearch] = useState("");
  const {
    data: dogs,
    fetchNextPage,
    hasNextPage,
    isLoading,
    isFetchingNextPage,
    isSuccess,
  } = useInfiniteQuery({
    queryKey: ["dogs", search],
    queryFn: ({ pageParam = 0 }) =>
      getDogBreeds({ page: pageParam + 1, limit: 10, search }),
    initialPageParam: initialPage - 1,
    getNextPageParam: (data, _, page) => {
      if (data.hasNextPage) return page + 1;
      return;
    },
  });

  const parsedDogsData = isSuccess
    ? dogs?.pages
        .map(page => page.data)
        .flat()
        .map(dogs => ({
          value: dogs.id.toString(),
          label: dogs.name,
        }))
    : [];

  const [value, setValue] = useState("");

  return (
    <AsyncCombobox
      options={parsedDogsData}
      fetchNextPage={fetchNextPage}
      getOptionLabel={option => option.label}
      getOptionValue={option => option.value}
      onSearchChange={value => setSearch(value)}
      isLoading={isLoading}
      isFetchingNextPage={isFetchingNextPage}
      placeholder="Select a dog"
      hasNextPage={hasNextPage}
      value={value}
      onSelect={setValue}
    />
  );
};

AsyncCombobox Props

PropertyTypeDescription
optionsArray<{ value: string, label: string }>List of available options.
valueanySelected value.
onSelect(option: any) => voidFunction triggered when an option is selected.
getOptionLabel(option: any) => stringFunction returning the option label.
getOptionValue(option: any) => stringFunction returning the option value.
placeholderstringInput placeholder text.
onSearchChange(search: string) => voidFunction called when typing in the search input.
isLoadingbooleanIndicates if data is being loaded.
isFetchingNextPagebooleanIndicates if the next page is being loaded.
hasNextPagebooleanIndicates if there are more options to load.
fetchNextPage() => voidFunction to load more options.
closeOnSelectbooleanClose the dropdown when an option is selected.
chakraStyles{ control?: CSSObject, menuList?: CSSObject, option?: CSSObject, scrollArea?: CSSObject, scrollbar?: CSSObject, scrollThumb?: CSSObject, scrollCorner?: CSSObject, loadingMessage?: CSSObject, emptyMessage?: CSSObject }Customize the component styles.

Styling

chakra-combobox allows style customization via the chakraStyles property:

<AsyncCombobox
  options={options}
  value={selectedOption}
  onSelect={setSelectedOption}
  getOptionLabel={option => option.label}
  getOptionValue={option => option.value}
  placeholder="Select an option"
  fetchNextPage={fetchNextPage}
  onSearchChange={value => setSearch(value)}
  isLoading={isLoading}
  isFetchingNextPage={isFetchingNextPage}
  hasNextPage={hasNextPage}
  closeOnSelect={false}
  chakraStyles={{
    control: base => ({ ...base, borderColor: "blue.500" }),
    menuList: base => ({ ...base, background: "gray.50" }),
    option: base => ({ ...base, color: "black" }),
  }}
/>

Virtualization Support

The component uses react-virtual to render only visible elements on the screen, improving performance when dealing with large lists.

Documentation & Demo

For a full demonstration and detailed documentation, visit the Storybook Documentation.

Conclusion

chakra-combobox is a flexible solution for creating highly customizable asynchronous dropdowns, optimized for performance and seamlessly integrated with Chakra UI.

3.1.3

4 months ago

3.1.2

4 months ago

3.1.1

4 months ago

3.1.0

4 months ago

2.1.0

4 months ago

3.0.0

5 months ago

2.0.4

5 months ago

2.0.3

5 months ago

2.0.2

5 months ago

2.0.1

5 months ago

2.0.0

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago