import * as React from 'react'; import { Box as ReakitBox } from 'reakit'; import { useClassName, createComponent, createElement, createHook, useOptionsState } from '../utils'; import { Box, BoxProps } from '../Box'; import { FieldWrapper, FieldWrapperProps } from '../FieldWrapper'; import { Set, SetProps } from '../Set'; import { Checkbox, CheckboxProps } from './Checkbox'; import * as styles from './Checkbox.styles'; export type LocalCheckboxGroupProps = { /** Default value(s) of the checkbox group */ defaultValue?: Array; /** Disables the checkbox group */ disabled?: boolean; name: string; /** Checkbox group options */ options: Array; /** Are the checkbox inputs layed out horizontally or vertically? */ orientation?: 'vertical' | 'horizontal'; spacing?: SetProps['spacing']; /** State of the checkbox group. Can be any color in the palette. */ state?: string; /** Controlled value of the checkbox group */ value?: Array; /** Function to invoke when checkbox group has changed */ onChange?: (value: Array, targetValue: string) => void; /** Function to invoke when checkbox group has blurred */ onBlur?: (value: Array) => void; }; export type CheckboxGroupProps = Omit & LocalCheckboxGroupProps; const useProps = createHook( (props, { themeKey }) => { const { defaultValue, disabled, onBlur, onChange, options, orientation, overrides, name, spacing, state, value, ...restProps } = props; //////////////////////////////////////////// const boxProps = Box.useProps(restProps); //////////////////////////////////////////// const className = useClassName({ style: styles.CheckboxGroup, styleProps: props, themeKey, prevClassName: boxProps.className, }); //////////////////////////////////////////// const { getOptionItemProps } = useOptionsState({ defaultValue, onBlur, onChange, type: 'checkbox', value, isNativeInput: true, }); //////////////////////////////////////////// return { role: 'group', ...boxProps, className, children: ( {options.map((option, i) => ( ))} ), }; }, { defaultProps: { orientation: 'vertical', spacing: 'minor-2' }, themeKey: 'CheckboxGroup' } ); export const CheckboxGroup = createComponent( (props) => { const textProps = useProps(props); return createElement({ children: props.children, component: ReakitBox, use: props.use, htmlProps: textProps }); }, { attach: { useProps, displayName: 'CheckboxGroup', }, themeKey: 'CheckboxGroup', } ); //////////////////////////////////////////////////////////////// export type LocalCheckboxGroupFieldProps = { /** Additional props for the CheckboxGroup component */ checkboxGroupProps?: CheckboxGroupProps; }; export type CheckboxGroupFieldProps = BoxProps & FieldWrapperProps & CheckboxGroupProps & LocalCheckboxGroupFieldProps; const useCheckboxGroupFieldProps = createHook( (props, { themeKey }) => { const { defaultChecked, description, disabled, hint, isOptional, isRequired, label, name, options, orientation, onBlur, onChange, overrides, checkboxGroupProps, state, tooltip, tooltipTriggerComponent, value, validationText, ...restProps } = props; const boxProps = Box.useProps(restProps); const className = useClassName({ style: styles.CheckboxGroupField, styleProps: props, themeKey, prevClassName: boxProps.className, }); return { ...boxProps, className, children: ( {({ elementProps }) => ( )} ), }; }, { themeKey: 'CheckboxGroupField' } ); export const CheckboxGroupField = createComponent( (props) => { const checkboxGroupFieldProps = useCheckboxGroupFieldProps(props); return createElement({ children: props.children, component: ReakitBox, use: props.use, htmlProps: checkboxGroupFieldProps, }); }, { attach: { useProps, displayName: 'CheckboxGroupField', }, defaultProps: { use: 'fieldset', }, themeKey: 'CheckboxGroupField', } );