### Install chakra-react-select Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Install the package using yarn. ```bash yarn add chakra-react-select ``` -------------------------------- ### Install chakra-react-select Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Install the chakra-react-select package from NPM after setting up Chakra UI. ```sh npm i chakra-react-select ``` -------------------------------- ### Install Chakra UI and Emotion Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Install the necessary Chakra UI and Emotion packages before installing chakra-react-select. Ensure you are using Chakra UI v3 and React v18 or above. ```sh npm i @chakra-ui/react @emotion/react # ...or... yarn add @chakra-ui/react @emotion/react ``` -------------------------------- ### React Select ClassNames Example Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Illustrates how classNames are applied to sub-components in react-select, similar to the original package. Useful for styling with CSS, CSS modules, or styled-components. ```html
...
...
...
``` -------------------------------- ### React Hook Form useController Single Select (JS) Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Demonstrates using the useController hook with chakra-react-select for a single select input in a React Hook Form setup. This JavaScript example covers basic integration. ```javascript import * as React from 'react' import { useForm, useController } from 'react-hook-form' import { Select } from 'chakra-react-select' const options = [ { label: 'Ocean', value: 'ocean' }, { label: 'Blue', value: 'blue' }, { label: 'Purple', value: 'purple' }, { label: 'Red', value: 'red' }, ] function App() { const { control, handleSubmit } = useForm() const { field } = useController({ name: 'select', control, rules: { required: true }, }) const onSubmit = (values) => console.log(values) return (
{error &&

{error.message}

} )} />
) } export default App ``` -------------------------------- ### Custom Option and MultiValue Components with Icons Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Create custom Option and MultiValueContainer components to display icons associated with each option. This example uses react-icons/gi for various food icons and Chakra UI's Box and Icon components. ```tsx import { Box, Icon, useSlotRecipe } from "@chakra-ui/react"; import { type GroupBase, Select, type SelectComponentsConfig, chakraComponents, } from "chakra-react-select"; import { GiCherry, GiChocolateBar, GiCoffeeBeans, GiStrawberry, } from "react-icons/gi"; interface FlavorOption { label: string; value: string; Icon: React.FC; iconColor: string; } const flavorOptions: FlavorOption[] = [ { value: "coffee", label: "Coffee", Icon: GiCoffeeBeans, iconColor: "orange.700", }, { value: "chocolate", label: "Chocolate", Icon: GiChocolateBar, iconColor: "yellow.800", }, { value: "strawberry", label: "Strawberry", Icon: GiStrawberry, iconColor: "red.500", }, { value: "cherry", label: "Cherry", Icon: GiCherry, iconColor: "red.600", }, ]; const customComponents: SelectComponentsConfig< FlavorOption, true, GroupBase > = { Option: ({ children, ...props }) => ( {children} ), MultiValueContainer: ({ children, ...props }) => { const tagStyles = useSlotRecipe({ key: "tag" })(); return ( {children} ); }, }; const OptionsWithIconsExample = () => ( ) } export default App ``` -------------------------------- ### React Hook Form Multi Select with Zod Validation (TS) Source: https://github.com/csandman/chakra-react-select/blob/main/README.md This TypeScript example demonstrates integrating chakra-react-select with React Hook Form and Zod for validating multi-select inputs. It includes Zod schema definition and form setup. ```typescript import * as React from 'react' import { useForm, Controller, FormProvider, SubmitHandler, } from 'react-hook-form' import { z } from 'zod' import { zodResolver } from '@hookform/resolvers/zod' import { Select, Option } from 'chakra-react-select' const options: Option[] = [ { label: 'Ocean', value: 'ocean' }, { label: 'Blue', value: 'blue' }, { label: 'Purple', value: 'purple' }, { label: 'Red', value: 'red' }, ] const schema = z.object({ select: z .array(z.object({ label: z.string(), value: z.string() })) .min(1, 'Please select at least one color'), }) type Inputs = { select: Option[] } function App() { const methods = useForm({ resolver: zodResolver(schema), defaultValues: { select: [], }, }) const { handleSubmit, control } = methods const onSubmit: SubmitHandler = (values) => console.log(values) return (
( <> ) } export default App ``` -------------------------------- ### React Hook Form Multi Select with Zod Validation (JS) Source: https://github.com/csandman/chakra-react-select/blob/main/README.md This JavaScript example shows how to integrate chakra-react-select with React Hook Form and Zod for schema-driven validation of multi-select inputs. ```javascript import * as React from 'react' import { useForm, Controller, FormProvider, useFormContext, } from 'react-hook-form' import { z } from 'zod' import { zodResolver } from '@hookform/resolvers/zod' import { Select } from 'chakra-react-select' const options = [ { label: 'Ocean', value: 'ocean' }, { label: 'Blue', value: 'blue' }, { label: 'Purple', value: 'purple' }, { label: 'Red', value: 'red' }, ] const schema = z.object({ select: z .array(z.object({ label: z.string(), value: z.string() })) .min(1, 'Please select at least one color'), }) function App() { const methods = useForm({ resolver: zodResolver(schema), defaultValues: { select: [], }, }) const { handleSubmit, control } = methods const onSubmit = (values) => console.log(values) return (
( <> {error &&

{error.message}

} )} />
) } export default App ``` -------------------------------- ### React Hook Form Single Select with Zod Validation (JS) Source: https://github.com/csandman/chakra-react-select/blob/main/README.md This JavaScript example shows how to use Zod with React Hook Form and chakra-react-select to validate a single select input. ```javascript import * as React from 'react' import { useForm, Controller, FormProvider, useFormContext, } from 'react-hook-form' import { z } from 'zod' import { zodResolver } from '@hookform/resolvers/zod' import { Select } from 'chakra-react-select' const options = [ { label: 'Ocean', value: 'ocean' }, { label: 'Blue', value: 'blue' }, { label: 'Purple', value: 'purple' }, { label: 'Red', value: 'red' }, ] const schema = z.object({ select: z.object({ label: z.string(), value: z.string() }), }) function App() { const methods = useForm({ resolver: zodResolver(schema), defaultValues: { select: undefined, }, }) const { handleSubmit, control } = methods const onSubmit = (values) => console.log(values) return (
( <> ) } export default App ``` -------------------------------- ### React Hook Form Multi Select with Yup Validation (JS) Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Integrate chakra-react-select with React Hook Form and Yup for advanced validation. This JavaScript example demonstrates setting up a schema with Yup for a multi-select input. ```javascript import * as React from 'react' import { useForm, Controller, FormProvider, useFormContext, } from 'react-hook-form' import * as yup from 'yup' import { yupResolver } from '@hookform/resolvers/yup' import { Select } from 'chakra-react-select' const options = [ { label: 'Ocean', value: 'ocean' }, { label: 'Blue', value: 'blue' }, { label: 'Purple', value: 'purple' }, { label: 'Red', value: 'red' }, ] const schema = yup.object({ select: yup.array().of(yup.object()).min(1, 'Please select at least one color').required(), }) function App() { const methods = useForm({ resolver: yupResolver(schema), defaultValues: { select: [], }, }) const { handleSubmit, control } = methods const onSubmit = (values) => console.log(values) return (
( <> )} /> ) } ``` -------------------------------- ### React Hook Form Single Select with Zod Validation (TS) Source: https://github.com/csandman/chakra-react-select/blob/main/README.md This TypeScript example demonstrates using Zod with React Hook Form and chakra-react-select for validating single select inputs. It includes schema definition and form integration. ```typescript import * as React from 'react' import { useForm, Controller, FormProvider, SubmitHandler, } from 'react-hook-form' import { z } from 'zod' import { zodResolver } from '@hookform/resolvers/zod' import { Select, Option } from 'chakra-react-select' const options: Option[] = [ { label: 'Ocean', value: 'ocean' }, { label: 'Blue', value: 'blue' }, { label: 'Purple', value: 'purple' }, { label: 'Red', value: 'red' }, ] const schema = z.object({ select: z.object({ label: z.string(), value: z.string() }), }) type Inputs = { select: Option } function App() { const methods = useForm({ resolver: zodResolver(schema), defaultValues: { select: undefined, }, }) const { handleSubmit, control } = methods const onSubmit: SubmitHandler = (values) => console.log(values) return (
( <> {error &&

{error.message}

} )} />
) } export default App ``` -------------------------------- ### Import Select Components (ESM) Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Import the base select package, async select, creatable select, or async creatable select using ES module syntax. ```javascript import { AsyncCreatableSelect, AsyncSelect, CreatableSelect, Select, } from "chakra-react-select"; ``` -------------------------------- ### Run Codemod Transformation Source: https://github.com/csandman/chakra-react-select/blob/main/codemod/README.md Use this command to run a codemod transformation on your project. Replace `` with the codemod name and `` with the target files or directory. Use `--dry` for a dry run or `--print` to see changes. ```sh npx crs-codemod@latest ``` -------------------------------- ### Import Select Components (CommonJS) Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Import the base select package, async select, creatable select, or async creatable select using CommonJS syntax. ```javascript const { AsyncCreatableSelect, AsyncSelect, CreatableSelect, Select, } = require("chakra-react-select"); ``` -------------------------------- ### Style MenuPortal with CSS and classNamePrefix Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Alternatively, style the `MenuPortal` using CSS by providing a `classNamePrefix` to the Select component and targeting the generated class (e.g., `prefix__menu-portal`). ```tsx // example.js import "styles.css"; return ; }; ``` -------------------------------- ### Use useChakraSelectProps with GooglePlacesAutocomplete Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Style the `react-google-places-autocomplete` component using `useChakraSelectProps` by passing the hook's output to the `selectProps` prop. This allows you to leverage Chakra UI's styling for the Google Places autocomplete input. ```tsx import { useState } from "react"; import { useChakraSelectProps } from "chakra-react-select"; import GooglePlacesAutocomplete from "react-google-places-autocomplete"; const GooglePlacesAutocomplete = () => { const [place, setPlace] = useState(null); const selectProps = useChakraSelectProps({ value: place, onChange: setPlace, }); return ( ); }; export default GooglePlacesAutocomplete; ``` -------------------------------- ### Apply Different Sizes to Select Component Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Use the `size` prop to control the visual size of the Select component, with options 'sm', 'md', and 'lg'. Defaults to 'md' if not specified. ```tsx return ( <> {/* Default */} ({ ...provided, // This is the z-index of the normal select in Chakra. zIndex: "var(--chakra-z-index-dropdown)", }), }} chakraStyles={ { // All other component styles } } /> ); ``` -------------------------------- ### Customize Tag Color Palette and Option Variants Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Set a global `tagColorPalette` for all selected option tags, or define a specific `colorPalette` on individual options. The option-specific `colorPalette` overrides the global setting. ```tsx return ( `). ```js return ( <> {/* Chakra UI Menu Style */} ); ``` -------------------------------- ### Customize Select Focus Ring Color Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Use the `focusRingColor` prop with Chakra color tokens to emulate the styling of a control component when it is focused. ```tsx return ; }; ``` -------------------------------- ### Customize Selected Option Color Palette Source: https://github.com/csandman/chakra-react-select/blob/main/README.md When using `selectedOptionStyle="color"`, you can change the highlight color using `selectedOptionColorPalette`. This prop accepts named colors from your theme and uses `colorPalette.solid` for the background and `colorPalette.contrast` for the text. This prop only accepts named colors from your theme, not arbitrary hex/rgb values. ```tsx return ( <> ); ``` -------------------------------- ### Applying Select Variants Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Use the `variant` prop to change the styling of the Select component. Options include `outline` (default) and `subtle`. Custom variants from your Chakra theme can also be used. ```tsx return ( <> ); ``` -------------------------------- ### Customize Tag Variants for Select Component Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Control the visual style of selected option tags using the `tagVariant` prop ('subtle', 'solid', 'outline'). Individual options can override this with their own `variant` key. ```tsx return ( )} /> ) } ``` -------------------------------- ### Customize Dropdown Indicator Icon Rotation Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Use the `dropdownIndicator` key within `chakraStyles` to target the SVG element and apply transformations, such as rotating the down chevron based on menu open state. ```javascript const chakraStyles = { dropdownIndicator: (prev, { selectProps }) => ({ ...prev, "> svg": { transform: `rotate(${selectProps.menuIsOpen ? -180 : 0}deg)`, }, }), }; ``` -------------------------------- ### Style Select as Invalid or ReadOnly Source: https://github.com/csandman/chakra-react-select/blob/main/README.md Pass `invalid` to style the select like Chakra's Input with an invalid state, or `readOnly` to make it non-interactive. These props can also be passed to a wrapping `Field.Root` to achieve the same effect. ```tsx return ( <> {/* This will show up with a red border */} {/* This will show up grayed out and will not be interactive */} {/* Additionally, it will have a red border and the error message will be shown */} This error message shows because of an invalid Field.Root ); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.