-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add password field #71545
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
Add password field #71545
Changes from 1 commit
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 |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { | ||
| Button, | ||
| privateApis, | ||
| __experimentalInputControlSuffixWrapper as InputControlSuffixWrapper, | ||
| } from '@wordpress/components'; | ||
| import { useCallback, useState } from '@wordpress/element'; | ||
| import { seen, unseen } from '@wordpress/icons'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import type { DataFormControlProps } from '../types'; | ||
| import { unlock } from '../lock-unlock'; | ||
|
|
||
| const { ValidatedInputControl } = unlock( privateApis ); | ||
|
|
||
| export default function Password< Item >( { | ||
| data, | ||
| field, | ||
| onChange, | ||
| hideLabelFromVision, | ||
| }: DataFormControlProps< Item > ) { | ||
| const { id, label, placeholder, description } = field; | ||
| const value = field.getValue( { item: data } ); | ||
| const [ isVisible, setIsVisible ] = useState( false ); | ||
| const [ customValidity, setCustomValidity ] = | ||
| useState< | ||
| React.ComponentProps< | ||
| typeof ValidatedInputControl | ||
| >[ 'customValidity' ] | ||
| >( undefined ); | ||
|
|
||
| const onChangeControl = useCallback( | ||
| ( newValue: string ) => | ||
| onChange( { | ||
| [ id ]: newValue, | ||
| } ), | ||
| [ id, onChange ] | ||
| ); | ||
|
|
||
| const toggleVisibility = useCallback( () => { | ||
| setIsVisible( ( prev ) => ! prev ); | ||
| }, [] ); | ||
|
|
||
| return ( | ||
| <ValidatedInputControl | ||
| required={ !! field.isValid?.required } | ||
| onValidate={ ( newValue: any ) => { | ||
| const message = field.isValid?.custom?.( | ||
| { | ||
| ...data, | ||
| [ id ]: newValue, | ||
| }, | ||
| field | ||
| ); | ||
|
|
||
| if ( message ) { | ||
| setCustomValidity( { | ||
| type: 'invalid', | ||
| message, | ||
| } ); | ||
| return; | ||
| } | ||
|
|
||
| setCustomValidity( undefined ); | ||
| } } | ||
| customValidity={ customValidity } | ||
| label={ label } | ||
| placeholder={ placeholder } | ||
| value={ value ?? '' } | ||
| help={ description } | ||
| onChange={ onChangeControl } | ||
| hideLabelFromVision={ hideLabelFromVision } | ||
| type={ isVisible ? 'text' : 'password' } | ||
| suffix={ | ||
| <InputControlSuffixWrapper variant="control"> | ||
| <Button | ||
| icon={ isVisible ? unseen : seen } | ||
| onClick={ toggleVisibility } | ||
| size="small" | ||
| variant="tertiary" | ||
| aria-label={ | ||
| isVisible ? 'Hide password' : 'Show password' | ||
| } | ||
| /> | ||
| </InputControlSuffixWrapper> | ||
| } | ||
| __next40pxDefaultSize | ||
| /> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import type { | ||
| DataViewRenderFieldProps, | ||
| SortDirection, | ||
| NormalizedField, | ||
| FieldTypeDefinition, | ||
| } from '../types'; | ||
| import { renderFromElements } from '../utils'; | ||
| import { | ||
| OPERATOR_CONTAINS, | ||
| OPERATOR_IS, | ||
| OPERATOR_IS_ALL, | ||
| OPERATOR_IS_ANY, | ||
| OPERATOR_IS_NONE, | ||
| OPERATOR_IS_NOT, | ||
| OPERATOR_IS_NOT_ALL, | ||
| OPERATOR_NOT_CONTAINS, | ||
| OPERATOR_STARTS_WITH, | ||
| } from '../constants'; | ||
|
|
||
| function sort( valueA: any, valueB: any, direction: SortDirection ) { | ||
| return direction === 'asc' | ||
| ? valueA.localeCompare( valueB ) | ||
| : valueB.localeCompare( valueA ); | ||
| } | ||
|
|
||
| export default { | ||
| sort, | ||
| isValid: { | ||
| custom: ( item: any, field: NormalizedField< any > ) => { | ||
| const value = field.getValue( { item } ); | ||
| if ( field?.elements ) { | ||
| const validValues = field.elements.map( ( f ) => f.value ); | ||
| if ( ! validValues.includes( value ) ) { | ||
| return __( 'Value must be one of the elements.' ); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| }, | ||
| }, | ||
| Edit: 'password', | ||
| render: ( { item, field }: DataViewRenderFieldProps< any > ) => { | ||
| return field.elements | ||
| ? renderFromElements( { item, field } ) | ||
| : '•'.repeat( field.getValue( { item } ).length ); | ||
|
||
| }, | ||
| enableSorting: true, | ||
|
||
| filterBy: { | ||
| defaultOperators: [ OPERATOR_IS_ANY, OPERATOR_IS_NONE ], | ||
|
||
| validOperators: [ | ||
| OPERATOR_IS, | ||
| OPERATOR_IS_NOT, | ||
| OPERATOR_CONTAINS, | ||
| OPERATOR_NOT_CONTAINS, | ||
| OPERATOR_STARTS_WITH, | ||
| // Multiple selection | ||
| OPERATOR_IS_ANY, | ||
| OPERATOR_IS_NONE, | ||
| OPERATOR_IS_ALL, | ||
| OPERATOR_IS_NOT_ALL, | ||
| ], | ||
| }, | ||
| } satisfies FieldTypeDefinition< any >; | ||
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.
Can this use the
utility/validated-text.tsxcomponent instead? I understand it just needs to accept a suffix. It'd be great if the utility was renamed toutils/validated-input.tsxas well (we forgot to that recently).