Skip to content

Commit

Permalink
Fix issues with object-spreading boolean and direct props (#145)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
goto-bus-stop authored Mar 13, 2019
1 parent 7088087 commit aba43cb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ function nanoHtmlCreateElement (tag, props, children) {
}
// If a property is boolean, set itself to the key
if (BOOL_PROPS.indexOf(key) !== -1) {
if (val === 'true') val = key
else if (val === 'false') continue
if (String(val) === 'true') val = key
else if (String(val) === 'false') continue
}
// If a property prefers being set directly vs setAttribute
if (key.slice(0, 2) === 'on' || DIRECT_PROPS.indexOf(key) !== -1) {
Expand Down
8 changes: 5 additions & 3 deletions lib/set-attribute.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var DIRECT_PROPS = require('./direct-props')

module.exports = function nanohtmlSetAttribute (el, attr, value) {
if (typeof attr === 'object') {
for (var i in attr) {
Expand All @@ -10,12 +12,12 @@ module.exports = function nanohtmlSetAttribute (el, attr, value) {
if (!attr) return
if (attr === 'className') attr = 'class'
if (attr === 'htmlFor') attr = 'for'
if (attr.slice(0, 2) === 'on') {
if (attr.slice(0, 2) === 'on' || DIRECT_PROPS.indexOf(attr) !== -1) {
el[attr] = value
} else {
// assume a boolean attribute if the value === true
// no need to do typeof because "false" would've caused an early return
// assume a boolean attribute if the value === true or false
if (value === true) value = attr
if (value === false) return
el.setAttribute(attr, value)
}
}
23 changes: 23 additions & 0 deletions tests/browser/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ test('create inputs', function (t) {
t.end()
})

test('create inputs with object spread', function (t) {
t.plan(7)

var expected = 'testing'
var props = { type: 'text', value: expected }
var result = html`<input ${props} />`
t.equal(result.tagName, 'INPUT', 'created an input')
t.equal(result.value, expected, 'set the value of an input')

props = { type: 'checkbox', checked: true, disabled: false, indeterminate: true }
result = html`<input ${props} />`
t.equal(result.getAttribute('type'), 'checkbox', 'created a checkbox')
t.equal(result.getAttribute('checked'), 'checked', 'set the checked attribute')
t.equal(result.getAttribute('disabled'), null, 'should not have set the disabled attribute')
t.equal(result.indeterminate, true, 'should have set indeterminate property')

props = { indeterminate: true }
result = html`<input ${props} />`
t.equal(result.indeterminate, true, 'should have set indeterminate property')

t.end()
})

test('can update and submit inputs', function (t) {
t.plan(2)
document.body.innerHTML = ''
Expand Down

0 comments on commit aba43cb

Please sign in to comment.