-
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.
DataForm: implement first prototype using duplicate page action (#63032)
Co-authored-by: oandregal <[email protected]> Co-authored-by: fabiankaegy <[email protected]> Co-authored-by: youknowriad <[email protected]>
- Loading branch information
1 parent
e35fc99
commit 566b82a
Showing
8 changed files
with
167 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,106 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { Dispatch, SetStateAction } from 'react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { TextControl } from '@wordpress/components'; | ||
import { useCallback, useMemo } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { Form, Field, NormalizedField } from './types'; | ||
import { normalizeFields } from './normalize-fields'; | ||
|
||
type DataFormProps< Item > = { | ||
data: Item; | ||
fields: Field< Item >[]; | ||
form: Form; | ||
onChange: Dispatch< SetStateAction< Item > >; | ||
}; | ||
|
||
type DataFormControlProps< Item > = { | ||
data: Item; | ||
field: NormalizedField< Item >; | ||
onChange: Dispatch< SetStateAction< Item > >; | ||
}; | ||
|
||
function DataFormTextControl< Item >( { | ||
data, | ||
field, | ||
onChange, | ||
}: DataFormControlProps< Item > ) { | ||
const { id, header, placeholder } = field; | ||
const value = field.getValue( { item: data } ); | ||
|
||
const onChangeControl = useCallback( | ||
( newValue: string ) => | ||
onChange( ( prevItem: Item ) => ( { | ||
...prevItem, | ||
[ id ]: newValue, | ||
} ) ), | ||
[ id, onChange ] | ||
); | ||
|
||
return ( | ||
<TextControl | ||
label={ header } | ||
placeholder={ placeholder } | ||
value={ value } | ||
onChange={ onChangeControl } | ||
/> | ||
); | ||
} | ||
|
||
const controls: { | ||
[ key: string ]: < Item >( | ||
props: DataFormControlProps< Item > | ||
) => JSX.Element; | ||
} = { | ||
text: DataFormTextControl, | ||
}; | ||
|
||
function getControlForField< Item >( field: NormalizedField< Item > ) { | ||
if ( ! field.type ) { | ||
return null; | ||
} | ||
|
||
if ( ! Object.keys( controls ).includes( field.type ) ) { | ||
return null; | ||
} | ||
|
||
return controls[ field.type ]; | ||
} | ||
|
||
export default function DataForm< Item >( { | ||
data, | ||
fields, | ||
form, | ||
onChange, | ||
}: DataFormProps< Item > ) { | ||
const visibleFields = useMemo( | ||
() => | ||
normalizeFields( | ||
fields.filter( | ||
( { id } ) => !! form.visibleFields?.includes( id ) | ||
) | ||
), | ||
[ fields, form.visibleFields ] | ||
); | ||
|
||
return visibleFields.map( ( field ) => { | ||
const DataFormControl = getControlForField( field ); | ||
return DataFormControl ? ( | ||
<DataFormControl | ||
key={ field.id } | ||
data={ data } | ||
field={ field } | ||
onChange={ onChange } | ||
/> | ||
) : null; | ||
} ); | ||
} |
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
566b82a
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 566b82a.
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/9780824572
📝 Reported issues:
/test/e2e/specs/editor/blocks/navigation-frontend-interactivity.spec.js