简体   繁体   English

AirBnB React-dates 未显示所选范围

[英]AirBnB React-dates not showing selected range

I'm trying to add AirBnB React-dates .我正在尝试添加AirBnB React-dates All of functionality working except one thing (Not opening the month of already selected dates).除了一件事之外,所有功能都在工作(不打开已选择日期的月份)。

When I'm assigning the start and end dates then it selects the range but not showing the selected month.当我分配开始日期和结束日期时,它会选择范围但不显示所选月份。 It showing the current month instead.它显示当前月份。

For example: If I set start date = 2017-05-05 and end date =2017-05-09 then it shows the selection, but in next time calender picker open it only shows current month, so I have to click the next month, next month to see the previous selected dates;例如:如果我设置开始日期 = 2017-05-05 和结束日期 =2017-05-09 那么它会显示选择,但在下一次日历选择器打开时它只显示当月,所以我必须点击下个月, 下个月查看之前选择的日期;

My codes:我的代码:

import React from 'react';
import ReactDOM from 'react-dom';
import moment from 'moment';

import { DateRangePicker } from 'react-dates';


var SelectedStartDate = moment('2017-05-05');
var SelectedEndDate = moment('2017-05-09');


class HomePageDatePicker extends React.Component {
constructor(props) {
    super(props);
    this.state = {
        focusedInput: null,
        startDate: SelectedStartDate,
        endDate:SelectedEndDate
    };
    this.onDatesChange = this.onDatesChange.bind(this);
    this.onFocusChange = this.onFocusChange.bind(this);
}

onDatesChange({ startDate, endDate }) {

    this.setState({ startDate, endDate });
}

onFocusChange(focusedInput) {
    this.setState({ focusedInput });
}

render() {
    const { focusedInput, startDate, endDate } = this.state;
    return (
        <div>
            <DateRangePicker
                {...this.props}
                onDatesChange={this.onDatesChange}
                onFocusChange={this.onFocusChange}
                focusedInput={focusedInput}
                startDate={startDate}
                endDate={endDate}                                     
                startDateId="datepicker_start_home"
                endDateId="datepicker_end_home"                    
                startDatePlaceholderText="Check In"
                endDatePlaceholderText="Check Out"
            />
        </div>
    );
    }
}

When open the picker,it showing the February month instead of may month.打开选择器时,它显示的是二月而不是五月。

You need to add a selected state still like this:您需要像这样添加一个选定的状态:

import React from 'react';
import ReactDOM from 'react-dom';
import moment from 'moment';

import { DateRangePicker } from 'react-dates';


var SelectedStartDate = moment('2017-05-05');
var SelectedEndDate = moment('2017-05-09');


class HomePageDatePicker extends React.Component {
constructor(props) {
    super(props);
    this.state = {
        focusedInput: null,
        startDate: SelectedStartDate,
        endDate:SelectedEndDate
    };
    this.onDatesChange = this.onDatesChange.bind(this);
    this.onFocusChange = this.onFocusChange.bind(this);
}

onDatesChange({ startDate, endDate }) {

    this.setState({ startDate, endDate });
}

onFocusChange(focusedInput) {
    this.setState({ focusedInput });
}

render() {
    const { focusedInput, startDate, endDate } = this.state;
    return (
        <div>
            <DateRangePicker
                {...this.props}
                onDatesChange={this.onDatesChange}
                onFocusChange={this.onFocusChange}
                focusedInput={focusedInput}
                //Here is the change:
                date={startDate}
                startDate={startDate}
                endDate={endDate}                                     
                startDateId="datepicker_start_home"
                endDateId="datepicker_end_home"                    
                startDatePlaceholderText="Check In"
                endDatePlaceholderText="Check Out"
            />
        </div>
    );
    }
}

试试这个 DateRangePicker 道具

initialVisibleMonth={() => startDate}

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

相关问题 Airbnb 反应日期范围选择器仅显示当前月份日历未选择日期月份日历 - Airbnb react date range picker only showing current month calendar not selected dates month calender Airbnb用非英语语言环境反应日期日历 - Airbnb react-dates calendar with non-English locale 是否可以持续显示 Airbnb 反应日期日历? - Is it possible to persistently display Airbnb react-dates calendar? airbnb / react-dates-无法编辑/在日期字段中键入 - airbnb/react-dates - unable to edit / type in date fields airbnb/react-dates DayPickerRangeController 在某些 function 调用上设置可见月份 - airbnb/react-dates DayPickerRangeController set visible month on some function call Airbnb / 反应日期 | 有没有办法无限滚动几个月而不是点击下个月? - Airbnb / react-dates | Is there a way to infinity scroll months instead of clicking next month? 如何将 airbnb react-dates 添加到我的 HTML 页面? - How do I add airbnb react-dates to my HTML page? 如何在具有时刻范围的反应日期中禁用多个日期 - How to disable multiple date in react-dates with moment range 使用带有 Typescript 的反应日期 - Using react-dates with Typescript react-dates DayPickerRangeController,如果用户在已选择时单击它,是否有办法清除 startDate 或 endDate? - react-dates DayPickerRangeController, is there a way to clean startDate or endDate if user clicks it when already selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM