Skip to content
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

Accept selection state from mutating select elements #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports.update = function (fromNode, toNode, opts) {
}
}
// copy values for form elements
if ((f.nodeName === 'INPUT' && f.type !== 'file') || f.nodeName === 'SELECT') {
if (f.nodeName === 'INPUT' && f.type !== 'file') {
// Keep value if not set on mutating element
if (t.getAttribute('value') === null) t.value = f.value
} else if (f.nodeName === 'TEXTAREA') {
if (t.getAttribute('value') === null) f.value = t.value
Expand Down
32 changes: 30 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,29 @@ test('custom event listeners and properties are ignored', function (t) {
el.click()
})

test('input values get copied', function (t) {
test('input value gets copied from mutating element', function (t) {
t.plan(1)
var el = yo`<input type="text" />`
el.value = 'hi'
var newEl = yo`<input type="text" />`
newEl.setAttribute('value', 'hi')
yo.update(el, newEl)
t.equal(el.value, 'hi')
})

test('input value can be cleared from mutating element', function (t) {
t.plan(1)
var el = yo`<input type="text" />`
el.setAttribute('value', 'hi')
var newEl = yo`<input type="text" />`
newEl.setAttribute('value', '')
yo.update(el, newEl)
t.equal(el.value, '')
})

test('input value is kept if mutating element doesn\'t have one', function (t) {
t.plan(1)
var el = yo`<input type="text" />`
el.setAttribute('value', 'hi')
var newEl = yo`<input type="text" />`
yo.update(el, newEl)
t.equal(el.value, 'hi')
Expand All @@ -55,3 +74,12 @@ test('textarea values get copied', function (t) {
yo.update(el, textarea('bar'))
t.equal(el.value, 'bar')
})

test('select element selection state gets copied', function (t) {
t.plan(1)
var el = yo`<select><option>0</option><option>1</option></select>`
var newEl = yo`<select><option>0</option><option selected>1</option></select>`

yo.update(el, newEl)
t.equal(el.selectedIndex, 1)
})