aboutsummaryrefslogtreecommitdiff
path: root/thirdparty/preact/src/linked-state.js
diff options
context:
space:
mode:
authortg(x) <*@tg-x.net>2016-10-05 00:02:10 +0200
committertg(x) <*@tg-x.net>2016-10-05 00:02:10 +0200
commitec62d29c90958aa8d41474ed2fe5a179d6fafed7 (patch)
tree367cc55bc6772cf194ed6c4778cd344d581d3d7c /thirdparty/preact/src/linked-state.js
parentfda241d74d5c1c39203b64da676c684d4dc9d800 (diff)
parentd3ccf4103900b8d990b1970d135695b938d94eae (diff)
Merge branch 'master' of taler.net:/var/git/wallet-webex
Diffstat (limited to 'thirdparty/preact/src/linked-state.js')
-rw-r--r--thirdparty/preact/src/linked-state.js28
1 files changed, 28 insertions, 0 deletions
diff --git a/thirdparty/preact/src/linked-state.js b/thirdparty/preact/src/linked-state.js
new file mode 100644
index 000000000..ed72bd8bc
--- /dev/null
+++ b/thirdparty/preact/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<path.length-1; i++) {
+ obj = obj[path[i]] || (obj[path[i]] = {});
+ }
+ obj[path[i]] = v;
+ v = s[p0];
+ }
+ component.setState({ [p0]: v });
+ };
+}