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

FormData is updated too late for md-checkbox #5028

Closed
fetishcoder opened this issue Sep 28, 2023 · 5 comments
Closed

FormData is updated too late for md-checkbox #5028

fetishcoder opened this issue Sep 28, 2023 · 5 comments
Assignees

Comments

@fetishcoder
Copy link

What is affected?

Component

Description

When I do:
The example in material-web/docs/intro.md
And I add a oninput or onchange event on the forms like so:

<form oninput="console.log(...new FormData(event.target.form))">
  <md-outlined-text-field label="Email" type="email"
      name="email" required></md-outlined-text-field>
  <label>
    Subscribe
    <md-checkbox name="subscribe"></md-checkbox>
  </label>

  <md-text-button type="reset">Reset</md-text-button>
  <md-outlined-button>Submit</md-outlined-button>
</form>

I would expect
When I click the checkbox, the console should display:
(2) ['email', ''] (2) ['subscribe', 'on']

However
It displays
(2) ['email', '']
When I click the checkbox again (turning it off...) the console will now display
(2) ['email', ''] (2) ['subscribe', 'on']

ps.
When I put the same event listener on the form with the html checkbox, I see the expected logs.
Also, the md-radio seemed to work as expected.

Reproduction

code included

Workaround

I have not found a workaround

Is this a regression?

No or unsure. This never worked, or I haven't tried before.

Affected versions

latest

Browser/OS/Node environment

chrome

@asyncLiz
Copy link
Collaborator

blargh, probably because it's updated asynchronously.

As a workaround, you should be able to await the element's updateComplete

async function handleChange(event) {
  await event.target.updateComplete;
  console.log(...new FormData(event.target.form))
}

@fetishcoder
Copy link
Author

Yes, I am looking at the code a bit and see that in the
md-checkbox this.internals.setFormValue is called in protected override update(changed: PropertyValues<Checkbox>)
where in the
md-radio this.internals.setFormValue is called in set checked (the setter of checked)

@asyncLiz asyncLiz self-assigned this Oct 31, 2023
copybara-service bot pushed a commit that referenced this issue Dec 5, 2023
… event

Fixes #5028

The input event is dispatched before the change event, so we need to update state there instead of on change.

PiperOrigin-RevId: 588177488
copybara-service bot pushed a commit that referenced this issue Dec 6, 2023
… event

Fixes #5028

The input event is dispatched before the change event, so we need to update state there instead of on change.

PiperOrigin-RevId: 588177488
@maicol07
Copy link

@asyncLiz it seems the fix didn't fix the formdata value when the checkbox is checked which returns 'false'. It should instead return 'on'

@asyncLiz
Copy link
Collaborator

@asyncLiz it seems the fix didn't fix the formdata value when the checkbox is checked which returns 'false'. It should instead return 'on'

Are you using @material/web@nightly? The next release with this fix hasn't been published.

@maicol07
Copy link

@asyncLiz it seems the fix didn't fix the formdata value when the checkbox is checked which returns 'false'. It should instead return 'on'

Are you using @material/web@nightly? The next release with this fix hasn't been published.

Nevermind, it was my fault with custom code. Works in latest nightly, by the way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants