创建用于react-day-picker的自定义组件输入

时间:2018-09-11 12:13:18

标签: javascript reactjs material-ui react-day-picker

我正在为DayPickerInput创建一个自定义的材料UI组件,但是问题是当 onDayChange 被触发时,它会给出一个错误。

handleToDateChange = (selectedDay, modifiers, dayPickerInput) => {
    const val = dayPickerInput.getInput().value;
}

Uncaught TypeError: Cannot read property 'value' of null

DayPickerInput组件

 <DayPickerInput
      component={props => <DatePickerCustomInput {...props} />}
      value={toDate}
      placeholder=""
      formatDate={this.setDatePickerformatDate}
      onDayChange={this.handleToDateChange}
  />

DayPickerInput自定义组件

class DatePickerCustomInput extends Component {
   render() {
       return (
           <TextField {...this.props}
               InputProps={{
                   startAdornment: (
                       <InputAdornment position="start">
                           <DateRange />
                       </InputAdornment>
                   ),
               }}

               value={this.props.value}
           />
       );
   }
}

重现该问题:https://codesandbox.io/s/387r9plx65

2 个答案:

答案 0 :(得分:0)

尝试执行此操作!

handleToDateChange = (selectedDay, modifiers, dayPickerInput) => {
    const val = dayPickerInput.state.value;
}

答案 1 :(得分:0)

From the docs hereonDayChange方法的签名为

onDayChange (day: date, modifiers: Object, e: SyntheticEvent) ⇒ void

day是一个日期对象,它引用选择器中的选定日期。您无需从事件中提取值即可获取所选日期。