Skip to content

Commit 8a8cb7d

Browse files
Merge pull request Hacker0x01#3915 from qburst/master
Calendar icon fix Hacker0x01#3525
2 parents 714d177 + 88afd6e commit 8a8cb7d

File tree

7 files changed

+83
-1
lines changed

7 files changed

+83
-1
lines changed

docs-site/src/components/Examples/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ import SelectsRange from "../../examples/selectsRange";
9090
import selectsRangeWithDisabledDates from "../../examples/selectsRangeWithDisabledDates";
9191
import CalendarStartDay from "../../examples/calendarStartDay";
9292
import ExternalForm from "../../examples/externalForm";
93+
import CalendarIcon from "../../examples/calendarIcon";
9394

9495
import "./style.scss";
9596
import "react-datepicker/dist/react-datepicker.css";
@@ -105,6 +106,10 @@ export default class exampleComponents extends React.Component {
105106
title: "Default",
106107
component: Default,
107108
},
109+
{
110+
title: "Calendar Icon",
111+
component: CalendarIcon,
112+
},
108113
{
109114
title: "Calendar container",
110115
component: CalendarContainer,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
() => {
2+
const [startDate, setStartDate] = useState(new Date());
3+
return (
4+
<DatePicker
5+
showIcon
6+
selected={startDate}
7+
onChange={(date) => setStartDate(date)}
8+
/>
9+
);
10+
};

docs/datepicker.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ General datepicker component.
4444
| `injectTimes` | `array` | | |
4545
| `inline` | `bool` | | |
4646
| `isClearable` | `bool` | | |
47+
| `showIcon` | `bool` | | |
4748
| `locale` | `string` | | |
4849
| `maxDate` | `instanceOf(Date)` | | |
4950
| `maxTime` | `instanceOf(Date)` | | |

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
| `injectTimes` | `array` | | |
5353
| `inline` | `bool` | | |
5454
| `isClearable` | `bool` | | |
55+
| `showIcon` | `bool` | | |
5556
| `locale` | `union(string\|shape)` | | |
5657
| `maxDate` | `instanceOfDate` | | |
5758
| `maxTime` | `instanceOfDate` | | |

src/index.jsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ export default class DatePicker extends React.Component {
181181
injectTimes: PropTypes.array,
182182
inline: PropTypes.bool,
183183
isClearable: PropTypes.bool,
184+
showIcon: PropTypes.bool,
184185
locale: PropTypes.oneOfType([
185186
PropTypes.string,
186187
PropTypes.shape({ locale: PropTypes.object }),
@@ -1176,8 +1177,22 @@ export default class DatePicker extends React.Component {
11761177
};
11771178

11781179
renderInputContainer() {
1180+
const { showIcon } = this.props;
11791181
return (
1180-
<div className="react-datepicker__input-container">
1182+
<div
1183+
className={`react-datepicker__input-container ${
1184+
showIcon ? "react-datepicker__view-calendar-icon" : ""
1185+
}`}
1186+
>
1187+
{showIcon && (
1188+
<svg
1189+
className="react-datepicker__calendar-icon"
1190+
xmlns="http://www.w3.org/2000/svg"
1191+
viewBox="0 0 448 512"
1192+
>
1193+
<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" />
1194+
</svg>
1195+
)}
11811196
{this.renderAriaLiveRegion()}
11821197
{this.renderDateInput()}
11831198
{this.renderClearButton()}

src/stylesheets/datepicker.scss

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,17 @@
517517
position: relative;
518518
display: inline-block;
519519
width: 100%;
520+
521+
.react-datepicker__calendar-icon {
522+
position: absolute;
523+
padding: 0.5rem;
524+
}
525+
}
526+
527+
.react-datepicker__view-calendar-icon {
528+
input {
529+
padding: 6px 10px 5px 25px;
530+
}
520531
}
521532

522533
.react-datepicker__year-read-view,
@@ -701,3 +712,9 @@
701712
width: 1px;
702713
white-space: nowrap;
703714
}
715+
716+
.react-datepicker__calendar-icon {
717+
width: 1em;
718+
height: 1em;
719+
vertical-align: -0.125em;
720+
}

test/datepicker_test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,4 +2092,37 @@ describe("DatePicker", () => {
20922092
);
20932093
});
20942094
});
2095+
2096+
it("should not customize the className attribute if showIcon is set to false", () => {
2097+
let datePicker = TestUtils.renderIntoDocument(
2098+
<DatePicker selected={utils.newDate("2021-04-15")} />
2099+
);
2100+
let showIconClass = TestUtils.findRenderedDOMComponentWithClass(
2101+
datePicker,
2102+
"react-datepicker__input-container "
2103+
).getAttribute("class");
2104+
expect(showIconClass).to.equal("react-datepicker__input-container ");
2105+
});
2106+
2107+
it("should display the Calendar icon if showIcon is set to true", () => {
2108+
let datePicker = TestUtils.renderIntoDocument(
2109+
<DatePicker selected={utils.newDate("2021-04-15")} showIcon />
2110+
);
2111+
let showIconClass = TestUtils.findRenderedDOMComponentWithClass(
2112+
datePicker,
2113+
"react-datepicker__input-container"
2114+
).getAttribute("class");
2115+
expect(showIconClass).to.equal(
2116+
"react-datepicker__input-container react-datepicker__view-calendar-icon"
2117+
);
2118+
2119+
datePicker = TestUtils.renderIntoDocument(
2120+
<DatePicker selected={utils.newDate("2021-04-15")} showIcon />
2121+
);
2122+
showIconClass = TestUtils.findRenderedDOMComponentWithClass(
2123+
datePicker,
2124+
"react-datepicker__calendar-icon"
2125+
).getAttribute("class");
2126+
expect(showIconClass).to.equal("react-datepicker__calendar-icon");
2127+
});
20952128
});

0 commit comments

Comments
 (0)