Skip to content

Commit

Permalink
Merge pull request #3915 from qburst/master
Browse files Browse the repository at this point in the history
Calendar icon fix  #3525
  • Loading branch information
martijnrusschen authored Feb 3, 2023
2 parents 714d177 + 88afd6e commit 8a8cb7d
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs-site/src/components/Examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import SelectsRange from "../../examples/selectsRange";
import selectsRangeWithDisabledDates from "../../examples/selectsRangeWithDisabledDates";
import CalendarStartDay from "../../examples/calendarStartDay";
import ExternalForm from "../../examples/externalForm";
import CalendarIcon from "../../examples/calendarIcon";

import "./style.scss";
import "react-datepicker/dist/react-datepicker.css";
Expand All @@ -105,6 +106,10 @@ export default class exampleComponents extends React.Component {
title: "Default",
component: Default,
},
{
title: "Calendar Icon",
component: CalendarIcon,
},
{
title: "Calendar container",
component: CalendarContainer,
Expand Down
10 changes: 10 additions & 0 deletions docs-site/src/examples/calendarIcon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
() => {
const [startDate, setStartDate] = useState(new Date());
return (
<DatePicker
showIcon
selected={startDate}
onChange={(date) => setStartDate(date)}
/>
);
};
1 change: 1 addition & 0 deletions docs/datepicker.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ General datepicker component.
| `injectTimes` | `array` | | |
| `inline` | `bool` | | |
| `isClearable` | `bool` | | |
| `showIcon` | `bool` | | |
| `locale` | `string` | | |
| `maxDate` | `instanceOf(Date)` | | |
| `maxTime` | `instanceOf(Date)` | | |
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
| `injectTimes` | `array` | | |
| `inline` | `bool` | | |
| `isClearable` | `bool` | | |
| `showIcon` | `bool` | | |
| `locale` | `union(string\|shape)` | | |
| `maxDate` | `instanceOfDate` | | |
| `maxTime` | `instanceOfDate` | | |
Expand Down
17 changes: 16 additions & 1 deletion src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export default class DatePicker extends React.Component {
injectTimes: PropTypes.array,
inline: PropTypes.bool,
isClearable: PropTypes.bool,
showIcon: PropTypes.bool,
locale: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({ locale: PropTypes.object }),
Expand Down Expand Up @@ -1176,8 +1177,22 @@ export default class DatePicker extends React.Component {
};

renderInputContainer() {
const { showIcon } = this.props;
return (
<div className="react-datepicker__input-container">
<div
className={`react-datepicker__input-container ${
showIcon ? "react-datepicker__view-calendar-icon" : ""
}`}
>
{showIcon && (
<svg
className="react-datepicker__calendar-icon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 448 512"
>
<path d="M96 32V64H48C21.5 64 0 85.5 0 112v48H448V112c0-26.5-21.5-48-48-48H352V32c0-17.7-14.3-32-32-32s-32 14.3-32 32V64H160V32c0-17.7-14.3-32-32-32S96 14.3 96 32zM448 192H0V464c0 26.5 21.5 48 48 48H400c26.5 0 48-21.5 48-48V192z" />
</svg>
)}
{this.renderAriaLiveRegion()}
{this.renderDateInput()}
{this.renderClearButton()}
Expand Down
17 changes: 17 additions & 0 deletions src/stylesheets/datepicker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,17 @@
position: relative;
display: inline-block;
width: 100%;

.react-datepicker__calendar-icon {
position: absolute;
padding: 0.5rem;
}
}

.react-datepicker__view-calendar-icon {
input {
padding: 6px 10px 5px 25px;
}
}

.react-datepicker__year-read-view,
Expand Down Expand Up @@ -701,3 +712,9 @@
width: 1px;
white-space: nowrap;
}

.react-datepicker__calendar-icon {
width: 1em;
height: 1em;
vertical-align: -0.125em;
}
33 changes: 33 additions & 0 deletions test/datepicker_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2092,4 +2092,37 @@ describe("DatePicker", () => {
);
});
});

it("should not customize the className attribute if showIcon is set to false", () => {
let datePicker = TestUtils.renderIntoDocument(
<DatePicker selected={utils.newDate("2021-04-15")} />
);
let showIconClass = TestUtils.findRenderedDOMComponentWithClass(
datePicker,
"react-datepicker__input-container "
).getAttribute("class");
expect(showIconClass).to.equal("react-datepicker__input-container ");
});

it("should display the Calendar icon if showIcon is set to true", () => {
let datePicker = TestUtils.renderIntoDocument(
<DatePicker selected={utils.newDate("2021-04-15")} showIcon />
);
let showIconClass = TestUtils.findRenderedDOMComponentWithClass(
datePicker,
"react-datepicker__input-container"
).getAttribute("class");
expect(showIconClass).to.equal(
"react-datepicker__input-container react-datepicker__view-calendar-icon"
);

datePicker = TestUtils.renderIntoDocument(
<DatePicker selected={utils.newDate("2021-04-15")} showIcon />
);
showIconClass = TestUtils.findRenderedDOMComponentWithClass(
datePicker,
"react-datepicker__calendar-icon"
).getAttribute("class");
expect(showIconClass).to.equal("react-datepicker__calendar-icon");
});
});

0 comments on commit 8a8cb7d

Please sign in to comment.