Repository navigation
ToggleControl: Convert to TypeScript #43717
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
783e8d4
ToggleControl: Convert to TypeScript
mirka 9fafd7a
Mark `label` as required
mirka a37d0b1
Convert tests
mirka 14b46fa
Fixup `help` control
mirka 8251a42
Add changelog
mirka 8cf2432
Export without contextConnect
mirka a3f120b
Add main JSDoc
mirka af35b85
Fix override breakage in editor preferences modal ☹️
mirka acd3541
Update snapshots
mirka e3c35fb
Merge branch 'trunk' into ts-toggle-control
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
This file was deleted.
Oops, something went wrong.
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,97 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import type { ChangeEvent } from 'react'; | ||
| import { css } from '@emotion/react'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useInstanceId } from '@wordpress/compose'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import FormToggle from '../form-toggle'; | ||
| import BaseControl from '../base-control'; | ||
| import type { WordPressComponentProps } from '../ui/context/wordpress-component'; | ||
| import type { ToggleControlProps } from './types'; | ||
| import { HStack } from '../h-stack'; | ||
| import { useCx } from '../utils'; | ||
| import { space } from '../ui/utils/space'; | ||
|
|
||
| /** | ||
| * ToggleControl is used to generate a toggle user interface. | ||
| * | ||
| * ```jsx | ||
| * import { ToggleControl } from '@wordpress/components'; | ||
| * import { useState } from '@wordpress/element'; | ||
| * | ||
| * const MyToggleControl = () => { | ||
| * const [ value, setValue ] = useState( false ); | ||
| * | ||
| * return ( | ||
| * <ToggleControl | ||
| * label="Fixed Background" | ||
| * checked={ value } | ||
| * onChange={ () => setValue( ( state ) => ! state ) } | ||
| * /> | ||
| * ); | ||
| * }; | ||
| * ``` | ||
| */ | ||
| export function ToggleControl( { | ||
| __nextHasNoMarginBottom, | ||
| label, | ||
| checked, | ||
| help, | ||
| className, | ||
| onChange, | ||
| disabled, | ||
| }: WordPressComponentProps< ToggleControlProps, 'input', false > ) { | ||
| function onChangeToggle( event: ChangeEvent< HTMLInputElement > ) { | ||
| onChange( event.target.checked ); | ||
| } | ||
| const instanceId = useInstanceId( ToggleControl ); | ||
| const id = `inspector-toggle-control-${ instanceId }`; | ||
|
|
||
| const cx = useCx(); | ||
| const classes = cx( | ||
| 'components-toggle-control', | ||
| className, | ||
| ! __nextHasNoMarginBottom && css( { marginBottom: space( 3 ) } ) | ||
| ); | ||
|
|
||
| let describedBy, helpLabel; | ||
| if ( help ) { | ||
| describedBy = id + '__help'; | ||
| helpLabel = typeof help === 'function' ? help( checked ) : help; | ||
| } | ||
|
|
||
| return ( | ||
| <BaseControl | ||
| id={ id } | ||
| help={ helpLabel } | ||
| className={ classes } | ||
| __nextHasNoMarginBottom | ||
| > | ||
| <HStack justify="flex-start" spacing={ 3 }> | ||
| <FormToggle | ||
| id={ id } | ||
| checked={ checked } | ||
| onChange={ onChangeToggle } | ||
| aria-describedby={ describedBy } | ||
| disabled={ disabled } | ||
| /> | ||
| <label | ||
| htmlFor={ id } | ||
| className="components-toggle-control__label" | ||
| > | ||
| { label } | ||
| </label> | ||
| </HStack> | ||
| </BaseControl> | ||
| ); | ||
| } | ||
|
|
||
| export default ToggleControl; | ||
This file was deleted.
Oops, something went wrong.
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,58 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import type { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useState } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import ToggleControl from '..'; | ||
|
|
||
| const meta: ComponentMeta< typeof ToggleControl > = { | ||
| title: 'Components/ToggleControl', | ||
| component: ToggleControl, | ||
| argTypes: { | ||
| checked: { control: { type: null } }, | ||
| help: { control: { type: 'text' } }, | ||
| label: { control: { type: 'text' } }, | ||
| onChange: { action: 'onChange' }, | ||
| }, | ||
| parameters: { | ||
| controls: { expanded: true }, | ||
| docs: { source: { state: 'open' } }, | ||
| }, | ||
| }; | ||
| export default meta; | ||
|
|
||
| const Template: ComponentStory< typeof ToggleControl > = ( { | ||
| onChange, | ||
| ...props | ||
| } ) => { | ||
| const [ checked, setChecked ] = useState( true ); | ||
| return ( | ||
| <ToggleControl | ||
| { ...props } | ||
| checked={ checked } | ||
| onChange={ ( ...changeArgs ) => { | ||
| setChecked( ...changeArgs ); | ||
| onChange( ...changeArgs ); | ||
| } } | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export const Default = Template.bind( {} ); | ||
| Default.args = { | ||
| label: 'Enable something', | ||
| }; | ||
|
|
||
| export const WithHelpText = Template.bind( {} ); | ||
| WithHelpText.args = { | ||
| ...Default.args, | ||
| help: 'This is some help text.', | ||
| }; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
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.
For back compat, we set a custom value (12px) here instead of simply passing through the
__nextHasNoMarginBottomto BaseControl. This is because ToggleControl was setting a custom margin-bottom value (12px) that was different from BaseControl's margin-bottom (8px).