@6thquake/react-material
Version:
React components that implement Google's Material Design.
81 lines (70 loc) • 2.6 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
/**
* @ignore - do not document.
*/
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { withFormsy, propTypes } from 'formsy-react';
import { compose } from 'recompose';
import TextFieldStandalone from '@material-ui/core/TextField';
import withFormItem from '../Form/withFormItem';
import withForm from '../Form/withForm';
import withStyles from '../styles/withStyles';
const style = theme => ({});
class TextField extends Component {
constructor(...args) {
super(...args);
this.onChange = event => {
// setValue() will set the value of the component, which in
// turn will validate it and the rest of the form
// Important: Don't skip this step. This pattern is required
// for Formsy to work.
const value = event.target.value;
this.props.setValue(value);
const {
onChange
} = this.props;
onChange && onChange(event, value);
};
}
renderFormComponent() {
const _this$props = this.props,
{
getErrorMessage,
getValue,
isFormDisabled,
isValid,
isPristine
} = _this$props,
rest = _objectWithoutPropertiesLoose(_this$props, ["getErrorMessage", "getErrorMessages", "getValue", "hasValue", "isFormDisabled", "isValid", "isPristine", "isFormSubmitted", "isRequired", "isValidValue", "resetValue", "setValidations", "setValue", "showRequired", "showError", "validationError", "validationErrors", "validations", "innerRef", "value", "onChange"]);
let error = false;
let helperText = ' ';
const isDisabled = isFormDisabled();
if (!isDisabled) {
if (!isPristine()) {
helperText = getErrorMessage() || ' ';
error = !isValid();
}
}
return React.createElement(React.Fragment, null, React.createElement(TextFieldStandalone, _extends({
error: error,
value: getValue(),
helperText: helperText,
disabled: isDisabled,
onChange: this.onChange
}, rest)));
}
render() {
return this.renderFormComponent();
}
}
TextField.displayName = 'TextField';
process.env.NODE_ENV !== "production" ? TextField.propTypes = _extends({
classes: PropTypes.object.isRequired
}, propTypes) : void 0;
TextField.defaultProps = {};
const FormComponent = compose(withFormsy, withFormItem, withStyles(style, {
name: 'RMTextField'
}))(TextField);
export default compose(withForm)(FormComponent, TextFieldStandalone);