Skip to content

Custom commands #97

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

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
20 changes: 20 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ <h2>date</h2>
</calendar-date>

<h2>multi</h2>
<button commandfor="multi" command="--previous">Previous</button>
<button commandfor="multi" command="--next">Next</button>
<button commandfor="multi" command="--today">Today</button>
<calendar-multi
id="multi"
months="2"
Expand Down Expand Up @@ -161,6 +164,23 @@ <h2>multi</h2>
date.focusedDate = toIso(d);
});
}

// polyfill for non-chrome browsers
if (!window.CommandEvent) {
document.addEventListener("click", (e) => {
const source = e.target.closest("button[commandfor]");

if (source) {
const command = source.getAttribute("command");
const commandfor = source.getAttribute("commandfor");

const event = new Event("command");
event.command = command;
event.source = source;
document.getElementById(commandfor)?.dispatchEvent(event);
}
});
}
</script>
</body>
</html>
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
},
"overrides": {
"playwright": {
".": "1.50.1",
".": "1.52.0",
"@web/test-runner-playwright": "0.11.0"
}
}
Expand Down
28 changes: 20 additions & 8 deletions src/calendar-base/useCalendarBase.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
useState,
useEvent,
useHost,
useEffect,
useMemo,
useRef,
} from "atomico";
import { useState, useEvent, useHost, useEffect, useMemo } from "atomico";
import { PlainDate, PlainYearMonth } from "../utils/temporal.js";
import { useDateProp, useDateFormatter } from "../utils/hooks.js";
import { clamp, toDate, getToday } from "../utils/date.js";
Expand Down Expand Up @@ -105,6 +98,10 @@ function usePagination({
};
}

interface CommandEvent extends Event {
command: string;
}

export function useCalendarBase({
months,
pageBy,
Expand Down Expand Up @@ -151,6 +148,20 @@ export function useCalendarBase({
}
}

function onCommand(event: CommandEvent) {
switch (event.command) {
case "--today":
goto(getToday());
break;
case "--next":
next?.();
break;
case "--previous":
previous?.();
break;
}
}

return {
format: useDateFormatter(formatOptions, locale),
formatVerbose: useDateFormatter(formatVerboseOptions, locale),
Expand All @@ -168,5 +179,6 @@ export function useCalendarBase({
next,
previous,
focus,
onCommand,
};
}
64 changes: 64 additions & 0 deletions src/calendar-date/calendar-date.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
type MonthInstance,
sendShiftPress,
getCalendarVisibleHeading,
type CalendarInstance,
} from "../utils/test.js";
import { CalendarMonth } from "../calendar-month/calendar-month.js";
import type { Pagination } from "../calendar-base/useCalendarBase.js";
Expand All @@ -37,6 +38,7 @@ type TestProps = {
locale: string;
focusedDate: string;
pageBy: Pagination;
id?: string;
};

function Fixture({ children, ...props }: Partial<TestProps>): VNodeAny {
Expand Down Expand Up @@ -867,4 +869,66 @@ describe("CalendarDate", () => {
);
});
});

describe("custom commands", () => {
it("supports a next/previous commands", async () => {
const test = await mount(
<div>
<button command="--previous" commandfor="fixture">
Previous
</button>
<button command="--next" commandfor="fixture">
Next
</button>
<Fixture id="fixture" value="2020-01-01" />
</div>
);

const calendar = test.querySelector<CalendarInstance>("calendar-date")!;
const prevButton = document.querySelector("[command='--previous']")!;
const nextButton = document.querySelector("[command='--next']")!;

expect(getCalendarHeading(calendar)).to.have.text("January 2020");
await click(nextButton);
expect(getCalendarHeading(calendar)).to.have.text("February 2020");
await click(prevButton);
expect(getCalendarHeading(calendar)).to.have.text("January 2020");
});

it("supports a today command", async () => {
const test = await mount(
<div>
<button command="--today" commandfor="fixture">
Today
</button>
<Fixture id="fixture" value="2020-01-01" />
</div>
);

const calendar = test.querySelector<CalendarInstance>("calendar-date")!;
const month = getMonth(calendar);
const todayButton = document.querySelector("[command='--today']")!;
const todaysDate = toDate(getToday());

const heading = getCalendarHeading(calendar);
expect(heading).to.have.text("January 2020");

await click(todayButton);

expect(heading).to.have.text(
todaysDate.toLocaleDateString("en-GB", {
month: "long",
year: "numeric",
})
);
const button = getDayButton(
month,
todaysDate.toLocaleDateString("en-GB", {
month: "long",
day: "numeric",
})
);
expect(button).to.have.attribute("tabindex", "0");
});
});
});
2 changes: 1 addition & 1 deletion src/calendar-date/calendar-date.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const CalendarDate = c(
}

return (
<host shadowDom focus={calendar.focus}>
<host shadowDom focus={calendar.focus} oncommand={calendar.onCommand}>
<CalendarBase
{...props}
{...calendar}
Expand Down
2 changes: 1 addition & 1 deletion src/calendar-multi/calendar-multi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const CalendarMulti = c(
}

return (
<host shadowDom focus={calendar.focus}>
<host shadowDom focus={calendar.focus} oncommand={calendar.onCommand}>
<CalendarBase
{...props}
{...calendar}
Expand Down
2 changes: 1 addition & 1 deletion src/calendar-range/calendar-range.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const CalendarRange = c(
const range = tentative ? sort(tentative, hovered ?? tentative) : value;

return (
<host shadowDom focus={calendar.focus}>
<host shadowDom focus={calendar.focus} oncommand={calendar.onCommand}>
<CalendarBase
{...props}
{...calendar}
Expand Down