diff --git a/packages/dataviews/src/field-types/index.tsx b/packages/dataviews/src/field-types/index.tsx index 651221f66ecc07..1075858c6e0669 100644 --- a/packages/dataviews/src/field-types/index.tsx +++ b/packages/dataviews/src/field-types/index.tsx @@ -2,7 +2,7 @@ * Internal dependencies */ import { default as integer } from './integer'; -import type { FieldType, Option } from '../types'; +import type { FieldType, ValidationContext } from '../types'; /** * @@ -17,9 +17,9 @@ export default function getFieldTypeDefinition( type?: FieldType ) { return { sort: () => 0, - isValid: ( value: any, elements?: Option[] ) => { - if ( elements ) { - const validValues = elements.map( ( f ) => f.value ); + isValid: ( value: any, context?: ValidationContext ) => { + if ( context?.elements ) { + const validValues = context?.elements?.map( ( f ) => f.value ); if ( ! validValues.includes( value ) ) { return false; } diff --git a/packages/dataviews/src/field-types/integer.tsx b/packages/dataviews/src/field-types/integer.tsx index e6886d4d901b3d..eaa90e8f1e4e26 100644 --- a/packages/dataviews/src/field-types/integer.tsx +++ b/packages/dataviews/src/field-types/integer.tsx @@ -1,13 +1,13 @@ /** * Internal dependencies */ -import type { SortDirection, Option } from '../types'; +import type { SortDirection, ValidationContext } from '../types'; function sort( a: any, b: any, direction: SortDirection ) { return direction === 'asc' ? a - b : b - a; } -function isValid( value: any, elements?: Option[] ) { +function isValid( value: any, context?: ValidationContext ) { // TODO: this implicitely means the value is required. if ( value === '' ) { return false; @@ -17,8 +17,8 @@ function isValid( value: any, elements?: Option[] ) { return false; } - if ( elements ) { - const validValues = elements.map( ( f ) => f.value ); + if ( context?.elements ) { + const validValues = context?.elements.map( ( f ) => f.value ); if ( ! validValues.includes( Number( value ) ) ) { return false; } diff --git a/packages/dataviews/src/normalize-fields.ts b/packages/dataviews/src/normalize-fields.ts index 4dd29817bf0537..8d08b08715c9a3 100644 --- a/packages/dataviews/src/normalize-fields.ts +++ b/packages/dataviews/src/normalize-fields.ts @@ -32,10 +32,10 @@ export function normalizeFields< Item >( const isValid = field.isValid ?? - function isValid( item, elements ) { + function isValid( item, context ) { return fieldTypeDefinition.isValid( getValue( { item } ), - elements + context ); }; diff --git a/packages/dataviews/src/types.ts b/packages/dataviews/src/types.ts index 0fde2529068265..7029db7d17b1af 100644 --- a/packages/dataviews/src/types.ts +++ b/packages/dataviews/src/types.ts @@ -46,6 +46,10 @@ export type ItemRecord = Record< string, unknown >; export type FieldType = 'text' | 'integer'; +export type ValidationContext = { + elements?: Option[]; +}; + /** * A dataview field for a specific property of a data type. */ @@ -88,7 +92,7 @@ export type Field< Item > = { /** * Callback used to validate the field. */ - isValid?: ( item: Item, elements?: Option[] ) => boolean; + isValid?: ( item: Item, context?: ValidationContext ) => boolean; /** * Whether the field is sortable. @@ -135,7 +139,7 @@ export type NormalizedField< Item > = Field< Item > & { getValue: ( args: { item: Item } ) => any; render: ComponentType< { item: Item } >; sort: ( a: Item, b: Item, direction: SortDirection ) => number; - isValid: ( item: Item, elements?: Option[] ) => boolean; + isValid: ( item: Item, context?: ValidationContext ) => boolean; }; /** diff --git a/packages/dataviews/src/validation.ts b/packages/dataviews/src/validation.ts index 46fe18a7cbaf5b..e350be68d54219 100644 --- a/packages/dataviews/src/validation.ts +++ b/packages/dataviews/src/validation.ts @@ -13,6 +13,6 @@ export function isItemValid< Item >( fields.filter( ( { id } ) => !! form.visibleFields?.includes( id ) ) ); return _fields.every( ( field ) => { - return field.isValid( item, field.elements ); + return field.isValid( item, { elements: field.elements } ); } ); }