UNPKG

929 BJavaScriptView Raw
1import { isString, delve } from './util';
2
3/** Create an Event handler function that sets a given state property.
4 * @param {Component} component The component whose state should be updated
5 * @param {string} key A dot-notated key path to update in the component's state
6 * @param {string} eventPath A dot-notated key path to the value that should be retrieved from the Event or component
7 * @returns {function} linkedStateHandler
8 * @private
9 */
10export function createLinkedState(component, key, eventPath) {
11 let path = key.split('.');
12 return function(e) {
13 let t = e && e.target || this,
14 state = {},
15 obj = state,
16 v = isString(eventPath) ? delve(e, eventPath) : t.nodeName ? (t.type.match(/^che|rad/) ? t.checked : t.value) : e,
17 i = 0;
18 for ( ; i<path.length-1; i++) {
19 obj = obj[path[i]] || (obj[path[i]] = !i && component.state[path[i]] || {});
20 }
21 obj[path[i]] = v;
22 component.setState(state);
23 };
24}