import * as React from 'react'; import { Box as ReakitBox } from 'reakit'; import { useClassName, createComponent, createElement, createHook, omit, useUniqueId } from '../utils'; import { Box, BoxProps } from '../Box'; import { Label } from '../Label'; import { FieldWrapper, FieldWrapperProps } from '../FieldWrapper'; import * as styles from './Checkbox.styles'; export type LocalCheckboxProps = { /** Automatically focus on the checkbox */ autoFocus?: boolean; /** Indicates if the checkbox is checked. */ checked?: boolean; /** Spreads the props onto the `` element of the checkbox. */ inputProps?: React.InputHTMLAttributes; /** Is the checkbox checked by default? */ defaultChecked?: boolean; /** Disables the checkbox */ disabled?: boolean; /** Forwards the ref to the checkbox. */ checkboxRef?: React.Ref; /** Indicates if the checkbox is indeterminate. */ indeterminate?: boolean; /** Makes the checkbox required and sets aria-invalid to true */ isRequired?: boolean; /** Checkbox label */ label?: string; /** Sets the name of the checkbox. */ name?: string; /** State of the checkbox. Can be any color in the palette. */ state?: string; /** Initial value of the checkbox */ value?: boolean | string; /** Function to invoke when focus is lost */ onBlur?: React.FocusEventHandler; /** Function to invoke when checkbox has changed */ onChange?: React.FormEventHandler; /** Function to invoke when checkbox is focused */ onFocus?: React.FocusEventHandler; }; export type CheckboxProps = Omit & LocalCheckboxProps; const useProps = createHook( (props, { themeKey }) => { const { autoFocus, checked, checkboxRef, inputProps, defaultChecked, disabled, indeterminate, isRequired, label, name, onBlur, onChange, onFocus, state, value, ...restProps } = props; const boxProps = Box.useProps(restProps); const className = useClassName({ style: styles.Checkbox, styleProps: props, themeKey, prevClassName: boxProps.className, }); const checkboxIconClassName = useClassName({ style: styles.CheckboxIcon, styleProps: props, themeKey, themeKeySuffix: 'Icon', }); const hiddenCheckboxClassName = useClassName({ style: styles.HiddenCheckbox, styleProps: props, themeKey, themeKeySuffix: 'HiddenInput', }); const checkboxLabelClassName = useClassName({ style: styles.CheckboxLabel, styleProps: props, themeKey, themeKeySuffix: 'Label', }); const labelId = useUniqueId(); const checkboxId = useUniqueId(); return { ...boxProps, 'aria-describedby': labelId, 'aria-invalid': state === 'danger', 'aria-required': isRequired, className, children: ( {/** // @ts-ignore */} {label && ( )} ), }; }, { themeKey: 'Checkbox' } ); export const Checkbox = createComponent( (props) => { const textProps = useProps(props); return createElement({ children: props.children, component: ReakitBox, use: props.use, htmlProps: textProps }); }, { attach: { useProps, displayName: 'Checkbox', }, defaultProps: { use: 'label', }, themeKey: 'Checkbox', } ); //////////////////////////////////////////////////////////////// export type LocalCheckboxFieldProps = { /** Label for the checkbox */ checkboxLabel?: string; /** Additional props for the Checkbox component */ checkboxProps?: CheckboxProps; }; export type CheckboxFieldProps = BoxProps & FieldWrapperProps & CheckboxProps & LocalCheckboxFieldProps; const useCheckboxFieldProps = createHook( (props, { themeKey }) => { const { autoFocus, checked, checkboxLabel, checkboxProps, checkboxRef, description, defaultChecked, disabled, hint, indeterminate, isOptional, isRequired, label, name, onBlur, onChange, onFocus, overrides, state, tooltip, tooltipTriggerComponent, validationText, value, ...restProps } = props; const boxProps = Box.useProps(restProps); const className = useClassName({ style: styles.CheckboxField, styleProps: props, themeKey, prevClassName: boxProps.className, }); return { ...boxProps, className, children: ( {({ elementProps }) => ( )} ), }; }, { defaultProps: { checkboxProps: {} }, themeKey: 'CheckboxField' } ); export const CheckboxField = createComponent( (props) => { const checkboxFieldProps = useCheckboxFieldProps(props); return createElement({ children: props.children, component: ReakitBox, use: props.use, htmlProps: checkboxFieldProps, }); }, { attach: { useProps, displayName: 'CheckboxField', }, themeKey: 'CheckboxField', } );