0

我正在使用 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 日),对此有何帮助。?

4

2 回答 2

3

您应该能够使用该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;
}
于 2019-04-03T17:20:21.093 回答
1

你可以试试道具

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:根据文档,您是否尝试过使用 prop

excludeDates={[new Date(), subDays(new Date(), 1)]}
于 2019-04-03T17:23:23.867 回答