javascript - 在 onChange (ReactJS) 中获取 2 个组合日期选择器的值

标签 javascript reactjs datepicker momentjs material-ui

我正在使用materialui的DatePicker组件,我需要在一个函数中获取两个DatePicker组件的值来创建一个范围。

我创建了一个handleChange函数,它已经允许检索DatePicker组件的值,但对于2个组件我不能这样做。

我想知道我需要采取的步骤。

这是我的代码:

import React from 'react';
import moment from 'moment';
import 'moment/locale/fr'
import DatePicker from 'material-ui/DatePicker';
import areIntlLocalesSupported from 'intl-locales-supported';


let DateTimeFormat;

if (areIntlLocalesSupported(['fr'])) {
  DateTimeFormat = global.Intl.DateTimeFormat;
} else {
    const IntlPolyfill = require('intl');
    DateTimeFormat = IntlPolyfill.DateTimeFormat;
    require('intl/locale-data/jsonp/fr');
    require('intl/locale-data/jsonp/fa-IR');
}


class SearchByDate extends React.Component {

  handleSearchByDate = (evt, date) => {
    console.log(date)
  }

  render() {
    return (
      <div>
        <DatePicker
            ref="dateChoiceOne"
            name="dateChoiceOne"
            hintText="Date début"
            DateTimeFormat={DateTimeFormat}
            okLabel="OK"
            cancelLabel="Annuler"
            locale="fr"
            onChange={this.handleSearchByDate}/>

        <DatePicker
            ref="dateChoiceTwo"
            name="dateChoiceTwo"
            hintText="Date fin"
            DateTimeFormat={DateTimeFormat}
            okLabel="OK"
            cancelLabel="Annuler"
            locale="fr"
            onChange={this.handleSearchByDate} />
      </div> 
    )
  }
}

export default SearchByDate;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

最佳答案

由于第一个 DatePicker 和第二个 DatePicker 的日期选择发生在不同的时间,因此您无法将这 2 个日期作为参数获取到函数中。

相反,您应该创建 2 个不同的函数,每个函数对应一个 DatePicker。这些函数会将日期保存为 startDateendDate,您可以在 componentDidUpdate 中检查状态更新;一旦您设置了两个日期,您就可以做任何您想做的事情。

class SearchByDate extends React.Component {
  componentDidUpdate = () => {
    if (this.state.startDate && this.state.endDate) {
      // Both dates are set, do something with it
    }
  }

  handleChangeStartDate = (evt, date) => {
    this.setState({
      startDate: date,
    });
  }

  handleChangeEndDate = (evt, date) => {
    this.setState({
      endDate: date,
    });
  }

  render() {
    return (
      <div>
        <DatePicker
          ref="dateChoiceOne"
          name="dateChoiceOne"
          hintText="Date début"
          DateTimeFormat={DateTimeFormat}
          okLabel="OK"
          cancelLabel="Annuler"
          locale="fr"
          onChange={this.handleChangeStartDate}
        />

        <DatePicker
          ref="dateChoiceTwo"
          name="dateChoiceTwo"
          hintText="Date fin"
          DateTimeFormat={DateTimeFormat}
          okLabel="OK"
          cancelLabel="Annuler"
          locale="fr"
          onChange={this.handleChangeEndDate}
        />
      </div> 
    )
  }
}

关于javascript - 在 onChange (ReactJS) 中获取 2 个组合日期选择器的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46950659/

相关文章:

delphi - TDateTimePicker MaxDate - 不允许将控件设置为当前日期

javascript - 对移动日期选择器的任何建议

javascript - 确定请求是通过浏览器书签/收藏夹还是链接?

javascript - 在运行时将字符串附加到 DOM 并在 X 秒后将其删除

javascript - react hook useState不能存储json或string

reactjs - 使用 Workbox PWA react PWA

javascript - 使用动态路由将单个 JSON 文件中的数据从 React 中的父组件传递到子组件

javascript - 单击时不显示 Bootstrap 日期选择器

javascript - 如何更改knockout js网格中的列名称

基于Javascript的免费手绘绘图库?