houdini/app/javascript/legacy/components/set-state-from-value.js
2020-04-23 14:09:14 -05:00

18 lines
468 B
JavaScript

// License: LGPL-3.0-or-later
module.exports = function (state, ev){
var target = ev.target
var names = target.name.split('.')
var value = target.type === 'checkbox' ? target.checked : target.value
var nestedState = state
for(var i = 0, len = names.length - 1; i < len; ++i) {
if(nestedState[names[i]] === undefined) return state
nestedState = nestedState[names[i]]
}
var lastKey = names[names.length - 1]
nestedState[lastKey] = value
return state
}