import React from "react"; import {Meta} from "@storybook/react"; import {checkbox} from "@heroui/theme"; import {CloseIcon} from "@heroui/shared-icons"; import {button} from "@heroui/theme"; import {useForm} from "react-hook-form"; import {Form} from "@heroui/form"; import {ValidationErrors} from "@react-types/shared"; import {Checkbox, CheckboxIconProps, CheckboxProps} from "../src"; export default { title: "Components/Checkbox", component: Checkbox, argTypes: { color: { control: { type: "select", }, options: ["default", "primary", "secondary", "success", "warning", "danger"], }, radius: { control: { type: "select", }, options: ["none", "sm", "md", "lg", "full"], }, size: { control: { type: "select", }, options: ["sm", "md", "lg"], }, lineThrough: { control: { type: "boolean", }, }, isDisabled: { control: { type: "boolean", }, }, validationBehavior: { control: { type: "select", }, options: ["aria", "native"], }, }, } as Meta; const defaultProps: CheckboxProps = { ...checkbox.defaultVariants, children: "Option", }; const ControlledTemplate = (args: CheckboxProps) => { const [selected, setSelected] = React.useState(true); React.useEffect(() => { // eslint-disable-next-line no-console console.log("Checkbox ", selected); }, [selected]); return (
Subscribe (controlled)

Selected: {selected ? "true" : "false"}

); }; const FormTemplate = (args: CheckboxProps) => { return (
{ e.preventDefault(); const checkbox = e.target["check"] as HTMLInputElement; if (checkbox.checked) { alert(`Submitted value: ${checkbox.value}`); } else { alert("Checkbox is not checked"); } }} > Check
); }; const GroupTemplate = (args: CheckboxProps) => { const items = ["Apple", "Banana", "Orange", "Mango"]; const [selectedItems, setSelectedItems] = React.useState([]); const isSelected = (value: string) => { return selectedItems.some((selected) => selected === value); }; const handleValueChange = (value: string) => { setSelectedItems([value]); }; return (

List of Fruits

{items.map((item, index) => ( handleValueChange(item)} > {item} {isSelected(item) ? "/ state: true" : "/ state: false"} ))}
); }; const WithReactHookFormTemplate = (args: CheckboxProps) => { const { register, formState: {errors}, handleSubmit, } = useForm(); const onSubmit = (data: any) => { // eslint-disable-next-line no-console console.log(data); alert("Submitted value: " + data.example); }; return (
{errors.example && This field is required} ); }; const WithFormTemplate = (args: CheckboxProps) => { const [submitted, setSubmitted] = React.useState<{[key: string]: FormDataEntryValue} | null>( null, ); const [errors, setErrors] = React.useState(undefined); const onSubmit = (e) => { e.preventDefault(); const data = Object.fromEntries(new FormData(e.currentTarget)); const terms = data.terms; if (terms !== "true") { setErrors({terms: "You must agree to the terms and conditions"}); return; } // Clear errors and submit setErrors(undefined); setSubmitted(data); }; return (
setErrors(undefined)} {...args} > I agree to the terms and conditions {errors?.terms && {errors.terms}} {submitted && (
Submitted data:
{JSON.stringify(submitted, null, 2)}
)}
); }; export const Default = { args: { ...defaultProps, }, }; export const IsDisabled = { args: { ...defaultProps, isDisabled: true, }, }; export const DefaultSelected = { args: { ...defaultProps, defaultSelected: true, }, }; export const CustomIconNode = { args: { ...defaultProps, icon: , }, }; export const Group = { render: GroupTemplate, args: { ...defaultProps, }, }; export const WithReactHookForm = { render: WithReactHookFormTemplate, args: { ...defaultProps, }, }; export const CustomIconFunction = { args: { ...defaultProps, // eslint-disable-next-line react/display-name icon: (props: CheckboxIconProps) => , }, }; export const AlwaysSelected = { args: { ...defaultProps, isSelected: true, }, }; export const IsIndeterminate = { args: { ...defaultProps, isIndeterminate: true, }, }; export const LineThrough = { args: { ...defaultProps, lineThrough: true, }, }; export const DisableAnimation = { args: { ...defaultProps, disableAnimation: true, }, }; export const Controlled = { render: ControlledTemplate, args: { ...defaultProps, }, }; export const Required = { render: FormTemplate, args: { ...defaultProps, isRequired: true, }, }; export const WithForm = { render: WithFormTemplate, args: { ...defaultProps, }, };