-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
68 lines (61 loc) · 1.72 KB
/
index.jsx
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
import React, { Component } from 'react';
import { FormState, Form } from 'react-formstate';
FormState.rfsProps.updateFormState.suppress = true;
import { validationAdapter } from 'react-formstate-validation';
validationAdapter.plugInto(FormState);
const Input = ({label, value, help, onChange, onBlur}) => {
return (
<div>
<div>{label}</div>
<input type='text' value={value} onChange={onChange} onBlur={onBlur}/>
<div>{help}</div>
</div>
);
};
class RfsInput extends Component {
constructor(props) {
super(props);
}
render() {
const {grabRef, fieldState, handleValueChange, showValidationMessage, ...other} = this.props;
if (grabRef) { grabRef(this); }
return (
<Input
value={fieldState.getValue()}
help={fieldState.getMessage()}
onChange={e => handleValueChange(e.target.value)}
onBlur={showValidationMessage}
{...other}
/>
);
}
};
export default class TestForm extends Component {
constructor(props) {
super(props);
this.formState = new FormState(this);
this.state = this.formState.createUnitOfWork().injectModel(props.model);
props.grabFormRef(this);
}
render() {
return (
<Form formState={this.formState} onSubmit={e => this.handleSubmit(e)}>
<RfsInput
formField='test'
label='Test'
required
fsv={v => v.minLength(8)}
grabRef={this.props.grabInputRef}
/>
<input type='submit' value='Submit' disabled={this.formState.isInvalid()}/>
</Form>
);
}
handleSubmit(e) {
e.preventDefault();
const model = this.formState.createUnitOfWork().createModel();
if (model) {
alert(JSON.stringify(model));
}
}
}