formsy-material-ui
Version:
A formsy-react compatibility wrapper for Material-UI form components.
73 lines (60 loc) • 1.95 kB
JSX
import React from 'react';
import createClass from 'create-react-class';
import PropTypes from 'prop-types';
import Formsy from 'formsy-react';
import SelectField from 'material-ui/SelectField';
import { setMuiComponentAndMaybeFocus } from './utils';
const FormsySelect = createClass({
propTypes: {
children: PropTypes.node,
name: PropTypes.string.isRequired,
onChange: PropTypes.func,
requiredError: PropTypes.string,
validationError: PropTypes.string,
validationErrors: PropTypes.object,
validations: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
value: PropTypes.any,
},
mixins: [Formsy.Mixin],
getInitialState() {
return {
hasChanged: false,
};
},
handleChange(event, index, value) {
this.setValue(value);
this.setState({
hasChanged: value !== '',
});
if (this.props.onChange) this.props.onChange(event, value, index);
},
setMuiComponentAndMaybeFocus: setMuiComponentAndMaybeFocus,
render() {
const {
requiredError,
validations, // eslint-disable-line no-unused-vars
validationError, // eslint-disable-line no-unused-vars
validationErrors, // eslint-disable-line no-unused-vars
value: valueProp,
onChange,
...rest
} = this.props;
const { isRequired, isPristine, isValid, isFormSubmitted } = this;
const isRequiredError = isRequired() && !isPristine() && !isValid() && isFormSubmitted() && requiredError;
const errorText = this.getErrorMessage() || isRequiredError;
const value = this.state.hasChanged ? this.getValue() : valueProp;
return (
<SelectField
disabled={this.isFormDisabled()}
errorText={errorText}
onChange={this.handleChange}
ref={this.setMuiComponentAndMaybeFocus}
value={value}
{...rest}
>
{this.props.children}
</SelectField>
);
},
});
export default FormsySelect;