问题描述
我正在使用materialui的DatePicker组件,并且需要在一个函数中获取两个DatePicker组件的值才能确定范围.
I'm using the DatePicker component of materialui and I need to get the value of two DatePicker components in one function to make a range.
我创建了一个handleChange函数,该函数已经允许检索DatePicker组件的值,但是对于2个组件,我无法做到这一点.
I created a handleChange function that already allows to retrieve the value of a DatePicker component but for 2 component I can not do it.
我想知道我需要采取的步骤.
I would like to know the steps I need to take.
这是我的代码:
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选择日期的时间不同,因此无法将两个日期作为函数的参数.
As the date selection from the 1st DatePicker and the 2nd DatePicker happens at different times, you can't get the 2 dates as arguments into the function.
相反,您应该创建2个不同的函数,每个DatePicker一个.这些函数会将日期另存为startDate
和endDate
,您可以在componentDidUpdate
中检查状态更新;设定完两个日期后,您就可以做任何想做的事.
Instead, you should create 2 differents functions, one for each DatePicker. Theses functions will save the dates as startDate
and endDate
, and you can check for state updates in componentDidUpdate
; once you have both dates set, you can do whatever you wanted to.
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>
)
}
}
这篇关于在onChange(ReactJS)中获取2个复合日期选择器的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!