import { Field } from "formik"; import type { FieldInputProps, FieldMetaProps, FieldProps, FormikProps, FormikValues } from "formik"; import { Switch as HeadlessSwitch } from "@headlessui/react"; import { classNames } from "../../utils"; type SwitchProps = { label: string checked: boolean disabled?: boolean onChange: (value: boolean) => void field?: FieldInputProps form?: FormikProps meta?: FieldMetaProps } export const Switch = ({ label, checked: $checked, disabled = false, onChange: $onChange, field, form, }: SwitchProps) => { const checked = field?.checked ?? $checked return ( {label} { form?.setFieldValue(field?.name ?? '', value) $onChange && $onChange(value) }} className={classNames( checked ? 'bg-teal-500 dark:bg-blue-500' : 'bg-gray-200 dark:bg-gray-600', 'ml-4 relative inline-flex flex-shrink-0 h-6 w-11 border-2 border-transparent rounded-full cursor-pointer transition-colors ease-in-out duration-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500' )} > {({ checked }) => ( ) } export type SwitchFormikProps = SwitchProps & FieldProps & React.InputHTMLAttributes; export const SwitchFormik = (props: SwitchProps) => interface SwitchGroupProps { name: string; label?: string; description?: string; className?: string; } const SwitchGroup = ({ name, label, description }: SwitchGroupProps) => ( {label &&
{label} {description && ( {description} )}
} {({ field, form: { setFieldValue }, }: any) => ( { setFieldValue(field?.name ?? '', value) }} className={classNames( field.value ? 'bg-teal-500 dark:bg-blue-500' : 'bg-gray-200', 'ml-4 relative inline-flex flex-shrink-0 h-6 w-11 border-2 border-transparent rounded-full cursor-pointer transition-colors ease-in-out duration-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500' )} > )}
); export { SwitchGroup }