use with formik #111
-
Is your feature request related to a problem? Please describe. Describe the solution you'd like Describe alternatives you've considered Here is my code, it works with the normal Chakra UI Select. <Field name="subject" validate={validateSubject}>
{({ field, form }: FieldProps) => (
<FormControl
isInvalid={!!form.errors.subject && !!form.touched.subject}
isRequired
>
<FormLabel mt="4">Subject</FormLabel>
<Select
{...field}
id="subject"
placeholder="Select subject.."
options={options}
rounded="4"
/>
<FormErrorMessage>{form.errors.subject}</FormErrorMessage>
</FormControl>
)}
</Field> But I get this error with the new select.
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
I wouldn't think this component would work as a drop in replacement for Chakra's built in Any chance you could provide a minimally reproduced example using one of the templates I offer in the Readme? If you do, I can make the changes needed for it to work properly. |
Beta Was this translation helpful? Give feedback.
-
I had some free time so I made an example of using this component with Formik. I personally use react-hook-form, so I don't remember how Formik works that well but I made it work based on this example I found. Here it is, let me know if it helps: https://codesandbox.io/s/chakra-react-select-single-select-with-formik-pwz2s?file=/example.js |
Beta Was this translation helpful? Give feedback.
-
Here is a working Typescript example we use (getting some data for the dropdown via an async API call): import { FormControl, FormLabel, FormHelperText, Box } from '@chakra-ui/react';
import { FieldProps } from 'formik';
import { Select } from "chakra-react-select";
import { AccountList, AccountOut } from '@/types';
import { getData} from '@/api';
interface Option{
value: string
label: string;
}
type OptionList= Array<Option>;
export const AsyncSelect = ({
field,
form: { setFieldValue },
...props
}: FieldProps) => {
const { data, isLoading, error } = getData();
const defaultValue = (options: OptionList, value: string) => {
return options ? options.find(option => option.value === value) : ""
}
return (
<FormControl>
<FormLabel>Some label</FormLabel>
<FormHelperText>Start typing</FormHelperText>
{isLoading ? (
<div>Loading</div>
) : error ? (
<div>Error</div>
) : data ? (
<Box>
<Select
defaultValue={defaultValue(data, field.value)}
placeholder="Select an value..."
options={data}
onChange={
(option: Option) => setFieldValue(field.name, option.value)
}
/>
</Box>
) : null}
</FormControl>
)
}; |
Beta Was this translation helpful? Give feedback.
-
Doesn't really work in my case import { FieldProps } from 'formik'
import { Select } from "chakra-react-select"
interface Option {
value: string
label: string;
}
export const AsyncSelect = ({ field, form: { setFieldValue }, ...props }: FieldProps) => {
return (
<Select
onChange={ (option: Option) => setFieldValue(field.name, option.value) }
/>
)
}; results in I've got my own version of the component, which still doesn't work, unfortunately import { useField, FieldProps } from "formik"
import { Select as ChakraSelect, InputProps } from "chakra-react-select"
type Props = InputProps & FieldProps
export const Select = ({ options, name, form: { setFieldValue } }: Props) =>{
const [ field ] = useField(name as string)
return (
<ChakraSelect
name={field.name}
placeholder=""
useBasicStyles
options={options}
value={options ? options.find((option: any) => option.value == field.value) : ""}
onChange={(option: any) => setFieldValue(field.name, option.value)}
/>
)
} used like <Select name="role.id" options={getRoles() as Option[]} /> results in Still very new to TypeScript and React. Any help is appreciated |
Beta Was this translation helpful? Give feedback.
-
For any newcomers, this is final form of a working component I ended up. Had to wait before posting I guess import { useField } from "formik"
import { Select as ChakraSelect } from "chakra-react-select"
export interface Option {
label: string
value: string
}
type Props = {
name: string
options: Option[]
}
export const Select = (props: Props) =>{
const [ field, meta, helpers ] = useField(props.name)
const { setValue } = helpers
return (
<ChakraSelect
name={field.name}
placeholder=''
useBasicStyles
options={props.options}
value={props.options ? props.options.find((option: any) => option.value == field.value) : ""}
onChange={(option: any) => setValue(option.value)}
/>
)
} |
Beta Was this translation helpful? Give feedback.
Here is a working Typescript example we use (getting some data for the dropdown via an async API call):