-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Field API: simplify field normalization #73387
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cfc93cf
Implement getFormat in all FieldTypeDefinitions
oandregal 8e08c82
Use getFormat in normalizeFields
oandregal 379c335
Tell TypeScript the format is proper
oandregal dc5f713
Export normalize function
oandregal 1cf3064
Remove the concept of field type definition
oandregal 92a1eef
Simplify normalized field
oandregal 741b019
Add changelog
oandregal 6a05ef6
NormalizedField: refactor, there are optional props
oandregal 407ad42
Fix FormatDate issues
oandregal 96b68c0
Revert unnecessary exports
oandregal 93d0632
Fix normalizeFields test
oandregal 49d43ca
NormalizedFieldDate
oandregal c336ab3
Fix sorting
oandregal 2fedda2
Remove leftover
oandregal 893b71a
Use WordPress defaults when field type is not date
oandregal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,45 +8,66 @@ import { __ } from '@wordpress/i18n'; | |
| */ | ||
| import type { | ||
| DataViewRenderFieldProps, | ||
| SortDirection, | ||
| FieldTypeDefinition, | ||
| Field, | ||
| NormalizedField, | ||
| Operator, | ||
| Rules, | ||
| SortDirection, | ||
| } from '../types'; | ||
| import { | ||
| OPERATOR_IS_ALL, | ||
| OPERATOR_IS_ANY, | ||
| OPERATOR_IS_NONE, | ||
| OPERATOR_IS_NOT_ALL, | ||
| } from '../constants'; | ||
|
|
||
| // Sort arrays by length, then alphabetically by joined string | ||
| function sort( valueA: any, valueB: any, direction: SortDirection ) { | ||
| const arrA = Array.isArray( valueA ) ? valueA : []; | ||
| const arrB = Array.isArray( valueB ) ? valueB : []; | ||
| if ( arrA.length !== arrB.length ) { | ||
| return direction === 'asc' | ||
| ? arrA.length - arrB.length | ||
| : arrB.length - arrA.length; | ||
| } | ||
|
|
||
| const joinedA = arrA.join( ',' ); | ||
| const joinedB = arrB.join( ',' ); | ||
| return direction === 'asc' | ||
| ? joinedA.localeCompare( joinedB ) | ||
| : joinedB.localeCompare( joinedA ); | ||
| } | ||
| import { getControl } from '../dataform-controls'; | ||
| import hasElements from './utils/has-elements'; | ||
| import getValueFromId from './utils/get-value-from-id'; | ||
| import setValueFromId from './utils/set-value-from-id'; | ||
| import getFilterBy from './utils/get-filter-by'; | ||
|
|
||
| function render( { item, field }: DataViewRenderFieldProps< any > ) { | ||
| const value = field.getValue( { item } ) || []; | ||
| return value.join( ', ' ); | ||
| } | ||
|
|
||
| const arrayFieldType: FieldTypeDefinition< any > = { | ||
| sort, | ||
| isValid: { | ||
| const defaultOperators: Operator[] = [ OPERATOR_IS_ANY, OPERATOR_IS_NONE ]; | ||
|
Member
Author
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. The changes for all field types are about converting a (template) object into a function that returns the actual (normalized) object. |
||
| const validOperators: Operator[] = [ | ||
| OPERATOR_IS_ANY, | ||
| OPERATOR_IS_NONE, | ||
| OPERATOR_IS_ALL, | ||
| OPERATOR_IS_NOT_ALL, | ||
| ]; | ||
|
|
||
| export default function normalizeField< Item >( | ||
| field: Field< Item > | ||
| ): NormalizedField< Item > { | ||
| const getValue = field.getValue || getValueFromId( field.id ); | ||
| const setValue = field.setValue || setValueFromId( field.id ); | ||
|
|
||
| const sort = ( a: any, b: any, direction: SortDirection ) => { | ||
| // Sort arrays by length, then alphabetically by joined string | ||
| const valueA = getValue( a ); | ||
| const valueB = getValue( b ); | ||
| const arrA = Array.isArray( valueA ) ? valueA : []; | ||
| const arrB = Array.isArray( valueB ) ? valueB : []; | ||
| if ( arrA.length !== arrB.length ) { | ||
| return direction === 'asc' | ||
| ? arrA.length - arrB.length | ||
| : arrB.length - arrA.length; | ||
| } | ||
|
|
||
| const joinedA = arrA.join( ',' ); | ||
| const joinedB = arrB.join( ',' ); | ||
| return direction === 'asc' | ||
| ? joinedA.localeCompare( joinedB ) | ||
| : joinedB.localeCompare( joinedA ); | ||
| }; | ||
|
|
||
| const isValid: Rules< Item > = { | ||
| elements: true, | ||
| custom: ( item: any, field: NormalizedField< any > ) => { | ||
| const value = field.getValue( { item } ); | ||
| custom: ( item: any, normalizedField ) => { | ||
| const value = normalizedField.getValue( { item } ); | ||
|
|
||
| if ( | ||
| ! [ undefined, '', null ].includes( value ) && | ||
|
|
@@ -62,19 +83,33 @@ const arrayFieldType: FieldTypeDefinition< any > = { | |
|
|
||
| return null; | ||
| }, | ||
| }, | ||
| Edit: 'array', // Use array control | ||
| render, | ||
| enableSorting: true, | ||
| filterBy: { | ||
| defaultOperators: [ OPERATOR_IS_ANY, OPERATOR_IS_NONE ], | ||
| validOperators: [ | ||
| OPERATOR_IS_ANY, | ||
| OPERATOR_IS_NONE, | ||
| OPERATOR_IS_ALL, | ||
| OPERATOR_IS_NOT_ALL, | ||
| ], | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| export default arrayFieldType; | ||
| return { | ||
| id: field.id, | ||
| type: 'array', | ||
| label: field.label || field.id, | ||
| header: field.header || field.label || field.id, | ||
| description: field.description, | ||
| placeholder: field.placeholder, | ||
| getValue, | ||
| setValue, | ||
| elements: field.elements, | ||
| getElements: field.getElements, | ||
| hasElements: hasElements( field ), | ||
| render: field.render ?? render, | ||
| Edit: getControl( field, 'array' ), | ||
| sort: field.sort ?? sort, | ||
| isValid: { | ||
| ...isValid, | ||
| ...field.isValid, | ||
| }, | ||
| isVisible: field.isVisible, | ||
| enableSorting: field.enableSorting ?? true, | ||
| enableGlobalSearch: field.enableGlobalSearch ?? false, | ||
| enableHiding: field.enableHiding ?? true, | ||
| readOnly: field.readOnly ?? false, | ||
| filterBy: getFilterBy( field, defaultOperators, validOperators ), | ||
| format: {}, | ||
| }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.