Skip to content

Commit

Permalink
DataViews: Enable types (#61185)
Browse files Browse the repository at this point in the history
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: oandregal <oandregal@git.wordpress.org>
  • Loading branch information
3 people authored Apr 29, 2024
1 parent 01b24ce commit 6773fd5
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/**
* Internal dependencies
*/
import type { Field } from './types';

/**
* Apply default values and normalize the fields config.
*
* @param {Object[]} fields Raw Fields.
* @return {Object[]} Normalized fields.
* @param fields Fields config.
* @return Normalized fields config.
*/
export function normalizeFields( fields ) {
export function normalizeFields( fields: Field[] ): Field[] {
return fields.map( ( field ) => {
const getValue = field.getValue || ( ( { item } ) => item[ field.id ] );

Expand Down
79 changes: 79 additions & 0 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* External dependencies
*/
import type { ReactNode } from 'react';

type Item = Record< string, any >;

interface Option {
value: any;
label: string;
}

interface filterByConfig {
operators?: string[];
isPrimary?: boolean;
}

export interface Field {
/**
* The unique identifier of the field.
*/
id: string;

/**
* The label of the field. Defaults to the id.
*/
header?: string;

/**
* Callback used to retrieve the value of the field from the item.
* Defaults to `item[ field.id ]`.
*/
getValue?: ( { item }: { item: Item } ) => any;

/**
* Callback used to render the field. Defaults to `field.getValue`.
*/
render?: ( { item }: { item: Item } ) => ReactNode;

/**
* The width of the field column.
*/
width?: string | number;

/**
* The minimum width of the field column.
*/
maxWidth?: string | number;

/**
* The maximum width of the field column.
*/
minWidth?: string | number;

/**
* Whether the field is sortable.
*/
enableSorting?: boolean;

/**
* Whether the field is searchable.
*/
enableGlobalSearch?: boolean;

/**
* Whether the field is filterable.
*/
enableHiding?: boolean;

/**
* The list of options to pick from when using the field as a filter.
*/
elements?: Option[];

/**
* Filter config for the field.
*/
filterBy?: filterByConfig;
}
20 changes: 20 additions & 0 deletions packages/dataviews/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "https://json.schemastore.org/tsconfig.json",
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "build-types"
},
"references": [
{ "path": "../a11y" },
{ "path": "../components" },
{ "path": "../compose" },
{ "path": "../element" },
{ "path": "../i18n" },
{ "path": "../icons" },
{ "path": "../keycodes" },
{ "path": "../primitives" },
{ "path": "../private-apis" }
],
"include": [ "src/**/*.ts", "src/**/*.tsx" ]
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
{ "path": "packages/compose" },
{ "path": "packages/core-data" },
{ "path": "packages/data" },
{ "path": "packages/dataviews" },
{ "path": "packages/data-controls" },
{ "path": "packages/date" },
{ "path": "packages/deprecated" },
Expand Down

0 comments on commit 6773fd5

Please sign in to comment.