Skip to content
Merged
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
78 changes: 78 additions & 0 deletions settings/src/frame/AddPicker.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { render } from "solid-js/web";
import { AddPicker } from "./AddPicker";

const options = [
{ id: "comments", name: "Comments" },
{ id: "history", name: "History" },
{ id: "outline", name: "Outline" },
];

let dispose: (() => void) | undefined;
afterEach(() => {
dispose?.();
document.body.replaceChildren();
});

function mount(list = options) {
const onPick = vi.fn();
const root = document.createElement("div");
document.body.append(root);
dispose = render(() => <AddPicker options={list} onPick={onPick} />, root);
const menu = root.querySelector<HTMLElement>(".settings-picker")!;
// happy-dom has no popover api: stand in for it, toggle event and all
let open = false;
const set = (next: boolean) => {
open = next;
menu.dispatchEvent(
Object.assign(new Event("toggle"), { newState: next ? "open" : "closed" })
);
};
menu.togglePopover = () => (set(!open), !open);
menu.hidePopover = () => set(false);
const input = root.querySelector<HTMLInputElement>("input")!;
const type = (value: string) => {
input.value = value;
input.dispatchEvent(new Event("input", { bubbles: true }));
};
const key = (key: string) =>
input.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true }));
const items = () =>
[...root.querySelectorAll(".settings-picker-item")].map((el) =>
el.textContent?.trim()
);
root.querySelector<HTMLButtonElement>(".settings-button")!.click();
return { onPick, type, key, items, isOpen: () => open };
}

describe("AddPicker", () => {
it("filters what's offered by what's typed, and enter adds the highlighted one", () => {
const { onPick, type, key, items, isOpen } = mount();
expect(isOpen()).toBe(true);
expect(items()).toEqual(["Comments", "History", "Outline"]);
type("o");
expect(items()).toEqual(["Comments", "History", "Outline"]);
type("out");
expect(items()).toEqual(["Outline"]);
type("");
key("ArrowDown");
key("Enter");
expect(onPick).toHaveBeenCalledWith("history");
expect(isOpen()).toBe(false);
});

it("adds what was typed as a tool id when nothing matches", () => {
const { onPick, type, key, items } = mount();
type(" my-tool ");
expect(items()).toEqual(["Add my-tool"]);
key("Enter");
expect(onPick).toHaveBeenCalledWith("my-tool");
});

it("says when there's nothing left to add", () => {
mount([]);
expect(
document.querySelector(".settings-picker-empty")?.textContent
).toMatch(/already here/);
});
});
135 changes: 135 additions & 0 deletions settings/src/frame/AddPicker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { createMemo, createSignal, For, Show } from "solid-js";
import { filterOptions, type LaneItem } from "./lanes";
import { placePopover } from "./place";

// "Add…", and a search over `options` that pops out of it, like the title
// bar's "Open with…". type to filter, arrows to move, enter to add. enter on
// something that matches nothing adds what you typed, as a tool id
export function AddPicker(props: {
options: LaneItem[];
onPick: (id: string) => void;
}) {
let button!: HTMLButtonElement;
let menu!: HTMLDivElement;
let input!: HTMLInputElement;
let list!: HTMLDivElement;
const [open, setOpen] = createSignal(false);
const [query, setQuery] = createSignal("");
const [highlighted, setHighlighted] = createSignal(0);
const matches = createMemo(() => filterOptions(props.options, query()));
const typed = () => query().trim();

function pick(id: string) {
menu.hidePopover();
props.onPick(id);
}

function highlight(index: number) {
setHighlighted(index);
list.children[index]?.scrollIntoView({ block: "nearest" });
}

function place() {
placePopover(menu, button, { width: 220 });
}

return (
<>
<button
ref={button}
type="button"
class="settings-button"
aria-haspopup="listbox"
aria-expanded={open()}
data-open={open() ? "" : undefined}
onClick={() => {
if (!open()) {
setQuery("");
setHighlighted(0);
place();
}
menu.togglePopover();
}}
>
Add…
</button>
<div
ref={menu}
popover="auto"
class="settings-picker"
on:toggle={(event: ToggleEvent) => {
setOpen(event.newState === "open");
if (event.newState === "open") input.focus();
}}
>
<input
ref={input}
class="settings-picker-input"
placeholder="Add or filter tools…"
aria-label="Add or filter tools"
spellcheck={false}
autocomplete="off"
value={query()}
onInput={(event) => {
setQuery(event.currentTarget.value);
setHighlighted(0);
}}
onKeyDown={(event) => {
const count = matches().length;
if (event.key === "ArrowDown") {
event.preventDefault();
highlight(Math.min(highlighted() + 1, count - 1));
} else if (event.key === "ArrowUp") {
event.preventDefault();
highlight(Math.max(highlighted() - 1, 0));
} else if (event.key === "Enter") {
event.preventDefault();
const match = matches()[highlighted()];
if (match) pick(match.id);
else if (typed()) pick(typed());
}
}}
/>
<div ref={list} class="settings-picker-list" role="listbox">
<For each={matches()}>
{(option, index) => (
<button
type="button"
role="option"
class="settings-picker-item"
aria-selected={highlighted() === index()}
data-highlight={highlighted() === index() ? "" : undefined}
onPointerEnter={() => setHighlighted(index())}
onClick={() => pick(option.id)}
>
{option.name}
</button>
)}
</For>
<Show when={!matches().length}>
<Show
when={typed()}
fallback={
<p class="settings-picker-empty">
{props.options.length
? "Nothing matches"
: "Everything's already here. Type a tool id to add another."}
</p>
}
>
<button
type="button"
role="option"
class="settings-picker-item"
data-highlight=""
onClick={() => pick(typed())}
>
Add <code class="settings-code">{typed()}</code>
</button>
</Show>
</Show>
</div>
</div>
</>
);
}
Loading
Loading