Skip to content

Commit

Permalink
Feature/week day class name (#2113)
Browse files Browse the repository at this point in the history
* Add week day class name feature

* Extend docs for week day class name feature

* Add test to week day class name feature
  • Loading branch information
micama committed Apr 16, 2020
1 parent 279f9ff commit b21bf47
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/datepicker.md
Expand Up @@ -17,6 +17,7 @@ General datepicker component.
| `dateFormat` | `union(string\|array)` | `'MM/dd/yyyy'` | |
| `dateFormatCalendar` | `string` | `'LLLL yyyy'` | |
| `dayClassName` | `func` | | |
| `weekDayClassName` | `func` | | |
| `disabled` | `bool` | `false` | |
| `disabledKeyboardNavigation` | `bool` | `false` | |
| `dropdownMode` (required) | `enum('scroll'\|'select')` | `'scroll'` | |
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Expand Up @@ -20,6 +20,7 @@
| `dateFormat` | `union(string\|array)` | `"MM/dd/yyyy"` | |
| `dateFormatCalendar` | `string` | `"LLLL yyyy"` | |
| `dayClassName` | `func` | | |
| `weekDayClassName` | `func` | | |
| `disabled` | `bool` | `false` | |
| `disabledDayAriaLabelPrefix` | `string` | | |
| `disabledKeyboardNavigation` | `bool` | `false` | |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"author": "HackerOne",
"name": "react-datepicker",
"description": "A simple and reusable datepicker component for React",
"version": "2.14.1",
"version": "2.14.2-SNAPSHOT",
"license": "MIT",
"homepage": "https://github.com/Hacker0x01/react-datepicker",
"main": "dist/index.js",
Expand Down
14 changes: 13 additions & 1 deletion src/calendar.jsx
Expand Up @@ -75,6 +75,7 @@ export default class Calendar extends React.Component {
dateFormat: PropTypes.oneOfType([PropTypes.string, PropTypes.array])
.isRequired,
dayClassName: PropTypes.func,
weekDayClassName: PropTypes.func,
disabledDayAriaLabelPrefix: PropTypes.string,
monthClassName: PropTypes.func,
timeClassName: PropTypes.func,
Expand Down Expand Up @@ -336,8 +337,19 @@ export default class Calendar extends React.Component {
[0, 1, 2, 3, 4, 5, 6].map(offset => {
const day = addDays(startOfWeek, offset);
const weekDayName = this.formatWeekday(day, this.props.locale);

const weekDayClassName = this.props.weekDayClassName
? this.props.weekDayClassName(day)
: undefined;

return (
<div key={offset} className="react-datepicker__day-name">
<div
key={offset}
className={classnames(
"react-datepicker__day-name",
weekDayClassName
)}
>
{weekDayName}
</div>
);
Expand Down
2 changes: 2 additions & 0 deletions src/index.jsx
Expand Up @@ -129,6 +129,7 @@ export default class DatePicker extends React.Component {
dateFormat: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
dateFormatCalendar: PropTypes.string,
dayClassName: PropTypes.func,
weekDayClassName: PropTypes.func,
disabledDayAriaLabelPrefix: PropTypes.string,
monthClassName: PropTypes.func,
timeClassName: PropTypes.func,
Expand Down Expand Up @@ -768,6 +769,7 @@ export default class DatePicker extends React.Component {
onMonthChange={this.props.onMonthChange}
onYearChange={this.props.onYearChange}
dayClassName={this.props.dayClassName}
weekDayClassName={this.props.weekDayClassName}
monthClassName={this.props.monthClassName}
timeClassName={this.props.timeClassName}
showTimeSelect={this.props.showTimeSelect}
Expand Down
18 changes: 18 additions & 0 deletions test/calendar_test.js
Expand Up @@ -13,6 +13,7 @@ import sinon from "sinon";
import * as utils from "../src/date_utils";
import eo from "date-fns/locale/eo";
import fi from "date-fns/locale/fi";
import { isSunday } from "date-fns";

// TODO Possibly rename
const DATE_FORMAT = "MM/dd/yyyy";
Expand Down Expand Up @@ -166,6 +167,23 @@ describe("Calendar", function() {
.forEach(dayName => expect(dayName.text()).to.have.length(1));
});

it("should contain the correct class when using the weekDayClassName prop", () => {
const func = date => (isSunday(date) ? "sunday" : undefined);

const calendar = mount(
<Calendar
dateFormat={dateFormat}
dropdownMode="scroll"
onClickOutside={() => {}}
onSelect={() => {}}
weekDayClassName={func}
/>
);

const sunday = calendar.find(".react-datepicker__day-name.sunday");
expect(sunday).to.have.length(1);
});

it("should render the months correctly adjusted by monthSelectedIn", () => {
const selected = utils.newDate("2018-11-19");
const calendar = getCalendar({ inline: true, monthsShown: 2, selected });
Expand Down

0 comments on commit b21bf47

Please sign in to comment.