import * as React from 'react'; import { ViewProps as RNViewProps } from 'react-native'; import { createComponent, createElement, createHook, useOptionsState } from 'bumbag/utils'; import { Box, BoxProps } from '../Box'; import { SetProps } from '../Set'; import { CheckboxProps } from './Checkbox'; import { FieldWrapper, FieldWrapperProps } from '../FieldWrapper'; import * as styles from './Checkbox.styles'; export type LocalCheckboxGroupProps = { /** Alignment of the checkbox group */ align?: 'left' | 'right'; /** Default value(s) of the checkbox group */ defaultValue?: Array; /** Disables the checkbox group */ disabled?: boolean; /** Checkbox group options */ options: Array; /** Are the checkbox inputs layed out horizontally or vertically? */ orientation?: 'vertical' | 'horizontal'; /** Color of the checkbox group. Can be any color in the palette. */ palette?: string; 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; /** Overrides for the CheckboxGroup `Set` component. */ setProps?: Partial; }; export type CheckboxGroupProps = BoxProps & RNViewProps & LocalCheckboxGroupProps; const useProps = createHook( (props) => { const { align, colorMode, defaultValue, disabled, onChange, options, orientation, overrides, palette, spacing, state, value, variant, setProps, ...restProps } = props; //////////////////////////////////////////// const boxProps = Box.useProps(props); //////////////////////////////////////////// const { getOptionItemProps } = useOptionsState({ defaultValue, onChange, type: 'checkbox', value, }); //////////////////////////////////////////// return { ...boxProps, children: ( {options.map((option, i) => ( ))} ), }; }, { defaultProps: { orientation: 'vertical', spacing: 'minor-2' }, themeKey: 'native.CheckboxGroup' } ); export const CheckboxGroup = createComponent( (props) => { const htmlProps = useProps(props); return createElement({ children: props.children, component: styles.CheckboxGroup, htmlProps, }); }, { attach: { useProps, displayName: 'native.CheckboxGroup', }, themeKey: 'native.CheckboxGroup', } ); //////////////////////////////////////////////////////////////// export type LocalCheckboxGroupFieldProps = { /** Additional props for the Checkbox component */ checkboxGroupProps?: Partial; }; export type CheckboxGroupFieldProps = BoxProps & FieldWrapperProps & CheckboxGroupProps & LocalCheckboxGroupFieldProps; const useCheckboxGroupFieldProps = createHook( (props) => { const { align, checkboxGroupProps, colorMode, defaultValue, description, disabled, hint, isOptional, isRequired, label, options, onChange, orientation, overrides, palette, spacing, setProps, state, value, variant, validationText, ...restProps } = props; const boxProps = Box.useProps(restProps); return { ...boxProps, children: ( ), }; }, { defaultProps: { align: 'left', palette: 'primary', variant: 'default', }, themeKey: 'CheckboxGroupField', } ); export const CheckboxGroupField = createComponent( (props) => { const CheckboxGroupFieldProps = useCheckboxGroupFieldProps(props); return createElement({ children: props.children, component: styles.CheckboxGroupField, htmlProps: CheckboxGroupFieldProps, }); }, { attach: { useProps: useCheckboxGroupFieldProps, displayName: 'CheckboxGroupField', }, themeKey: 'CheckboxGroupField', } );