Description
Hi!
I noticed that the chosen behaviour for boolean transform is
which is:
it's
true
if value of attribute starts witht
,y
or a non-0 number.
and it's tested:
react-to-web-component/tests/react-to-webcomponent.test.jsx
Lines 352 to 353 in 002a007
On the other hand HTML boolean (https://developer.mozilla.org/en-US/docs/Glossary/Boolean/HTML) attribute is considered true
just because it's present:
If an HTML tag contains a boolean attribute - no matter the value of that attribute - the attribute is set to
true
on that element. If an HTML tag does not contain the attribute, the attribute is set tofalse
.
That leads to interesting behaviour - if attribute is present, but not assigned a value (i.e. it's HTML Boolean), it's cast to false
(not just undefined
but actually the polar opposite)
I detected it wile using with Lit booleans (https://lit.dev/docs/templates/expressions/#boolean-attribute-expressions) - Lit implements HTML boolean.
I think it can be resolved in a backwards compatible way.
I see that transforms are given the attribute name as 2nd argument
- in constructor
- in
attributeChangedCallback
So parse could be made into
parse: (value, attribute) =>
// Check if it's HTML boolean
value === undefined
|| value === ''
|| value === attribute
/// Check if it's boolean by value
|| /^[ty1-9]/i.test(value),
I don't have an idea for stringify
, though. It would have to be a choice of the author (i.e. differently named transform) I guess