Skip to content

Instantly share code, notes, and snippets.

@ilovett
Last active July 27, 2018 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilovett/60dbbe44a373e582c94e23e6dc53b388 to your computer and use it in GitHub Desktop.
Save ilovett/60dbbe44a373e582c94e23e6dc53b388 to your computer and use it in GitHub Desktop.
DatePicker wrapped with ValidatorComponent for use with `material-ui-pickers` and `react-material-ui-form-validator` with `material-ui@v1`
import React from 'react';
import DatePicker from 'material-ui-pickers/DatePicker';
import { ValidatorComponent } from 'react-material-ui-form-validator';
export default class DatePickerValidator extends ValidatorComponent {
setInputRef = (ref) => {
this.input = ref;
}
render() {
const { isValid } = this.state;
const { errorMessages, validators, validatorListener, ...other } = this.props;
let { InputLabelProps, FormHelperTextProps, helperText } = this.props;
const errorMessage = this.getErrorMessage();
if ( ! isValid && errorMessage ) {
helperText = errorMessage;
FormHelperTextProps = { ...(FormHelperTextProps || {}), error: true };
InputLabelProps = { ...(InputLabelProps || {}), error: true };
}
return (
<DatePicker
{...other}
InputLabelProps={ InputLabelProps }
FormHelperTextProps={ FormHelperTextProps }
helperText={ helperText }
InputProps={{
inputRef: this.setInputRef
}}
/>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment