forked from foxhound87/mobx-react-form
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.js
More file actions
executable file
·153 lines (127 loc) · 3.36 KB
/
State.js
File metadata and controls
executable file
·153 lines (127 loc) · 3.36 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { observe } from 'mobx';
import _ from 'lodash';
import Options from './Options';
import Bindings from './Bindings';
import utils from './utils';
export default class State {
mode = 'mixed';
strict = false;
form;
options;
bindings;
$extra;
disposers = {
interceptor: {},
observer: {},
};
$struct = [];
initial = {
props: {},
fields: {},
};
current = {
props: {},
fields: {},
};
constructor({
form, initial, options, bindings,
}) {
this.set('form', form);
this.initProps(initial);
this.options = new Options();
this.options.set(options);
this.bindings = new Bindings();
this.bindings.register(bindings);
this.observeOptions();
}
initProps(initial) {
const initialProps = _.pick(initial, [
...utils.props.separated,
...utils.props.validation,
...utils.props.function,
...utils.props.handlers,
]);
this.set('initial', 'props', initialProps);
const $unified = utils.hasUnifiedProps(initial);
const $separated = utils.hasSeparatedProps(initial);
if ($unified && $separated) {
console.warn( // eslint-disable-line
'WARNING: Your mobx-react-form instance ', this.form.name,
' is running in MIXED Mode (Unified + Separated) as fields properties definition.',
'This mode is experimental, use it at your own risk, or use only one mode.',
);
}
if (($separated || utils.isStruct(initial.fields)) && !$unified) {
const struct = utils.$try(initial.struct || initial.fields);
this.struct(struct);
this.strict = true;
this.mode = 'separated';
return;
}
this.struct(initial.struct);
this.mode = 'unified';
}
/**
Get/Set Fields Structure
*/
struct(data = null) {
if (data) this.$struct = data;
return this.$struct;
}
/**
Get Props/Fields
*/
get(type, subtype) {
return this[type][subtype];
}
/**
Set Props/Fields
*/
set(type, subtype, state = null) {
if (type === 'form') {
// subtype is the form here
this.form = subtype;
}
if (type === 'initial') {
Object.assign(this.initial[subtype], state);
Object.assign(this.current[subtype], state);
}
if (type === 'current') {
Object.assign(this.current[subtype], state);
}
}
extra(data = null) {
if (_.isString(data)) return _.get(this.$extra, data);
if (data === null) return this.$extra;
this.$extra = data;
return null;
}
observeOptions() {
// Fix Issue #201
observe(this.options.options, utils.checkObserve([{
// start observing fields validateOnChange
type: 'update',
key: 'validateOnChange',
to: true,
exec: () => this.form.each(field => field.observeValidationOnChange()),
}, {
// stop observing fields validateOnChange
type: 'update',
key: 'validateOnChange',
to: false,
exec: () => this.form.each(field => field.disposeValidationOnChange()),
}, {
// start observing fields validateOnBlur
type: 'update',
key: 'validateOnBlur',
to: true,
exec: () => this.form.each(field => field.observeValidationOnBlur()),
}, {
// stop observing fields validateOnBlur
type: 'update',
key: 'validateOnBlur',
to: false,
exec: () => this.form.each(field => field.disposeValidationOnBlur()),
}]));
}
}