-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add checkboxgroup component with story and extended the checkbo…
…x type (#2213)
- Loading branch information
Showing
5 changed files
with
274 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@kadena/react-ui": patch | ||
--- | ||
|
||
Add checkboxgroup component |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
packages/libs/react-ui/src/components/Form/Checkbox/CheckboxGroup.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import React from 'react'; | ||
|
||
import { getVariants } from '../../../storyDecorators'; | ||
import { Checkbox } from './Checkbox'; | ||
import { groupClass } from './Checkbox.css'; | ||
import type { ICheckboxProps } from './CheckboxGroup'; | ||
import { CheckboxGroup } from './CheckboxGroup'; | ||
|
||
const directions = getVariants(groupClass); | ||
|
||
const meta: Meta<ICheckboxProps> = { | ||
title: 'Form/CheckboxGroup', | ||
parameters: { | ||
status: { type: 'stable' }, | ||
controls: { | ||
hideNoControlsWarning: true, | ||
sort: 'requiredFirst', | ||
}, | ||
docs: { | ||
description: { | ||
component: 'The checkbox component', | ||
}, | ||
}, | ||
}, | ||
argTypes: { | ||
isDisabled: { | ||
control: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
direction: { | ||
options: directions.direction, | ||
control: { | ||
type: 'select', | ||
}, | ||
}, | ||
isReadOnly: { | ||
control: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
isInvalid: { | ||
control: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
label: { | ||
control: { | ||
type: 'text', | ||
}, | ||
}, | ||
errorMessage: { | ||
control: { | ||
type: 'text', | ||
}, | ||
}, | ||
description: { | ||
control: { | ||
type: 'text', | ||
}, | ||
}, | ||
tag: { | ||
control: { | ||
type: 'text', | ||
}, | ||
}, | ||
info: { | ||
control: { | ||
type: 'text', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
type CheckboxGroupStoryType = StoryObj<ICheckboxProps>; | ||
|
||
const checkboxes = Array.from(Array(10).keys()).map((key) => { | ||
return ( | ||
<Checkbox key={key} value={`value:${key}`}>{`Option: ${key}`}</Checkbox> | ||
); | ||
}); | ||
|
||
export const Base: CheckboxGroupStoryType = { | ||
args: { | ||
direction: 'row', | ||
}, | ||
render: (props: ICheckboxProps) => { | ||
return <CheckboxGroup {...props}>{checkboxes}</CheckboxGroup>; | ||
}, | ||
}; | ||
|
||
export const _Horizontal: CheckboxGroupStoryType = { | ||
args: { | ||
direction: 'row', | ||
label: 'Label', | ||
tag: 'tag', | ||
info: 'info', | ||
description: 'description', | ||
}, | ||
render: (props: ICheckboxProps) => { | ||
return <CheckboxGroup {...props}>{checkboxes}</CheckboxGroup>; | ||
}, | ||
}; | ||
|
||
export const _Vertical: CheckboxGroupStoryType = { | ||
args: { | ||
direction: 'column', | ||
label: 'Label', | ||
tag: 'tag', | ||
description: 'description', | ||
}, | ||
render: (props: ICheckboxProps) => { | ||
return <CheckboxGroup {...props}>{checkboxes}</CheckboxGroup>; | ||
}, | ||
}; | ||
|
||
export const MultilineLabel: CheckboxGroupStoryType = { | ||
args: { | ||
direction: 'row', | ||
label: 'Label', | ||
tag: 'tag', | ||
info: 'info', | ||
description: 'description', | ||
}, | ||
render: (props: ICheckboxProps) => { | ||
return ( | ||
<CheckboxGroup {...props}> | ||
<Checkbox value="something"> | ||
Hello this is a checkbox with a long label, probably enough words to | ||
cause the checkbox label to be multiline | ||
</Checkbox> | ||
<Checkbox value="something else"> | ||
Hello this is a checkbox with a long label, probably enough words to | ||
cause the checkbox label to be multiline | ||
</Checkbox> | ||
<Checkbox value="3">Option: 3</Checkbox> | ||
<Checkbox value="4">Option: 4</Checkbox> | ||
</CheckboxGroup> | ||
); | ||
}, | ||
}; | ||
|
||
export default meta; |
83 changes: 83 additions & 0 deletions
83
packages/libs/react-ui/src/components/Form/Checkbox/CheckboxGroup.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import type { RecipeVariants } from '@vanilla-extract/recipes'; | ||
import type { ReactElement } from 'react'; | ||
import React, { createContext } from 'react'; | ||
import type { AriaCheckboxGroupProps } from 'react-aria'; | ||
import { useCheckboxGroup } from 'react-aria'; | ||
import type { CheckboxGroupState } from 'react-stately'; | ||
import { useCheckboxGroupState } from 'react-stately'; | ||
import { FormFieldHeader } from '../FormFieldHeader/FormFieldHeader'; | ||
import { FormFieldHelpText } from '../FormFieldHelpText/FormFieldHelpText'; | ||
import { groupClass, layoutClass } from './Checkbox.css'; | ||
|
||
type Direction = NonNullable<RecipeVariants<typeof groupClass>>['direction']; | ||
|
||
export interface ICheckboxProps extends AriaCheckboxGroupProps { | ||
children: ReactElement[] | ReactElement; | ||
direction: Direction; | ||
isReadOnly?: boolean; | ||
label?: string; | ||
tag?: string; | ||
info?: string; | ||
} | ||
|
||
export const CheckboxContext = createContext<CheckboxGroupState | null>(null); | ||
|
||
export function CheckboxGroup(props: ICheckboxProps) { | ||
const { | ||
children, | ||
description, | ||
direction = 'row', | ||
errorMessage, | ||
isDisabled, | ||
isInvalid = false, | ||
isReadOnly, | ||
info, | ||
label, | ||
tag, | ||
} = props; | ||
const state = useCheckboxGroupState(props); | ||
const { | ||
descriptionProps, | ||
groupProps, | ||
errorMessageProps, | ||
labelProps, | ||
validationDetails, | ||
validationErrors, | ||
} = useCheckboxGroup(props, state); | ||
|
||
const error = | ||
typeof errorMessage === 'function' | ||
? errorMessage({ isInvalid, validationErrors, validationDetails }) | ||
: errorMessage ?? validationErrors.join(' '); | ||
|
||
return ( | ||
<div {...groupProps} className={layoutClass}> | ||
{label && ( | ||
<FormFieldHeader | ||
label={label} | ||
tag={tag} | ||
info={info} | ||
isDisabled={isDisabled} | ||
{...labelProps} | ||
/> | ||
)} | ||
<div className={groupClass({ direction })}> | ||
<CheckboxContext.Provider value={state}> | ||
{React.Children.map(children, (child) => | ||
React.cloneElement(child, { isReadOnly }), | ||
)} | ||
</CheckboxContext.Provider> | ||
</div> | ||
{description && !isInvalid && ( | ||
<FormFieldHelpText {...descriptionProps}> | ||
{description} | ||
</FormFieldHelpText> | ||
)} | ||
{isInvalid && ( | ||
<FormFieldHelpText {...errorMessageProps} intent="negative"> | ||
{error || (errorMessage as string)} | ||
</FormFieldHelpText> | ||
)} | ||
</div> | ||
); | ||
} |