Skip to content

Commit a92d5e7

Browse files
Merge pull request #4392 from Rafatcb/disable-clear-button
Disable clear button when the component is disabled
2 parents 13c1136 + d857179 commit a92d5e7

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

src/index.jsx

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,10 @@ export default class DatePicker extends React.Component {
360360
this.props.openToDate
361361
? this.props.openToDate
362362
: this.props.selectsEnd && this.props.startDate
363-
? this.props.startDate
364-
: this.props.selectsStart && this.props.endDate
365-
? this.props.endDate
366-
: newDate();
363+
? this.props.startDate
364+
: this.props.selectsStart && this.props.endDate
365+
? this.props.endDate
366+
: newDate();
367367

368368
// Convert the date from string format to standard Date format
369369
modifyHolidays = () =>
@@ -384,8 +384,8 @@ export default class DatePicker extends React.Component {
384384
minDate && isBefore(defaultPreSelection, startOfDay(minDate))
385385
? minDate
386386
: maxDate && isAfter(defaultPreSelection, endOfDay(maxDate))
387-
? maxDate
388-
: defaultPreSelection;
387+
? maxDate
388+
: defaultPreSelection;
389389
return {
390390
open: this.props.startOpen || false,
391391
preventFocus: false,
@@ -1175,14 +1175,14 @@ export default class DatePicker extends React.Component {
11751175
typeof this.props.value === "string"
11761176
? this.props.value
11771177
: typeof this.state.inputValue === "string"
1178-
? this.state.inputValue
1179-
: this.props.selectsRange
1180-
? safeDateRangeFormat(
1181-
this.props.startDate,
1182-
this.props.endDate,
1183-
this.props,
1184-
)
1185-
: safeDateFormat(this.props.selected, this.props);
1178+
? this.state.inputValue
1179+
: this.props.selectsRange
1180+
? safeDateRangeFormat(
1181+
this.props.startDate,
1182+
this.props.endDate,
1183+
this.props,
1184+
)
1185+
: safeDateFormat(this.props.selected, this.props);
11861186

11871187
return React.cloneElement(customInput, {
11881188
[customInputRef]: (input) => {
@@ -1216,6 +1216,7 @@ export default class DatePicker extends React.Component {
12161216
renderClearButton = () => {
12171217
const {
12181218
isClearable,
1219+
disabled,
12191220
selected,
12201221
startDate,
12211222
endDate,
@@ -1230,7 +1231,12 @@ export default class DatePicker extends React.Component {
12301231
return (
12311232
<button
12321233
type="button"
1233-
className={`react-datepicker__close-icon ${clearButtonClassName}`.trim()}
1234+
className={classnames(
1235+
"react-datepicker__close-icon",
1236+
clearButtonClassName,
1237+
{ "react-datepicker__close-icon--disabled": disabled },
1238+
)}
1239+
disabled={disabled}
12341240
aria-label={ariaLabelClose}
12351241
onClick={this.onClearClick}
12361242
title={clearButtonTitle}

src/stylesheets/datepicker.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,15 @@
663663
vertical-align: middle;
664664
content: "\00d7";
665665
}
666+
667+
&--disabled {
668+
cursor: default;
669+
670+
&::after {
671+
cursor: default;
672+
background-color: $datepicker__muted-color;
673+
}
674+
}
666675
}
667676

668677
.react-datepicker__today-button {

test/datepicker_test.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,23 @@ describe("DatePicker", () => {
500500
expect(datePicker.state.inputValue).toBeNull();
501501
});
502502

503+
it("should disable the clear button when the component is disabled", () => {
504+
const onChange = jest.fn();
505+
const { getByLabelText } = render(
506+
<DatePicker
507+
ariaLabelClose="clear"
508+
disabled
509+
selected={utils.newDate("2023-11-25")}
510+
isClearable
511+
onChange={onChange}
512+
/>,
513+
);
514+
const clearButton = getByLabelText("clear");
515+
expect(clearButton).toHaveProperty("disabled", true);
516+
fireEvent.click(clearButton);
517+
expect(onChange).not.toHaveBeenCalled();
518+
});
519+
503520
it("should return focus to input when clear button is used", (done) => {
504521
var div = document.createElement("div");
505522
document.body.appendChild(div);

0 commit comments

Comments
 (0)