Skip to content

Commit

Permalink
Add validation
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Jul 24, 2024
1 parent 5dd8e8e commit 16f9f08
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
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 @@ -88,12 +88,13 @@ function DataFormNumberControl< Item >( {
}

const controls: {
// TODO: make the key type specific to FieldType.
[ key: string ]: < Item >(
props: DataFormControlProps< Item >
) => JSX.Element;
} = {
text: DataFormTextControl,
number: DataFormNumberControl,
integer: DataFormNumberControl,
};

function getControlForField< Item >( field: NormalizedField< Item > ) {
Expand Down
1 change: 1 addition & 0 deletions packages/dataviews/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { default as DataForm } from './components/dataform';
export { VIEW_LAYOUTS } from './layouts';
export { filterSortAndPaginate } from './filter-and-sort-data-view';
export type * from './types';
export { isItemValid } from './validation';
2 changes: 1 addition & 1 deletion packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type Operator =

export type ItemRecord = Record< string, unknown >;

export type FieldType = 'text';
export type FieldType = 'text' | 'integer';

/**
* A dataview field for a specific property of a data type.
Expand Down
30 changes: 30 additions & 0 deletions packages/dataviews/src/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Internal dependencies
*/
import { normalizeFields } from './normalize-fields';
import type { Field } from './types';

export function isItemValid< Item >(
item: Item,
fields: Field< Item >[]
): boolean {
const _fields = normalizeFields( fields );
return _fields.every( ( field ) => {
const value = field.getValue( { item } );

// TODO: this implicitely means the value is required.
if ( field.type === 'integer' && value === '' ) {
return false;
}

if (
field.type === 'integer' &&
! Number.isInteger( Number( value ) )
) {
return false;
}

// Nothing to validate.
return true;
} );
}
25 changes: 17 additions & 8 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { store as noticesStore } from '@wordpress/notices';
import { useMemo, useState } from '@wordpress/element';
import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
import { parse } from '@wordpress/blocks';
import { DataForm } from '@wordpress/dataviews';
import { DataForm, isItemValid } from '@wordpress/dataviews';
import {
Button,
TextControl,
Expand Down Expand Up @@ -48,7 +48,7 @@ const fields = [
getValue: ( { item } ) => item.title,
},
{
type: 'number',
type: 'integer',
id: 'menu_order',
label: __( 'Order' ),
description: __( 'Determines the order of pages.' ),
Expand Down Expand Up @@ -653,12 +653,18 @@ function ReorderModal( { items, closeModal, onActionPerformed } ) {

async function onOrder( event ) {
event.preventDefault();

if (
! Number.isInteger( Number( orderInput ) ) ||
orderInput?.trim?.() === ''
! isItemValid(
item,
fields.filter( ( field ) =>
formOrderAction.visibleFields.includes( field.id )
)
)
) {
return;
}

try {
await editEntityRecord( 'postType', item.type, item.id, {
menu_order: orderInput,
Expand All @@ -682,9 +688,12 @@ function ReorderModal( { items, closeModal, onActionPerformed } ) {
} );
}
}
const saveIsDisabled =
! Number.isInteger( Number( orderInput ) ) ||
orderInput?.trim?.() === '';
const isSaveDisabled = ! isItemValid(
item,
fields.filter( ( field ) =>
formOrderAction.visibleFields.includes( field.id )
)
);
return (
<form onSubmit={ onOrder }>
<VStack spacing="5">
Expand Down Expand Up @@ -714,7 +723,7 @@ function ReorderModal( { items, closeModal, onActionPerformed } ) {
variant="primary"
type="submit"
accessibleWhenDisabled
disabled={ saveIsDisabled }
disabled={ isSaveDisabled }
__experimentalIsFocusable
>
{ __( 'Save' ) }
Expand Down

0 comments on commit 16f9f08

Please sign in to comment.