在React中禁用材料ui日历中的特定日期

时间:2018-03-26 12:32:02

标签: javascript reactjs datepicker material-ui redux-form

我正在使用材料-ui v0.20.0用于React js 这是我的DatePicker组件

<Field
    name='appointmentDate'
    label="Select Date"
    component={this.renderDatePicker}
/>

renderDatePicker = ({ input, label, meta: { touched, error }, ...custom,props }) => {
    return (
        <DatePicker 
          {...input} 
          {...custom} 
          autoOk={true} 
          floatingLabelText={label}
          dateForm='MM/DD/YYYY' 
          shouldDisableDate={this.disabledDate}
          minDate={ new Date()}
          value={ input.value !== '' ? input.value : null }
          onChange={(event, value) => input.onChange(value)} 
        />
    );
};

如果我想禁用任何一天/ s,我应该在disabledDate(){...}中写什么?

4 个答案:

答案 0 :(得分:2)

以下是需要添加的示例代码。 您可以参考此链接了解更多详情 - http://www.material-ui.com/#/components/date-picker

您可以根据需要添加条件以禁用日期。

&#13;
&#13;
import React from 'react';
import DatePicker from 'material-ui/DatePicker';

function disableWeekends(date) {
  return date.getDay() === 0 || date.getDay() === 6;
}

function disableRandomDates() {
  return Math.random() > 0.7;
}
/**
 * `DatePicker` can disable specific dates based on the return value of a callback.
 */
const DatePickerExampleDisableDates = () => (
  <div>
    <DatePicker hintText="Weekends Disabled" shouldDisableDate={disableWeekends} />
    <DatePicker hintText="Random Dates Disabled" shouldDisableDate={disableRandomDates} />
  </div>
);

export default DatePickerExampleDisableDates;
&#13;
&#13;
&#13;

答案 1 :(得分:0)

考虑一下   上面的代码是切割的(演示代码在组件内部)

 disableWeekends(date) {
   /* date interdites french format dd/mm for all year ! 
    01/01
    01/05
    08/08
    14/07
    15/08
    01/11
    11/11
    25/12 
    replace date.getFullYear() by the desired year otherwise
    */
    // in the following array format is us month are starting from 0 till 11
    const dateInterditesRaw = [
      new Date(date.getFullYear(),0,1),
      new Date(date.getFullYear(),4,1),
      new Date(date.getFullYear(),7,8),
      new Date(date.getFullYear(),6,14),
      new Date(date.getFullYear(),7,15),
      new Date(date.getFullYear(),10,1),
      new Date(date.getFullYear(),10,11),
      new Date(date.getFullYear(),11,25),
    ];

    /* make a new array with the getTime() without it date comparaison are 
    never true  */

    const dateInterdites = dateInterditesRaw.map((arrVal) => {return 
    arrVal.getTime()});

    /*exclude all sunday and use the includes array method to check if the 
    date.getTime() value is 
    in the array dateInterdites */

    return date.getDay() === 0 || dateInterdites.includes(date.getTime());
  }

render() {
  return(
         <DatePicker 
          floatingLabelText="Jour de la visite" 
          value={this.props.dateVisite} 
          shouldDisableDate={this.disableWeekends}
          onChange={this.handleChangeDateVisit}
      />
 );
}
}

答案 2 :(得分:0)

在更新的API中,它是“ disablePast ”属性。

检查https://material-ui-pickers.dev/api/DatePicker

答案 3 :(得分:0)

对于特定日期:

示例:禁用 2021 年 5 月 3 日

<DatePicker
  shouldDisableDate={(date) => date.getTime() === new Date('2021-05-03T00:00').getTime()}
/>