-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
DataViews: Refactor the edit function to be based on discrete controls #64404
Merged
Merged
Changes from all commits
Commits
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 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { BaseControl, TimePicker } from '@wordpress/components'; | ||
import { useCallback } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { DataFormControlProps } from '../types'; | ||
|
||
export default function DateTime< Item >( { | ||
data, | ||
field, | ||
onChange, | ||
}: DataFormControlProps< Item > ) { | ||
const { id, label } = field; | ||
const value = field.getValue( { item: data } ); | ||
|
||
const onChangeControl = useCallback( | ||
( newValue: string | null ) => onChange( { [ id ]: newValue } ), | ||
[ id, onChange ] | ||
); | ||
|
||
return ( | ||
<fieldset> | ||
<BaseControl.VisualLabel as="legend"> | ||
{ label } | ||
</BaseControl.VisualLabel> | ||
<TimePicker | ||
currentTime={ value } | ||
onChange={ onChangeControl } | ||
hideLabelFromVision | ||
/> | ||
</fieldset> | ||
); | ||
} |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __experimentalNumberControl as NumberControl } from '@wordpress/components'; | ||
import { useCallback } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { DataFormControlProps } from '../types'; | ||
|
||
export default function Integer< Item >( { | ||
data, | ||
field, | ||
onChange, | ||
hideLabelFromVision, | ||
}: DataFormControlProps< Item > ) { | ||
const { id, label, description } = field; | ||
const value = field.getValue( { item: data } ) ?? ''; | ||
const onChangeControl = useCallback( | ||
( newValue: string | undefined ) => | ||
onChange( { | ||
[ id ]: Number( newValue ), | ||
} ), | ||
[ id, onChange ] | ||
); | ||
|
||
return ( | ||
<NumberControl | ||
label={ label } | ||
help={ description } | ||
value={ value } | ||
onChange={ onChangeControl } | ||
__next40pxDefaultSize | ||
hideLabelFromVision={ hideLabelFromVision } | ||
/> | ||
); | ||
} |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { SelectControl } from '@wordpress/components'; | ||
import { useCallback } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { DataFormControlProps } from '../types'; | ||
|
||
export default function Select< Item >( { | ||
data, | ||
field, | ||
onChange, | ||
hideLabelFromVision, | ||
}: DataFormControlProps< Item > ) { | ||
const { id, label } = field; | ||
const value = field.getValue( { item: data } ) ?? ''; | ||
const onChangeControl = useCallback( | ||
( newValue: any ) => | ||
onChange( { | ||
[ id ]: newValue, | ||
} ), | ||
[ id, onChange ] | ||
); | ||
|
||
const elements = [ | ||
/* | ||
* Value can be undefined when: | ||
* | ||
* - the field is not required | ||
* - in bulk editing | ||
* | ||
*/ | ||
{ label: __( 'Select item' ), value: '' }, | ||
...( field?.elements ?? [] ), | ||
]; | ||
|
||
return ( | ||
<SelectControl | ||
label={ label } | ||
value={ value } | ||
options={ elements } | ||
onChange={ onChangeControl } | ||
__next40pxDefaultSize | ||
__nextHasNoMarginBottom | ||
hideLabelFromVision={ hideLabelFromVision } | ||
/> | ||
); | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { TextControl } from '@wordpress/components'; | ||
import { useCallback } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { DataFormControlProps } from '../types'; | ||
|
||
export default function Text< Item >( { | ||
data, | ||
field, | ||
onChange, | ||
hideLabelFromVision, | ||
}: DataFormControlProps< Item > ) { | ||
const { id, label, placeholder } = field; | ||
const value = field.getValue( { item: data } ); | ||
|
||
const onChangeControl = useCallback( | ||
( newValue: string ) => | ||
onChange( { | ||
[ id ]: newValue, | ||
} ), | ||
[ id, onChange ] | ||
); | ||
|
||
return ( | ||
<TextControl | ||
label={ label } | ||
placeholder={ placeholder } | ||
value={ value ?? '' } | ||
onChange={ onChangeControl } | ||
__next40pxDefaultSize | ||
__nextHasNoMarginBottom | ||
hideLabelFromVision={ hideLabelFromVision } | ||
/> | ||
); | ||
} |
This file contains 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
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.
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.
Does this mean that, for TypeScript, getControlByType always returns a control? When implementing this same idea the issue I had was working with the null through all the chain of components.
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.
We we need to throw an error here?
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.
The reason, is what @oandregal said above, it's the way to force typescript to consider that this function should always return a defined control.