-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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 ?? | ||
|
@@ -41,11 +41,25 @@ export function normalizeFields< Item >( | |
|
||
const Edit = field.Edit || fieldTypeDefinition.Edit; | ||
|
||
const renderFromElements = ( { item }: { item: Item } ) => { | ||
const value = getValue( { item } ); | ||
const label = field?.elements?.find( ( element ) => { | ||
// Intentionally using == here to allow for type coercion. | ||
// eslint-disable-next-line eqeqeq | ||
return element.value == value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't the issue in the field though. If the field is "string" its values should be string both in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #64391 may be a better approach. |
||
} )?.label; | ||
|
||
return label || value; | ||
}; | ||
|
||
const render = | ||
field.render || ( field.elements ? renderFromElements : getValue ); | ||
|
||
return { | ||
...field, | ||
label: field.label || field.id, | ||
getValue, | ||
render: field.render || getValue, | ||
render, | ||
sort, | ||
isValid, | ||
Edit, | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :)