Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(chore) Rename getClassNamesForModifiers and add tests #2661

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/DayPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import type { MouseEvent, FocusEvent, KeyboardEvent, ChangeEvent } from "react";
import { UI, DayFlag, SelectionState } from "./UI.js";
import type { CalendarDay } from "./classes/CalendarDay.js";
import { DateLib, defaultLocale } from "./classes/DateLib.js";
import { getClassNamesForModifiers } from "./helpers/getClassNamesForModifiers.js";
import { getComponents } from "./helpers/getComponents.js";
import { getDataAttributes } from "./helpers/getDataAttributes.js";
import { getDayCellClassNames } from "./helpers/getDayCellClassNames.js";
import { getDefaultClassNames } from "./helpers/getDefaultClassNames.js";
import { getFormatters } from "./helpers/getFormatters.js";
import { getMonthOptions } from "./helpers/getMonthOptions.js";
Expand Down Expand Up @@ -486,13 +486,13 @@ export function DayPicker(props: DayPickerProps) {
);
}

const style = getStyleForModifiers(
const cellStyle = getStyleForModifiers(
modifiers,
styles,
props.modifiersStyles
);

const className = getClassNamesForModifiers(
const cellClassNames = getDayCellClassNames(
modifiers,
classNames,
props.modifiersClassNames
Expand All @@ -513,8 +513,8 @@ export function DayPicker(props: DayPickerProps) {
key={`${dateLib.format(date, "yyyy-MM-dd")}_${dateLib.format(day.displayMonth, "yyyy-MM")}`}
day={day}
modifiers={modifiers}
className={className.join(" ")}
style={style}
className={cellClassNames.join(" ")}
style={cellStyle}
role="gridcell"
aria-selected={modifiers.selected || undefined}
aria-label={ariaLabel}
Expand Down
26 changes: 0 additions & 26 deletions src/helpers/getClassNamesForModifiers.ts

This file was deleted.

64 changes: 64 additions & 0 deletions src/helpers/getDayCellClassNames.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { DayFlag, SelectionState } from "../UI.js";
import type { ModifiersClassNames, ClassNames } from "../types/index.js";

import { getDayCellClassNames } from "./getDayCellClassNames.js";
import { getDefaultClassNames } from "./getDefaultClassNames.js";

describe("getDayCellClassNames", () => {
const baseClass = "day";
const classNames: ClassNames = {
...getDefaultClassNames(),
[DayFlag.disabled]: "disabled",
[SelectionState.selected]: "selected-style"
};
const modifiersClassNames: ModifiersClassNames = {
today: "today-class",
weekend: "weekend-class"
};

it("should return base class when no modifiers are active", () => {
const modifiers = { today: false, weekend: false };
const result = getDayCellClassNames(
modifiers,
classNames,
modifiersClassNames
);
expect(result).toEqual([baseClass]);
});

it("should return class names for active modifiers", () => {
const modifiers = { today: true, weekend: false };
const result = getDayCellClassNames(
modifiers,
classNames,
modifiersClassNames
);
expect(result).toEqual([baseClass, "today-class"]);
});

it("should return class names for active day flags", () => {
const modifiers = { selected: true };
const result = getDayCellClassNames(
modifiers,
classNames,
modifiersClassNames
);
expect(result).toEqual([baseClass, "selected-style"]);
});

it("should return class names for active selection states", () => {
const modifiers = { selected: true };
const result = getDayCellClassNames(modifiers, classNames);
expect(result).toEqual([baseClass, "selected-style"]);
});

it("should prioritize modifiersClassNames over classNames", () => {
const modifiers = { today: true, selected: true };
const result = getDayCellClassNames(
modifiers,
classNames,
modifiersClassNames
);
expect(result).toEqual([baseClass, "today-class", "selected-style"]);
});
});
26 changes: 26 additions & 0 deletions src/helpers/getDayCellClassNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { DayFlag, SelectionState, UI } from "../UI.js";
import type { ModifiersClassNames, ClassNames } from "../types/index.js";

export function getDayCellClassNames(
modifiers: Record<string, boolean>,
classNames: ClassNames,
modifiersClassNames: ModifiersClassNames = {}
) {
const result = Object.entries(modifiers)
.filter(([, active]) => active === true)
.reduce(
(val, [modifier]) => {
if (modifiersClassNames[modifier]) {
val.push(modifiersClassNames[modifier as string]);
} else if (classNames[DayFlag[modifier as DayFlag]]) {
val.push(classNames[DayFlag[modifier as DayFlag]]);
} else if (classNames[SelectionState[modifier as SelectionState]]) {
val.push(classNames[SelectionState[modifier as SelectionState]]);
}
return val;
},
[classNames[UI.Day]] as string[]
);

return result;
}
Loading