Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions packages/dataviews/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- DataViews: Pass only the eligible items to a bulk action's `callback`. A bulk action is offered when any one selected item is eligible for it, so the callback could run against items it had declared, through `isEligible`, that it could not handle. [#81198](https://github.com/WordPress/gutenberg/pull/81198)
- DataViews: Fix the `between` date filter discarding a manually entered `From`/`To` date on blur. The control now commits an incomplete range with an unfilled bound — which neither filters nor renders a chip — instead of waiting for both dates, so a typed date survives tabbing away and a range can be entered manually at all. [#81150](https://github.com/WordPress/gutenberg/pull/81150)
- DataViews: Render the filter chip for an incomplete `between` range as if no value were set — matching how the filter itself does not apply — instead of showing a dangling bound or the literal string "undefined". A `null` bound, produced when an unfilled bound round-trips through JSON persistence, is now treated as unfilled too. [#80830](https://github.com/WordPress/gutenberg/pull/80830)
- DataViews: Treat an empty value as valid in the `array` field type's built-in validation, matching the other field types — use the `required` rule to enforce non-empty values. [#81378](https://github.com/WordPress/gutenberg/pull/81378)

### Internal

Expand Down
10 changes: 6 additions & 4 deletions packages/dataviews/src/field-types/array.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ function render( { item, field }: DataViewRenderFieldProps< any > ) {
function isValidCustom< Item >( item: Item, field: NormalizedField< Item > ) {
const value = field.getValue( { item } );

if (
! [ undefined, '', null ].includes( value ) &&
! Array.isArray( value )
) {
// Allow empty values; use the `required` rule to enforce non-empty ones.
if ( [ undefined, '', null ].includes( value ) ) {
return null;
}

if ( ! Array.isArray( value ) ) {
return __( 'Value must be an array.' );
}

Expand Down
41 changes: 41 additions & 0 deletions packages/dataviews/src/hooks/test/use-form-validity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2263,6 +2263,47 @@ describe( 'useFormValidity', () => {
expect( isValid ).toBe( false );
} );

it( 'array is valid if value is empty', () => {
const item = { id: 1, tags: undefined };
const fields: Field< {} >[] = [
{
id: 'tags',
type: 'array',
},
];
const form = { fields: [ 'tags' ] };
const {
result: {
current: { validity, isValid },
},
} = renderHook( () => useFormValidity( item, fields, form ) );
expect( validity ).toEqual( undefined );
expect( isValid ).toBe( true );
} );

it( 'array is invalid if values are not strings when not empty', () => {
const item = { id: 1, tags: [ 'valid', 42 ] };
const fields: Field< {} >[] = [
{
id: 'tags',
type: 'array',
},
];
const form = { fields: [ 'tags' ] };
const {
result: {
current: { validity, isValid },
},
} = renderHook( () => useFormValidity( item, fields, form ) );
expect( validity?.tags ).toEqual( {
custom: {
type: 'invalid',
message: 'Every value must be a string.',
},
} );
expect( isValid ).toBe( false );
} );

it( 'should return early on custom sync validation failure', async () => {
const item = { status: 'draft' };
const fields: Field< any >[] = [
Expand Down
Loading