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

add option for specifying weekday format #69

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
4 changes: 4 additions & 0 deletions src/calendar-base/calendar-base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export const props = {
type: Function,
value: (date: Date) => false,
},
formatWeekday: {
type: String,
value: (): "narrow" | "short" => "narrow",
},
firstDayOfWeek: {
type: Number,
value: (): DaysOfWeek => 1,
Expand Down
6 changes: 2 additions & 4 deletions src/calendar-month/CalendarMonthContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface CalendarMonthContextBase {
focusedDate: PlainDate;
showOutsideDays?: boolean;
locale?: string;
formatWeekday: "narrow" | "short";
}

export interface CalendarDateContext extends CalendarMonthContextBase {
Expand All @@ -36,11 +37,8 @@ export type CalendarMonthContextValue =
const t = today();

export const CalendarMonthContext = createContext<CalendarMonthContextValue>({
type: "date",
firstDayOfWeek: 1,
isDateDisallowed: () => false,
focusedDate: t,
page: { start: t.toPlainYearMonth(), end: t.toPlainYearMonth() },
});
} as CalendarMonthContextValue);

customElements.define("calendar-month-ctx", CalendarMonthContext);
34 changes: 34 additions & 0 deletions src/calendar-month/calendar-month.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function Fixture({
focusedDate = today(),
dir,
type = "date",
formatWeekday = "narrow",
...props
}: Partial<DateTestProps | RangeTestProps | MultiTestProps>): VNodeAny {
return (
Expand All @@ -58,6 +59,7 @@ function Fixture({
end: focusedDate.toPlainYearMonth(),
},
focusedDate,
formatWeekday,
// @ts-expect-error - not sure why this is a problem
type,
...props,
Expand Down Expand Up @@ -613,5 +615,37 @@ describe("CalendarMonth", () => {
const button = getDayButton(month, "15 janvier");
expect(button).to.exist;
});

it("has configurable week day formatting", async () => {
const month = await mount(
<Fixture
focusedDate={PlainDate.from("2020-01-15")}
formatWeekday="short"
/>
);
const grid = getGrid(month);

const accessibleHeadings = grid.querySelectorAll(
"th span:not([aria-hidden])"
);
expect(accessibleHeadings[0]).to.have.trimmed.text("Monday");
expect(accessibleHeadings[1]).to.have.trimmed.text("Tuesday");
expect(accessibleHeadings[2]).to.have.trimmed.text("Wednesday");
expect(accessibleHeadings[3]).to.have.trimmed.text("Thursday");
expect(accessibleHeadings[4]).to.have.trimmed.text("Friday");
expect(accessibleHeadings[5]).to.have.trimmed.text("Saturday");
expect(accessibleHeadings[6]).to.have.trimmed.text("Sunday");

const visualHeadings = grid.querySelectorAll(
"th span[aria-hidden='true']"
);
expect(visualHeadings[0]).to.have.trimmed.text("Mon");
expect(visualHeadings[1]).to.have.trimmed.text("Tue");
expect(visualHeadings[2]).to.have.trimmed.text("Wed");
expect(visualHeadings[3]).to.have.trimmed.text("Thu");
expect(visualHeadings[4]).to.have.trimmed.text("Fri");
expect(visualHeadings[5]).to.have.trimmed.text("Sat");
expect(visualHeadings[6]).to.have.trimmed.text("Sun");
});
});
});
2 changes: 1 addition & 1 deletion src/calendar-month/calendar-month.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const CalendarMonth = c(
{calendar.daysLong.map((dayName, i) => (
<th part="th" scope="col">
<span class="vh">{dayName}</span>
<span aria-hidden="true">{calendar.daysShort[i]}</span>
<span aria-hidden="true">{calendar.daysVisible[i]}</span>
</th>
))}
</tr>
Expand Down
10 changes: 7 additions & 3 deletions src/calendar-month/useCalendarMonth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const isLTR = (e: Event) => (e.target as HTMLElement).matches(":dir(ltr)");

const dayOptions = { month: "long", day: "numeric" } as const;
const monthOptions = { month: "long" } as const;
const shortDayOptions = { weekday: "narrow" } as const;
const longDayOptions = { weekday: "long" } as const;
const dispatchOptions = { bubbles: true };

Expand All @@ -37,11 +36,16 @@ export function useCalendarMonth({ props, context }: UseCalendarMonthOptions) {
page,
locale,
focusedDate,
formatWeekday,
} = context;

const todaysDate = today();
const daysLong = useDayNames(longDayOptions, firstDayOfWeek, locale);
const daysShort = useDayNames(shortDayOptions, firstDayOfWeek, locale);
const visibleDayOptions = useMemo(
() => ({ weekday: formatWeekday }),
[formatWeekday]
);
const daysVisible = useDayNames(visibleDayOptions, firstDayOfWeek, locale);
const dayFormatter = useDateFormatter(dayOptions, locale);
const formatter = useDateFormatter(monthOptions, locale);

Expand Down Expand Up @@ -173,7 +177,7 @@ export function useCalendarMonth({ props, context }: UseCalendarMonthOptions) {
weeks,
yearMonth,
daysLong,
daysShort,
daysVisible,
formatter,
getDayProps,
};
Expand Down