-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Quick edit: add Date as field and
datetime
as field type (#64267)
Co-authored-by: oandregal <[email protected]> Co-authored-by: ntsekouras <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: mirka <[email protected]> Co-authored-by: tyxla <[email protected]> Co-authored-by: jameskoster <[email protected]>
- Loading branch information
1 parent
b11bdc0
commit 0ceac8f
Showing
11 changed files
with
206 additions
and
18 deletions.
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
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
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
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,95 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { BaseControl, TimePicker, SelectControl } from '@wordpress/components'; | ||
import { useCallback } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { | ||
SortDirection, | ||
ValidationContext, | ||
DataFormControlProps, | ||
} from '../types'; | ||
|
||
function sort( a: any, b: any, direction: SortDirection ) { | ||
const timeA = new Date( a ).getTime(); | ||
const timeB = new Date( b ).getTime(); | ||
|
||
return direction === 'asc' ? timeA - timeB : timeB - timeA; | ||
} | ||
|
||
function isValid( value: any, context?: ValidationContext ) { | ||
if ( context?.elements ) { | ||
const validValues = context?.elements.map( ( f ) => f.value ); | ||
if ( ! validValues.includes( value ) ) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
function Edit< Item >( { | ||
data, | ||
field, | ||
onChange, | ||
}: DataFormControlProps< Item > ) { | ||
const { id, label } = field; | ||
const value = field.getValue( { item: data } ); | ||
|
||
const onChangeControl = useCallback( | ||
( newValue: string | null ) => | ||
onChange( ( prevItem: Item ) => ( { | ||
...prevItem, | ||
[ id ]: newValue, | ||
} ) ), | ||
[ id, onChange ] | ||
); | ||
|
||
if ( field.elements ) { | ||
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 | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<fieldset> | ||
<BaseControl.VisualLabel as="legend"> | ||
{ label } | ||
</BaseControl.VisualLabel> | ||
<TimePicker | ||
currentTime={ value } | ||
onChange={ onChangeControl } | ||
hideLabelFromVision | ||
/> | ||
</fieldset> | ||
); | ||
} | ||
|
||
export default { | ||
sort, | ||
isValid, | ||
Edit, | ||
}; |
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
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.
0ceac8f
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.
Flaky tests detected in 0ceac8f.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/10315263934
📝 Reported issues:
/test/e2e/specs/widgets/customizing-widgets.spec.js