UI Bloom

React Select Input

These components are built to seamlessly integrate React Select with shadcn/ui. They provide a smooth developer experience when working with react-select in projects that use shadcn's design system. By using these components, you can apply shadcn-style customization and styling, along with built-in error handling when used within a form. Additionally, when used inside a Form component, these components come with helper functions that simplify validation and data handling, making form integration much easier and more consistent.

How To Use

Update global.css

Be sure to import the following CSS, as it includes a utility class used to hide the scrollbar within the component. If you also want to hide the scrollbar in the input area where selected values are displayed, you can add the same CSS to your global or main stylesheet.

/* no scroll bar for react-select input field */
.no-scrollbar {
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.no-scrollbar::-webkit-scrollbar {
  display: none;
}

Install

Run the following command in terminal to add these components in your project.

Now, let's go through each component and helper function in detail. This will help you understand their purpose, how to use them, and where they fit best within your project.

Simple Select Component

About

This is a simple select component. You need to provide the options prop, and the component will render the selection list based on those options.

Import

First, import this component in the file where you want to use it.

import {
ReactSelect,
convertToOptions,
convertToOption,
} from "@/components/bloom/react-select-input";

Preview

You can now use it within your code as needed.

Open in

Accepted Props

This component accepts all the props supported by the default react-select base component.

View all available props in the official React Select documentation

Async Select Component

About

This is an async select component. You need to provide a loadOptions prop, which should be an asynchronous function that returns a promise. This allows you to fetch options dynamically from a backend or external API.

Import

First, import this component in the file where you want to use it.

import {
ReactAsyncSelect,
convertToOptions,
MyOption,
flattenOptions,
convertToOption,
} from "@/components/bloom/react-select-input";

Preview

You can now use it within your code as needed.

Open in

Accepted Props

This component accepts all the props supported by the default react-select async component.

View all available props in the official React Select documentation

Creatable Select Component

About

This is a creatable select component. It works with the options prop, just like a standard select. Additionally, it allows users to create and select new options that are not already in the list. This is useful when the available choices are dynamic or user-defined.

Import

First, import this component in the file where you want to use it.

import {
ReactCreatableSelect,
convertToOptions,
convertToOption,
} from "@/components/bloom/react-select-input";

Preview

You can now use it within your code as needed.

Open in

Accepted Props

This component accepts all the props supported by the default react-select creatable component.

View all available props in the official React Select documentation

Async Creatable Select Component

About

This is an async creatable select component. It requires a loadOptions prop, which should be an asynchronous function—typically used to fetch options from an API. In addition to loading options dynamically, it also allows users to create new options if the desired one doesn't already exist.

Import

First, import this component in the file where you want to use it.

import {
ReactAsyncCreatableSelect,
convertToOptions,
MyOption,
flattenOptions,
convertToOption,
} from "@/components/bloom/react-select-input";

Preview

You can now use it within your code as needed.

Open in

Accepted Props

This component accepts all the props supported by the default react-select async component.

View all available props in the official React Select documentation

Usage Restriction

The default value in select component for single select must be an object null you can get it using convertToOption functions this way your form.reset function will work properly and for multi select default value must be an empty array.

Helper Functions

These functions help manipulate and transform options in a structured way.

flattenOptions

About

This utility function is used to convert grouped options into a flat array. It removes any nested structure and returns a single-level array of options, making it easier to work with components that expect flat data.

Import

You can import it like this and use it to convert group options to a single level

import { flattenOptions } from '@/components/bloom/react-select-input';

Accepted Parameters

PropTypeDefault
options
OptionsOrGroups<MyOption, GroupBase<MyOption>>
-

convertToOption

About

Retrieves an option object based on a given string value and an array of options. This function is particularly useful for setting the default value or selected value in a single-select input.

Import

import { convertToOption } from '@/components/bloom/react-select-input';

Use

const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
color: convertToOption("", options),
},
});

Accepted Parameters

PropTypeDefault
value
string
-
options
OptionsOrGroups<MyOption, GroupBase<MyOption>>
-
valueField?
string
value

convertToOptions

Get an array of string and array of options and return the array of an option object to use in default value or value use if select accepts multiple values.

Import

import { convertToOptions } from '@/components/bloom/react-select-input';

Use

const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
color: convertToOptions(["smoothie", "juice"], groupedOptions),
},
});

Accepted Parameters

PropTypeDefault
value?
string[]
[]
options
OptionsOrGroups<MyOption, GroupBase<MyOption>>
-
valueField?
string
value

getFields

This function extracts a specific field from an array of selected option objects. It is especially useful in form submit handlers when you need to retrieve only a particular property (e.g., id, value, or label) from the selected options.

For example, if you have selected options with multiple fields but only need the value field for submission, this function helps simplify that extraction.

Import

import { getFields } from '@/components/bloom/react-select-input';

Accepted Parameters

PropTypeDefault
field?
string[]
[]
options
MyOption[]
-

Dependencies

This component depends on the react-select library.

View react-select Documentation