-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (72 loc) · 1.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"use strict";
const assign = require('deep-assign');
const React = require('react');
/**
* @class BindingComponent
*/
class BindingComponent extends React.Component {
/**
* @getter bindStateValue
*/
get bindStateValue() {
let path;
let pathIndex = 0;
let state;
try {
path = this.props.bindStateName.split('.');
state = this.props.bindStateCtx.state;
do {
let subpath = path[pathIndex];
state = state[subpath];
pathIndex++;
} while (pathIndex < path.length);
} catch (error) {
return null;
} finally {
return state;
}
}
/**
* @getter bindStateValue
*/
set bindStateValue(val) {
if (typeof this.props.bindStateType === 'function') {
if (this.props.bindStateType === Number && isNaN(val)) {
val = 0;
} else if (this.props.bindStateType === Boolean && typeof val === 'string') {
val = (val === 'true') ? true : false;
} else {
val = this.props.bindStateType(val);
}
}
this.props.bindStateCtx.setState((prevState, currentProps) => {
let path;
let pathReversed;
let overlay = val;
let pathIndex = 0;
try {
path = currentProps.bindStateName.split('.');
pathReversed = path.reverse();
} catch (error) {
return prevState;
}
do {
let subpath = pathReversed[pathIndex];
overlay = { [subpath]: overlay };
pathIndex++;
} while (pathIndex < pathReversed.length);
const nextState = assign({}, prevState, overlay);
const entry = path[0];
return {
[entry]: nextState[entry]
};
});
}
/**
* @event onChange
*/
onChange(event) {
this.bindStateValue = event.target.value;
}
}
exports.BindingComponent = BindingComponent;