import React from 'react'; import style from './style'; import Autocomplete from '../autocomplete'; import Button from '../button'; import Checkbox from '../checkbox'; import DatePicker from '../date_picker'; import Dropdown from '../dropdown'; import Input from '../input'; import RadioGroup from '../radio/radio_group'; import Slider from '../slider'; import Switch from '../switch'; import TimePicker from '../time_picker'; class Form extends React.Component { static propTypes = { attributes: React.PropTypes.array, className: React.PropTypes.string, onChange: React.PropTypes.func, onError: React.PropTypes.func, onSubmit: React.PropTypes.func, onValid: React.PropTypes.func, storage: React.PropTypes.string }; static defaultProps = { attributes: [], className: '' }; state = { attributes: this.storage(this.props) }; componentWillReceiveProps (next_props) { if (next_props.attributes) { const attributes = this.storage(next_props); this.setState({attributes}); this.setValue(attributes.map((item) => { return item; })); } } onSubmit = (event) => { event.preventDefault(); if (this.props.onSubmit) { this.props.onSubmit(event, this); } }; onChange = (event) => { let is_valid = true; const value = this.getValue(); for (const attr of this.state.attributes) { if (attr.required && value[attr.ref] !== undefined && value[attr.ref].trim() === '') { is_valid = false; if (this.refs[attr.ref].setError) this.refs[attr.ref].setError('Requited field'); break; } } if (this.props.onChange) this.props.onChange(event, this); if (this.props.storage) this.storage(this.props, value); if (is_valid) { if (this.refs.submit) this.refs.submit.getDOMNode().removeAttribute('disabled'); if (this.props.onValid) this.props.onValid(event, this); } else { if (this.refs.submit) this.refs.submit.getDOMNode().setAttribute('disabled', true); if (this.props.onError) this.props.onError(event, this); } }; render () { const className = `${style.root} ${this.props.className}`; const attributes = this.state.attributes.map((attribute, index) => { if (attribute.type === 'autocomplete') { return ; } else if (attribute.type === 'submit') { return