-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
RadioControl: Convert component to TypeScript #41568
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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
This file was deleted.
Oops, something went wrong.
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,107 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { isEmpty } from 'lodash'; | ||
import classnames from 'classnames'; | ||
import type { ChangeEvent } from 'react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useInstanceId } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BaseControl from '../base-control'; | ||
import type { WordPressComponentProps } from '../ui/context'; | ||
import type { RadioControlProps } from './types'; | ||
|
||
/** | ||
* Render a user interface to select the user type using radio inputs. | ||
* | ||
* ```jsx | ||
* import { RadioControl } from '@wordpress/components'; | ||
* import { useState } from '@wordpress/element'; | ||
* | ||
* const MyRadioControl = () => { | ||
* const [ option, setOption ] = useState( 'a' ); | ||
* | ||
* return ( | ||
* <RadioControl | ||
* label="User type" | ||
* help="The type of the current user" | ||
* selected={ option } | ||
* options={ [ | ||
* { label: 'Author', value: 'a' }, | ||
* { label: 'Editor', value: 'e' }, | ||
* ] } | ||
* onChange={ ( value ) => setOption( value ) } | ||
* /> | ||
* ); | ||
* }; | ||
* ``` | ||
*/ | ||
export function RadioControl( | ||
// ref is omitted until we have `WordPressComponentPropsWithoutRef` or add | ||
// ref forwarding to RadioControl. | ||
props: Omit< | ||
WordPressComponentProps< RadioControlProps, 'input', false >, | ||
'ref' | ||
> | ||
) { | ||
const { | ||
label, | ||
className, | ||
selected, | ||
help, | ||
onChange, | ||
hideLabelFromVision, | ||
options = [], | ||
...additionalProps | ||
} = props; | ||
const instanceId = useInstanceId( RadioControl ); | ||
const id = `inspector-radio-control-${ instanceId }`; | ||
const onChangeValue = ( event: ChangeEvent< HTMLInputElement > ) => | ||
onChange( event.target.value ); | ||
|
||
if ( isEmpty( options ) ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<BaseControl | ||
label={ label } | ||
id={ id } | ||
hideLabelFromVision={ hideLabelFromVision } | ||
help={ help } | ||
className={ classnames( className, 'components-radio-control' ) } | ||
> | ||
{ options.map( ( option, index ) => ( | ||
<div | ||
key={ `${ id }-${ index }` } | ||
className="components-radio-control__option" | ||
> | ||
<input | ||
id={ `${ id }-${ index }` } | ||
className="components-radio-control__input" | ||
type="radio" | ||
name={ id } | ||
value={ option.value } | ||
onChange={ onChangeValue } | ||
checked={ option.value === selected } | ||
aria-describedby={ | ||
!! help ? `${ id }__help` : undefined | ||
} | ||
{ ...additionalProps } | ||
/> | ||
<label htmlFor={ `${ id }-${ index }` }> | ||
{ option.label } | ||
</label> | ||
</div> | ||
) ) } | ||
</BaseControl> | ||
); | ||
} | ||
|
||
export default RadioControl; |
This file was deleted.
Oops, something went wrong.
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,72 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import RadioControl from '..'; | ||
|
||
const meta: ComponentMeta< typeof RadioControl > = { | ||
component: RadioControl, | ||
title: 'Components/RadioControl', | ||
argTypes: { | ||
onChange: { | ||
action: 'onChange', | ||
}, | ||
selected: { | ||
control: { type: null }, | ||
}, | ||
label: { | ||
control: { type: 'text' }, | ||
}, | ||
help: { | ||
control: { type: 'text' }, | ||
}, | ||
}, | ||
parameters: { | ||
controls: { | ||
expanded: true, | ||
}, | ||
docs: { source: { state: 'open' } }, | ||
}, | ||
}; | ||
export default meta; | ||
|
||
const Template: ComponentStory< typeof RadioControl > = ( { | ||
onChange, | ||
options, | ||
...args | ||
} ) => { | ||
const [ value, setValue ] = useState( options?.[ 0 ]?.value ); | ||
|
||
return ( | ||
<RadioControl | ||
{ ...args } | ||
selected={ value } | ||
options={ options } | ||
onChange={ ( v ) => { | ||
setValue( v ); | ||
onChange( v ); | ||
} } | ||
/> | ||
); | ||
}; | ||
|
||
export const Default: ComponentStory< typeof RadioControl > = Template.bind( | ||
{} | ||
); | ||
Default.args = { | ||
label: 'Post visibility', | ||
options: [ | ||
{ label: 'Public', value: 'public' }, | ||
{ label: 'Private', value: 'private' }, | ||
{ label: 'Password Protected', value: 'password' }, | ||
], | ||
}; |
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.
A bit late to the party, but I was curious about why it was necessary to omit
ref
? Was it causing any errors ?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.
Opened #41641 to add ref forwarding support
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.
👍
Didn't want to add any runtime changes.
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.
Sounds good, and I agree that it's a good rationale! I was mostly curious to know in case it caused any type errors :)