Package Exports
- chakra-react-select
Readme
chakra-react-select
This component is a wrapper for the popular react component react-select made using the UI library Chakra UI.
Check out the demo here: https://codesandbox.io/s/chakra-react-select-demo-65ohb?file=/example.js
Usage
In order to use this package, you'll need to have @chakra-ui/react
set up like in the guide in their docs. Then install this package:
npm i chakra-react-select
Then you can import the base select package, the async select, the creatable select or the async creatable select:
import {
Select,
AsyncSelect,
CreatableSelect,
AsyncCreatableSelect,
} from "chakra-react-select";
In order to use this component, you can implement it and use it like you would normally use react-select. It should accept all of the props that the original takes, however customizing the theme
or the components
could break this implementation so change them at your own risk. There are also a few extra things you can do with this wrapper that pull from the chakra library.
- You can pass the
size
prop with eithersm
,md
, orlg
(default ismd
). These will reflect the sizes available on the Chakra<Input />
component (with the exception ofxs
because it's too small to work).
return (
<>
<Select size="sm" />
<Select size="md" /> {/* Default */}
<Select size="lg" />
</>
);
- You can pass the
colorScheme
prop to the select component to change all of the selected options tags' colors. You can view the whole list of available color schemes in the Chakra docs, or if you have a custom color palette, any of the custom color names in that will be available instead.- Alternatively you can add the
colorScheme
key to any of your options objects and it will only style that option when selected.
- Alternatively you can add the
return (
<Select
{/* The global color scheme */}
colorScheme="purple"
options={[
{
label: "I am red",
value: "i-am-red",
colorScheme: "red", // The option color scheme overrides the global
},
{
label: "I fallback to purple",
value: "i-am-purple",
},
]}
/>
);
- You can pass the
tagVariant
prop with eithersubtle
,solid
, oroutline
(default issubtle
). These will reflect thevariant
prop available on the Chakra<Tag />
component.- Alternatively you can add the
variant
key to any of your options objects and it will only style that option when selected. This will override thetagVariant
prop on the select if both are set
- Alternatively you can add the
return (
<Select
{/* The global variant */}
tagVariant="solid"
options={[
{
label: "I have the outline style",
value: "i-am-outlined",
variant: "outline", // The option variant overrides the global
},
{
label: "I fallback to the global `solid`",
value: "i-am-solid",
},
]}
/>
);
- You can pass
isInvalid
to the select component to style it like the Chakra<Input />
is styled when it receives the same prop.- You can pass
isInvalid
orisDisabled
to a<FormControl />
which surrounds this component and it will output their corresponding<Input />
styles.
- You can pass
return (
<>
{/* This will show up with a red border */}
<Select isInvalid />
{/* This will show up with a red border, and grayed out */}
<FormControl isInvalid isDisabled>
<FormLabel>Invalid & Disabled Select</FormLabel>
<Select />
<FormErrorMessage>
This error message shows because of an invalid FormControl
</FormErrorMessage>
</FormControl>
</>
);
- One thing I added which isn't specific to Chakra or react-select is sticky group headers. It adds a border to the bottom of the header and keeps it in view while its corresponding group of options is visible. This can be very nice for when you have long lists of grouped options so you can always tell which group of options you're looking at. To add it, pass the
hasStickyGroupHeaders
prop to the select component.
return <Select hasStickyGroupHeaders />;
- In your options objects, you can add the key
isFixed: true
to emulate the example in the react-select docs. This will prevent the options which have this flag from having the remove button on its corresponding tag. This only applies when usingisMulti
is passed.
return (
<Select
isMulti
options={[
{
label: "I can't be removed",
value: "fixed",
isFixed: true,
},
{
label: "I can be removed",
value: "not-fixed",
},
]}
/>
);
- In
v1.3.0
you can now pass the propselectedOptionStyle
with either"color"
or"check"
(defaults to"color"
). Until this version I had forgotten to style the selected options in the menu for both the single select, or the multi-select withhideSelectedOptions
set tofalse
. The default option"color"
will style a selected option similar to how react-select does it, by highlighting the selected option in the color blue. Alternatively if you pass"check"
for the value, the selected option will be styled like the Chakra UI Menu component and include a check icon next to the selected option(s). IfisMulti
andselectedOptionStyle="check"
are passed, space will only be added for the check marks ifhideSelectedOptions={false}
is also passed.
return (
<>
<Select selectedOptionStyle="color" /> {/* Default */}
<Select selectedOptionStyle="check" /> {/* Chakra UI Menu Style */}
</>
);
- If you choose to stick with the default
selectedOptionStyle="color"
, you have one additional styling option. If you do not like the default of blue for the highlight color, you can pass theselectedOptionColor
prop to change it. This prop will accept any named color from your color theme, and it will use the500
value in light mode or the300
value in dark mode.
return (
<>
<Select selectedOptionColor="blue" /> {/* Default */}
<Select selectedOptionColor="purple" />
</>
);
If you have any other questions or requests, leave it as an issue. I'm sure there are some features of react-select
that I missed and I definitely want to make this wrapper as good as it can be!
Roadmap
Since releasing this project, there have been a few things brought to my attention from users that I would like to update in the near future.
react-select v5
It was brought to my attention in this issue that react-select had a version 5 release almost immediately after I released this package (great timing right 😏). This version is rebuilt in TypeScript so you no longer need to install @types/react-select
to access the types. I made a first pass at upgrading to the new version, and the errors I faced along with some comments I have been receiving from people have made me realize that I probably set up the types incorrectly when I first made this project. I still plan to make the switch soon, however I will need to take a deep dive into the inner workings of react-select's TypeScript support along with how TypeScript works itself, as i am very new to it. If anyone would like to help me make the upgrade/fix the way my types are implemented, I'd greatly appreciate it!
Better Customization
It has been requested multiple times for me to include some way to customize the components to the same degree that they can be customized in the original react-select package. This will involve a great deal of improvement to the flexibility of this wrapper which was originally intended to be a basic wrapper in order to match Chakra UI's styles. I plan to do this, however I am still working out the implementation details.
As of right now, my plan is to nix the theme
prop and rely on passing custom components
which extend the components I have made for this wrapper. If I had both, I believe there would be too much room for error and I wouldn't be confident that everything would look right. Besides, Chakra has a built in prop system for styling (style props, sx
, and __css
) so implementing the theme prop on top of that would most likely be overkill.