Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Fix for the radio button not working with onChange #287

Merged
merged 4 commits into from
Jan 19, 2017

Conversation

cedmax
Copy link
Contributor

@cedmax cedmax commented Jan 19, 2017

As suggested in the comment to the merged PR (ref: #247) the event normalisation has broken the support for onChange on radio buttons. This is a potential fix

As suggested in the comment to the merged PR (ref: preactjs#247) the event normalisation has broken the support for onChange on radio buttons. This is a potential fix
@@ -337,7 +337,7 @@ function applyEventNormalization({ nodeName, attributes }) {
}
if (props.onchange) {
nodeName = nodeName.toLowerCase();
let attr = nodeName==='input' && String(attributes.type).toLowerCase()==='checkbox' ? 'onclick' : 'oninput',
let attr = nodeName==='input' && ['checkbox', 'radio'].indexOf(String(attributes.type).toLowerCase()) > -1 ? 'onclick' : 'oninput',
Copy link
Member

Choose a reason for hiding this comment

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

Looks great! Any chance we could shorten this to the technique used here?

let attr = nodeName==='input' && attributes.type.match(/^che|rad/i) ? 'onclick' : 'oninput',

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, it makes sense, I will make the change

Copy link
Contributor Author

@cedmax cedmax Jan 19, 2017

Choose a reason for hiding this comment

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

just one thing: do you want me to lose the String(...).toLowerCase(): it was in the original, that's why I kept it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The PR is updated without it anyway, if you need it back let me know

Copy link
Member

Choose a reason for hiding this comment

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

We might need the String() wrap, the regex handles case insensitivity.

@developit
Copy link
Member

(left this inline but it got collapsed by github haha)

Good catch on that, it would have bailed for <input /> without a type. Let's switch to .test() since that doesn't care if the argument is a string:

let attr = nodeName==='input' && /^che|rad/i.test(attributes.type) ? 'onclick' : 'oninput',

we're good to merge once that's in place :)

I might add a few tests for posterity post-merge.

@cedmax
Copy link
Contributor Author

cedmax commented Jan 19, 2017

The PR is updated with the changes, thanks for the speedy process

@developit
Copy link
Member

thanks for the speedy fix!

@developit developit merged commit 97c795d into preactjs:master Jan 19, 2017
@ustccjw
Copy link
Contributor

ustccjw commented Sep 4, 2017

@developit why delete onclick for radio now?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants