@kahitsan/ksui 0.20.0 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kahitsan/ksui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"description": "ksui is a standalone set of SolidJS UI components for KahitSan/Hilinga and any SolidJS app. Published to the public npm registry and consumed as a normal dependency. Ships source under a `solid` export condition so the consumer's vite-plugin-solid compiles it with only solid-js externalized; it depends on nothing but solid-js + lucide-solid and injects its own CSS.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { render, fireEvent, screen } from "@solidjs/testing-library";
|
|
3
|
+
import BadgeSelect, { type BadgeSelectOption } from "./BadgeSelect";
|
|
4
|
+
|
|
5
|
+
// BadgeSelect is the generic, domain-free inline picker lifted from kserp's
|
|
6
|
+
// RoleBadgeSelect (U1). The caller injects options, the value↔label mapping,
|
|
7
|
+
// and (optionally) the badge tone; nothing here is role-specific. The popup
|
|
8
|
+
// renders into a Portal (document.body), so popup assertions use `screen`.
|
|
9
|
+
|
|
10
|
+
const OPTIONS: BadgeSelectOption[] = [
|
|
11
|
+
{ value: "admin", label: "Admin", description: "Full access" },
|
|
12
|
+
{ value: "member", label: "Member" },
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
describe("BadgeSelect", () => {
|
|
16
|
+
it("renders the selected option's label on the trigger", () => {
|
|
17
|
+
const { getByRole } = render(() => (
|
|
18
|
+
<BadgeSelect value="admin" options={OPTIONS} onChange={() => {}} />
|
|
19
|
+
));
|
|
20
|
+
expect(getByRole("button").textContent).toContain("Admin");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("falls back to the raw value when no option matches", () => {
|
|
24
|
+
const { getByRole } = render(() => (
|
|
25
|
+
<BadgeSelect value="ghost" options={OPTIONS} onChange={() => {}} />
|
|
26
|
+
));
|
|
27
|
+
expect(getByRole("button").textContent).toContain("ghost");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("opens the popup and emits the chosen value on select", async () => {
|
|
31
|
+
const onChange = vi.fn();
|
|
32
|
+
const { getByRole } = render(() => (
|
|
33
|
+
<BadgeSelect value="admin" options={OPTIONS} onChange={onChange} />
|
|
34
|
+
));
|
|
35
|
+
fireEvent.click(getByRole("button"));
|
|
36
|
+
const memberOpt = await screen.findByText("Member");
|
|
37
|
+
fireEvent.click(memberOpt);
|
|
38
|
+
expect(onChange).toHaveBeenCalledWith("member");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("does not emit when re-selecting the current value", async () => {
|
|
42
|
+
const onChange = vi.fn();
|
|
43
|
+
const { getByRole } = render(() => (
|
|
44
|
+
<BadgeSelect value="admin" options={OPTIONS} onChange={onChange} />
|
|
45
|
+
));
|
|
46
|
+
fireEvent.click(getByRole("button"));
|
|
47
|
+
// Wait for the popup, then click the option row for the active value.
|
|
48
|
+
await screen.findByText("Member");
|
|
49
|
+
const adminRows = screen.getAllByText("Admin");
|
|
50
|
+
fireEvent.click(adminRows[adminRows.length - 1]);
|
|
51
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("applies the injected badgeClass to the trigger (DI tone)", () => {
|
|
55
|
+
const { getByRole } = render(() => (
|
|
56
|
+
<BadgeSelect
|
|
57
|
+
value="admin"
|
|
58
|
+
options={OPTIONS}
|
|
59
|
+
onChange={() => {}}
|
|
60
|
+
badgeClass={(v) => (v === "admin" ? "tone-special" : "tone-default")}
|
|
61
|
+
/>
|
|
62
|
+
));
|
|
63
|
+
expect(getByRole("button").className).toContain("tone-special");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("disables the trigger when disabled", () => {
|
|
67
|
+
const { getByRole } = render(() => (
|
|
68
|
+
<BadgeSelect value="admin" options={OPTIONS} disabled onChange={() => {}} />
|
|
69
|
+
));
|
|
70
|
+
expect((getByRole("button") as HTMLButtonElement).disabled).toBe(true);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("exposes listbox/option ARIA semantics when open", async () => {
|
|
74
|
+
const { getByRole } = render(() => (
|
|
75
|
+
<BadgeSelect value="admin" options={OPTIONS} onChange={() => {}} />
|
|
76
|
+
));
|
|
77
|
+
const trigger = getByRole("button");
|
|
78
|
+
expect(trigger.getAttribute("aria-haspopup")).toBe("listbox");
|
|
79
|
+
expect(trigger.getAttribute("aria-expanded")).toBe("false");
|
|
80
|
+
fireEvent.click(trigger);
|
|
81
|
+
const listbox = await screen.findByRole("listbox");
|
|
82
|
+
expect(listbox).toBeTruthy();
|
|
83
|
+
expect(trigger.getAttribute("aria-expanded")).toBe("true");
|
|
84
|
+
// The active option advertises its selected state to assistive tech.
|
|
85
|
+
const selected = screen.getAllByRole("option").find((o) => o.getAttribute("aria-selected") === "true");
|
|
86
|
+
expect(selected?.textContent).toContain("Admin");
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { createEffect, createMemo, createSignal, For, onCleanup, Show, type JSX } from "solid-js";
|
|
2
|
+
import { Portal } from "solid-js/web";
|
|
3
|
+
|
|
4
|
+
export interface BadgeSelectOption {
|
|
5
|
+
/** Stable identity of the option (what `value` matches and `onChange` emits). */
|
|
6
|
+
value: string;
|
|
7
|
+
label: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface BadgeSelectProps {
|
|
12
|
+
/** Currently-selected option value. */
|
|
13
|
+
value: string;
|
|
14
|
+
options: BadgeSelectOption[];
|
|
15
|
+
disabled?: boolean;
|
|
16
|
+
loading?: boolean;
|
|
17
|
+
/** Map the selected value to the trigger badge's classes (DI). Defaults to a
|
|
18
|
+
* neutral zinc chip — the caller injects its own tone mapping. */
|
|
19
|
+
badgeClass?: (value: string) => string;
|
|
20
|
+
onChange: (next: string) => void | Promise<void>;
|
|
21
|
+
/** Show the inline search box when options exceed this count. Default 5. */
|
|
22
|
+
searchThreshold?: number;
|
|
23
|
+
searchPlaceholder?: string;
|
|
24
|
+
/** Tooltip on the trigger when enabled. Default "Click to change". */
|
|
25
|
+
title?: string;
|
|
26
|
+
/** Empty-state copy when there are no options at all. Default "No options". */
|
|
27
|
+
emptyLabel?: string;
|
|
28
|
+
/** Empty-state copy when a search filters everything out. Default "No matching options". */
|
|
29
|
+
emptyFilteredLabel?: string;
|
|
30
|
+
/** Copy shown while `loading`. Default "Loading…". */
|
|
31
|
+
loadingLabel?: string;
|
|
32
|
+
testId?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const POPUP_MIN_WIDTH = 200;
|
|
36
|
+
const POPUP_MAX_HEIGHT = 320;
|
|
37
|
+
const DEFAULT_SEARCH_THRESHOLD = 5;
|
|
38
|
+
|
|
39
|
+
// Inline, click-to-edit badge picker. Renders as a clickable badge; clicking
|
|
40
|
+
// opens a dropdown anchored to the badge with all options. When more than
|
|
41
|
+
// `searchThreshold` options are present an inline search box auto-appears so a
|
|
42
|
+
// long list stays scannable. Selecting commits immediately via onChange.
|
|
43
|
+
//
|
|
44
|
+
// The popup is rendered into a Portal with `position: fixed`, so it escapes
|
|
45
|
+
// ancestors that have `overflow: hidden` (e.g. rounded card containers) and
|
|
46
|
+
// flips above the trigger when there isn't enough room below it.
|
|
47
|
+
//
|
|
48
|
+
// Domain-free: the caller supplies the options, the value↔label mapping, and
|
|
49
|
+
// (optionally) the badge tone via `badgeClass`. Nothing here knows what the
|
|
50
|
+
// values mean.
|
|
51
|
+
//
|
|
52
|
+
// Intentionally distinct from SearchableSelect: this is a badge-scoped single
|
|
53
|
+
// select — a compact inline chip trigger plus per-value tone DI (`badgeClass`),
|
|
54
|
+
// not a form-control picker. Kept separate so neither component grows a
|
|
55
|
+
// trigger-shape/styling switch; do not merge them.
|
|
56
|
+
export default function BadgeSelect(props: BadgeSelectProps): JSX.Element {
|
|
57
|
+
const [open, setOpen] = createSignal(false);
|
|
58
|
+
const [busy, setBusy] = createSignal(false);
|
|
59
|
+
const [query, setQuery] = createSignal("");
|
|
60
|
+
const [popupStyle, setPopupStyle] = createSignal<JSX.CSSProperties>({});
|
|
61
|
+
let triggerRef: HTMLButtonElement | undefined;
|
|
62
|
+
let popupRef: HTMLDivElement | undefined;
|
|
63
|
+
let searchRef: HTMLInputElement | undefined;
|
|
64
|
+
|
|
65
|
+
const threshold = () => props.searchThreshold ?? DEFAULT_SEARCH_THRESHOLD;
|
|
66
|
+
const showSearch = () => (props.options?.length ?? 0) > threshold();
|
|
67
|
+
|
|
68
|
+
const filtered = createMemo(() => {
|
|
69
|
+
const q = query().trim().toLowerCase();
|
|
70
|
+
if (!q) return props.options;
|
|
71
|
+
return props.options.filter(
|
|
72
|
+
(o) =>
|
|
73
|
+
o.label.toLowerCase().includes(q) ||
|
|
74
|
+
o.value.toLowerCase().includes(q) ||
|
|
75
|
+
(o.description?.toLowerCase().includes(q) ?? false),
|
|
76
|
+
);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const defaultBadge = () => "bg-zinc-800 text-zinc-400 border border-transparent";
|
|
80
|
+
const badgeClass = () => props.badgeClass?.(props.value) ?? defaultBadge();
|
|
81
|
+
|
|
82
|
+
const emptyStateMessage = () => {
|
|
83
|
+
if (props.loading) return props.loadingLabel ?? "Loading…";
|
|
84
|
+
return query() ? (props.emptyFilteredLabel ?? "No matching options") : (props.emptyLabel ?? "No options");
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const updatePosition = () => {
|
|
88
|
+
if (!triggerRef) return;
|
|
89
|
+
const rect = triggerRef.getBoundingClientRect();
|
|
90
|
+
const vpHeight = window.innerHeight;
|
|
91
|
+
const vpWidth = window.innerWidth;
|
|
92
|
+
const width = Math.max(POPUP_MIN_WIDTH, rect.width);
|
|
93
|
+
const spaceBelow = vpHeight - rect.bottom;
|
|
94
|
+
const spaceAbove = rect.top;
|
|
95
|
+
const flipUp = spaceBelow < POPUP_MAX_HEIGHT && spaceAbove > spaceBelow;
|
|
96
|
+
const top = flipUp ? Math.max(8, rect.top - POPUP_MAX_HEIGHT - 4) : rect.bottom + 4;
|
|
97
|
+
const maxHeight = Math.max(
|
|
98
|
+
160,
|
|
99
|
+
Math.min(POPUP_MAX_HEIGHT, flipUp ? spaceAbove - 12 : spaceBelow - 12),
|
|
100
|
+
);
|
|
101
|
+
const left = Math.min(Math.max(8, rect.left), vpWidth - width - 8);
|
|
102
|
+
setPopupStyle({
|
|
103
|
+
position: "fixed",
|
|
104
|
+
top: `${top}px`,
|
|
105
|
+
left: `${left}px`,
|
|
106
|
+
width: `${width}px`,
|
|
107
|
+
"max-height": `${maxHeight}px`,
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
createEffect(() => {
|
|
112
|
+
if (!open()) return;
|
|
113
|
+
setQuery("");
|
|
114
|
+
updatePosition();
|
|
115
|
+
if (showSearch()) queueMicrotask(() => searchRef?.focus());
|
|
116
|
+
|
|
117
|
+
const onDocClick = (e: MouseEvent) => {
|
|
118
|
+
const target = e.target as Node;
|
|
119
|
+
if (triggerRef?.contains(target)) return;
|
|
120
|
+
if (popupRef?.contains(target)) return;
|
|
121
|
+
setOpen(false);
|
|
122
|
+
};
|
|
123
|
+
const onEsc = (e: KeyboardEvent) => {
|
|
124
|
+
if (e.key === "Escape") setOpen(false);
|
|
125
|
+
};
|
|
126
|
+
const onReflow = () => updatePosition();
|
|
127
|
+
|
|
128
|
+
document.addEventListener("mousedown", onDocClick);
|
|
129
|
+
document.addEventListener("keydown", onEsc);
|
|
130
|
+
window.addEventListener("resize", onReflow);
|
|
131
|
+
window.addEventListener("scroll", onReflow, true);
|
|
132
|
+
onCleanup(() => {
|
|
133
|
+
document.removeEventListener("mousedown", onDocClick);
|
|
134
|
+
document.removeEventListener("keydown", onEsc);
|
|
135
|
+
window.removeEventListener("resize", onReflow);
|
|
136
|
+
window.removeEventListener("scroll", onReflow, true);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const handleSelect = async (next: string) => {
|
|
141
|
+
if (next === props.value) {
|
|
142
|
+
setOpen(false);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
setBusy(true);
|
|
146
|
+
try {
|
|
147
|
+
await props.onChange(next);
|
|
148
|
+
} finally {
|
|
149
|
+
setBusy(false);
|
|
150
|
+
setOpen(false);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const currentLabel = () =>
|
|
155
|
+
props.options.find((o) => o.value === props.value)?.label ?? props.value;
|
|
156
|
+
|
|
157
|
+
return (
|
|
158
|
+
<div class="relative inline-block">
|
|
159
|
+
<button
|
|
160
|
+
ref={triggerRef}
|
|
161
|
+
type="button"
|
|
162
|
+
data-testid={props.testId}
|
|
163
|
+
aria-haspopup="listbox"
|
|
164
|
+
aria-expanded={open()}
|
|
165
|
+
disabled={props.disabled || busy()}
|
|
166
|
+
onClick={() => !props.disabled && setOpen((o) => !o)}
|
|
167
|
+
class={`text-xs font-medium px-1.5 py-0.5 rounded cursor-pointer transition-colors hover:ring-1 hover:ring-amber-500/40 ${badgeClass()} ${
|
|
168
|
+
props.disabled ? "cursor-not-allowed opacity-60" : ""
|
|
169
|
+
}`}
|
|
170
|
+
title={props.disabled ? "" : (props.title ?? "Click to change")}
|
|
171
|
+
>
|
|
172
|
+
{busy() ? "…" : currentLabel()}
|
|
173
|
+
</button>
|
|
174
|
+
<Show when={open()}>
|
|
175
|
+
<Portal>
|
|
176
|
+
<div
|
|
177
|
+
ref={popupRef}
|
|
178
|
+
role="listbox"
|
|
179
|
+
class="z-[100] rounded-md border border-zinc-700 bg-zinc-900/95 backdrop-blur shadow-xl overflow-hidden flex flex-col"
|
|
180
|
+
style={popupStyle()}
|
|
181
|
+
>
|
|
182
|
+
<Show when={showSearch()}>
|
|
183
|
+
<div class="px-2 py-1.5 border-b border-zinc-800">
|
|
184
|
+
<input
|
|
185
|
+
ref={searchRef}
|
|
186
|
+
type="text"
|
|
187
|
+
value={query()}
|
|
188
|
+
onInput={(e) => setQuery(e.currentTarget.value)}
|
|
189
|
+
placeholder={props.searchPlaceholder ?? "Search…"}
|
|
190
|
+
class="w-full px-2 py-1 text-xs bg-zinc-950 border border-zinc-800 rounded text-zinc-200 placeholder:text-zinc-600 focus:outline-none focus:border-amber-500/50"
|
|
191
|
+
/>
|
|
192
|
+
</div>
|
|
193
|
+
</Show>
|
|
194
|
+
<div class="flex-1 overflow-y-auto">
|
|
195
|
+
<Show
|
|
196
|
+
when={!props.loading && filtered().length > 0}
|
|
197
|
+
fallback={<div class="px-3 py-2 text-xs text-zinc-500">{emptyStateMessage()}</div>}
|
|
198
|
+
>
|
|
199
|
+
<For each={filtered()}>
|
|
200
|
+
{(opt) => (
|
|
201
|
+
<button
|
|
202
|
+
type="button"
|
|
203
|
+
role="option"
|
|
204
|
+
aria-selected={opt.value === props.value}
|
|
205
|
+
onClick={() => handleSelect(opt.value)}
|
|
206
|
+
class={`w-full text-left px-3 py-2 text-xs hover:bg-amber-500/10 transition-colors flex items-center justify-between gap-2 ${
|
|
207
|
+
opt.value === props.value ? "text-amber-400" : "text-zinc-200"
|
|
208
|
+
}`}
|
|
209
|
+
>
|
|
210
|
+
<span class="flex flex-col">
|
|
211
|
+
<span class="font-medium">{opt.label}</span>
|
|
212
|
+
<Show when={opt.description}>
|
|
213
|
+
<span class="text-[10px] text-zinc-500">{opt.description}</span>
|
|
214
|
+
</Show>
|
|
215
|
+
</span>
|
|
216
|
+
<Show when={opt.value === props.value}>
|
|
217
|
+
<span class="text-amber-400" aria-hidden="true">✓</span>
|
|
218
|
+
</Show>
|
|
219
|
+
</button>
|
|
220
|
+
)}
|
|
221
|
+
</For>
|
|
222
|
+
</Show>
|
|
223
|
+
</div>
|
|
224
|
+
</div>
|
|
225
|
+
</Portal>
|
|
226
|
+
</Show>
|
|
227
|
+
</div>
|
|
228
|
+
);
|
|
229
|
+
}
|
|
@@ -84,6 +84,18 @@ describe("FileField", () => {
|
|
|
84
84
|
expect(getByTestId("ff-drop")).toBeTruthy();
|
|
85
85
|
});
|
|
86
86
|
|
|
87
|
+
it("loads a pre-seeded image value via the presigned (https) URL into the preview", async () => {
|
|
88
|
+
const presignUrl = vi.fn(async () => "https://signed.example/preview.png?sig=abc");
|
|
89
|
+
const { getByTestId } = render(() => (
|
|
90
|
+
<FileField testId="ff" value={imageHandle} onUpload={vi.fn(async () => imageHandle)} presignUrl={presignUrl} />
|
|
91
|
+
));
|
|
92
|
+
// A value handle present at mount presigns eagerly and shows the done card.
|
|
93
|
+
expect(getByTestId("ff-done")).toBeTruthy();
|
|
94
|
+
await waitFor(() => expect(presignUrl).toHaveBeenCalledWith(imageHandle));
|
|
95
|
+
const img = (await waitFor(() => getByTestId("ff-preview"))) as HTMLImageElement;
|
|
96
|
+
expect(img.getAttribute("src")).toBe("https://signed.example/preview.png?sig=abc");
|
|
97
|
+
});
|
|
98
|
+
|
|
87
99
|
it("renders a non-image handle with a file icon (no presign)", () => {
|
|
88
100
|
const presignUrl = vi.fn(async () => "x");
|
|
89
101
|
const { getByTestId } = render(() => (
|
package/src/index.ts
CHANGED
|
@@ -64,6 +64,7 @@ export { default as KpiCard, type KpiCardProps, type KpiTone } from "./component
|
|
|
64
64
|
export { default as RadioCardGroup } from "./components/base/RadioCardGroup";
|
|
65
65
|
export { default as FormErrorBanner } from "./components/base/FormErrorBanner";
|
|
66
66
|
export { default as TagPill } from "./components/base/TagPill";
|
|
67
|
+
export { default as BadgeSelect, type BadgeSelectProps, type BadgeSelectOption } from "./components/base/BadgeSelect";
|
|
67
68
|
export { default as DateTile, type DateTileProps } from "./components/base/DateTile";
|
|
68
69
|
export { default as Button, type ButtonProps, type ButtonIntent, type ButtonVariant } from "./components/base/Button";
|
|
69
70
|
export { default as ThemeToggle, type ThemeToggleProps, type ThemeToggleValue } from "./components/base/ThemeToggle";
|