/
formsy-material-ui.jsx
154 lines (128 loc) · 3.52 KB
/
formsy-material-ui.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
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
'use strict';
const React = require('react');
const Formsy = require('formsy-react');
const Checkbox = require('material-ui/lib/checkbox');
const DatePicker = require('material-ui/lib/date-picker/date-picker');
const RadioButtonGroup = require('material-ui/lib/radio-button-group');
const SelectField = require('material-ui/lib/select-field');
const TextField = require('material-ui/lib/text-field');
const TimePicker = require('material-ui/lib/time-picker/time-picker');
const Toggle = require('material-ui/lib/toggle');
let FormComponentMixin = {
propTypes: {
name: React.PropTypes.string.isRequired,
validations: React.PropTypes.string,
validationError: React.PropTypes.string,
hintText: React.PropTypes.string,
floatingLabelText: React.PropTypes.string
},
handleChange: function (event) {
this.setValue(event.target.value);
},
handleValueChange: function (event, value) {
this.setValue(value);
}
};
let FormsyCheckbox = React.createClass({
mixins: [ Formsy.Mixin, FormComponentMixin ],
componentDidMount: function () {
this.setValue(this._checkbox.isChecked());
},
render: function () {
return (
<Checkbox
{...this.props}
ref={(c) => this._checkbox = c}
onCheck={this.handleValueChange} />
);
}
});
let FormsyDate = React.createClass({
mixins: [ Formsy.Mixin, FormComponentMixin ],
render: function () {
return (
<DatePicker
formatDate={(date) => date.toISOString().substring(0,10)}
{...this.props}
onChange={this.handleValueChange} />
);
}
});
let FormsyRadio = React.createClass({
mixins: [ Formsy.Mixin ],
// Material-UI replaces any component inside RadioButtonGroup with RadioButton, so no need to render it here
render: function () {}
});
let FormsyRadioGroup = React.createClass({
mixins: [ Formsy.Mixin, FormComponentMixin ],
componentDidMount: function () {
this.setValue(this._radio.getSelectedValue());
},
render: function () {
return (
<RadioButtonGroup
{...this.props}
ref={(c) => this._radio = c}
onChange={this.handleValueChange} >
{this.props.children}
</RadioButtonGroup>
);
}
});
let FormsySelect = React.createClass({
mixins: [ Formsy.Mixin, FormComponentMixin],
render: function () {
return (
<SelectField
{...this.props}
onChange={this.handleChange}
value={this.getValue()} />
);
}
});
let FormsyText = React.createClass({
mixins: [ Formsy.Mixin, FormComponentMixin ],
render: function () {
return (
<TextField
{...this.props}
onBlur={this.handleChange}
errorText={this.getErrorMessage()}
value={this.getValue()} />
);
}
});
let FormsyTime = React.createClass({
mixins: [ Formsy.Mixin, FormComponentMixin ],
render: function () {
return (
<TimePicker
{...this.props}
onChange={this.handleValueChange} />
);
}
});
let FormsyToggle = React.createClass({
mixins: [ Formsy.Mixin, FormComponentMixin ],
componentDidMount: function () {
this.setValue(this._toggle.isToggled());
},
render: function () {
return (
<Toggle
{...this.props}
ref={(c) => this._toggle = c}
onToggle={this.handleValueChange} />
);
}
});
module.exports = {
FormsyCheckbox: FormsyCheckbox,
FormsyDate: FormsyDate,
FormsyRadio: FormsyRadio,
FormsyRadioGroup: FormsyRadioGroup,
FormsySelect: FormsySelect,
FormsyText: FormsyText,
FormsyTime: FormsyTime,
FormsyToggle: FormsyToggle
};