Last active
July 27, 2018 02:18
-
-
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`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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