Support && operator-based conditionals in expressions
See original GitHub issueWhat do you think of supporting &&
operator-based conditionals in interpolated expressions?
Current syntax:
html`<input type="checkbox" name="reviewed" ${data.reviewed ? 'checked' : ''} />`
Proposed syntax to support:
html`<input type="checkbox" name="reviewed" ${data.reviewed && 'checked'} />`
The proposed syntax is simply more succinct, and I believe it’s popular in the react ecosystem. While the above example doesn’t save much text, the : ''
gets in the way more when you have multi-line expressions – it becomes a trailing piece that is often overlooked.
Currently, the proposed syntax works in bel if the first condition is truthy, but if it is falsy, bel renders its falsy value, and you’d end up with <input type="checkbox" name="reviewed" undefined />
. I believe the way around this would be for bel (or maybe hyperx?) to not return anything from an interpolated expression that evaluates to undefined
or false
. I believe this is how JSX does it.
If for some reason you want to render undefined
or false
, I think it would be reasonable to convert it to a string (since it’s a string once it’s in the DOM anyway). I think the exception to this would be the value 0
– it may be unexpected to have to convert 0
to a string just to render it to the DOM. 0
comes in to play when you want to do ${rows.length && 'foo'}
vs. ${rows.length > 0 && 'foo'}
.
/cc @yoshuawuyts
Issue Analytics
- State:
- Created 7 years ago
- Reactions:6
- Comments:8 (5 by maintainers)
I’m on board with this; seems useful enough and can’t think of a reason to render the value
undefined
like ever@mjstahl yeah, hyperx is not an easy codebase. i’d have to go in with some console.logs myself to be sure, but I think the places to look at would be the places that call
strfn
andconcat
. Those functions are used to stringify text and attribute values and to concatenate them if there is a${templatePart}
inside.