Skip to content

Commit

Permalink
Fix type errors for default prop values
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Jun 26, 2024
1 parent a5cf5a4 commit 4897186
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
13 changes: 7 additions & 6 deletions packages/dataviews/src/dataviews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,28 @@ import { normalizeFields } from './normalize-fields';
import BulkActionsToolbar from './bulk-actions-toolbar';
import type { Action, Field, View, ViewBaseProps } from './types';

interface DataViewsProps< Item > {
type ItemWithId = { id: string };

type DataViewsProps< Item > = {
view: View;
onChangeView: ( view: View ) => void;
fields: Field< Item >[];
search?: boolean;
searchLabel?: string;
actions?: Action< Item >[];
data: Item[];
getItemId?: ( item: Item ) => string;
isLoading?: boolean;
paginationInfo: {
totalItems: number;
totalPages: number;
};
supportedLayouts: string[];
onSelectionChange?: ( items: Item[] ) => void;
}
} & ( Item extends ItemWithId
? { getItemId?: ( item: Item ) => string }
: { getItemId: ( item: Item ) => string } );

function defaultGetItemId( item: { id: string } ) {
return item.id;
}
const defaultGetItemId = ( item: ItemWithId ) => item.id;

const defaultOnSelectionChange = () => {};

Expand Down
7 changes: 5 additions & 2 deletions packages/dataviews/src/normalize-fields.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/**
* Internal dependencies
*/
import type { Field, NormalizedField } from './types';
import type { Field, NormalizedField, ItemRecord } from './types';

const defaultGetValue = ( field: string ) => ( args: { item: ItemRecord } ) =>
args.item[ field ];

/**
* Apply default values and normalize the fields config.
Expand All @@ -13,7 +16,7 @@ export function normalizeFields< Item >(
fields: Field< Item >[]
): NormalizedField< Item >[] {
return fields.map( ( field ) => {
const getValue = field.getValue || ( ( { item } ) => item[ field.id ] );
const { getValue = defaultGetValue( field.id ) } = field;

return {
...field,
Expand Down
35 changes: 24 additions & 11 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ export type Operator =
| 'isAll'
| 'isNotAll';

export type ItemRecord = Record< string, unknown >;

/**
* A dataview field for a specific property of a data type.
*/
export interface Field< Item > {
export type Field< Item > = {
/**
* The unique identifier of the field.
*/
Expand All @@ -51,12 +53,6 @@ export interface Field< Item > {
*/
header?: string;

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

/**
* Callback used to render the field. Defaults to `field.getValue`.
*/
Expand Down Expand Up @@ -101,10 +97,27 @@ export interface Field< Item > {
* Filter config for the field.
*/
filterBy?: FilterByConfig | undefined;
}

export type NormalizedField< Item > = Field< Item > &
Required< Pick< Field< Item >, 'header' | 'getValue' | 'render' > >;
} & ( Item extends ItemRecord
? {
/**
* Callback used to retrieve the value of the field from the item.
* Defaults to `item[ field.id ]`.
*/
getValue?: ( args: { item: Item } ) => any;
}
: {
/**
* Callback used to retrieve the value of the field from the item.
* Defaults to `item[ field.id ]`.
*/
getValue: ( args: { item: Item } ) => any;
} );

export type NormalizedField< Item > = Field< Item > & {
header: string;
getValue: ( args: { item: Item } ) => any;
render: ( args: { item: Item } ) => ReactNode;
};

/**
* A collection of dataview fields for a data type.
Expand Down
2 changes: 1 addition & 1 deletion packages/dataviews/src/view-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ export default function ViewList< Item >( props: ViewListProps< Item > ) {
} = props;
const baseId = useInstanceId( ViewList, 'view-list' );
const selectedItem = data?.findLast( ( item ) =>
selection.includes( item.id )
selection.includes( getItemId( item ) )
);

const mediaField = fields.find(
Expand Down

0 comments on commit 4897186

Please sign in to comment.