Skip to content

Conversation

@oandregal
Copy link
Member

@oandregal oandregal commented Oct 30, 2025

What?

This PR is a refactor: doesn't change the behavior, just the internal structure of the code.

It normalizes all the form fields at any nested level in a single place, while before this normalization happened at several layers (the top-level ones in DataForm, some others in the specific layout, etc.).

Why?

To simplify the code. And to support this follow-up PR #72588

How?

  • Create a normalizedForm method that substitutes to normalizeFormFields and normalizeLayout.
  • The normalizeForm method recursively normalizes any nested level.
  • Simplifies types by coalescing SimpleFormField and CombinedFormField into one FormField.

Testing Instructions

  • Automated tests (unit, e2e) should pass.
  • Verify there's no behavioral change in the local storybook (npm run storybook:dev) as compared to the storybook for trunk (see online).
  • Verify quick edit in site editor still works as before (Site Editor > Pages).

@oandregal oandregal self-assigned this Oct 30, 2025
@oandregal oandregal added [Type] Enhancement A suggestion for improvement. [Feature] DataViews Work surrounding upgrading and evolving views in the site editor and beyond labels Oct 30, 2025
<div className="dataforms-layouts-row__field-control">
<RegularLayout
data={ data }
field={ fieldDefinition }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a bug: we were passing a field definition, but the regular layout expected the form field.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the type of field that would have used it:

{
 id: 'name',
  layout: { type: 'row' },
},

It still worked fine at runtime because the only thing we check is the field.id (and that's the same as the field definition).

I'm not sure why TypeScript didn't raise this issue. The type expected by RegularLayout is a FormField (see), but the fieldDefinition is a NormalizedField.

export type SimpleFormField = {
id: string;
layout?: Layout;
label?: string;
Copy link
Member Author

@oandregal oandregal Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While unsupported by the type, label and description were passed down in the normalization. This just clarifies the types to support them by having a single FormField type.

onChange,
validity,
}: DataFormProps< Item > ) {
const normalizedForm = useMemo( () => normalizeForm( form ), [ form ] );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By doing this at this level, layouts and any other components do not need to normalize anything. They can simply operate with the normalized versions of layout and fields.

const { fields } = useContext( DataFormContext );
const layout = field.layout as NormalizedCardLayout;

const layout: NormalizedCardLayout = normalizeLayout( {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the kind of computation we get rid of. It means developing a layout is simpler.

@oandregal oandregal force-pushed the update/normalize-form-fields branch from bc00954 to eee6d62 Compare October 31, 2025 11:33
Array.isArray( field.children ) && {
children: normalizeForm( {
fields: field.children,
layout: DEFAULT_LAYOUT,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another win: clarifying and add test for how the layout is inherited across nested levels. Only the top-level fields inherit the form layout. Any other layers, are normalized to use the default (regular).

@oandregal oandregal marked this pull request as ready for review October 31, 2025 12:01
@github-actions
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: oandregal <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

Flaky tests detected in ee3bdf8.
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/18971898579
📝 Reported issues:

@oandregal
Copy link
Member Author

I'm going to merge this refactor to continue work at #72588

@oandregal oandregal merged commit 1a54868 into trunk Oct 31, 2025
38 checks passed
@oandregal oandregal deleted the update/normalize-form-fields branch October 31, 2025 12:45
@github-actions github-actions bot added this to the Gutenberg 22.1 milestone Oct 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] DataViews Work surrounding upgrading and evolving views in the site editor and beyond [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants