From 30b577138dda685f65a8529be1866afa6e321845 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 4 Oct 2016 11:50:26 +0200 Subject: Squashed 'thirdparty/preact/' content from commit b2d90cc git-subtree-dir: thirdparty/preact git-subtree-split: b2d90cc116f1d1998f7a7c98dc6986bf4c1841f4 --- src/linked-state.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/linked-state.js (limited to 'src/linked-state.js') diff --git a/src/linked-state.js b/src/linked-state.js new file mode 100644 index 000000000..ed72bd8bc --- /dev/null +++ b/src/linked-state.js @@ -0,0 +1,28 @@ +import { isString, delve } from './util'; + +/** Create an Event handler function that sets a given state property. + * @param {Component} component The component whose state should be updated + * @param {string} key A dot-notated key path to update in the component's state + * @param {string} eventPath A dot-notated key path to the value that should be retrieved from the Event or component + * @returns {function} linkedStateHandler + * @private + */ +export function createLinkedState(component, key, eventPath) { + let path = key.split('.'), + p0 = path[0]; + return function(e) { + let t = e && e.currentTarget || this, + s = component.state, + obj = s, + v = isString(eventPath) ? delve(e, eventPath) : t.nodeName ? ((t.nodeName+t.type).match(/^input(che|rad)/i) ? t.checked : t.value) : e, + i; + if (path.length>1) { + for (i=0; i