Skip to content

Commit

Permalink
Do not pass elements but an object with elements
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Aug 1, 2024
1 parent e314f61 commit d13edc0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
8 changes: 4 additions & 4 deletions packages/dataviews/src/field-types/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import { default as integer } from './integer';
import type { FieldType, Option } from '../types';
import type { FieldType, ValidationContext } from '../types';

/**
*
Expand All @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/dataviews/src/field-types/integer.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/dataviews/src/normalize-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
};

Expand Down
8 changes: 6 additions & 2 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/dataviews/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } );

This comment has been minimized.

Copy link
@youknowriad

youknowriad Aug 1, 2024

Contributor

I think you can probably just use field here as it also implements to the ValidationContext interface.

} );
}

0 comments on commit d13edc0

Please sign in to comment.