Skip to content

Commit aba43cb

Browse files
Fix issues with object-spreading boolean and direct props (#145)
* add test for spread boolean props * Fix issues with object-spreading boolean and direct props Previously, - `<input ${{ disabled: true }}>` added a `disabled="true"` attribute. - `<input ${{ disabled: false }}>` added a `disabled="false"` attribute. - `<input ${{ indeterminate: true }}>` added an `indeterminate="true"` attribute. Now: - `<input ${{ disabled: true }}>` adds a `disabled="disabled"` attribute. - `<input ${{ disabled: false }}>` does not add a `disabled` attribute. - `<input ${{ indeterminate: true }}>` sets `.indeterminate = true` instead of adding an attribute.
1 parent 7088087 commit aba43cb

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

lib/browser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ function nanoHtmlCreateElement (tag, props, children) {
6363
}
6464
// If a property is boolean, set itself to the key
6565
if (BOOL_PROPS.indexOf(key) !== -1) {
66-
if (val === 'true') val = key
67-
else if (val === 'false') continue
66+
if (String(val) === 'true') val = key
67+
else if (String(val) === 'false') continue
6868
}
6969
// If a property prefers being set directly vs setAttribute
7070
if (key.slice(0, 2) === 'on' || DIRECT_PROPS.indexOf(key) !== -1) {

lib/set-attribute.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var DIRECT_PROPS = require('./direct-props')
2+
13
module.exports = function nanohtmlSetAttribute (el, attr, value) {
24
if (typeof attr === 'object') {
35
for (var i in attr) {
@@ -10,12 +12,12 @@ module.exports = function nanohtmlSetAttribute (el, attr, value) {
1012
if (!attr) return
1113
if (attr === 'className') attr = 'class'
1214
if (attr === 'htmlFor') attr = 'for'
13-
if (attr.slice(0, 2) === 'on') {
15+
if (attr.slice(0, 2) === 'on' || DIRECT_PROPS.indexOf(attr) !== -1) {
1416
el[attr] = value
1517
} else {
16-
// assume a boolean attribute if the value === true
17-
// no need to do typeof because "false" would've caused an early return
18+
// assume a boolean attribute if the value === true or false
1819
if (value === true) value = attr
20+
if (value === false) return
1921
el.setAttribute(attr, value)
2022
}
2123
}

tests/browser/elements.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,29 @@ test('create inputs', function (t) {
2121
t.end()
2222
})
2323

24+
test('create inputs with object spread', function (t) {
25+
t.plan(7)
26+
27+
var expected = 'testing'
28+
var props = { type: 'text', value: expected }
29+
var result = html`<input ${props} />`
30+
t.equal(result.tagName, 'INPUT', 'created an input')
31+
t.equal(result.value, expected, 'set the value of an input')
32+
33+
props = { type: 'checkbox', checked: true, disabled: false, indeterminate: true }
34+
result = html`<input ${props} />`
35+
t.equal(result.getAttribute('type'), 'checkbox', 'created a checkbox')
36+
t.equal(result.getAttribute('checked'), 'checked', 'set the checked attribute')
37+
t.equal(result.getAttribute('disabled'), null, 'should not have set the disabled attribute')
38+
t.equal(result.indeterminate, true, 'should have set indeterminate property')
39+
40+
props = { indeterminate: true }
41+
result = html`<input ${props} />`
42+
t.equal(result.indeterminate, true, 'should have set indeterminate property')
43+
44+
t.end()
45+
})
46+
2447
test('can update and submit inputs', function (t) {
2548
t.plan(2)
2649
document.body.innerHTML = ''

0 commit comments

Comments
 (0)