Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/components/src/private-apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Badge from './badge';
import { DateCalendar, DateRangeCalendar, TZDate } from './calendar';
import {
ValidatedCheckboxControl,
ValidatedInputControl,
ValidatedNumberControl,
ValidatedTextControl,
ValidatedToggleControl,
Expand All @@ -33,6 +34,7 @@ lock( privateApis, {
DateCalendar,
DateRangeCalendar,
TZDate,
ValidatedInputControl,
ValidatedCheckboxControl,
ValidatedNumberControl,
ValidatedTextControl,
Expand Down
14 changes: 13 additions & 1 deletion packages/dataviews/src/dataform-controls/email.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* WordPress dependencies
*/
import { atSymbol } from '@wordpress/icons';

/**
* Internal dependencies
*/
Expand All @@ -12,7 +17,14 @@ export default function Email< Item >( {
}: DataFormControlProps< Item > ) {
return (
<ValidatedText
Copy link
Member

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?

Copy link
Contributor

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 ValidatedText to use ValidatedInputControl instead of ValidatedTextControl. 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if text doesn't need a prefix right now (and so it could still use ValidatedText), 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.

{ ...{ data, field, onChange, hideLabelFromVision, type: 'email' } }
{ ...{
data,
field,
onChange,
hideLabelFromVision,
type: 'email',
icon: atSymbol,
} }
/>
);
}
14 changes: 13 additions & 1 deletion packages/dataviews/src/dataform-controls/telephone.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* WordPress dependencies
*/
import { mobile } from '@wordpress/icons';

/**
* Internal dependencies
*/
Expand All @@ -12,7 +17,14 @@ export default function Telephone< Item >( {
}: DataFormControlProps< Item > ) {
return (
<ValidatedText
{ ...{ data, field, onChange, hideLabelFromVision, type: 'tel' } }
{ ...{
data,
field,
onChange,
hideLabelFromVision,
type: 'tel',
icon: mobile,
} }
/>
);
}
14 changes: 13 additions & 1 deletion packages/dataviews/src/dataform-controls/url.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* WordPress dependencies
*/
import { link } from '@wordpress/icons';

/**
* Internal dependencies
*/
Expand All @@ -12,7 +17,14 @@ export default function Url< Item >( {
}: DataFormControlProps< Item > ) {
return (
<ValidatedText
{ ...{ data, field, onChange, hideLabelFromVision, type: 'url' } }
{ ...{
data,
field,
onChange,
hideLabelFromVision,
type: 'url',
icon: link,
} }
/>
);
}
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';

/**
Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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 prefix and keep the flexibility of the inner component?

Why restrict to icon?

};

export default function ValidatedText< Item >( {
Expand All @@ -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 );

Expand All @@ -45,7 +54,7 @@ export default function ValidatedText< Item >( {
);

return (
<ValidatedTextControl
<ValidatedInputControl
required={ !! field.isValid?.required }
onValidate={ ( newValue: any ) => {
const message = field.isValid?.custom?.(
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ValidatedInputControl has already this prop. I think we don't need it here.

/>
);
}
4 changes: 2 additions & 2 deletions packages/dataviews/src/test/dataform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe( 'DataForm component', () => {
expect( onChange ).toHaveBeenCalledTimes( newValue.length );
for ( let i = 0; i < newValue.length; i++ ) {
expect( onChange ).toHaveBeenNthCalledWith( i + 1, {
title: newValue[ i ],
title: newValue.slice( 0, i + 1 ),
} );
}
} );
Expand Down Expand Up @@ -408,7 +408,7 @@ describe( 'DataForm component', () => {
expect( onChange ).toHaveBeenCalledTimes( newValue.length );
for ( let i = 0; i < newValue.length; i++ ) {
expect( onChange ).toHaveBeenNthCalledWith( i + 1, {
title: newValue[ i ],
title: newValue.slice( 0, i + 1 ),
} );
}
} );
Expand Down
Loading