@alepha/ui 0.25.0 → 0.25.2
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 +4 -5
- package/src/components/account/__tests__/account-router.spec.ts +201 -0
- package/src/components/account/account-connections.tsx +103 -0
- package/src/components/account/account-delete-dialog.tsx +184 -0
- package/src/components/account/account-header.tsx +57 -0
- package/src/components/account/account-keys.tsx +221 -0
- package/src/components/account/account-layout.tsx +63 -0
- package/src/components/account/account-password-dialog.tsx +152 -0
- package/src/components/account/account-profile.tsx +229 -0
- package/src/components/account/account-router-options.tsx +74 -0
- package/src/components/account/account-router-page.tsx +69 -0
- package/src/components/account/account-router.tsx +214 -0
- package/src/components/account/account-security.tsx +150 -0
- package/src/components/account/account-sessions.tsx +140 -0
- package/src/components/admin/__tests__/admin-keys-token-dialog.browser.spec.tsx +62 -0
- package/src/components/admin/__tests__/admin-layout.browser.spec.tsx +82 -0
- package/src/components/admin/__tests__/admin-router-page.spec.ts +54 -0
- package/src/components/admin/__tests__/admin-router.spec.ts +208 -0
- package/src/components/admin/__tests__/admin-user-cell.browser.spec.tsx +99 -0
- package/src/components/admin/__tests__/admin-user-detail.spec.ts +29 -0
- package/src/components/admin/admin-analytics-query-panel.tsx +149 -0
- package/src/components/admin/admin-analytics-results.tsx +95 -0
- package/src/components/admin/admin-analytics.tsx +104 -0
- package/src/components/admin/admin-audits.tsx +12 -15
- package/src/components/admin/admin-files.tsx +10 -18
- package/src/components/admin/admin-jobs-executions-panel.tsx +2 -1
- package/src/components/admin/admin-jobs.tsx +3 -1
- package/src/components/admin/admin-keys-token-dialog.tsx +123 -0
- package/src/components/admin/admin-keys.tsx +54 -3
- package/src/components/admin/admin-layout.tsx +107 -0
- package/src/components/admin/admin-payments.tsx +4 -5
- package/src/components/admin/admin-router-options.tsx +130 -0
- package/src/components/admin/admin-router-page.tsx +74 -0
- package/src/components/admin/admin-router.tsx +379 -0
- package/src/components/admin/admin-sessions.tsx +2 -16
- package/src/components/admin/admin-user-cell.tsx +70 -0
- package/src/components/admin/admin-user-detail-overview-tab.tsx +28 -0
- package/src/components/admin/admin-user-detail-profile-policy.spec.ts +146 -0
- package/src/components/admin/admin-user-detail-profile-policy.ts +147 -0
- package/src/components/admin/admin-user-detail-security-tab.tsx +1 -1
- package/src/components/admin/admin-user-detail.tsx +76 -32
- package/src/components/admin/admin-users-status-filter.tsx +2 -0
- package/src/components/admin/admin-workflows-execution-sheet.tsx +277 -0
- package/src/components/admin/admin-workflows-executions-panel.tsx +170 -0
- package/src/components/admin/admin-workflows-status-badge.tsx +40 -0
- package/src/components/admin/admin-workflows.tsx +207 -0
- package/src/components/alepha-table/alepha-table.tsx +73 -17
- package/src/components/app-actions/__tests__/app-actions.browser.spec.tsx +79 -0
- package/src/components/app-actions/app-actions.tsx +113 -0
- package/src/components/app-shell/app-shell.tsx +122 -0
- package/src/components/auth/auth-reset-password.tsx +81 -8
- package/src/components/button-user/button-user.tsx +58 -1
- package/src/components/control/control.tsx +14 -71
- package/src/components/control-password/control-password.tsx +87 -0
- package/src/components/control-select/__tests__/control-select-clear-label.browser.spec.tsx +101 -0
- package/src/components/control-select/__tests__/control-select-deselect.browser.spec.tsx +126 -0
- package/src/components/control-select/__tests__/control-select-searchable.browser.spec.tsx +142 -0
- package/src/components/control-select/control-select.tsx +133 -111
- package/src/components/control-upload/control-upload.tsx +27 -1
- package/src/components/markdown-view/markdown-view.tsx +6 -0
- package/src/components/nav-shell/nav-page.tsx +3 -3
- package/src/components/settings/__tests__/settings-nav.browser.spec.tsx +123 -0
- package/src/components/settings/__tests__/settings-section.browser.spec.tsx +130 -0
- package/src/components/settings/settings-danger-section.tsx +59 -0
- package/src/components/settings/settings-layout.tsx +56 -0
- package/src/components/settings/settings-nav.tsx +131 -0
- package/src/components/settings/settings-row.tsx +81 -0
- package/src/components/settings/settings-section.tsx +64 -0
- package/src/components/ui/alert-dialog.tsx +1 -1
- package/src/components/ui/avatar.tsx +2 -0
- package/src/components/ui/breadcrumb.tsx +1 -1
- package/src/components/ui/calendar.tsx +11 -6
- package/src/components/ui/card.tsx +5 -5
- package/src/components/ui/carousel.tsx +4 -4
- package/src/components/ui/chart.tsx +2 -0
- package/src/components/ui/collapsible.tsx +2 -0
- package/src/components/ui/combobox.tsx +3 -1
- package/src/components/ui/context-menu.tsx +3 -3
- package/src/components/ui/dialog.tsx +6 -1
- package/src/components/ui/direction.tsx +2 -0
- package/src/components/ui/drawer.tsx +134 -37
- package/src/components/ui/dropdown-menu.tsx +13 -3
- package/src/components/ui/empty.tsx +4 -1
- package/src/components/ui/input-otp.tsx +2 -0
- package/src/components/ui/menubar.tsx +2 -2
- package/src/components/ui/pagination.tsx +2 -2
- package/src/components/ui/popover.tsx +2 -0
- package/src/components/ui/radio-group.tsx +2 -0
- package/src/components/ui/scroll-area.tsx +2 -0
- package/src/components/ui/select.tsx +1 -1
- package/src/components/ui/separator.tsx +2 -0
- package/src/components/ui/sheet.tsx +4 -1
- package/src/components/ui/sidebar.tsx +1 -1
- package/src/components/ui/spinner.tsx +1 -0
- package/src/components/ui/switch.tsx +2 -0
- package/src/components/ui/tabs.tsx +2 -0
- package/src/components/ui/toggle-group.tsx +2 -0
- package/src/components/ui/tooltip.tsx +2 -0
- package/src/lib/__tests__/rehype-safe-img.spec.ts +121 -0
- package/src/lib/i18n-fr.ts +68 -0
- package/src/lib/rehype-safe-img.ts +150 -0
- package/src/lib/resize-image.ts +135 -0
- package/src/styles.css +126 -1
- /package/src/components/{admin/admin-user-detail-provider-labels.ts → auth/provider-labels.ts} +0 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FormField,
|
|
3
|
+
formFieldAriaProps,
|
|
4
|
+
} from "@alepha/ui/components/control-base/form-field";
|
|
5
|
+
import type { IconComponent } from "@alepha/ui/components/control-base/icon-hint";
|
|
6
|
+
import { Button } from "@alepha/ui/components/ui/button";
|
|
7
|
+
import { Input } from "@alepha/ui/components/ui/input";
|
|
8
|
+
import { Eye, EyeOff } from "lucide-react";
|
|
9
|
+
import { type HTMLAttributes, useState } from "react";
|
|
10
|
+
|
|
11
|
+
export interface ControlPasswordProps {
|
|
12
|
+
id?: string;
|
|
13
|
+
name?: string;
|
|
14
|
+
label?: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
error?: string;
|
|
17
|
+
required?: boolean;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
autoComplete?: string;
|
|
20
|
+
autoFocus?: boolean;
|
|
21
|
+
minLength?: number;
|
|
22
|
+
maxLength?: number;
|
|
23
|
+
inputProps?: HTMLAttributes<HTMLElement>;
|
|
24
|
+
icon?: IconComponent;
|
|
25
|
+
value: string;
|
|
26
|
+
onChange: (v: string) => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Masked password field with a reveal toggle.
|
|
31
|
+
*
|
|
32
|
+
* Lives outside `Control` so a hand-built form — a dialog that is not driven by
|
|
33
|
+
* a schema — gets the same field the schema path renders, rather than pairing a
|
|
34
|
+
* bare `<Input type="password">` with no way to check what was typed.
|
|
35
|
+
*/
|
|
36
|
+
export const ControlPassword = (props: ControlPasswordProps) => {
|
|
37
|
+
const [reveal, setReveal] = useState(false);
|
|
38
|
+
const Icon = props.icon;
|
|
39
|
+
return (
|
|
40
|
+
<FormField
|
|
41
|
+
id={props.id}
|
|
42
|
+
label={props.label}
|
|
43
|
+
description={props.description}
|
|
44
|
+
error={props.error}
|
|
45
|
+
required={props.required}
|
|
46
|
+
>
|
|
47
|
+
<div className="relative">
|
|
48
|
+
{Icon && (
|
|
49
|
+
<Icon className="text-muted-foreground absolute top-1/2 left-3 size-4 -translate-y-1/2 pointer-events-none" />
|
|
50
|
+
)}
|
|
51
|
+
<Input
|
|
52
|
+
{...props.inputProps}
|
|
53
|
+
{...formFieldAriaProps({
|
|
54
|
+
id: props.id,
|
|
55
|
+
error: props.error,
|
|
56
|
+
description: props.description,
|
|
57
|
+
})}
|
|
58
|
+
id={props.id}
|
|
59
|
+
name={props.name}
|
|
60
|
+
type={reveal ? "text" : "password"}
|
|
61
|
+
autoComplete={props.autoComplete ?? "current-password"}
|
|
62
|
+
autoFocus={props.autoFocus}
|
|
63
|
+
disabled={props.disabled}
|
|
64
|
+
required={props.required}
|
|
65
|
+
minLength={props.minLength}
|
|
66
|
+
maxLength={props.maxLength}
|
|
67
|
+
value={props.value}
|
|
68
|
+
onChange={(e) => props.onChange(e.target.value)}
|
|
69
|
+
className={[props.inputProps?.className, Icon ? "pr-9 pl-9" : "pr-9"]
|
|
70
|
+
.filter(Boolean)
|
|
71
|
+
.join(" ")}
|
|
72
|
+
/>
|
|
73
|
+
<Button
|
|
74
|
+
type="button"
|
|
75
|
+
variant="ghost"
|
|
76
|
+
size="icon"
|
|
77
|
+
disabled={props.disabled}
|
|
78
|
+
onClick={() => setReveal((r) => !r)}
|
|
79
|
+
aria-label={reveal ? "Hide password" : "Show password"}
|
|
80
|
+
className="text-muted-foreground hover:text-foreground absolute top-1/2 right-1 size-7 -translate-y-1/2"
|
|
81
|
+
>
|
|
82
|
+
{reveal ? <EyeOff className="size-4" /> : <Eye className="size-4" />}
|
|
83
|
+
</Button>
|
|
84
|
+
</div>
|
|
85
|
+
</FormField>
|
|
86
|
+
);
|
|
87
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { render } from "@testing-library/react";
|
|
2
|
+
import { Alepha, z } from "alepha";
|
|
3
|
+
import { AlephaLogger } from "alepha/logger";
|
|
4
|
+
import { AlephaContext } from "alepha/react";
|
|
5
|
+
import { useForm } from "alepha/react/form";
|
|
6
|
+
import { AlephaReactI18n } from "alepha/react/i18n";
|
|
7
|
+
import type { ReactNode } from "react";
|
|
8
|
+
import { describe, expect, it } from "vitest";
|
|
9
|
+
import { ControlSelect } from "../control-select.tsx";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `ControlSelect` picks between a native `Select` and a `Combobox` on option
|
|
13
|
+
* count (>20 → combobox). The two paths disagreed about what the `clearable`
|
|
14
|
+
* row's label *is*: the Select path made it a genuine selected value, the
|
|
15
|
+
* combobox passed it through as a placeholder and muted the trigger. So the
|
|
16
|
+
* same filter, meaning the same thing, changed colour the day its option list
|
|
17
|
+
* grew past 20 — which is exactly what happened to the quests board, where
|
|
18
|
+
* "All status" and "All areas" read as set and "All tags" read as unset.
|
|
19
|
+
*
|
|
20
|
+
* These assert on the render path rather than the colour of one of them: what
|
|
21
|
+
* matters is that the two agree, so a future change to either has to move both.
|
|
22
|
+
*/
|
|
23
|
+
describe("ControlSelect clear label", () => {
|
|
24
|
+
const mount = (alepha: Alepha, ui: ReactNode) =>
|
|
25
|
+
render(
|
|
26
|
+
<AlephaContext.Provider value={alepha}>{ui}</AlephaContext.Provider>,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const start = async () => {
|
|
30
|
+
const alepha = Alepha.create().with(AlephaLogger).with(AlephaReactI18n);
|
|
31
|
+
await alepha.start();
|
|
32
|
+
return alepha;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const items = (count: number) =>
|
|
36
|
+
Array.from({ length: count }, (_, i) => `tag-${i}`);
|
|
37
|
+
|
|
38
|
+
const Probe = (props: { count: number; clearable?: boolean }) => {
|
|
39
|
+
const form = useForm({
|
|
40
|
+
schema: z.object({ tag: z.text().optional() as never }),
|
|
41
|
+
handler: () => {},
|
|
42
|
+
});
|
|
43
|
+
return (
|
|
44
|
+
<ControlSelect
|
|
45
|
+
input={form.input.tag}
|
|
46
|
+
label="Tag"
|
|
47
|
+
clearable={props.clearable}
|
|
48
|
+
clearLabel="All tags"
|
|
49
|
+
items={items(props.count)}
|
|
50
|
+
/>
|
|
51
|
+
);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Both paths render their trigger as the field's only `combobox` role — the
|
|
56
|
+
* shadcn `SelectTrigger` and the Base UI `ComboboxTrigger` alike — so one
|
|
57
|
+
* query reaches whichever one the option count selected.
|
|
58
|
+
*/
|
|
59
|
+
const trigger = (ui: ReturnType<typeof render>) =>
|
|
60
|
+
ui.getByRole("combobox") as HTMLElement;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* "Reads as greyed", asked in the one way that covers both paths. The two
|
|
64
|
+
* arrive there by different mechanisms and no stylesheet runs under jsdom to
|
|
65
|
+
* flatten them: the combobox trigger takes `text-muted-foreground` outright,
|
|
66
|
+
* while `SelectTrigger` always carries the conditional
|
|
67
|
+
* `data-placeholder:text-muted-foreground` and only greys once Base UI
|
|
68
|
+
* stamps `data-placeholder` on it — which it does exactly when `SelectValue`
|
|
69
|
+
* falls back to its placeholder instead of rendering a chosen value.
|
|
70
|
+
*/
|
|
71
|
+
const readsAsPlaceholder = (el: HTMLElement) =>
|
|
72
|
+
el.hasAttribute("data-placeholder") ||
|
|
73
|
+
el.className.split(/\s+/).includes("text-muted-foreground");
|
|
74
|
+
|
|
75
|
+
it("renders the clear label as a value on the Select path", async () => {
|
|
76
|
+
const alepha = await start();
|
|
77
|
+
const ui = mount(alepha, <Probe count={5} clearable />);
|
|
78
|
+
|
|
79
|
+
expect(trigger(ui).textContent).toContain("All tags");
|
|
80
|
+
expect(readsAsPlaceholder(trigger(ui))).toBe(false);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("renders the clear label the same way past the 20-option threshold", async () => {
|
|
84
|
+
const alepha = await start();
|
|
85
|
+
const ui = mount(alepha, <Probe count={25} clearable />);
|
|
86
|
+
|
|
87
|
+
expect(trigger(ui).textContent).toContain("All tags");
|
|
88
|
+
expect(readsAsPlaceholder(trigger(ui))).toBe(false);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("still reads as a placeholder when there is no clear row", async () => {
|
|
92
|
+
const alepha = await start();
|
|
93
|
+
|
|
94
|
+
const short = mount(alepha, <Probe count={5} />);
|
|
95
|
+
expect(readsAsPlaceholder(trigger(short))).toBe(true);
|
|
96
|
+
short.unmount();
|
|
97
|
+
|
|
98
|
+
const long = mount(alepha, <Probe count={25} />);
|
|
99
|
+
expect(readsAsPlaceholder(trigger(long))).toBe(true);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { fireEvent, render, waitFor } from "@testing-library/react";
|
|
2
|
+
import { Alepha, z } from "alepha";
|
|
3
|
+
import { AlephaLogger } from "alepha/logger";
|
|
4
|
+
import { AlephaContext } from "alepha/react";
|
|
5
|
+
import { type BaseInputField, useFieldValue, useForm } from "alepha/react/form";
|
|
6
|
+
import { AlephaReactI18n } from "alepha/react/i18n";
|
|
7
|
+
import type { ReactNode } from "react";
|
|
8
|
+
import { describe, expect, it } from "vitest";
|
|
9
|
+
import { ControlSelect } from "../control-select.tsx";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Base UI's single-select `Combobox` re-selects on every item press — it never
|
|
13
|
+
* emits `null` — so a searchable select had no way back to the empty state once
|
|
14
|
+
* a value was picked. The native-`Select` render path has always been able to
|
|
15
|
+
* (its `clearable` row), which made the gap invisible until a field crossed the
|
|
16
|
+
* option count that switches it to the combobox.
|
|
17
|
+
*/
|
|
18
|
+
describe("ControlSelect combobox deselection", () => {
|
|
19
|
+
const mount = (alepha: Alepha, ui: ReactNode) =>
|
|
20
|
+
render(
|
|
21
|
+
<AlephaContext.Provider value={alepha}>{ui}</AlephaContext.Provider>,
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
const start = async () => {
|
|
25
|
+
const alepha = Alepha.create().with(AlephaLogger).with(AlephaReactI18n);
|
|
26
|
+
await alepha.start();
|
|
27
|
+
return alepha;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const Probe = (props: {
|
|
31
|
+
required?: boolean;
|
|
32
|
+
clearable?: boolean;
|
|
33
|
+
clearLabel?: string;
|
|
34
|
+
}) => {
|
|
35
|
+
const form = useForm({
|
|
36
|
+
schema: z.object(
|
|
37
|
+
props.required
|
|
38
|
+
? { tag: z.text() }
|
|
39
|
+
: { tag: z.text().optional() as never },
|
|
40
|
+
),
|
|
41
|
+
handler: () => {},
|
|
42
|
+
});
|
|
43
|
+
return (
|
|
44
|
+
<>
|
|
45
|
+
<ControlSelect
|
|
46
|
+
input={form.input.tag}
|
|
47
|
+
label="Tag"
|
|
48
|
+
combobox
|
|
49
|
+
clearable={props.clearable}
|
|
50
|
+
clearLabel={props.clearLabel}
|
|
51
|
+
items={["docs", "react"]}
|
|
52
|
+
/>
|
|
53
|
+
<Reporter input={form.input.tag} />
|
|
54
|
+
</>
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Renders the bound value so the assertions read the form state rather than
|
|
60
|
+
* the trigger's label, which falls back to the placeholder and would pass for
|
|
61
|
+
* the wrong reason.
|
|
62
|
+
*/
|
|
63
|
+
const Reporter = (props: { input: BaseInputField }) => {
|
|
64
|
+
const [value] = useFieldValue(props.input);
|
|
65
|
+
return (
|
|
66
|
+
<span data-testid="value">
|
|
67
|
+
{value === undefined ? "∅" : String(value)}
|
|
68
|
+
</span>
|
|
69
|
+
);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const openPopup = (ui: ReturnType<typeof render>) => {
|
|
73
|
+
fireEvent.keyDown(ui.getByRole("combobox"), { key: "ArrowDown" });
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
it("offers the clearable row so a filter can go back to 'All …'", async () => {
|
|
77
|
+
const alepha = await start();
|
|
78
|
+
const ui = mount(alepha, <Probe clearable clearLabel="All zones" />);
|
|
79
|
+
|
|
80
|
+
openPopup(ui);
|
|
81
|
+
fireEvent.click(await ui.findByRole("option", { name: "docs" }));
|
|
82
|
+
await waitFor(() => {
|
|
83
|
+
expect(ui.getByTestId("value").textContent).toBe("docs");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
openPopup(ui);
|
|
87
|
+
fireEvent.click(await ui.findByRole("option", { name: "All zones" }));
|
|
88
|
+
await waitFor(() => {
|
|
89
|
+
expect(ui.getByTestId("value").textContent).toBe("∅");
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("clears an optional field when the selected row is pressed again", async () => {
|
|
94
|
+
const alepha = await start();
|
|
95
|
+
const ui = mount(alepha, <Probe />);
|
|
96
|
+
|
|
97
|
+
openPopup(ui);
|
|
98
|
+
fireEvent.click(await ui.findByRole("option", { name: "docs" }));
|
|
99
|
+
await waitFor(() => {
|
|
100
|
+
expect(ui.getByTestId("value").textContent).toBe("docs");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
openPopup(ui);
|
|
104
|
+
fireEvent.click(await ui.findByRole("option", { name: "docs" }));
|
|
105
|
+
await waitFor(() => {
|
|
106
|
+
expect(ui.getByTestId("value").textContent).toBe("∅");
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("keeps the value on a required field", async () => {
|
|
111
|
+
const alepha = await start();
|
|
112
|
+
const ui = mount(alepha, <Probe required />);
|
|
113
|
+
|
|
114
|
+
openPopup(ui);
|
|
115
|
+
fireEvent.click(await ui.findByRole("option", { name: "docs" }));
|
|
116
|
+
await waitFor(() => {
|
|
117
|
+
expect(ui.getByTestId("value").textContent).toBe("docs");
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
openPopup(ui);
|
|
121
|
+
fireEvent.click(await ui.findByRole("option", { name: "docs" }));
|
|
122
|
+
await waitFor(() => {
|
|
123
|
+
expect(ui.getByTestId("value").textContent).toBe("docs");
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { fireEvent, render, waitFor } from "@testing-library/react";
|
|
2
|
+
import { Alepha, z } from "alepha";
|
|
3
|
+
import { AlephaLogger } from "alepha/logger";
|
|
4
|
+
import { AlephaContext } from "alepha/react";
|
|
5
|
+
import { type BaseInputField, useFieldValue, useForm } from "alepha/react/form";
|
|
6
|
+
import { AlephaReactI18n } from "alepha/react/i18n";
|
|
7
|
+
import type { ReactNode } from "react";
|
|
8
|
+
import { describe, expect, it } from "vitest";
|
|
9
|
+
import { ControlSelect } from "../control-select.tsx";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* ControlSelect used to pick a *primitive* by option count: a native `Select`
|
|
13
|
+
* below ~20 options, a `Combobox` above. That made per-option `description`,
|
|
14
|
+
* `tag` and `disabled` — plus `triggerClassName` and deselection — appear or
|
|
15
|
+
* vanish depending on how many siblings an option happened to have. Now a
|
|
16
|
+
* single Combobox renders every list and the count only decides whether the
|
|
17
|
+
* popup carries a search field.
|
|
18
|
+
*/
|
|
19
|
+
describe("ControlSelect searchable", () => {
|
|
20
|
+
const mount = (alepha: Alepha, ui: ReactNode) =>
|
|
21
|
+
render(
|
|
22
|
+
<AlephaContext.Provider value={alepha}>{ui}</AlephaContext.Provider>,
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const start = async () => {
|
|
26
|
+
const alepha = Alepha.create().with(AlephaLogger).with(AlephaReactI18n);
|
|
27
|
+
await alepha.start();
|
|
28
|
+
return alepha;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const SHORT = [
|
|
32
|
+
{ value: "user", label: "User", description: "Everyone", disabled: true },
|
|
33
|
+
{ value: "editor", label: "Editor", tag: "write" },
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
const LONG = Array.from({ length: 30 }, (_, i) => `opt-${i}`);
|
|
37
|
+
|
|
38
|
+
const Probe = (props: {
|
|
39
|
+
searchable?: boolean;
|
|
40
|
+
items?: (typeof SHORT)[number][] | string[];
|
|
41
|
+
}) => {
|
|
42
|
+
const form = useForm({
|
|
43
|
+
schema: z.object({ role: z.text().optional() as never }),
|
|
44
|
+
handler: () => {},
|
|
45
|
+
});
|
|
46
|
+
return (
|
|
47
|
+
<>
|
|
48
|
+
<ControlSelect
|
|
49
|
+
input={form.input.role}
|
|
50
|
+
label="Role"
|
|
51
|
+
searchable={props.searchable}
|
|
52
|
+
items={(props.items ?? SHORT) as never}
|
|
53
|
+
/>
|
|
54
|
+
<Reporter input={form.input.role} />
|
|
55
|
+
</>
|
|
56
|
+
);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Reads the bound value rather than the trigger label, which falls back to
|
|
61
|
+
* the placeholder and would pass for the wrong reason.
|
|
62
|
+
*/
|
|
63
|
+
const Reporter = (props: { input: BaseInputField }) => {
|
|
64
|
+
const [value] = useFieldValue(props.input);
|
|
65
|
+
return (
|
|
66
|
+
<span data-testid="value">
|
|
67
|
+
{value === undefined ? "∅" : String(value)}
|
|
68
|
+
</span>
|
|
69
|
+
);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const openPopup = (ui: ReturnType<typeof render>) => {
|
|
73
|
+
fireEvent.keyDown(ui.getByRole("combobox"), { key: "ArrowDown" });
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const searchInput = (ui: ReturnType<typeof render>) =>
|
|
77
|
+
ui.queryByPlaceholderText("Search…");
|
|
78
|
+
|
|
79
|
+
it("omits the search field on a short list", async () => {
|
|
80
|
+
const alepha = await start();
|
|
81
|
+
const ui = mount(alepha, <Probe />);
|
|
82
|
+
|
|
83
|
+
openPopup(ui);
|
|
84
|
+
await ui.findByRole("option", { name: /Editor/ });
|
|
85
|
+
expect(searchInput(ui)).toBeNull();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("adds the search field above the threshold", async () => {
|
|
89
|
+
const alepha = await start();
|
|
90
|
+
const ui = mount(alepha, <Probe items={LONG} />);
|
|
91
|
+
|
|
92
|
+
openPopup(ui);
|
|
93
|
+
await ui.findByRole("option", { name: "opt-0" });
|
|
94
|
+
expect(searchInput(ui)).not.toBeNull();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("honors an explicit `searchable` in both directions", async () => {
|
|
98
|
+
const alepha = await start();
|
|
99
|
+
|
|
100
|
+
const forcedOn = mount(alepha, <Probe searchable />);
|
|
101
|
+
openPopup(forcedOn);
|
|
102
|
+
await forcedOn.findByRole("option", { name: /Editor/ });
|
|
103
|
+
expect(forcedOn.queryByPlaceholderText("Search…")).not.toBeNull();
|
|
104
|
+
forcedOn.unmount();
|
|
105
|
+
|
|
106
|
+
const forcedOff = mount(alepha, <Probe searchable={false} items={LONG} />);
|
|
107
|
+
openPopup(forcedOff);
|
|
108
|
+
await forcedOff.findByRole("option", { name: "opt-0" });
|
|
109
|
+
expect(forcedOff.queryByPlaceholderText("Search…")).toBeNull();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("still renders description and tag on a short list", async () => {
|
|
113
|
+
const alepha = await start();
|
|
114
|
+
const ui = mount(alepha, <Probe />);
|
|
115
|
+
|
|
116
|
+
openPopup(ui);
|
|
117
|
+
// Both were silently dropped by the native-`Select` path.
|
|
118
|
+
expect(await ui.findByText("Everyone")).toBeTruthy();
|
|
119
|
+
expect(await ui.findByText("write")).toBeTruthy();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("selects without a search field, and refuses a disabled row", async () => {
|
|
123
|
+
const alepha = await start();
|
|
124
|
+
const ui = mount(alepha, <Probe />);
|
|
125
|
+
|
|
126
|
+
openPopup(ui);
|
|
127
|
+
fireEvent.click(await ui.findByRole("option", { name: /Editor/ }));
|
|
128
|
+
await waitFor(() => {
|
|
129
|
+
expect(ui.getByTestId("value").textContent).toBe("editor");
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Pressed second, so "still editor" proves the row is inert rather than
|
|
133
|
+
// matching the state the field started in. `disabled` reached the row only
|
|
134
|
+
// on the combobox path, so a mandatory option used to be selectable as
|
|
135
|
+
// soon as its list was short enough.
|
|
136
|
+
openPopup(ui);
|
|
137
|
+
fireEvent.click(await ui.findByRole("option", { name: /User/ }));
|
|
138
|
+
await waitFor(() => {
|
|
139
|
+
expect(ui.getByTestId("value").textContent).toBe("editor");
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
});
|