Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataForm: provide a better default for render when field has elements #64338

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't notice this before. I see this was addressed at #64367

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not important right now :)


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 } )
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to start adding some unit tests for this function

};

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
Loading