Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
26b113b
Make validation controlled and async
oandregal Aug 29, 2025
9b1fa80
Remove isItemValid
oandregal Oct 15, 2025
1acbc8f
Document useIsFormValid hook
oandregal Oct 15, 2025
c965fa8
Test useIsFormValid: port all existing test for isItemValid
oandregal Oct 15, 2025
219dd24
Update FieldValidity and FormValidity types
oandregal Oct 15, 2025
3cbe36d
useIsFormValid: update tests
oandregal Oct 15, 2025
744e219
Update docs
oandregal Oct 15, 2025
81579ac
Create getCustomValidity utility
oandregal Oct 15, 2025
9ff587b
Update tests
oandregal Oct 15, 2025
16676bb
Rename useIsFormValid to useFormValidity
oandregal Oct 15, 2025
522dd49
useFormValidity returns validity and isValid
oandregal Oct 15, 2025
b2600d0
Add a test for isValid
oandregal Oct 15, 2025
af2bc15
DataForm: datetime control uses validity prop
oandregal Oct 15, 2025
2121abb
DataForm: date control uses validity prop
oandregal Oct 15, 2025
e067e62
Add changelog
oandregal Oct 15, 2025
f8d2252
DataForm: date control attaches different class depending on type
oandregal Oct 15, 2025
1851f70
Fix unmounting issue with panel dropdown/modal
oandregal Oct 16, 2025
10862ef
getCustomValidity: fall through to elements or custom if validity.req…
oandregal Oct 16, 2025
e80ff81
Update changelog
oandregal Oct 16, 2025
84b0a5d
Update README
oandregal Oct 16, 2025
224089f
Story: better custom Edit
oandregal Oct 16, 2025
b1979b1
Support combined fields.
oandregal Oct 16, 2025
1027148
Make linter happy
oandregal Oct 16, 2025
375c4a8
story: remove unnecessary flags
oandregal Oct 16, 2025
22c8cb7
Move useFormValidity test to proper place
oandregal Oct 16, 2025
12f88bf
Improve useFormValidity hook
oandregal Oct 16, 2025
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
Next Next commit
Make validation controlled and async
  • Loading branch information
oandregal committed Oct 16, 2025
commit 26b113b1fc20b94ebe37d6e93f77294feaa03399
12 changes: 9 additions & 3 deletions packages/dataviews/src/components/dataform-context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@ import { createContext } from '@wordpress/element';
/**
* Internal dependencies
*/
import type { NormalizedField } from '../../types';
import type { NormalizedField, FormValidity } from '../../types';

type DataFormContextType< Item > = {
fields: NormalizedField< Item >[];
validity?: FormValidity;
};

const DataFormContext = createContext< DataFormContextType< any > >( {
fields: [],
validity: undefined,
} );
DataFormContext.displayName = 'DataFormContext';

export function DataFormProvider< Item >( {
fields,
validity,
children,
}: React.PropsWithChildren< { fields: NormalizedField< Item >[] } > ) {
}: React.PropsWithChildren< {
fields: NormalizedField< Item >[];
validity?: FormValidity;
} > ) {
return (
<DataFormContext.Provider value={ { fields } }>
<DataFormContext.Provider value={ { fields, validity } }>
{ children }
</DataFormContext.Provider>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/dataviews/src/components/dataform/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function DataForm< Item >( {
form,
fields,
onChange,
validity,
}: DataFormProps< Item > ) {
const normalizedFields = useMemo(
() => normalizeFields( fields ),
Expand All @@ -27,7 +28,7 @@ export default function DataForm< Item >( {
}

return (
<DataFormProvider fields={ normalizedFields }>
<DataFormProvider fields={ normalizedFields } validity={ validity }>
<DataFormLayout data={ data } form={ form } onChange={ onChange } />
</DataFormProvider>
);
Expand Down
83 changes: 3 additions & 80 deletions packages/dataviews/src/dataform-controls/array.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
/**
* External dependencies
*/
import deepMerge from 'deepmerge';

/**
* WordPress dependencies
*/
import { privateApis } from '@wordpress/components';
import { useCallback, useMemo, useState } from '@wordpress/element';
import { _n, sprintf } from '@wordpress/i18n';
import { useCallback, useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -23,18 +17,11 @@ export default function ArrayControl< Item >( {
field,
onChange,
hideLabelFromVision,
validity,
}: DataFormControlProps< Item > ) {
const { label, placeholder, elements, getValue, setValue } = field;
const value = getValue( { item: data } );

const [ customValidity, setCustomValidity ] = useState<
| {
type: 'validating' | 'valid' | 'invalid';
message: string;
}
| undefined
>( undefined );

// Convert stored values to element objects for the token field
const arrayValueAsElements = useMemo(
() =>
Expand All @@ -49,69 +36,6 @@ export default function ArrayControl< Item >( {
[ value, elements ]
);

const validateTokens = useCallback(
( tokens: ( string | { value: string; label?: string } )[] ) => {
// Extract actual values from tokens for validation
const tokenValues = tokens.map( ( token ) => {
if ( typeof token === 'object' && 'value' in token ) {
return token.value;
}
return token;
} );

// First, check if elements validation is required and any tokens are invalid
if ( field.isValid?.elements && elements ) {
const invalidTokens = tokenValues.filter( ( tokenValue ) => {
return ! elements.some(
( element ) => element.value === tokenValue
);
} );

if ( invalidTokens.length > 0 ) {
setCustomValidity( {
type: 'invalid',
message: sprintf(
/* translators: %s: list of invalid tokens */
_n(
'Please select from the available options: %s is invalid.',
'Please select from the available options: %s are invalid.',
invalidTokens.length
),
invalidTokens.join( ', ' )
),
} );
return;
}
}

// Then check custom validation if provided.
if ( field.isValid?.custom ) {
const result = field.isValid?.custom?.(
deepMerge(
data,
setValue( {
item: data,
value: tokenValues,
} ) as Partial< Item >
),
field
);

if ( result ) {
setCustomValidity( {
type: 'invalid',
message: result,
} );
return;
}
}

// If no validation errors, clear custom validity
setCustomValidity( undefined );
},
[ elements, data, field, setValue ]
);

const onChangeControl = useCallback(
( tokens: ( string | { value: string; label?: string } )[] ) => {
const valueTokens = tokens.map( ( token ) => {
Expand All @@ -130,8 +54,7 @@ export default function ArrayControl< Item >( {
return (
<ValidatedFormTokenField
required={ !! field.isValid?.required }
onValidate={ validateTokens }
customValidity={ customValidity }
customValidity={ validity?.custom ? validity.custom : undefined }
label={ hideLabelFromVision ? undefined : label }
value={ arrayValueAsElements }
onChange={ onChangeControl }
Expand Down
43 changes: 3 additions & 40 deletions packages/dataviews/src/dataform-controls/checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
/**
* External dependencies
*/
import deepMerge from 'deepmerge';

/**
* WordPress dependencies
*/
import { privateApis } from '@wordpress/components';
import { useState, useCallback } from '@wordpress/element';
import { useCallback } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -22,52 +17,20 @@ export default function Checkbox< Item >( {
onChange,
data,
hideLabelFromVision,
validity,
}: DataFormControlProps< Item > ) {
const { getValue, setValue, label, description } = field;
const [ customValidity, setCustomValidity ] =
useState<
React.ComponentProps<
typeof ValidatedCheckboxControl
>[ 'customValidity' ]
>( undefined );

const onChangeControl = useCallback( () => {
onChange(
setValue( { item: data, value: ! getValue( { item: data } ) } )
);
}, [ data, getValue, onChange, setValue ] );

const onValidateControl = useCallback(
( newValue: any ) => {
const message = field.isValid?.custom?.(
deepMerge(
data,
setValue( {
item: data,
value: newValue,
} ) as Partial< Item >
),
field
);

if ( message ) {
setCustomValidity( {
type: 'invalid',
message,
} );
return;
}

setCustomValidity( undefined );
},
[ data, field, setValue ]
);

return (
<ValidatedCheckboxControl
required={ !! field.isValid?.required }
onValidate={ onValidateControl }
customValidity={ customValidity }
customValidity={ validity?.custom ? validity.custom : undefined }
hidden={ hideLabelFromVision }
label={ label }
help={ description }
Expand Down
39 changes: 3 additions & 36 deletions packages/dataviews/src/dataform-controls/color.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { colord } from 'colord';
import deepMerge from 'deepmerge';

/**
* WordPress dependencies
Expand All @@ -12,7 +11,7 @@ import {
privateApis,
__experimentalInputControlPrefixWrapper as InputControlPrefixWrapper,
} from '@wordpress/components';
import { useCallback, useState } from '@wordpress/element';
import { useCallback } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -75,15 +74,10 @@ export default function Color< Item >( {
field,
onChange,
hideLabelFromVision,
validity,
}: DataFormControlProps< Item > ) {
const { label, placeholder, description, setValue } = field;
const value = field.getValue( { item: data } ) || '';
const [ customValidity, setCustomValidity ] =
useState<
React.ComponentProps<
typeof ValidatedInputControl
>[ 'customValidity' ]
>( undefined );

const handleColorChange = useCallback(
( colorObject: any ) => {
Expand All @@ -99,37 +93,10 @@ export default function Color< Item >( {
[ data, onChange, setValue ]
);

const onValidateControl = useCallback(
( newValue: any ) => {
const message = field.isValid?.custom?.(
deepMerge(
data,
setValue( {
item: data,
value: newValue,
} ) as Partial< Item >
),
field
);

if ( message ) {
setCustomValidity( {
type: 'invalid',
message,
} );
return;
}

setCustomValidity( undefined );
},
[ data, field, setValue ]
);

return (
<ValidatedInputControl
required={ !! field.isValid?.required }
onValidate={ onValidateControl }
customValidity={ customValidity }
customValidity={ validity?.custom ? validity.custom : undefined }
label={ label }
placeholder={ placeholder }
value={ value }
Expand Down
2 changes: 2 additions & 0 deletions packages/dataviews/src/dataform-controls/email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function Email< Item >( {
field,
onChange,
hideLabelFromVision,
validity,
}: DataFormControlProps< Item > ) {
return (
<ValidatedText
Expand All @@ -26,6 +27,7 @@ export default function Email< Item >( {
field,
onChange,
hideLabelFromVision,
validity,
type: 'email',
prefix: (
<InputControlPrefixWrapper variant="icon">
Expand Down
2 changes: 2 additions & 0 deletions packages/dataviews/src/dataform-controls/password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function Password< Item >( {
field,
onChange,
hideLabelFromVision,
validity,
}: DataFormControlProps< Item > ) {
const [ isVisible, setIsVisible ] = useState( false );

Expand All @@ -30,6 +31,7 @@ export default function Password< Item >( {
field,
onChange,
hideLabelFromVision,
validity,
type: isVisible ? 'text' : 'password',
suffix: (
<Button
Expand Down
Loading