-
Notifications
You must be signed in to change notification settings - Fork 119
FE-1106: Add CheckboxGroup + RadioGroup to DS #8923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bb3bc2c
Add radio group to ds
alex-e-leon 9586847
Update ark-ui to latest
alex-e-leon f2154a0
Radio group api adjustments
alex-e-leon 0dc964d
Added checkbox group component
alex-e-leon f7a8608
Checkbox group cleanup
alex-e-leon 9a9dae0
Better Checkboxgroup + RadioGroup sizing
alex-e-leon 47f855f
Added changeset
alex-e-leon b4fc37f
Fix checkboxgroup maxSelectable logic
alex-e-leon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| --- | ||
| "@hashintel/ds-components": patch | ||
| --- | ||
|
|
||
| Added CheckboxGroup and RadioGroup components |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
190 changes: 190 additions & 0 deletions
190
libs/@hashintel/ds-components/src/components/CheckboxGroup/checkbox-group.stories.tsx
This file contains hidden or 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,190 @@ | ||
| import { useState } from "react"; | ||
|
|
||
| import { css } from "@hashintel/ds-helpers/css"; | ||
|
|
||
| import { formInputSizes } from "../../util/form-shared"; | ||
| import { CheckboxGroup } from "./checkbox-group"; | ||
|
|
||
| import type { Story, StoryDefault } from "@ladle/react"; | ||
|
|
||
| type Props = React.ComponentProps<typeof CheckboxGroup>; | ||
|
|
||
| const layouts: NonNullable<Props["layout"]>[] = [ | ||
| "blockWithBorder", | ||
| "block", | ||
| "inline", | ||
| ]; | ||
|
|
||
| const fruitItems = [ | ||
| { value: "apple", label: "Apple" }, | ||
| { value: "banana", label: "Banana" }, | ||
| { value: "cherry", label: "Cherry" }, | ||
| ]; | ||
|
|
||
| const leftLabelItems = fruitItems.map((item) => ({ | ||
| ...item, | ||
| labelPlacement: "left" as const, | ||
| })); | ||
|
|
||
| const ControlledCheckboxGroup = ({ | ||
| defaultValue = ["apple"], | ||
| ...props | ||
| }: Omit<Props, "value" | "onChange" | "items"> & { | ||
| defaultValue?: string[]; | ||
| items?: Props["items"]; | ||
| }) => { | ||
| const [value, setValue] = useState(defaultValue); | ||
| return ( | ||
| <CheckboxGroup | ||
| {...props} | ||
| items={props.items ?? fruitItems} | ||
| value={value} | ||
| onChange={setValue} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export default { | ||
| title: "Components/CheckboxGroup", | ||
| parameters: { | ||
| layout: "centered", | ||
| }, | ||
| argTypes: { | ||
| layout: { control: { type: "select", options: layouts } }, | ||
| size: { control: { type: "select", options: formInputSizes } }, | ||
| disabled: { control: { type: "boolean" } }, | ||
| }, | ||
| args: { | ||
| layout: "block", | ||
| size: "md", | ||
| disabled: false, | ||
| }, | ||
| } satisfies StoryDefault<Props>; | ||
|
|
||
| const headingClass = css({ | ||
| fontSize: "[12px]", | ||
| fontWeight: "medium", | ||
| color: "neutral.s90", | ||
| marginBottom: "[8px]", | ||
| }); | ||
|
|
||
| const sectionClass = css({ | ||
| display: "flex", | ||
| flexDirection: "column", | ||
| gap: "[24px]", | ||
| }); | ||
|
|
||
| const subHeadingClass = css({ | ||
| fontSize: "[11px]", | ||
| color: "neutral.s70", | ||
| marginBottom: "[6px]", | ||
| }); | ||
|
|
||
| const layoutRowClass = css({ | ||
| display: "flex", | ||
| gap: "[40px]", | ||
| alignItems: "flex-start", | ||
| flexWrap: "wrap", | ||
| }); | ||
|
|
||
| export const Layouts: Story = () => ( | ||
| <div className={sectionClass}> | ||
| {layouts.map((layout) => ( | ||
| <div key={layout}> | ||
| <div className={headingClass}>layout={layout}</div> | ||
| <ControlledCheckboxGroup | ||
| layout={layout} | ||
| defaultValue={["apple", "cherry"]} | ||
| /> | ||
| </div> | ||
| ))} | ||
| <div> | ||
| <div className={headingClass}> | ||
| layout=blockWithBorder, labelPlacement=left | ||
| </div> | ||
| <ControlledCheckboxGroup | ||
| layout="blockWithBorder" | ||
| items={leftLabelItems} | ||
| /> | ||
| </div> | ||
| <div> | ||
| <div className={headingClass}>layout=block, labelPlacement=left</div> | ||
| <ControlledCheckboxGroup layout="block" items={leftLabelItems} /> | ||
| </div> | ||
| </div> | ||
| ); | ||
|
|
||
| Layouts.parameters = { | ||
| controls: { disable: true }, | ||
| }; | ||
|
|
||
| export const Sizes: Story = () => ( | ||
| <div className={sectionClass}> | ||
| {formInputSizes.map((size) => ( | ||
| <div key={size}> | ||
| <div className={headingClass}>size={size}</div> | ||
| <div className={layoutRowClass}> | ||
| {layouts.map((layout) => ( | ||
| <div key={layout}> | ||
| <div className={subHeadingClass}>{layout}</div> | ||
| <ControlledCheckboxGroup layout={layout} size={size} /> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| ); | ||
|
|
||
| Sizes.parameters = { | ||
| controls: { disable: true }, | ||
| }; | ||
|
|
||
| export const Disabled: Story = () => ( | ||
| <div className={sectionClass}> | ||
| <div> | ||
| <div className={headingClass}>whole group disabled</div> | ||
| <ControlledCheckboxGroup disabled defaultValue={["apple"]} /> | ||
| </div> | ||
| <div> | ||
| <div className={headingClass}>single option disabled</div> | ||
| <ControlledCheckboxGroup | ||
| items={[ | ||
| { value: "apple", label: "Apple" }, | ||
| { value: "banana", label: "Banana", disabled: true }, | ||
| { value: "cherry", label: "Cherry" }, | ||
| ]} | ||
| /> | ||
| </div> | ||
| </div> | ||
| ); | ||
|
|
||
| Disabled.parameters = { | ||
| controls: { disable: true }, | ||
| }; | ||
|
|
||
| export const MaxSelectable: Story = () => ( | ||
| <div className={sectionClass}> | ||
| <div> | ||
| <div className={headingClass}>maxSelectable=2</div> | ||
| <div className={subHeadingClass}> | ||
| Once two are selected, the remaining unchecked options are disabled | ||
| until one is unchecked. | ||
| </div> | ||
| <ControlledCheckboxGroup | ||
| maxSelectable={2} | ||
| defaultValue={[]} | ||
| items={[ | ||
| { value: "apple", label: "Apple" }, | ||
| { value: "banana", label: "Banana" }, | ||
| { value: "cherry", label: "Cherry" }, | ||
| { value: "date", label: "Date" }, | ||
| ]} | ||
| /> | ||
| </div> | ||
| </div> | ||
| ); | ||
|
|
||
| MaxSelectable.parameters = { | ||
| controls: { disable: true }, | ||
| }; |
123 changes: 123 additions & 0 deletions
123
libs/@hashintel/ds-components/src/components/CheckboxGroup/checkbox-group.tsx
This file contains hidden or 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,123 @@ | ||
| import { CheckboxGroup as ArkCheckboxGroup } from "@ark-ui/react/checkbox"; | ||
| import { useId } from "react"; | ||
|
|
||
| import { cx } from "@hashintel/ds-helpers/css"; | ||
|
|
||
| import { | ||
| getGroupFocusProps, | ||
| styles, | ||
| } from "../../util/radio-checkbox-group-shared"; | ||
| import { Checkbox } from "../Checkbox/checkbox"; | ||
|
|
||
| import type { SharedInputProps } from "../../util/form-shared"; | ||
|
|
||
| type CheckboxGroupProps<ValueType extends string = string> = { | ||
| /** How the options are arranged (defaults to `block`) */ | ||
| layout?: "block" | "inline" | "blockWithBorder"; | ||
| /** The selectable options */ | ||
| items: Array< | ||
| Omit< | ||
| React.ComponentProps<typeof Checkbox>, | ||
| "size" | "onChange" | "value" | "name" | "autoFocus" | "htmlForId" | ||
| > & { value: ValueType } | ||
| >; | ||
| maxSelectable?: number; | ||
| } & Omit<SharedInputProps<HTMLInputElement, NoInfer<ValueType>[]>, "inputRef"> & | ||
| React.AriaAttributes; | ||
|
|
||
| export const CheckboxGroup = <const ValueType extends string>({ | ||
| layout = "block", | ||
| items, | ||
| disabled, | ||
| invalid, | ||
| className, | ||
| value, | ||
| onChange, | ||
| onFocus, | ||
| onBlur, | ||
| ref, | ||
| required, | ||
| testId, | ||
| size = "md", | ||
| autoFocus, | ||
| name, | ||
| maxSelectable, | ||
| ...ariaProps | ||
| }: CheckboxGroupProps<ValueType>) => { | ||
| // A stable, shared `name` groups the underlying inputs for form submission. | ||
| const generatedName = useId(); | ||
| const groupName = name ?? generatedName; | ||
|
|
||
| const selectedValues = new Set(value); | ||
| const isRequired = !!required || items.some((item) => item.required); | ||
| const isInvalid = !!invalid || items.some((item) => item.invalid); | ||
| const hasSelection = items.some((item) => selectedValues.has(item.value)); | ||
|
|
||
| const atSelectionCap = | ||
| maxSelectable !== undefined && selectedValues.size >= maxSelectable; | ||
|
|
||
| return ( | ||
| <ArkCheckboxGroup | ||
| name={groupName} | ||
| disabled={disabled} | ||
| // `aria-required` isn't part of the `group` role's ARIA contract, but is | ||
| // the conventional signal that a selection is required for the group. | ||
| aria-required={isRequired ? true : undefined} | ||
| invalid={isInvalid ? true : undefined} | ||
| data-testid={testId} | ||
| ref={ref as React.Ref<HTMLDivElement>} | ||
| className={cx(styles({ layout, size }), className)} | ||
| {...getGroupFocusProps({ onFocus, onBlur })} | ||
| {...ariaProps} | ||
| > | ||
| {items.map((item, index) => { | ||
| const { | ||
| value: optionValue, | ||
| disabled: itemDisabled, | ||
| required: itemRequired, | ||
| ...itemProps | ||
| } = item; | ||
| const isSelected = selectedValues.has(optionValue); | ||
| // If the cap is reached, unchecked options are disabled so they can't | ||
| // be selected; already-selected options stay enabled so they can be | ||
| // unchecked to free up a slot. | ||
| const itemIsDisabled = | ||
| disabled === true || | ||
| itemDisabled === true || | ||
| (atSelectionCap && !isSelected); | ||
| // Native checkboxes have no "at least one of the group" constraint, so when | ||
| // the group is required we mark every option `required` while nothing is | ||
| // selected. That makes the group invalid (blocking submission) until one box | ||
| // is checked, at which point none of them need to be required anymore. | ||
| const itemIsRequired = | ||
| itemRequired === true || (required === true && !hasSelection); | ||
|
|
||
| return ( | ||
| <Checkbox | ||
| key={optionValue} | ||
| {...itemProps} | ||
| size={size} | ||
| name={groupName} | ||
| disabled={itemIsDisabled} | ||
| required={itemIsRequired} | ||
| value={isSelected} | ||
| htmlValue={optionValue} | ||
| onChange={(checked) => { | ||
| if (checked) { | ||
| if (isSelected || atSelectionCap) { | ||
| return; | ||
| } | ||
| onChange([...value, optionValue]); | ||
| } else { | ||
| onChange( | ||
| value.filter((candidate) => candidate !== optionValue), | ||
| ); | ||
| } | ||
| }} | ||
| autoFocus={autoFocus && index === 0} | ||
| /> | ||
| ); | ||
| })} | ||
| </ArkCheckboxGroup> | ||
| ); | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.