Repository navigation
UI: Recommend Checkbox family components #83771
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
10 commits
Select commit
Hold shift + click to select a range
4f149b5
UI: Recommend Checkbox family components
mirka eb3ac32
Add Checkbox recommendation changelog entries
mirka f5e8aaa
Add CheckboxControl migration guide
mirka a28383b
Remove legacy checkbox heading guidance
mirka 08ef7c3
Document checkbox group migration
mirka 6eb1ef0
Remove redundant checkbox lint examples
mirka 587136f
Merge trunk into recommend-checkbox-family
mirka d1f6652
Docs: Clarify CheckboxControl form value migration
mirka 5c2c7d7
Merge origin/trunk into recommend-checkbox-family
mirka b06bc5e
Merge remote-tracking branch 'origin/trunk' into HEAD
mirka 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
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
106 changes: 106 additions & 0 deletions
106
packages/components/src/checkbox-control/stories/migration-guide.mdx
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,106 @@ | ||
| import { Markdown, Meta } from '@storybook/addon-docs/blocks'; | ||
| import * as CheckboxControlStories from './index.story'; | ||
|
|
||
| <Meta of={ CheckboxControlStories } name="Migration Guide" /> | ||
|
|
||
| # CheckboxControl migration guide | ||
|
|
||
| Migrate from [`CheckboxControl`](?path=/docs/components-checkboxcontrol--docs) in `@wordpress/components` to [`CheckboxControl`](?path=/docs/design-system-components-form-checkboxcontrol--docs) in `@wordpress/ui`. | ||
|
|
||
| ## Basic migration | ||
|
|
||
| Update the import and replace the callback and help props: | ||
|
|
||
| ```jsx | ||
| // Before | ||
| import { CheckboxControl } from '@wordpress/components'; | ||
|
|
||
| <CheckboxControl | ||
| label="Is author" | ||
| checked={ isChecked } | ||
| onChange={ setChecked } | ||
| help="Give this user author permissions." | ||
| />; | ||
|
|
||
| // After | ||
| import { CheckboxControl } from '@wordpress/ui'; | ||
|
|
||
| <CheckboxControl | ||
| label="Is author" | ||
| checked={ isChecked } | ||
| onCheckedChange={ setChecked } | ||
| description="Give this user author permissions." | ||
| />; | ||
| ``` | ||
|
|
||
| ## Prop mapping | ||
|
|
||
| <Markdown>{ ` | ||
| | \`@wordpress/components\` | \`@wordpress/ui\` | Notes | | ||
| | --- | --- | --- | | ||
| | \`onChange\` | \`onCheckedChange\` | Receives the next checked state as a boolean. | | ||
| | \`help\` | \`description\` | Use plain text only. | | ||
| | \`help\` with links or other markup | \`details\` | Use when the help text needs semantic content. | | ||
| | \`checked\` | \`checked\` | Continue passing the state for a controlled checkbox. | | ||
| ` }</Markdown> | ||
|
|
||
| ## Form values | ||
|
|
||
| When a checked checkbox has a `name` but no `value`, the legacy component submits `"1"` with the form, while the new component submits `"on"`, the native default for checkboxes. If your form handler expects the legacy value, pass `value="1"` to the new component: | ||
|
|
||
| ```jsx | ||
| <CheckboxControl | ||
| name="isAuthor" | ||
| value="1" | ||
| label="Is author" | ||
| checked={ isChecked } | ||
| onCheckedChange={ setChecked } | ||
| /> | ||
| ``` | ||
|
|
||
| If the legacy component already has a custom `value`, keep that value instead. | ||
|
|
||
| ## Labels | ||
|
|
||
| The new `CheckboxControl` requires a `label` prop with a plain `string` value. The legacy component treats `label` as optional. Give every checkbox a descriptive label, even when the text is already visible elsewhere: | ||
|
|
||
| ```jsx | ||
| <CheckboxControl | ||
| label="Receive notifications" | ||
| hideLabelFromVision | ||
| checked={ isChecked } | ||
| onCheckedChange={ setChecked } | ||
| /> | ||
| ``` | ||
|
|
||
| For a custom label layout, compose the [`Checkbox`](?path=/docs/design-system-components-form-primitives-checkbox--docs) and [`Field`](?path=/docs/design-system-components-form-primitives-field--docs) primitives. | ||
|
|
||
| ## Checkbox groups | ||
|
|
||
| If a legacy checkbox uses `indeterminate` to show that only some items are selected, move the selected items into a `CheckboxGroup` instead of calculating the parent checkbox state yourself. Pass the child values to `allValues` and mark the select-all checkbox with `parent`. The group updates the parent checkbox when children change and selects or clears all children when the parent is clicked. | ||
|
|
||
| ```jsx | ||
| import { useState } from '@wordpress/element'; | ||
| import { CheckboxControl, CheckboxGroup } from '@wordpress/ui'; | ||
|
|
||
| function FruitCheckboxes() { | ||
| const [ selectedFruits, setSelectedFruits ] = useState( [ 'apple' ] ); | ||
|
|
||
| return ( | ||
| <CheckboxGroup | ||
| value={ selectedFruits } | ||
| onValueChange={ setSelectedFruits } | ||
| allValues={ [ 'apple', 'orange' ] } | ||
| aria-label="Fruit" | ||
| > | ||
| <CheckboxControl parent value="fruit" label="Select all fruit" /> | ||
| <CheckboxGroup.NestedItems> | ||
| <CheckboxControl value="apple" label="Apple" /> | ||
| <CheckboxControl value="orange" label="Orange" /> | ||
| </CheckboxGroup.NestedItems> | ||
| </CheckboxGroup> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| See the [Checkbox Groups stories](?path=/docs/design-system-components-form-checkbox-groups--docs) for other grouping patterns. | ||
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we document the default form value change from
"1"to"on"and showvalue="1"when preserving the legacy payload? A named checkbox migrated using these steps otherwise submits a different value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, added in d1f6652.