Skip to content
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

PostStatus: Prevent form submission from refreshing the page #69526

Merged
merged 3 commits into from
Mar 19, 2025

Conversation

SainathPoojary
Copy link
Contributor

@SainathPoojary SainathPoojary commented Mar 11, 2025

Closes: #69524

What?

Fixes an issue where enabling password protection and entering a password in the “Status & Visibility” popover caused an unintended form submission, leading to a page refresh.

Why?

When pressing Enter in the password input field, the browser attempted to submit the form, refreshing the page. Since the “Save Draft” and “Publish” buttons remain disabled unless there is content in the post (title or content)

How?

• Prevents default form submission behavior to stop the page from refreshing.

Testing Instructions

  1. Open the block editor and create a new post or page.
  2. Click on the link next to the “Status” label to open the “Status & Visibility” popover.
  3. Enable password protection and enter a password in the input field.
  4. Press Enter and confirm that:
    • The page does not refresh.
    • The popover closes as expected.
    • The “Save Draft” button remains disabled if no content exists.
    • Use Keyboard shortcut to save the post
  5. Add content (title or body) and verify that the “Save Draft” button is enabled, allowing you to save the post with the password.

Screenshots or screencast

Before:

2025-03-11.14-46-43.mp4

After:

2025-03-11.14-50-10.mp4

@SainathPoojary SainathPoojary marked this pull request as ready for review March 11, 2025 09:32
Copy link

github-actions bot commented Mar 11, 2025

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: SainathPoojary <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: hbhalodia <[email protected]>
Co-authored-by: carolinan <[email protected]>

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

@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Feature] Document Settings Document settings experience labels Mar 12, 2025
Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

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

Thanks, @SainathPoojary!

This was great in most cases, but I noticed that submitting from the password field doesn't work when the Status is set to "Scheduled" and the date picker is displayed.

Screencast

CleanShot.2025-03-12.at.18.27.44.mp4

@SainathPoojary
Copy link
Contributor Author

Hey @Mamaduka,

I’ve moved the logic to TextControl, and it’s now working for all cases.

2025-03-13.15-33-24.mp4

@SainathPoojary SainathPoojary requested a review from Mamaduka March 13, 2025 10:22
@Mamaduka
Copy link
Member

I’ve moved the logic to TextControl, and it’s now working for all cases.

The form provides onSumbit logic out of the box. I think the component should be using it since it's already wrapped in a form. We just need to debug. Why is it not working when scheduling inputs are rendered?

Moving logic to TextControl doesn't fix the root cause of the issue.

@SainathPoojary
Copy link
Contributor Author

Hey @Mamaduka,

I tried to find the root cause and discovered that this issue occurs because the onSubmit event, triggered by pressing Enter, only works when there is a single input field. However, when there are multiple input fields, it doesn’t trigger the onSubmit event. Below is a demonstration using simple HTML

2025-03-14.13-32-53.mp4

An alternative approach on triggering onSubmit would be to trigger onSubmit when Enter is pressed on the Password TextControl.

<form
  onSubmit={ ( event ) => {
      event.preventDefault();
      onClose();
  } }
>
<TextControl
  ...
  onKeyDown={(event) => {
    if (event.key === "Enter") {
      event.preventDefault();
      event.target.form.dispatchEvent(
        new CustomEvent("submit", {
          bubbles: true,
          cancelable: true,
        })
      );
    }
  }}
/>;

Please Let me know your thoughts.

@Mamaduka
Copy link
Member

Excellent detective work, @SainathPoojary!

In that case, I think going with the first approach (a4f23f9) makes more sense. Why?

  • We're not modifying expected form behavior when there are two or more input fields.
  • The new onSubmit callback will prevent actual form submissions from reloading the page, when only the password field is displayed.

Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

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

Thanks for the follow-ups, @SainathPoojary!

@Mamaduka Mamaduka merged commit 8e1d651 into WordPress:trunk Mar 19, 2025
60 checks passed
@github-actions github-actions bot added this to the Gutenberg 20.6 milestone Mar 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Document Settings Document settings experience [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't confirm the password for password protected pages by pressing enter
3 participants