0

Does anyone know how I can pass an array of dates to react-datetime? I'm trying to implement a datepicker which can display "booked" dates from my state but currently I only managed to disable all days before the current one. So how can I pass the dates in my state to <Datetime/>, in order to disable them?

 'use strict';

import React       from 'react';
import {PropTypes} from 'prop-types';
import Datetime    from 'react-datetime';

class DatePicker extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            dates: [{id: 1, date: 'Apr 30 2020 09:00:00 AM'}, {id: 2, date: 'May 1 2020 12:00:00 PM'}]
        };

    }

    render() {
        const {t} = this.props;

        let yesterday = Datetime.moment().subtract(1, 'day');

        let valid = function (current) {
            return current.isAfter(yesterday);
        };

        return (
            <div className="date-picker">
                <p>{t('Date Picker')}</p>

                <Datetime timeFormat={false} isValidDate={valid(yesterday)}/>

            </div>
        );
    }
}

export default DatePicker;
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
networkcore
  • 135
  • 1
  • 11

1 Answers1

1

Well first of all somethings wrong with your date format in state. Try setting up your dates using moment

this.state = {
   dates: [{
    id: 1,
    date: Datetime.moment("30 Apr 2020")
  },
  {
    id: 2,
    date: Datetime.moment("01 May 2020")
  }]
}

And your validation function should look something like this or you can extract it into a class method

  let valid = function(current, selected) {
    return !this.state.dates.some(day => current.isSame(day.date, "day"));
  };

and pass this function to Datetime

<Datetime timeFormat={false} isValidDate={valid}/>
rzvdv
  • 51
  • 2