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
Make boolean "false" a valid attribute value #592
Conversation
Codecov Report
@@ Coverage Diff @@
## master #592 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 1 1
Lines 136 136
Branches 41 41
=====================================
Hits 136 136
Continue to review full report at Codecov.
|
I need to do a bit more testing to make sure this does not break other stuff. Good job @frenzzy! |
Oh.. looks like this PR introduces inconsistency between client and server side rendering because on server side we can't check if attribute belongs to element without whitelisting. It is much easier to just not render falsey attributes. See react-dom/src/shared/DOMProperty.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kind of like the current behavior. For some attributes, like disabled
, it's preferable (to me) to remove it completely when false
. Or would it still function as expected if setting to false
?
For now, if you want to set an attribute to false, do strings work?
EDIT: Some quick testing leads me to believe that for <input disabled />
, you need the attribute to completely disappear to re-enable it. We could use null
for that, but it's more intuitive to model enable/disabled state with a boolean, I think.
// renders a disabled <input> to the DOM
h('input', { oncreate: (el) => { el.setAttribute('disabled', false)} })
After this PR only non-standard attributes will render
|
@SkaterDad I have a feeling this has been discussed before and we agreed with each other.
Why was that again? 🤔 |
@frenzzy This needs more tests and also more cowbell. 🐮🔔 |
Can you put tests to verify this? When I code this: h('input', { oncreate: (el) => { el.setAttribute('disabled', false)} }) Chrome renders this, which is still a disabled element functionally: <input disabled="false"> |
Yes, I'm scared about this change too. But do not have a real app to test this yet. |
@SkaterDad there are check in code: const element = <input>
const attributeName = 'disabled'
const value = false
if (attributeName in element) {
element[attributeName] = value
// => <input />
} else {
// use element.setAttribute
} |
@frenzzy Thanks for pointing that out. I must have missed that My use cases shouldn't be affected by this PR, then, since I'm not doing any SSR currently. |
I gonna close this PR because it introduces mismatching between client and server side rendering and does not help in any case to render boolean |
before:
<div data-attr={false} />
=><div></div>
after:
<div data-attr={false} />
=><div data-attr="false"></div>
Useful for attributes such as draggable and for custom data-attributes.
The does not affect props like
checked
and they will continue work as previously.Hope this change also decrease library size on a few bytes :)