如何使用Material ui reactjs禁用今天的过去日期?

时间:2016-12-25 17:39:01

标签: javascript html reactjs material-ui

我用反应材料ui创建日期范围选择器。此功能背后的逻辑是选择所需日期,如果已选择所需日期,则禁用所选日期的所有过去日期。如何实施这种反应材料ui?

这是我的代码,

import React from 'react';
import {render} from 'react-dom';
import DatePicker from 'material-ui/DatePicker';

function disablePrevDates(date) {
  return date.getDay() === 0;
}

class App extends React.Component {
    render() {
        return (
            <div>
                <DatePicker hintText="Check-in" shouldDisableDate={disablePrevDates} />
            </div>
        )
    }
}

export default App;

3 个答案:

答案 0 :(得分:3)

这是:

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

function disablePrevDates(startDate) {
  const startSeconds = Date.parse(startDate);
  return (date) => {
    return Date.parse(date) < startSeconds;
  }
}

class App extends React.Component {
    
    render() {
        const startDate = new Date();
//      or:
//      const startDate = new Date('December 20, 2016');
//      const startDate = this.state.firstDate;
      
        return (
            <div>
                <DatePicker 
                  hintText="Check-in" 
                  shouldDisableDate={disablePrevDates(startDate)}
                />
            </div>
        )
    }
}

export default App;
&#13;
&#13;
&#13;

答案 1 :(得分:0)

与您问题的标题相关。

const minDate = new Date(Date.now());

class App extends React.Component {
    render() {
        return (
            <div>
                <DatePicker hintText="Check-in" minDate={minDate} />
            </div>
        )
    }
}

答案 2 :(得分:0)

最好的方式:

class App extends React.Component {
    render() {
        return (
            <div>
                <DatePicker hintText="Check-in" disablePast />
            </div>
        )
    }
}