Skip to content

Commit

Permalink
Provide a better default for render when field has elements
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Aug 7, 2024
1 parent 19a2d83 commit c82506b
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/dataviews/src/normalize-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import getFieldTypeDefinition from './field-types';
import type { Field, NormalizedField, ItemRecord } from './types';
import type { Field, NormalizedField } from './types';

/**
* Apply default values and normalize the fields config.
Expand All @@ -18,7 +18,7 @@ export function normalizeFields< Item >(

const getValue =
field.getValue ||
( ( { item }: { item: ItemRecord } ) => item[ field.id ] );
( ( { item }: { item: Item } ) => item[ field.id as keyof Item ] );

const sort =
field.sort ??
Expand All @@ -41,11 +41,22 @@ export function normalizeFields< Item >(

const Edit = field.Edit || fieldTypeDefinition.Edit;

const renderFromElements = ( { item }: { item: Item } ) => {
const value = getValue( { item } );
return (
field?.elements?.find( ( element ) => element.value === value )
?.label || getValue( { item } )
);
};

const render =
field.render || ( field.elements ? renderFromElements : getValue );

return {
...field,
label: field.label || field.id,
getValue,
render: field.render || getValue,
render,
sort,
isValid,
Edit,
Expand Down

0 comments on commit c82506b

Please sign in to comment.