react-jam-ui
Version:
React JAM UI components
21 lines (19 loc) • 557 B
JavaScript
export default function twoWayBinding(ref) {
const target = this[ref];
const value = target.type === 'checkbox' ? target.checked : target.value;
const parts = ref.split('.');
const field = parts[0];
parts.shift();
const result = parts.length ? this.state[field] : value;
let tmp = result;
parts.forEach(function(field, i) {
if (i == parts.length - 1) {
tmp[field] = value
} else {
tmp = tmp[field] || {}
}
})
this.setState({
[]: result
});
}