| 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424 |
9x
1x
1x
4x
3x
1x
1x
1x
1x
1x
5x
1x
1x
1x
1x
3x
1x
1x
1x
4x
1x
1x
1x
1x
1x
4x
1x
3x
1x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
4x
1x
3x
12x
12x
12x
12x
3x
3x
3x
3x
3x
3x
3x
3x
3x
12x
3x
3x
3x
3x
7x
7x
7x
4x
7x
2x
2x
5x
2x
3x
1x
1x
1x
1x
3x
3x
3x
1x
2x
7x
7x
7x
7x
7x
1x
1x
1x
1x
1x
1x
1x
1x
2x
2x
2x
2x
2x
2x
2x
2x
2x
2x
1x
1x
1x
1x
1x
1x
1x
| import React from 'react';
import {devOnly, setObjProperty, getObjProperty, isNotBlank} from './utils';
export function createFormValidator(message, isValidFunction) {
return {
message : message,
validatorFunction : isValidFunction,
setRequired : false
};
}
export function createIsRequiredFormValidator(message) {
message = message || 'Field is required';
let validator = createFormValidator(message, function(value) {
if (typeof value == 'string') {
return isNotBlank(value);
} else {
return value == undefined ? false : value ? true : false;
}
});
validator.setRequired = true;
return validator;
}
export function createMinLengthFormValidator(minLength, message) {
message = message || 'Min length ' + minLength;
let validator = createFormValidator(message, function(value) {
return ! (value == null || value == undefined || value.trim().length < minLength);
});
validator.setRequired = true;
return validator;
}
export function createMaxLengthFormValidator(maxLength, message) {
message = message || 'Max length ' + maxLength;
let validator = createFormValidator(message, function(value) {
return (value == null || value == undefined || value.trim().length < maxLength);
});
return validator;
}
export function createEqLengthFormValidator(eqLength, message) {
message = message || 'Required length ' + eqLength;
let validator = createFormValidator(message, function(value) {
return (value == null || value == undefined || value.trim().length == eqLength);
});
return validator;
}
export function createRegexFormValidator(regex, message) {
Eif (typeof regex === 'string') {
regex = new RegExp(regex, "i");
}
message = message || 'Invalid pattern (' + regex + ')';
let validator = createFormValidator(message, function(value) {
if (value == undefined || value == null) {
return true;
}
return regex.test(value);
});
return validator;
}
export class BootstrapWrapper extends React.Component {
constructor(props) {
super();
this.props = props;
}
componentWillReceiveProps(nextProps) {
this.props = nextProps;
}
render() {
const type = this.props.type;
if (type == 'checkbox') {
return (<div className={` ${this.props.outerClassName} ${this.props.error != null ? 'has-error' : ''} `}>
<div className="checkbox"><label>{this.props.children} {this.props.label}</label></div>
</div>);
} else {
return (
<div className={` ${this.props.outerClassName} `}>
<div className={`form-group ${this.props.className} ${this.props.error != null ? 'has-error' : ''}`}>
<label className="form-control-label" htmlFor={this.props.inputId}>{this.props.label}</label>
{(() => {
return this.props.required ? (<span className="form-wizard-required"></span>) : null;
})()}
{this.props.children}
{(() => {
return this.props.error != null ? (<span className="help-block">{this.props.error}</span>) : null;
})()}
</div>
</div>
);
}
}
}
export class Input extends React.Component {
static contextTypes = {
wizard : React.PropTypes.object
};
static propTypes = {
type : React.PropTypes.string.isRequired, // input type
name : React.PropTypes.string.isRequired, // field name
label : React.PropTypes.string.isRequired, // label
required : React.PropTypes.bool, // if field is required
instantValidation : React.PropTypes.bool, // if validate onChange event
inputId : React.PropTypes.string, // id for an input otherwise autogenerated
placeholder : React.PropTypes.string, // input placeholder
validators : React.PropTypes.arrayOf(React.PropTypes.object), // validators
className : React.PropTypes.string,
defaultValue : React.PropTypes.string,
wrapper : React.PropTypes.func // wrapper component
};
static defaultProps = {
required : false,
validators : [],
className: "",
outerClassName: ""
};
constructor(props) {
super();
this.wizardIndex = -1;
this.props = {};
this.state = this._configureWithProps(props);
this.props = props;
this.state.error = null;
this.inputId = props.inputId || 'form-input-' + props.name;
this._handleChange = this._handleChange.bind(this);
this.validate = this.validate.bind(this);
this.value = this.value.bind(this);
}
componentWillReceiveProps(newProps) {
const stateUpdate = this._configureWithProps(newProps);
this.props = newProps;
this.setState(stateUpdate);
}
_configureWithProps(props) {
let required = props.required;
const validators = [];
const conditionalValidator = createFormValidator('Field is required', (val) => props.required == false || isNotBlank(val));
validators.push(conditionalValidator);
props.validators.forEach((validator) => {
if (validator.setRequired) {
required = true;
}
validators.push(validator);
});
this.validators = validators;
this.orgOnChange = props.onChange;
const st = {};
Eif (this.props.label != props.label) {
st.label = props.label;
}
Eif (this.props.required != props.required) {
st.required = props.required;
}
return st;
}
componentWillMount() {
// register field in form
this.wizardIndex = this.context.wizard.formInputs.push(this) -1;
Iif (isNotBlank(this.props.defaultValue)) {
this.value(this.props.defaultValue);
}
//prepare props for input
this.inputProps = Object.assign({}, this.props, {onChange:this._handleChange, id:this.inputId, value:this.context.wizard.formData[this.props.name]});
delete this.inputProps.formData; // clear it
delete this.inputProps.validators; // clear it
delete this.inputProps.required; // clear it
delete this.inputProps.options; // clear it
delete this.inputProps.instantValidation; // clear it
delete this.inputProps.defaultValue; // clear it
delete this.inputProps.inputId;
delete this.inputProps.outerClassName;
delete this.inputProps.wrapper;
if (this.props.type == 'checkbox') {
this.inputProps.className = (this.props.className ? ' ' + this.props.className : '') + ' form-wizard-input form-wizard-input-' + this.props.type;
} else {
this.inputProps.className = 'form-control' + (this.props.className ? ' ' + this.props.className : '') + ' form-wizard-input form-wizard-input-' + this.props.type;
}
}
componentWillUnmount() {
if (this.wizardIndex >= 0) {
delete this.context.wizard.formInputs[this.wizardIndex];
this.wizardIndex = -1;
}
}
validate() {
let value = getObjProperty(this.context.wizard.formData, this.props.name);
for(let i=0; i<this.validators.length; i++) {
let validator = this.validators[i];
let isValid = validator.validatorFunction(value);
if (! isValid) {
this.setState({error : validator.message});
return false;
}
}
this.setState({error : null});
return true;
}
value(newValue) {
const name = this.props.name;
const data = this.context.wizard.formData;
const old = getObjProperty(data, name);
if (newValue != undefined) { // set new value
let nv = newValue == '' ? null : newValue;
Iif (this.props.type == 'number' && nv != null) {
nv = parseInt(nv);
}
Eif (nv != old) {
setObjProperty(data, name, nv);
let instantValidation = this.props.instantValidation;
Eif (instantValidation == undefined) {
instantValidation = this.context.wizard.props.instantValidation;
}
Iif (instantValidation) {
this.validate();
}
this.forceUpdate();
}
}
return old;
}
_handleChange(event) {
let val = event.target.value;
Iif (event.target.type == 'checkbox') {
val = event.target.checked;
if (val == undefined || val == null) {
val = false;
}
}
this.value(val == undefined ? null : val);
Iif (this.orgOnChange) {
this.orgOnChange(event);
}
}
render() {
let type = this.props.type == undefined ? undefined : this.props.type.toLowerCase();
let input;
let dv = this.value();
if (dv == null) {
dv = '';
}
if (type == 'checkbox') {
const chval = this.value();
input = (<input key={this.inputId} type="checkbox" {...this.inputProps} value={chval==null?undefined:chval} checked={chval==undefined?false:chval} />);
} else if (type == 'textarea') {
input = (<textarea key={this.inputId} {...this.inputProps} value={dv}/>);
} else if (type == 'select') {
let opts = [];
opts.push((<option key="" value=""></option>));
let isa = Array.isArray(this.props.options);
for (let key in this.props.options) {
let val = this.props.options[key];
let rk = isa ? val : key;
opts.push((<option label={val} value={rk} key={rk}>{val}</option>));
}
input = (<select key={this.inputId} {...this.inputProps} value={dv}>{opts}</select>);
} else {
input = (<input key={this.inputId} type={type} {...this.inputProps} value={dv}/>);
}
let wrapper = this.props.wrapper;
Eif (wrapper == undefined) {
wrapper = this.context.wizard.props.wrapper;
}
Eif (wrapper == undefined || wrapper == null) {
return input;
} else {
const wProps = {
type : type,
outerClassName : this.props.outerClassName,
label : this.state.label,
required : this.state.required,
error : this.state.error,
inputId : this.inputId,
inputProps : this.props,
key : this.inputId + "-wrapper"
};
return React.createElement(wrapper, wProps, [input]);
}
}
}
export class Form extends React.Component {
static childContextTypes = {
wizard : React.PropTypes.object
};
static propTypes = {
formData : React.PropTypes.object, // where to save or get values
instantValidation : React.PropTypes.bool, // parameter to pass to Input(s)
processHTMLInputs : React.PropTypes.bool, // process html's inputs and selects
onValidationError : React.PropTypes.func, // callback func(event, form)
onSubmit : React.PropTypes.func, // callback func(event, form)
wrapper : React.PropTypes.func // default wrapper component
};
static defaultProps = {
instantValidation:false,
formData : {},
processHTMLInputs:true
};
constructor(props) {
super();
this.mounted = false;
this.formInputs = [];
this.htmlForm = null;
this.state = { };
this.componentWillReceiveProps(props);
this._handleOnSubmit = this._handleOnSubmit.bind(this);
this.data = this.data.bind(this);
}
componentWillReceiveProps(nextProps) {
this.props = nextProps;
this.formData = nextProps.formData;
this.formProps = Object.assign({}, nextProps);
delete this.formProps.formData; // clear it
delete this.formProps.processHTMLInputs; // clear it
delete this.formProps.instantValidation; // clear it
delete this.formProps.onValidationError; // clear it
delete this.formProps.onSubmit; // clear it
delete this.formProps.wrapper; // clear it
Iif (this.mounted) {
this.forceUpdate();
}
}
data() {
return this.formData;
}
getChildContext() {
const self = this;
return {
wizard: self
};
}
_handleOnSubmit(event) {
event.preventDefault();
devOnly(() => { console.debug("Submit (Form)", event, this.formData); });
// validation
let ret = true;
this.formInputs.forEach((fin) => {
if (! fin.validate()) {
ret = false;
devOnly(() => { console.debug('Invalid field', fin); });
}
});
if (this.props.processHTMLInputs && this.htmlForm) {
const out = this.item;
var elements = this.htmlForm.getElementsByTagName('input');
for (var i = 0; i < elements.length; ++i) {
var e = elements[i];
var name = e.getAttribute('name');
var value = e.getAttribute('value');
if (name && name.length > 0 && value && value.length > 0) {
setObjProperty(out, name, value);
}
}
elements = this.htmlForm.getElementsByTagName('select');
for (var i = 0; i < elements.length; ++i) {
var e = elements[i];
var name = e.getAttribute('name');
var value = e.options[e.selectedIndex].value;
if (name && name.length > 0 && value && value.length > 0) {
setObjProperty(out, name, value);
}
}
}
if (ret) {
if (this.props.onSubmit) {
this.props.onSubmit(event, this);
}
} else {
if (this.props.onValidationError) {
this.props.onValidationError(event, this);
}
}
}
componentDidMount() {
this.componentWillReceiveProps(this.props);
this.mounted = true;
}
componentWillUnmount() {
this.mounted = false;
}
render() {
return (<div>
<form {...this.formProps} onSubmit={this._handleOnSubmit}>
{this.props.children}
</form>
</div>);
}
}
|