简体   繁体   English

如何在React中为日期范围选择选择不同的输入

[英]How to select different inputs for a date range selection in react

I'm using a simple create-react-app and react-calendar-pane ( https://github.com/brillout/awesome-react-components ) to develop a simple date range selector for a calendar from scratch. 我正在使用一个简单的create-react-app和react-calendar-pane( https://github.com/brillout/awesome-react-components )从头开始为日历开发一个简单的日期范围选择器。

The calendar is being called as a component using: 日历通过以下方式被称为组件:

<Calendar date={moment("23/10/2015", "DD/MM/YYYY")} onSelect={this.onSelect} />

I have two inputs above the calendar; 在日历上方有两个输入; a 'from' and 'to' date inputs: “开始”和“结束”日期输入:

<label> From <input type="text" onClick={this.onClickFromDate} value={this.state.fromDate.format('DD/MM/YYYY')} /> </label>
<label> To <input type="text" onClick={this.onClickToDate} value={this.state.toDate.format('DD/MM/YYYY')} /> </label>

I'm using onClickFromDate and onClickToDate to invoke a state of activeness of the clicked input. 我正在使用onClickFromDateonClickToDate来调用单击输入的活动状态。 And onSelect to display in the console that a selection from the calendar has been made. 然后onSelect在控制台中显示已从日历中进行选择。

The issue I'm facing is that I can't figure out how to separate the two inputs so that when the 'from' input is clicked on, the user can select a date from the calendar and that date will appear in the 'from', with the same happening with the 'to' input. 我面临的问题是我无法弄清楚如何分隔两个输入,因此当单击“ from”输入时,用户可以从日历中选择一个日期,而该日期将出现在“ from”中”,“ to”输入也是如此。

As my code is now, when a date is selected from the calendar, both input display the selected date from the calendar, with the clicking on the inputs not doing anything. 就像我现在的代码一样,当从日历中选择一个日期时,两个输入都会显示从日历中选择的日期,而单击输入则不会执行任何操作。 I understand that is invoked using onSelect={this.onSelect} but I don't know how to differentiate between the two inputs to create a simple date range picker for this calendar. 我知道这是使用onSelect={this.onSelect}调用的,但是我不知道如何区分两个输入来为此日历创建一个简单的日期范围选择器。

Full code (App.js): 完整代码(App.js):

class App extends React.Component {
  constructor(){
    super();
    this.state={ fromDate:moment(), toDate:moment() };
  }

  onSelect=(e)=>{
    console.log('this is a date being selected');
  }

  onClickFromDate=(e)=>{
    console.log('this is a from date');
  }

  onClickToDate=(e)=>{
    console.log('this is a to date');
  }

  render() {
    return(
      <div>
        <div className="App">
          <header className="App-header">
            <h1 className="App-title">Welcome to React</h1>
          </header>
          <label> From <input type="text" onClick={this.onClickFromDate} value={this.state.fromDate.format('DD/MM/YYYY')} /> </label>
          <label> To <input type="text" onClick={this.onClickToDate} value={this.state.toDate.format('DD/MM/YYYY')} /> </label>
          <Calendar date={moment("23/10/2015", "DD/MM/YYYY")} onSelect={this.onSelect} />
        </div>
      </div>
    )
  }
}

On clicking of select, set clicktype value. 单击选择时,设置clicktype值。 Use this value to set the corresponding date in the onselect method 使用此值在onselect方法中设置相应的日期

  class App extends React.Component {
  constructor(){
    super();
    this.state={ 
     fromDate:moment(), 
     toDate:moment()  
     clicktype:null //1 for fromdate, 2 for todate  
     };
  }

  onSelect=(e)=>{

    console.log('this is a date being selected');
   if(this.state.clicktype==1){ //set from date  }
    else{ //set to date  }
  }

  onClickFromDate=(e)=>{
    console.log('this is a from date');
    this.setState({ clicktype:1})
  }

  onClickToDate=(e)=>{
    console.log('this is a to date');
    this.setState({ clicktype:2})
  }

  render() {
    return(
      <div>
        <div className="App">
          <header className="App-header">
            <h1 className="App-title">Welcome to React</h1>
          </header>
          <label> From <input type="text" onClick={this.onClickFromDate} value={this.state.fromDate.format('DD/MM/YYYY')} /> </label>
          <label> To <input type="text" onClick={this.onClickToDate} value={this.state.toDate.format('DD/MM/YYYY')} /> </label>
          <Calendar date={moment("23/10/2015", "DD/MM/YYYY")} onSelect={this.onSelect} />
        </div>
      </div>
    )
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM