我正在使用 react-date-picker,我想将它创建为一个日期选择器组件,我可以在页面的每个部分使用它。onchange 方法是我遇到问题的地方,因为在状态更改时,它不知道它正在更改的列。我在 onchange 方法中遗漏了一些东西。当我选择一个日期时,错误是状态总是为空。这是我控制台中的结果。
21/12/2018
export class DatePickerNew extends React.PureComponent<IIDateTimeProps, any>
{
classes: string[];
constructor(props: IIDateTimeProps) {
super(props);
this.state = {
dirty: false,
valid: false,
validated: false,
maxLength: this.props.maxLength || 250,
minLength: this.props.minLength || 1,
minNumber: this.props.minNumber || 0,
maxNumber: this.props.maxNumber || 999999999999999,
type: "date",
validationMessage: "",
selected:""
}
}
onChange = date => {
const result = Validation.inputValidation(date, this.state);
//date = DateTime.parse(date, DateTime.dateOutPutFormat);
this.setState({
validated: true,
valid: result.state,
dirty: true,
selected: moment(date).format('DD/MM/YYYY').toString(),
value:this.state.selected
});
console.log(this.state.value)
console.log(this.state.selected)
};
render() {
this.classes = this.props.classes || [];
return (
<ErrorBoundary errorMessage="Error rendering DatePicker">
<DatePick
id={this.props.id}
name={this.props.name}
title={this.props.title}
required={this.props.required}
value={this.state.selected}
onChange={this.onChange}
peekNextMonth
showMonthDropdown
showYearDropdown
dropdownMode="select"
type="text"
placeholderText="DD/MM/YYYY"
/>
</ErrorBoundary>);
}
}
这就是我在另一个组件中调用它的方式
<DatePickerNew
id="holidayDate"
title="Holiday Date"
label="Holiday Date"
type="text"
required={true}
name="holidayDate"
isClearable={true}
/>