繁体   English   中英

react-datepicker,禁用特定日期的问题

[英]react-datepicker, issues disabling a specific Date

我正在使用 react-datepicker 进行日期和日历实现,它工作正常我已经成功集成了该组件,但面临禁用默认日期的一个问题。 我正在使用下面的代码。

<DatePicker customInput={<ExampleCustomInput />} className="w-dtsr-date-picker"
  selected={this.state.startDate}  //called when the date is clicked
  onChange={this.handleChange}    //called only when the value is chnaged
  monthsShown={2}     //number of months to be shown
  minDate={this.props.dateProps.minDate}  //min days to be shown in the calendar
  maxDate={this.props.dateProps.maxDate}  //max days to be shown in the calendar
  onChangeRaw={this.handleDateChangeRaw}  //preventing the user from manually entering the dates or text in the input text box
/>  

默认情况下说我想禁用 4/25/2019 ,我不能这样做,我看到组件有一个名为dayClassName的属性,有没有人使用过,我不知道如何传递日期并将 css 应用到那个具体日期。

这是我在其中一个例子中发现的 -

dayClassName={date => getDate(date) < Math.random() * 31 ? 'random' : undefined}

它对我不起作用,不确定如何禁用此特定日期(2019 年 4 月 25 日),对此有何帮助。?

您应该能够使用dayClassName道具禁用特定日期并传递禁用用户点击的创建的 CSS 类。 dayClassName返回一个 JS 日期,因此您必须将其与要禁用的日期进行比较,然后传递禁用的 CSS 类名(如果为true

例子:

支柱:

dayClassName={date => date.getTime() === new Date('04/25/2019').getTime() ? 'disabled-date' : undefined}

CSS:

.disabled-date {
  pointer-events: none;
}

你可以试试道具

dayPickerProps={{
       disabledDays={[
            new Date(2017, 3, 12),
            new Date(2017, 3, 2)
          ]}
}}

或直接使用道具作为

disabledDays={[
        new Date(2017, 3, 12),
        new Date(2017, 3, 2)
    ]}

编辑:抱歉,将 DatePicker 误读为 DayPicker。 以上适用于 DayPicker 组件。

编辑 2:根据文档,您是否尝试过使用道具

excludeDates={[new Date(), subDays(new Date(), 1)]}

暂无
暂无

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

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