-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RadioControl: Convert component to TypeScript (#41568)
- Loading branch information
Petter Walbø Johnsgård
committed
Jun 8, 2022
1 parent
9abede0
commit 2eee3a2
Showing
8 changed files
with
231 additions
and
133 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
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.