-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Dataforms: Add icons to email and telephone controls #71514
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
adf041f
13c21cd
b8c1c16
5ede93d
00f95ac
e1b5412
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { privateApis } from '@wordpress/components'; | ||
| import { | ||
| Icon, | ||
| privateApis, | ||
| __experimentalInputControlPrefixWrapper as InputControlPrefixWrapper, | ||
| } from '@wordpress/components'; | ||
| import { useCallback, useState } from '@wordpress/element'; | ||
|
|
||
| /** | ||
|
|
@@ -10,14 +14,18 @@ import { useCallback, useState } from '@wordpress/element'; | |
| import type { DataFormControlProps } from '../../types'; | ||
| import { unlock } from '../../lock-unlock'; | ||
|
|
||
| const { ValidatedTextControl } = unlock( privateApis ); | ||
| const { ValidatedInputControl } = unlock( privateApis ); | ||
|
|
||
| export type DataFormValidatedTextControlProps< Item > = | ||
| DataFormControlProps< Item > & { | ||
| /** | ||
| * The input type of the control. | ||
| */ | ||
| type?: 'text' | 'email' | 'tel' | 'url'; | ||
| /** | ||
| * Optional icon to display as prefix. | ||
| */ | ||
| icon?: React.ComponentType | React.ReactElement; | ||
|
Contributor
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'm a bit late here, but wouldn't it be better if the prop is Why restrict to |
||
| }; | ||
|
|
||
| export default function ValidatedText< Item >( { | ||
|
|
@@ -26,13 +34,14 @@ export default function ValidatedText< Item >( { | |
| onChange, | ||
| hideLabelFromVision, | ||
| type, | ||
| icon, | ||
| }: DataFormValidatedTextControlProps< Item > ) { | ||
| const { id, label, placeholder, description } = field; | ||
| const value = field.getValue( { item: data } ); | ||
| const [ customValidity, setCustomValidity ] = | ||
| useState< | ||
| React.ComponentProps< | ||
| typeof ValidatedTextControl | ||
| typeof ValidatedInputControl | ||
| >[ 'customValidity' ] | ||
| >( undefined ); | ||
|
|
||
|
|
@@ -45,7 +54,7 @@ export default function ValidatedText< Item >( { | |
| ); | ||
|
|
||
| return ( | ||
| <ValidatedTextControl | ||
| <ValidatedInputControl | ||
| required={ !! field.isValid?.required } | ||
| onValidate={ ( newValue: any ) => { | ||
| const message = field.isValid?.custom?.( | ||
|
|
@@ -74,6 +83,14 @@ export default function ValidatedText< Item >( { | |
| onChange={ onChangeControl } | ||
| hideLabelFromVision={ hideLabelFromVision } | ||
| type={ type } | ||
| prefix={ | ||
| icon ? ( | ||
| <InputControlPrefixWrapper variant="icon"> | ||
| <Icon icon={ icon } /> | ||
| </InputControlPrefixWrapper> | ||
| ) : undefined | ||
| } | ||
| __next40pxDefaultSize | ||
|
Contributor
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.
|
||
| /> | ||
| ); | ||
| } | ||
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.
This validatedtext component was added recently to share logic among text, email, and telephone. Can we instead add a prefix in ValidatedText and update it for email and telephone?
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.
Yeah, probably it would be better to update
ValidatedTextto useValidatedInputControlinstead ofValidatedTextControl. I'm not sure though if there are any nuances doing so.In general it seems that InputControl is aimed to replace TextControl. I'll cc @ciampo for any input.
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.
Even if
textdoesn't need a prefix right now (and so it could still useValidatedText), that's something we want to open to consumers soon (see) — I've seen a few use cases already where people are using custom Edits just for the prefix.