1

I'm using react date selector plugin called react-flatpickr. When I want the flatpickr to be disabled, I need to set a prop called "disabled" on the Flatpickr component. disabled is not a boolean that can be set to true or false. disabled is a property without a value. Is there a way to initialize the Flatpickr component with the disabled prop only when readOnly is true, instead of this horribly duplicated code?

getFlatPickr(id, ref, defaultDate, minDate, readOnly){
    if (readOnly) {
        return (
            <Flatpickr
                id={id}
                name={id}
                disabled
                className={"form-control"}
                placeholder={"MM/DD/YYYY HH:MM AM/PM"}
                options={{
                    enableTime: true,
                    dateFormat: this.state.flatpickrDateFormat,
                }}
            />
        );
    } else {
        return (
            <Flatpickr
                id={id}
                name={id}
                className={"form-control"}
                placeholder={"MM/DD/YYYY HH:MM AM/PM"}
                options={{
                    enableTime: true,
                    dateFormat: this.state.flatpickrDateFormat
                }}
            />
        );
    }
}
4
  • Have you tried disabled={readOnly} ? Commented Feb 12, 2019 at 16:03
  • I have. The property is a single word - "disabled." It has no value, not true or false, so setting it to the value of readOnly does not work. Commented Feb 12, 2019 at 16:04
  • 3
    In react attributes passed without a value are assumed to be booleans and are given a true value. Commented Feb 12, 2019 at 16:11
  • 1
    If however you want the receiving component to not even register the prop then use disabled={readonly || undefined} Commented Feb 12, 2019 at 16:13

1 Answer 1

2

Sure, simply set the disabled prop directly to your readOnly variable :

getFlatPickr(id, ref, defaultDate, minDate, readOnly) {
        return (
            <Flatpickr
                id={id}
                name={id}
                disabled={readOnly}
                className={"form-control"}
                placeholder={"MM/DD/YYYY HH:MM AM/PM"}
                options={{
                    enableTime: true,
                    dateFormat: this.state.flatpickrDateFormat
                }}
            />
        );
    }
}

Even shorter syntax :

getFlatPickr = (id, ref, defaultDate, minDate, readOnly) => (
    <Flatpickr
        id={id}
        name={id}
        disabled={readOnly}
        className="form-control"
        placeholder="MM/DD/YYYY HH:MM AM/PM"
        options={{
            enableTime: true,
            dateFormat: this.state.flatpickrDateFormat
        }}
    />
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.