-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TextControl: Covert component to TypeScript (#40633)
Co-authored-by: Marco Ciampini <[email protected]>
- Loading branch information
Showing
7 changed files
with
185 additions
and
119 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 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,84 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { ChangeEvent, ForwardedRef } from 'react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useInstanceId } from '@wordpress/compose'; | ||
import { forwardRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BaseControl from '../base-control'; | ||
import type { WordPressComponentProps } from '../ui/context'; | ||
import type { TextControlProps } from './types'; | ||
|
||
function UnforwardedTextControl( | ||
props: WordPressComponentProps< TextControlProps, 'input', false >, | ||
ref: ForwardedRef< HTMLInputElement > | ||
) { | ||
const { | ||
label, | ||
hideLabelFromVision, | ||
value, | ||
help, | ||
className, | ||
onChange, | ||
type = 'text', | ||
...additionalProps | ||
} = props; | ||
const instanceId = useInstanceId( TextControl ); | ||
const id = `inspector-text-control-${ instanceId }`; | ||
const onChangeValue = ( event: ChangeEvent< HTMLInputElement > ) => | ||
onChange( event.target.value ); | ||
|
||
return ( | ||
<BaseControl | ||
label={ label } | ||
hideLabelFromVision={ hideLabelFromVision } | ||
id={ id } | ||
help={ help } | ||
className={ className } | ||
> | ||
<input | ||
className="components-text-control__input" | ||
type={ type } | ||
id={ id } | ||
value={ value } | ||
onChange={ onChangeValue } | ||
aria-describedby={ !! help ? id + '__help' : undefined } | ||
ref={ ref } | ||
{ ...additionalProps } | ||
/> | ||
</BaseControl> | ||
); | ||
} | ||
|
||
/** | ||
* TextControl components let users enter and edit text. | ||
* | ||
* | ||
* @example | ||
* ```jsx | ||
* import { TextControl } from '@wordpress/components'; | ||
* import { useState } from '@wordpress/element'; | ||
* | ||
* const MyTextControl = () => { | ||
* const [ className, setClassName ] = useState( '' ); | ||
* | ||
* return ( | ||
* <TextControl | ||
* label="Additional CSS Class" | ||
* value={ className } | ||
* onChange={ ( value ) => setClassName( value ) } | ||
* /> | ||
* ); | ||
* }; | ||
* ``` | ||
*/ | ||
export const TextControl = forwardRef( UnforwardedTextControl ); | ||
|
||
export default TextControl; |
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,66 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import TextControl from '..'; | ||
|
||
const meta: ComponentMeta< typeof TextControl > = { | ||
component: TextControl, | ||
title: 'Components/TextControl', | ||
argTypes: { | ||
onChange: { | ||
action: 'onChange', | ||
}, | ||
value: { | ||
control: { type: null }, | ||
}, | ||
}, | ||
parameters: { | ||
controls: { | ||
expanded: true, | ||
}, | ||
docs: { source: { state: 'open' } }, | ||
}, | ||
}; | ||
export default meta; | ||
|
||
const DefaultTemplate: ComponentStory< typeof TextControl > = ( { | ||
onChange, | ||
...args | ||
} ) => { | ||
const [ value, setValue ] = useState( '' ); | ||
|
||
return ( | ||
<TextControl | ||
{ ...args } | ||
value={ value } | ||
onChange={ ( v ) => { | ||
setValue( v ); | ||
onChange( v ); | ||
} } | ||
/> | ||
); | ||
}; | ||
|
||
export const Default: ComponentStory< | ||
typeof TextControl | ||
> = DefaultTemplate.bind( {} ); | ||
Default.args = {}; | ||
|
||
export const WithLabelAndHelpText: ComponentStory< | ||
typeof TextControl | ||
> = DefaultTemplate.bind( {} ); | ||
WithLabelAndHelpText.args = { | ||
...Default.args, | ||
label: 'Label Text', | ||
help: 'Help text to explain the input.', | ||
}; |
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,29 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { HTMLInputTypeAttribute } from 'react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { BaseControlProps } from '../base-control/types'; | ||
|
||
export type TextControlProps = Pick< | ||
BaseControlProps, | ||
'className' | 'hideLabelFromVision' | 'help' | 'label' | ||
> & { | ||
/** | ||
* A function that receives the value of the input. | ||
*/ | ||
onChange: ( value: string ) => void; | ||
/** | ||
* The current value of the input. | ||
*/ | ||
value: string | number; | ||
/** | ||
* Type of the input element to render. Defaults to "text". | ||
* | ||
* @default 'text' | ||
*/ | ||
type?: HTMLInputTypeAttribute; | ||
}; |
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