【问题标题】:react-datepicker, issues disabling a specific Datereact-datepicker,禁用特定日期的问题
【发布时间】:2019-04-03 17:09:34
【问题描述】:

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

【问题讨论】:

    标签: javascript reactjs react-redux react-datepicker


    【解决方案1】:

    您应该能够使用 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;
    }
    

    【讨论】:

    • 你可以在这里发布你使用的日期选择器组件吗?
    • 我的答案是基于react-datepicker项目使用包含的DatePicker组件。
    【解决方案2】:

    你可以试试道具

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

    或直接使用 prop as

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

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

    编辑 2: 根据documentation,您是否尝试过使用 prop

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 2012-04-02
    • 2010-10-15
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多