forked from react-toolbox/react-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathForm.js
More file actions
86 lines (75 loc) · 2.48 KB
/
Form.js
File metadata and controls
86 lines (75 loc) · 2.48 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
import React, { Component, PropTypes } from 'react';
import InjectAutocomplete from '../autocomplete/Autocomplete.js';
import InjectButton from '../button/Button.js';
import InjectCheckbox from '../checkbox/Checkbox.js';
import InjectDatePicker from '../date_picker/DatePicker.js';
import InjectDropdown from '../dropdown/Dropdown.js';
import InjectInput from '../input/Input.js';
import InjectRadioGroup from '../radio/RadioGroup.js';
import InjectSlider from '../slider/Slider.js';
import InjectSwitch from '../switch/Switch.js';
import InjectTimePicker from '../time_picker/TimePicker.js';
const factory = (
Autocomplete, Button, Checkbox, DatePicker, Dropdown,
Input, RadioGroup, Slider, Switch, TimePicker
) => {
const COMPONENTS = {
'autocomplete': Autocomplete,
'button': Button,
'checkbox': Checkbox,
'datepicker': DatePicker,
'dropdown': Dropdown,
'input': Input,
'radioGroup': RadioGroup,
'slider': Slider,
'switch': Switch,
'timepicker': TimePicker
};
class Form extends Component {
static propTypes = {
attributes: PropTypes.array,
children: PropTypes.node,
className: PropTypes.string,
model: PropTypes.object,
onChange: PropTypes.func,
onError: PropTypes.func,
onSubmit: PropTypes.func,
onValid: PropTypes.func,
storage: PropTypes.string
};
static defaultProps = {
attributes: [],
className: ''
};
onSubmit = (event) => {
event.preventDefault();
if (this.props.onSubmit) this.props.onSubmit(event);
};
onChange = (field, value, event) => {
if (this.props.onChange) this.props.onChange(field, value, event);
};
renderFields () {
return Object.keys(this.props.model).map((field, index) => {
const properties = this.props.model[field];
const Field = COMPONENTS[properties.kind.toLowerCase()];
return <Field key={index} {...properties} onChange={this.onChange.bind(this, field)} />;
});
}
render () {
return (
<form data-react-toolbox='form' className={this.props.className} onSubmit={this.onSubmit}>
{this.renderFields()}
{this.props.children}
</form>
);
}
}
return Form;
};
const Form = factory(
InjectAutocomplete, InjectButton, InjectCheckbox, InjectDatePicker, InjectDropdown,
InjectInput, InjectRadioGroup, InjectSlider, InjectSwitch, InjectTimePicker
);
export default Form;
export { factory as formFactory };
export { Form };