Skip to content

Forms: Fix syncing of shared styles for nested fields and related undo issue #41708

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 7 commits into from
Feb 17, 2025

Conversation

aaronrobertshaw
Copy link
Contributor

@aaronrobertshaw aaronrobertshaw commented Feb 11, 2025

Fixes #41704

Proposed changes:

  • Updates the useSharedFieldAttributes hook to factor in that form fields can be nested within other blocks and may not be immediate children of the primary form block.
  • Fixes multiple bugs related to style syncing
    • Prevents fields from using themselves as the source for shared attributes
    • Eliminates duplicate undo stack entries that were causing undo loops
    • Fixes undo when syncing styles on block insertion e.g. dropping field from second form into the current one.

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

N/A

Does this pull request change what data or activity we track or use?

No changes.

Testing instructions:

  1. Test nested field style syncing:

    • Add a form with fields nested in a Group block (example block markup provided below)
    • Enable "sync field styles" on nested fields, if not already set
    • Modify styles on a nested field and verify all opted-in fields update
    • Modify styles on a top-level field and verify nested fields update correctly
  2. Test undo functionality:

    • Make style changes to synced fields
    • Verify that undo/redo operations work smoothly without loops
    • Drop a new field into the form and verify undo still works appropriately
      • Note: this PR doesn't aim to make form and field undos perfect just improve the situation around syncing styles
  3. Test block insertion with synced styles:

    • Ensure some form fields have shared styles set
    • Add a new field to the beginning of the form (either by drag and dropping one or inserter)
    • Verify it correctly inherits the shared styles from existing fields
<!-- wp:jetpack/contact-form {"style":{"spacing":{"padding":{"top":"16px","right":"16px","bottom":"16px","left":"16px"}}}} -->
<div class="wp-block-jetpack-contact-form" style="padding-top:16px;padding-right:16px;padding-bottom:16px;padding-left:16px"><!-- wp:jetpack/field-name {"required":true,"placeholder":"Placeholder"} /-->

<!-- wp:jetpack/field-email {"required":true,"placeholder":"Placeholder"} /-->

<!-- wp:group {"style":{"spacing":{"padding":{"top":"var:preset|spacing|30","bottom":"var:preset|spacing|30","left":"var:preset|spacing|30","right":"var:preset|spacing|30"}},"color":{"background":"#eeeeee"}},"layout":{"type":"flex","flexWrap":"nowrap"}} -->
<div class="wp-block-group has-background" style="background-color:#eeeeee;padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--30)"><!-- wp:jetpack/field-name {"label":"Nested Field 1","required":true,"placeholder":"Placeholder","width":50} /-->

<!-- wp:jetpack/field-email {"label":"Nested Field 2","required":true,"placeholder":"Placeholder","width":50} /--></div>
<!-- /wp:group -->

<!-- wp:jetpack/field-textarea {"label":"Message","placeholder":"Placeholder"} /-->

<!-- wp:jetpack/button {"element":"button","text":"Contact Us","lock":{"remove":true}} /--></div>
<!-- /wp:jetpack/contact-form -->

Nested Field Syncing

Before After
Screen.Recording.2025-02-11.at.4.45.07.pm.mp4
Screen.Recording.2025-02-11.at.4.48.52.pm.mp4

Undo fix for general style sync

Before After
Screen.Recording.2025-02-12.at.4.22.18.pm.mp4
Screen.Recording.2025-02-12.at.4.24.06.pm.mp4

Source of truth and sync on mount fixes

Before After
Screen.Recording.2025-02-12.at.5.30.28.pm.mp4
Screen.Recording.2025-02-12.at.5.31.20.pm.mp4

@aaronrobertshaw aaronrobertshaw added [Type] Bug When a feature is broken and / or not performing as intended [Package] Forms [Feature] Forms Blocks Blocks designed to streamline user input and engagement, such as contact, newsletter sign-ups, etc. labels Feb 11, 2025
@aaronrobertshaw aaronrobertshaw requested review from talldan and a team February 11, 2025 07:04
@aaronrobertshaw aaronrobertshaw self-assigned this Feb 11, 2025
@@ -3,7 +3,7 @@
*/
import { useDispatch, useSelect } from '@wordpress/data';
import { useCallback, useEffect } from '@wordpress/element';
import { isEmpty, filter, first, map, pick, isNil } from 'lodash';
import { isEmpty, first, map, pick, isNil } from 'lodash';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A good followup here would be to switch out the use of lodash functions for vanilla JS.

Copy link
Contributor

github-actions bot commented Feb 11, 2025

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WordPress.com Simple site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin, and enable the fix/synced-styles-for-nested-fields branch.

  • To test on Simple, run the following command on your sandbox:

    bin/jetpack-downloader test jetpack fix/synced-styles-for-nested-fields
    

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions github-actions bot added [Block] Contact Form Form block (also see Contact Form label) [Feature] Contact Form labels Feb 11, 2025
Copy link
Contributor

github-actions bot commented Feb 11, 2025

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Choose a review path based on your changes:
    • A. Team Review: add the "[Status] Needs Team Review" label
      • For most changes, including minor cross-team impacts.
      • Example: Updating a team-specific component or a small change to a shared library.
    • B. Crew Review: add the "[Status] Needs Review" label
      • For significant changes to core functionality.
      • Example: Major updates to a shared library or complex features.
    • C. Both: Start with Team, then request Crew
      • For complex changes or when you need extra confidence.
      • Example: Refactor affecting multiple systems.
  3. Get at least one approval before merging.

Still unsure? Reach out in #jetpack-developers for guidance!

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Feb 11, 2025
@aaronrobertshaw aaronrobertshaw added the [Status] Needs Team Review Obsolete. Use Needs Review instead. label Feb 11, 2025
Copy link
Contributor

github-actions bot commented Feb 11, 2025

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/packages/forms/src/blocks/contact-form/util/with-shared-field-attributes.js 0/38 (0.00%) 0.00% 12 💔

Full summary · PHP report · JS report

Add label I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage. to override the failing coverage check.

talldan
talldan previously approved these changes Feb 11, 2025
Copy link
Contributor

@talldan talldan left a comment

Choose a reason for hiding this comment

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

LGTM.

I spotted a bug with the code in trunk (see the review comment), but I think it can be handled separately from this PR. I'll leave it up to you.

NIH, but something else is the useSelect will, I think, be pretty inefficient. It'll re-calculate siblings constantly, even when the user is only hovering different blocks. It'd be better to calculate siblings in the callback IMO.

@aaronrobertshaw
Copy link
Contributor Author

Appreciate the speedy review @talldan 👍

NIH, but something else is the useSelect will, I think, be pretty inefficient. It'll re-calculate siblings constantly, even when the user is only hovering different blocks. It'd be better to calculate siblings in the callback IMO.

I've pushed a commit I think improves the situation regarding repeatedly collecting siblings but I'll need to give it a proper test and more thought tomorrow.

I'm also not sure about the approach taken, so happy for further feedback to push it in the right direction.

@aaronrobertshaw aaronrobertshaw changed the title Forms: Fix syncing of shared styles for nested fields Forms: Fix syncing of shared styles for nested fields and related undo issue Feb 12, 2025
@aaronrobertshaw
Copy link
Contributor Author

I've updated this PR to not only fix the syncing of styles for the nested fields but also address some of the secondary issues noted in reviews and testing. These include:

  • Ensuring new blocks, whether inserted or dropped into the first position in the form, aren't used as the source of shared style attributes
  • Preventing the syncing of style attributes from causing an undo loop both when generally updating field styles and when dropping field blocks into an existing form with shared styles

The PR description and test instructions have been updated to reflect the latest state. Further enhancements or fixes should probably be done via follow-ups.

To that end, I think this is ready for a final review before merging 🙏

Copy link
Contributor

@talldan talldan left a comment

Choose a reason for hiding this comment

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

Tests well for me, and the code looks good. Thanks @aaronrobertshaw!

@talldan talldan added I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage. and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Feb 17, 2025
@talldan talldan merged commit d9110e8 into trunk Feb 17, 2025
67 of 69 checks passed
@talldan talldan deleted the fix/synced-styles-for-nested-fields branch February 17, 2025 07:21
@github-actions github-actions bot removed the [Status] Needs Team Review Obsolete. Use Needs Review instead. label Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Contact Form Form block (also see Contact Form label) [Feature] Contact Form [Feature] Forms Blocks Blocks designed to streamline user input and engagement, such as contact, newsletter sign-ups, etc. I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage. [Package] Forms [Type] Bug When a feature is broken and / or not performing as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Forms: Shared field styles break when fields are not immediate children of form
2 participants