Repository navigation
UI: Add ValidatedTextArea control
#81984
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
Changes from all commits
e47b1dc
4fd3d61
e687b3a
48626c9
7b06cf9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,8 @@ | ||
| import { privateApis } from '@wordpress/components'; | ||
| import { ValidatedTextareaControl } from '@wordpress/ui'; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested this is DataForm's validation story (working as expected). |
||
| import { useCallback } from '@wordpress/element'; | ||
| import type { DataFormControlProps } from '../../types'; | ||
| import { unlock } from '../../lock-unlock'; | ||
| import getCustomValidity from './utils/get-custom-validity'; | ||
|
|
||
| const { ValidatedTextareaControl } = unlock( privateApis ); | ||
|
|
||
| export default function Textarea< Item >( { | ||
| data, | ||
| field, | ||
|
|
@@ -20,7 +17,7 @@ export default function Textarea< Item >( { | |
| const { label, placeholder, description, setValue, isValid } = field; | ||
| const value = field.getValue( { item: data } ); | ||
|
|
||
| const onChangeControl = useCallback( | ||
| const onValueChangeControl = useCallback( | ||
| ( newValue: string ) => | ||
| onChange( setValue( { item: data, value: newValue } ) ), | ||
| [ data, onChange, setValue ] | ||
|
|
@@ -34,8 +31,13 @@ export default function Textarea< Item >( { | |
| label={ label } | ||
| placeholder={ placeholder } | ||
| value={ value ?? '' } | ||
| help={ description } | ||
| onChange={ onChangeControl } | ||
| description={ | ||
| typeof description === 'string' ? description : undefined | ||
| } | ||
| details={ | ||
| typeof description === 'string' ? undefined : description | ||
| } | ||
|
Comment on lines
+34
to
+39
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| onValueChange={ onValueChangeControl } | ||
| rows={ rows } | ||
| disabled={ disabled } | ||
| minLength={ | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from './validated-input-control'; | ||
| export * from './validated-textarea-control'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { ValidatedTextareaControl } from './validated-textarea-control'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react-vite'; | ||
| import { useState } from '@wordpress/element'; | ||
| import { ValidatedTextareaControl } from '../index'; | ||
| import { formDecorator } from '../../../stories/shared'; | ||
|
|
||
| const meta: Meta< typeof ValidatedTextareaControl > = { | ||
| title: 'Design System/Components/Form/With Validation/ValidatedTextareaControl', | ||
| component: ValidatedTextareaControl, | ||
| argTypes: { | ||
| customValidity: { control: false }, | ||
| defaultValue: { control: false }, | ||
| onValueChange: { action: 'onValueChange' }, | ||
| value: { control: false }, | ||
| }, | ||
| decorators: [ formDecorator ], | ||
| parameters: { | ||
| componentStatus: { | ||
| status: 'use-with-caution', | ||
| whereUsed: 'global', | ||
| notes: 'APIs and behavior are still prone to change.', | ||
| }, | ||
| }, | ||
| }; | ||
| export default meta; | ||
|
|
||
| type Story = StoryObj< typeof ValidatedTextareaControl >; | ||
|
|
||
| /** | ||
| * Native constraint validation (here, `required`) is reported inline when the | ||
| * control is blurred or the form is submitted. | ||
| */ | ||
| export const Default: Story = { | ||
| args: { | ||
| label: 'Text', | ||
| required: true, | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * Consumer-driven validation results are passed through the `customValidity` | ||
| * prop. Type the word "error" and blur the field to trigger the custom error. | ||
| */ | ||
| export const WithCustomValidity: Story = { | ||
| render: function Template( args ) { | ||
| const [ value, setValue ] = useState( '' ); | ||
|
|
||
| return ( | ||
| <ValidatedTextareaControl | ||
| { ...args } | ||
| value={ value } | ||
| onValueChange={ ( next ) => setValue( next ?? '' ) } | ||
| customValidity={ | ||
| value.toLowerCase() === 'error' | ||
| ? { | ||
| type: 'invalid', | ||
| message: 'The word "error" is not allowed.', | ||
| } | ||
| : undefined | ||
| } | ||
| /> | ||
| ); | ||
| }, | ||
| args: { | ||
| label: 'Text', | ||
| description: "The word 'error' will trigger an error.", | ||
| }, | ||
| }; |
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.
Working as expected:
Screen.Recording.2026-08-24.at.17.02.32.mov