2

I have used a date picker component from react-date-picker. I need to disable the entire date control and the button itself on a single button click. The button does get disabled but the DatePicker component doesn't. I have tried the following but didn't work. Is there a correct way to do it?

import DatePicker from 'react-date-picker';
...
...
this.state = {
  disabledDate: false
}
    ...
    ...
    ...

  noDateRememberButtonClick() {
    this.setState({
      disabledDate: true
    });
  }

    render() {
           return(
        <div className="wrapper">
          <div className="date-picker">
            <DatePicker
              onChange={this.dateOnChange}
              value={this.state.boughtDate}
              disabled={this.state.disabledDate} />
          </div>
          <button className="btn" onClick={this.noDateRememberButtonClick} disabled={this.state.disabledDate} type="button"> {BUTTON_LABEL}</button>
        </div>

)}
PineCone
  • 2,193
  • 12
  • 37
  • 78

4 Answers4

2
<DatePicker
  ref={endCalendarRef}
  readOnly={true}
  .....
  />
Ravi Kumar Karunanithi
  • 2,151
  • 2
  • 19
  • 41
1

react-date-picker doesn't support the disabled prop. See https://github.com/wojtekmaj/react-date-picker

Per shaz's recommendation, react-datepicker (https://github.com/Hacker0x01/react-datepicker/blob/master/docs/datepicker.md) does, if you would like to use that component instead

Galupuf
  • 2,827
  • 1
  • 12
  • 29
1

Try this. works for me

const CustomInput = (props) => {
    return (
        <input
            className={[classes.TransparentInput, "uk-input"].join(" ")}
            onClick={props.onClick}
            value={props.value}
            type="text"
            readOnly={true}
        />
    )
}
<DatePicker
  customInput={<CustomInput />}
  name={'from'}
  value={values['from']}
  onChange={e => setFieldValue('from', moment(e).format('L'))}
 />

The setFieldValue is from formik. You can choose not to use formik. your choice.

OLATOMIDE
  • 93
  • 1
  • 7
0

As a hacky workaround (don't feel like taking another library), this worked for me:

Add a disabled class on a container, that wraps the DatePicker

.disabled {
  pointer-events: none;

  input {
    background: transparent;
    box-shadow: none;
    border: none;
  }
}

EDIT: The input styling is only needed, if you wanna make the field look like plain text only.