@slipstream-ui/core 0.3.0 → 0.5.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/dist/index.d.mts +957 -56
- package/dist/index.d.ts +957 -56
- package/dist/index.js +2554 -777
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2524 -777
- package/dist/index.mjs.map +1 -1
- package/dist/manifests/app-shell.manifest.json +206 -0
- package/dist/manifests/auth-layout.manifest.json +99 -0
- package/dist/manifests/autocomplete.manifest.json +53 -5
- package/dist/manifests/badge.manifest.json +20 -3
- package/dist/manifests/breadcrumb.manifest.json +6 -0
- package/dist/manifests/button.manifest.json +20 -2
- package/dist/manifests/card.manifest.json +53 -6
- package/dist/manifests/confirm-dialog.manifest.json +132 -0
- package/dist/manifests/data-table.manifest.json +50 -0
- package/dist/manifests/date-picker.manifest.json +87 -0
- package/dist/manifests/dialog.manifest.json +56 -4
- package/dist/manifests/drawer.manifest.json +25 -4
- package/dist/manifests/empty-state.manifest.json +108 -0
- package/dist/manifests/file-upload.manifest.json +103 -0
- package/dist/manifests/filter-bar.manifest.json +100 -0
- package/dist/manifests/link.manifest.json +106 -0
- package/dist/manifests/menu.manifest.json +13 -1
- package/dist/manifests/metric-card.manifest.json +105 -0
- package/dist/manifests/page-header.manifest.json +124 -0
- package/dist/manifests/pagination.manifest.json +68 -34
- package/dist/manifests/password-input.manifest.json +75 -0
- package/dist/manifests/prose.manifest.json +75 -0
- package/dist/manifests/search.manifest.json +41 -9
- package/dist/manifests/section-heading.manifest.json +108 -0
- package/dist/manifests/select-menu.manifest.json +142 -0
- package/dist/manifests/select.manifest.json +55 -12
- package/dist/manifests/site-footer.manifest.json +94 -0
- package/dist/manifests/site-header.manifest.json +130 -0
- package/dist/manifests/stepper.manifest.json +95 -0
- package/dist/manifests/table-card.manifest.json +123 -0
- package/dist/manifests/table-of-contents.manifest.json +87 -0
- package/dist/manifests/time-picker.manifest.json +86 -0
- package/dist/manifests/timeline.manifest.json +69 -0
- package/dist/manifests/toast.manifest.json +101 -25
- package/package.json +2 -2
- package/src/components/alert/Alert.tsx +2 -2
- package/src/components/app-shell/AppShell.test.tsx +148 -0
- package/src/components/app-shell/AppShell.tsx +266 -0
- package/src/components/app-shell/index.ts +1 -0
- package/src/components/auth-layout/AuthLayout.test.tsx +34 -0
- package/src/components/auth-layout/AuthLayout.tsx +47 -0
- package/src/components/auth-layout/index.ts +1 -0
- package/src/components/autocomplete/Autocomplete.test.tsx +53 -0
- package/src/components/autocomplete/Autocomplete.tsx +160 -41
- package/src/components/badge/Badge.test.tsx +35 -0
- package/src/components/badge/Badge.tsx +16 -4
- package/src/components/breadcrumb/Breadcrumb.test.tsx +16 -0
- package/src/components/breadcrumb/Breadcrumb.tsx +10 -5
- package/src/components/button/Button.test.tsx +67 -0
- package/src/components/button/Button.tsx +37 -3
- package/src/components/button-group/ButtonGroup.test.tsx +6 -8
- package/src/components/button-group/ButtonGroup.tsx +17 -9
- package/src/components/card/Card.test.tsx +14 -0
- package/src/components/card/Card.tsx +15 -8
- package/src/components/confirm-dialog/ConfirmDialog.test.tsx +46 -0
- package/src/components/confirm-dialog/ConfirmDialog.tsx +86 -0
- package/src/components/confirm-dialog/index.ts +1 -0
- package/src/components/data-table/DataTable.test.tsx +92 -0
- package/src/components/data-table/DataTable.tsx +98 -7
- package/src/components/date-picker/DatePicker.test.tsx +30 -0
- package/src/components/date-picker/DatePicker.tsx +57 -0
- package/src/components/date-picker/index.ts +1 -0
- package/src/components/dialog/Dialog.test.tsx +71 -1
- package/src/components/dialog/Dialog.tsx +116 -23
- package/src/components/dialog/index.ts +1 -2
- package/src/components/drawer/Drawer.test.tsx +59 -0
- package/src/components/drawer/Drawer.tsx +49 -8
- package/src/components/empty-state/EmptyState.test.tsx +30 -0
- package/src/components/empty-state/EmptyState.tsx +48 -0
- package/src/components/empty-state/index.ts +1 -0
- package/src/components/file-upload/FileUpload.test.tsx +43 -0
- package/src/components/file-upload/FileUpload.tsx +143 -0
- package/src/components/file-upload/index.ts +1 -0
- package/src/components/filter-bar/FilterBar.test.tsx +32 -0
- package/src/components/filter-bar/FilterBar.tsx +71 -0
- package/src/components/filter-bar/index.ts +1 -0
- package/src/components/input/Input.tsx +1 -0
- package/src/components/input-group/InputGroup.test.tsx +6 -3
- package/src/components/input-group/InputGroup.tsx +9 -6
- package/src/components/link/Link.test.tsx +60 -0
- package/src/components/link/Link.tsx +85 -0
- package/src/components/link/index.ts +1 -0
- package/src/components/menu/Menu.test.tsx +21 -0
- package/src/components/menu/Menu.tsx +8 -3
- package/src/components/metric-card/MetricCard.test.tsx +28 -0
- package/src/components/metric-card/MetricCard.tsx +54 -0
- package/src/components/metric-card/index.ts +1 -0
- package/src/components/page-header/PageHeader.test.tsx +48 -0
- package/src/components/page-header/PageHeader.tsx +53 -0
- package/src/components/page-header/index.ts +1 -0
- package/src/components/pagination/Pagination.test.tsx +33 -0
- package/src/components/pagination/Pagination.tsx +69 -18
- package/src/components/password-input/PasswordInput.test.tsx +29 -0
- package/src/components/password-input/PasswordInput.tsx +41 -0
- package/src/components/password-input/index.ts +1 -0
- package/src/components/prose/Prose.test.tsx +35 -0
- package/src/components/prose/Prose.tsx +61 -0
- package/src/components/prose/index.ts +1 -0
- package/src/components/search/Search.test.tsx +16 -0
- package/src/components/search/Search.tsx +11 -2
- package/src/components/section-heading/SectionHeading.test.tsx +32 -0
- package/src/components/section-heading/SectionHeading.tsx +54 -0
- package/src/components/section-heading/index.ts +1 -0
- package/src/components/select/Select.test.tsx +40 -1
- package/src/components/select/Select.tsx +54 -14
- package/src/components/select/index.ts +1 -1
- package/src/components/select-menu/SelectMenu.test.tsx +105 -0
- package/src/components/select-menu/SelectMenu.tsx +368 -0
- package/src/components/select-menu/index.ts +1 -0
- package/src/components/site-footer/SiteFooter.test.tsx +30 -0
- package/src/components/site-footer/SiteFooter.tsx +79 -0
- package/src/components/site-footer/index.ts +1 -0
- package/src/components/site-header/SiteHeader.test.tsx +53 -0
- package/src/components/site-header/SiteHeader.tsx +124 -0
- package/src/components/site-header/index.ts +1 -0
- package/src/components/stepper/Stepper.test.tsx +35 -0
- package/src/components/stepper/Stepper.tsx +91 -0
- package/src/components/stepper/index.ts +1 -0
- package/src/components/switch/Switch.test.tsx +10 -0
- package/src/components/switch/Switch.tsx +8 -3
- package/src/components/table-card/TableCard.test.tsx +59 -0
- package/src/components/table-card/TableCard.tsx +48 -0
- package/src/components/table-card/index.ts +1 -0
- package/src/components/table-of-contents/TableOfContents.test.tsx +31 -0
- package/src/components/table-of-contents/TableOfContents.tsx +100 -0
- package/src/components/table-of-contents/index.ts +1 -0
- package/src/components/textarea/Textarea.tsx +1 -0
- package/src/components/time-picker/TimePicker.test.tsx +30 -0
- package/src/components/time-picker/TimePicker.tsx +56 -0
- package/src/components/time-picker/index.ts +1 -0
- package/src/components/timeline/Timeline.test.tsx +26 -0
- package/src/components/timeline/Timeline.tsx +53 -0
- package/src/components/timeline/index.ts +1 -0
- package/src/components/toast/Toast.test.tsx +5 -0
- package/src/components/toast/Toast.tsx +2 -16
- package/src/components/toast/Toaster.test.tsx +84 -0
- package/src/components/toast/Toaster.tsx +116 -0
- package/src/components/toast/index.ts +4 -0
- package/src/components/toast/toast-store.ts +117 -0
- package/src/index.ts +21 -0
- package/src/manifests/app-shell.manifest.json +206 -0
- package/src/manifests/auth-layout.manifest.json +99 -0
- package/src/manifests/autocomplete.manifest.json +53 -5
- package/src/manifests/badge.manifest.json +20 -3
- package/src/manifests/breadcrumb.manifest.json +6 -0
- package/src/manifests/button.manifest.json +20 -2
- package/src/manifests/card.manifest.json +53 -6
- package/src/manifests/confirm-dialog.manifest.json +132 -0
- package/src/manifests/data-table.manifest.json +50 -0
- package/src/manifests/date-picker.manifest.json +87 -0
- package/src/manifests/dialog.manifest.json +56 -4
- package/src/manifests/drawer.manifest.json +25 -4
- package/src/manifests/empty-state.manifest.json +108 -0
- package/src/manifests/file-upload.manifest.json +103 -0
- package/src/manifests/filter-bar.manifest.json +100 -0
- package/src/manifests/link.manifest.json +106 -0
- package/src/manifests/menu.manifest.json +13 -1
- package/src/manifests/metric-card.manifest.json +105 -0
- package/src/manifests/page-header.manifest.json +124 -0
- package/src/manifests/pagination.manifest.json +68 -34
- package/src/manifests/password-input.manifest.json +75 -0
- package/src/manifests/prose.manifest.json +75 -0
- package/src/manifests/search.manifest.json +41 -9
- package/src/manifests/section-heading.manifest.json +108 -0
- package/src/manifests/select-menu.manifest.json +142 -0
- package/src/manifests/select.manifest.json +55 -12
- package/src/manifests/site-footer.manifest.json +94 -0
- package/src/manifests/site-header.manifest.json +130 -0
- package/src/manifests/stepper.manifest.json +95 -0
- package/src/manifests/table-card.manifest.json +123 -0
- package/src/manifests/table-of-contents.manifest.json +87 -0
- package/src/manifests/time-picker.manifest.json +86 -0
- package/src/manifests/timeline.manifest.json +69 -0
- package/src/manifests/toast.manifest.json +101 -25
- package/src/test/tailwind-v4.test.ts +103 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { cn } from '../../utils/cn';
|
|
3
|
+
|
|
4
|
+
export type AuthLayoutProps = React.HTMLAttributes<HTMLDivElement> & {
|
|
5
|
+
title: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
/** Brand/mark above the card. */
|
|
8
|
+
brand?: React.ReactNode;
|
|
9
|
+
/** Links or legal copy under the card. */
|
|
10
|
+
footer?: React.ReactNode;
|
|
11
|
+
/** Rendered inside the card under the heading. */
|
|
12
|
+
children?: React.ReactNode;
|
|
13
|
+
/** Card width preset (default 'md'). */
|
|
14
|
+
size?: 'sm' | 'md' | 'lg';
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const sizeClasses: Record<NonNullable<AuthLayoutProps['size']>, string> = {
|
|
18
|
+
sm: 'max-w-sm',
|
|
19
|
+
md: 'max-w-md',
|
|
20
|
+
lg: 'max-w-lg',
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Centered auth surface: brand mark, title/description, the form card and a
|
|
25
|
+
* footer slot. Keeps sign-in, sign-up, reset and verify screens visually
|
|
26
|
+
* identical without each page re-deriving the layout.
|
|
27
|
+
*/
|
|
28
|
+
export const AuthLayout = React.forwardRef<HTMLDivElement, AuthLayoutProps>(function AuthLayout(
|
|
29
|
+
{ title, description, brand, footer, children, size = 'md', className, ...props },
|
|
30
|
+
ref
|
|
31
|
+
) {
|
|
32
|
+
return (
|
|
33
|
+
<div
|
|
34
|
+
ref={ref}
|
|
35
|
+
className={cn('flex min-h-screen flex-col items-center justify-center bg-muted/30 px-4 py-12', className)}
|
|
36
|
+
{...props}
|
|
37
|
+
>
|
|
38
|
+
{brand && <div className="mb-6">{brand}</div>}
|
|
39
|
+
<div className={cn('w-full rounded-xl border border-border bg-card p-6 shadow-sm sm:p-8', sizeClasses[size])}>
|
|
40
|
+
<h1 className="text-xl font-semibold tracking-tight">{title}</h1>
|
|
41
|
+
{description && <p className="mt-1 text-sm text-muted-foreground">{description}</p>}
|
|
42
|
+
<div className="mt-6">{children}</div>
|
|
43
|
+
</div>
|
|
44
|
+
{footer && <div className="mt-6 text-center text-sm text-muted-foreground">{footer}</div>}
|
|
45
|
+
</div>
|
|
46
|
+
);
|
|
47
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './AuthLayout';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { fireEvent, render, screen } from '@testing-library/react';
|
|
2
|
+
import userEvent from '@testing-library/user-event';
|
|
2
3
|
import { axe } from 'jest-axe';
|
|
3
4
|
import { describe, expect, it, vi } from 'vitest';
|
|
4
5
|
import { Autocomplete } from './Autocomplete';
|
|
@@ -75,3 +76,55 @@ describe('Autocomplete', () => {
|
|
|
75
76
|
expect(await axe(container)).toHaveNoViolations();
|
|
76
77
|
});
|
|
77
78
|
});
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
describe('Autocomplete options, groups, async and clear', () => {
|
|
82
|
+
it('renders grouped headings and descriptions', async () => {
|
|
83
|
+
const user = userEvent.setup();
|
|
84
|
+
render(
|
|
85
|
+
<Autocomplete
|
|
86
|
+
aria-label="Search"
|
|
87
|
+
options={[
|
|
88
|
+
{ value: 'ada', label: 'Ada Lovelace', group: 'People', description: 'Mathematician' },
|
|
89
|
+
{ value: 'alan', label: 'Alan Turing', group: 'People' },
|
|
90
|
+
{ value: 'rust', label: 'Rust', group: 'Languages' },
|
|
91
|
+
]}
|
|
92
|
+
/>
|
|
93
|
+
);
|
|
94
|
+
await user.click(screen.getByRole('combobox', { name: 'Search' }));
|
|
95
|
+
expect(screen.getByText('People')).toBeInTheDocument();
|
|
96
|
+
expect(screen.getByText('Languages')).toBeInTheDocument();
|
|
97
|
+
expect(screen.getByText('Mathematician')).toBeInTheDocument();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('uses a custom row renderer', async () => {
|
|
101
|
+
const user = userEvent.setup();
|
|
102
|
+
render(
|
|
103
|
+
<Autocomplete
|
|
104
|
+
aria-label="Search"
|
|
105
|
+
options={[{ value: 'x', label: 'Custom row' }]}
|
|
106
|
+
renderOption={(option) => <span>★ {option.label}</span>}
|
|
107
|
+
/>
|
|
108
|
+
);
|
|
109
|
+
await user.click(screen.getByRole('combobox', { name: 'Search' }));
|
|
110
|
+
expect(screen.getByText('★ Custom row')).toBeInTheDocument();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('loads options asynchronously with a loading row', async () => {
|
|
114
|
+
const user = userEvent.setup();
|
|
115
|
+
const loadOptions = vi.fn(async () => [{ value: 'remote', label: 'Remote result' }]);
|
|
116
|
+
render(<Autocomplete aria-label="Search" loadOptions={loadOptions} debounceMs={1} />);
|
|
117
|
+
await user.click(screen.getByRole('combobox', { name: 'Search' }));
|
|
118
|
+
expect(await screen.findByText('Remote result')).toBeInTheDocument();
|
|
119
|
+
expect(loadOptions).toHaveBeenCalled();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('clears the field with the clear button', async () => {
|
|
123
|
+
const user = userEvent.setup();
|
|
124
|
+
const onValueChange = vi.fn();
|
|
125
|
+
render(<Autocomplete aria-label="Search" clearable defaultValue="draft" onValueChange={onValueChange} />);
|
|
126
|
+
await user.click(screen.getByRole('button', { name: 'Clear input' }));
|
|
127
|
+
expect(onValueChange).toHaveBeenCalledWith('');
|
|
128
|
+
expect(screen.getByRole('combobox', { name: 'Search' })).toHaveValue('');
|
|
129
|
+
});
|
|
130
|
+
});
|
|
@@ -6,6 +6,16 @@ import { cn } from '../../utils/cn';
|
|
|
6
6
|
import { Input } from '../input/Input';
|
|
7
7
|
import { useCombobox } from '../search/use-combobox';
|
|
8
8
|
|
|
9
|
+
export type AutocompleteOption = {
|
|
10
|
+
value: string;
|
|
11
|
+
/** Display label; defaults to the value. */
|
|
12
|
+
label?: string;
|
|
13
|
+
/** Section heading the option renders under. */
|
|
14
|
+
group?: string;
|
|
15
|
+
/** Muted supporting line under the label. */
|
|
16
|
+
description?: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
9
19
|
export type AutocompleteProps = Omit<
|
|
10
20
|
React.InputHTMLAttributes<HTMLInputElement>,
|
|
11
21
|
'value' | 'defaultValue' | 'onChange' | 'children' | 'size'
|
|
@@ -14,28 +24,56 @@ export type AutocompleteProps = Omit<
|
|
|
14
24
|
value?: string;
|
|
15
25
|
/** Initial text when uncontrolled. */
|
|
16
26
|
defaultValue?: string;
|
|
17
|
-
/** Fires on every keystroke AND on suggestion selection
|
|
27
|
+
/** Fires on every keystroke AND on suggestion selection. */
|
|
18
28
|
onValueChange?: (value: string) => void;
|
|
19
|
-
/**
|
|
20
|
-
|
|
21
|
-
/**
|
|
29
|
+
/** Static suggestions (strings or option objects). */
|
|
30
|
+
options?: Array<string | AutocompleteOption>;
|
|
31
|
+
/** @deprecated Use `options`. */
|
|
32
|
+
suggestions?: string[];
|
|
33
|
+
/** Async source; called with the current query (debounced) when provided. */
|
|
34
|
+
loadOptions?: (query: string) => Promise<Array<string | AutocompleteOption>>;
|
|
35
|
+
/** Custom row rendering (receives the normalized option). */
|
|
36
|
+
renderOption?: (option: NormalizedOption, state: { active: boolean }) => React.ReactNode;
|
|
37
|
+
/** Show a clear button once there is text. */
|
|
38
|
+
clearable?: boolean;
|
|
39
|
+
/** Shown when no option matches. */
|
|
22
40
|
emptyText?: string;
|
|
41
|
+
/** Shown while async options load. */
|
|
42
|
+
loadingText?: string;
|
|
43
|
+
/** Debounce for `loadOptions`, in ms. Default 200. */
|
|
44
|
+
debounceMs?: number;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
type NormalizedOption = {
|
|
48
|
+
value: string;
|
|
49
|
+
label: string;
|
|
50
|
+
group?: string;
|
|
51
|
+
description?: string;
|
|
23
52
|
};
|
|
24
53
|
|
|
54
|
+
function normalize(entry: string | AutocompleteOption): NormalizedOption {
|
|
55
|
+
if (typeof entry === 'string') return { value: entry, label: entry };
|
|
56
|
+
return { value: entry.value, label: entry.label ?? entry.value, group: entry.group, description: entry.description };
|
|
57
|
+
}
|
|
58
|
+
|
|
25
59
|
/**
|
|
26
60
|
* Type-ahead input following the HTML autocomplete model: users may enter
|
|
27
|
-
* any value (free text is first-class), while matching
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* keep working alongside the custom listbox). The listbox portals with
|
|
31
|
-
* fixed positioning so scrollable containers never clip it.
|
|
61
|
+
* any value (free text is first-class), while matching options appear in a
|
|
62
|
+
* portalled listbox (grouped, optional async loading, custom rows).
|
|
63
|
+
* Arrow keys highlight, Enter accepts, Escape dismisses.
|
|
32
64
|
*/
|
|
33
65
|
export function Autocomplete({
|
|
34
66
|
value,
|
|
35
67
|
defaultValue = '',
|
|
36
68
|
onValueChange,
|
|
69
|
+
options,
|
|
37
70
|
suggestions,
|
|
71
|
+
loadOptions,
|
|
72
|
+
renderOption,
|
|
73
|
+
clearable = false,
|
|
38
74
|
emptyText,
|
|
75
|
+
loadingText,
|
|
76
|
+
debounceMs = 200,
|
|
39
77
|
placeholder,
|
|
40
78
|
id,
|
|
41
79
|
name,
|
|
@@ -47,31 +85,71 @@ export function Autocomplete({
|
|
|
47
85
|
const [internalValue, setInternalValue] = React.useState(defaultValue);
|
|
48
86
|
const current = value ?? internalValue;
|
|
49
87
|
|
|
88
|
+
const [asyncOptions, setAsyncOptions] = React.useState<NormalizedOption[]>([]);
|
|
89
|
+
const [loading, setLoading] = React.useState(false);
|
|
90
|
+
const requestId = React.useRef(0);
|
|
91
|
+
|
|
92
|
+
const source = React.useMemo<NormalizedOption[]>(() => {
|
|
93
|
+
const list = options ?? (suggestions ?? []).map(normalize);
|
|
94
|
+
return list.map(normalize);
|
|
95
|
+
}, [options, suggestions]);
|
|
96
|
+
|
|
97
|
+
// Debounced async source. Guarded by a request id so stale responses
|
|
98
|
+
// never overwrite newer ones.
|
|
99
|
+
React.useEffect(() => {
|
|
100
|
+
if (!loadOptions) return;
|
|
101
|
+
const id = ++requestId.current;
|
|
102
|
+
setLoading(true);
|
|
103
|
+
const timer = setTimeout(async () => {
|
|
104
|
+
try {
|
|
105
|
+
const result = await loadOptions(current);
|
|
106
|
+
if (requestId.current === id) setAsyncOptions(result.map(normalize));
|
|
107
|
+
} catch {
|
|
108
|
+
if (requestId.current === id) setAsyncOptions([]);
|
|
109
|
+
} finally {
|
|
110
|
+
if (requestId.current === id) setLoading(false);
|
|
111
|
+
}
|
|
112
|
+
}, debounceMs);
|
|
113
|
+
return () => clearTimeout(timer);
|
|
114
|
+
}, [loadOptions, current, debounceMs]);
|
|
115
|
+
|
|
116
|
+
const pool = loadOptions ? asyncOptions : source;
|
|
117
|
+
|
|
50
118
|
const items = React.useMemo(() => {
|
|
51
119
|
const q = current.trim().toLowerCase();
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return list
|
|
57
|
-
.filter((suggestion) => suggestion !== current)
|
|
58
|
-
.map((suggestion) => ({ value: suggestion, label: suggestion }));
|
|
59
|
-
}, [suggestions, current]);
|
|
120
|
+
return pool
|
|
121
|
+
.filter((option) => !q || option.label.toLowerCase().includes(q) || option.value.toLowerCase().includes(q))
|
|
122
|
+
.filter((option) => option.value !== current);
|
|
123
|
+
}, [pool, current]);
|
|
60
124
|
|
|
61
125
|
const combobox = useCombobox({
|
|
62
|
-
items,
|
|
126
|
+
items: items.map((item) => ({ value: item.value, label: item.label })),
|
|
63
127
|
onSelect: (item) => {
|
|
64
128
|
if (value === undefined) setInternalValue(item.label);
|
|
65
129
|
onValueChange?.(item.label);
|
|
66
130
|
},
|
|
67
131
|
});
|
|
68
132
|
|
|
69
|
-
const select = (
|
|
70
|
-
if (value === undefined) setInternalValue(label);
|
|
71
|
-
onValueChange?.(label);
|
|
133
|
+
const select = (option: NormalizedOption) => {
|
|
134
|
+
if (value === undefined) setInternalValue(option.label);
|
|
135
|
+
onValueChange?.(option.label);
|
|
72
136
|
combobox.setOpen(false);
|
|
73
137
|
};
|
|
74
138
|
|
|
139
|
+
// Render order with group headings derived from the option stream.
|
|
140
|
+
const grouped = React.useMemo(() => {
|
|
141
|
+
const out: Array<{ heading?: string; option?: NormalizedOption; index: number }> = [];
|
|
142
|
+
let lastGroup: string | undefined;
|
|
143
|
+
items.forEach((option, index) => {
|
|
144
|
+
if (option.group && option.group !== lastGroup) {
|
|
145
|
+
out.push({ heading: option.group, index: -1 });
|
|
146
|
+
lastGroup = option.group;
|
|
147
|
+
}
|
|
148
|
+
out.push({ option, index });
|
|
149
|
+
});
|
|
150
|
+
return out;
|
|
151
|
+
}, [items]);
|
|
152
|
+
|
|
75
153
|
return (
|
|
76
154
|
<div ref={combobox.anchorRef} className={cn('relative w-full', className)}>
|
|
77
155
|
<Input
|
|
@@ -80,6 +158,7 @@ export function Autocomplete({
|
|
|
80
158
|
placeholder={placeholder}
|
|
81
159
|
autoComplete="off"
|
|
82
160
|
value={current}
|
|
161
|
+
className={clearable && current ? 'pr-9' : undefined}
|
|
83
162
|
onChange={(event) => {
|
|
84
163
|
if (value === undefined) setInternalValue(event.target.value);
|
|
85
164
|
onValueChange?.(event.target.value);
|
|
@@ -96,12 +175,27 @@ export function Autocomplete({
|
|
|
96
175
|
{...inputProps}
|
|
97
176
|
{...combobox.inputProps}
|
|
98
177
|
/>
|
|
178
|
+
{clearable && current && (
|
|
179
|
+
<button
|
|
180
|
+
type="button"
|
|
181
|
+
onClick={() => {
|
|
182
|
+
if (value === undefined) setInternalValue('');
|
|
183
|
+
onValueChange?.('');
|
|
184
|
+
combobox.setOpen(true);
|
|
185
|
+
}}
|
|
186
|
+
aria-label="Clear input"
|
|
187
|
+
className="absolute right-2 top-1/2 -translate-y-1/2 rounded p-1 text-muted-foreground hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
|
188
|
+
>
|
|
189
|
+
✕
|
|
190
|
+
</button>
|
|
191
|
+
)}
|
|
99
192
|
{combobox.ready &&
|
|
100
193
|
createPortal(
|
|
101
194
|
<ul
|
|
102
195
|
id={combobox.listboxId}
|
|
103
196
|
role="listbox"
|
|
104
197
|
aria-label="Suggestions"
|
|
198
|
+
aria-busy={loading || undefined}
|
|
105
199
|
style={{
|
|
106
200
|
position: 'fixed',
|
|
107
201
|
top: combobox.pos!.top,
|
|
@@ -111,30 +205,55 @@ export function Autocomplete({
|
|
|
111
205
|
}}
|
|
112
206
|
className="max-h-60 overflow-y-auto rounded-md border border-border bg-popover p-1 text-popover-foreground shadow-md"
|
|
113
207
|
>
|
|
114
|
-
{items.length === 0 ? (
|
|
208
|
+
{loading && items.length === 0 ? (
|
|
209
|
+
<li role="presentation" className="px-3 py-2 text-sm text-muted-foreground">
|
|
210
|
+
{loadingText ?? 'Loading…'}
|
|
211
|
+
</li>
|
|
212
|
+
) : items.length === 0 ? (
|
|
115
213
|
<li role="presentation" className="px-3 py-2 text-sm text-muted-foreground">
|
|
116
214
|
{emptyText ?? 'No suggestions'}
|
|
117
215
|
</li>
|
|
118
216
|
) : (
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
217
|
+
grouped.map((entry, rowIndex) =>
|
|
218
|
+
entry.heading ? (
|
|
219
|
+
<li
|
|
220
|
+
key={`group-${entry.heading}-${rowIndex}`}
|
|
221
|
+
role="presentation"
|
|
222
|
+
className="px-3 pb-1 pt-2 text-xs font-medium uppercase tracking-wide text-muted-foreground"
|
|
223
|
+
>
|
|
224
|
+
{entry.heading}
|
|
225
|
+
</li>
|
|
226
|
+
) : (
|
|
227
|
+
<li
|
|
228
|
+
key={entry.option!.value}
|
|
229
|
+
id={combobox.optionId(entry.index)}
|
|
230
|
+
role="option"
|
|
231
|
+
aria-selected={combobox.activeIndex === entry.index}
|
|
232
|
+
onMouseDown={(event) => event.preventDefault()}
|
|
233
|
+
onMouseEnter={() => combobox.setActiveIndex(entry.index)}
|
|
234
|
+
onClick={() => select(entry.option!)}
|
|
235
|
+
className={cn(
|
|
236
|
+
'cursor-pointer rounded-sm px-3 py-1.5 text-sm outline-none',
|
|
237
|
+
combobox.activeIndex === entry.index
|
|
238
|
+
? 'bg-accent text-accent-foreground'
|
|
239
|
+
: 'hover:bg-accent/60'
|
|
240
|
+
)}
|
|
241
|
+
>
|
|
242
|
+
{renderOption ? (
|
|
243
|
+
renderOption(entry.option!, { active: combobox.activeIndex === entry.index })
|
|
244
|
+
) : (
|
|
245
|
+
<>
|
|
246
|
+
<span className="block">{entry.option!.label}</span>
|
|
247
|
+
{entry.option!.description && (
|
|
248
|
+
<span className="mt-0.5 block text-xs text-muted-foreground">
|
|
249
|
+
{entry.option!.description}
|
|
250
|
+
</span>
|
|
251
|
+
)}
|
|
252
|
+
</>
|
|
253
|
+
)}
|
|
254
|
+
</li>
|
|
255
|
+
)
|
|
256
|
+
)
|
|
138
257
|
)}
|
|
139
258
|
</ul>,
|
|
140
259
|
document.body
|
|
@@ -58,3 +58,38 @@ describe('Badge variants and sizing escape hatches', () => {
|
|
|
58
58
|
expect(container.firstChild).toHaveClass('text-foreground', 'gap-1', 'px-3');
|
|
59
59
|
});
|
|
60
60
|
});
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
describe('Badge asChild', () => {
|
|
64
|
+
it('renders a link with badge styling', () => {
|
|
65
|
+
render(
|
|
66
|
+
<Badge asChild>
|
|
67
|
+
<a href="/tags/react">React</a>
|
|
68
|
+
</Badge>
|
|
69
|
+
);
|
|
70
|
+
const link = screen.getByRole('link', { name: 'React' });
|
|
71
|
+
expect(link.tagName).toBe('A');
|
|
72
|
+
expect(link).toHaveClass('inline-flex', 'bg-primary');
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
describe('Badge semantic and alias variants', () => {
|
|
78
|
+
it('renders success, warning, info, primary and danger surfaces', () => {
|
|
79
|
+
const { container } = render(
|
|
80
|
+
<>
|
|
81
|
+
<Badge variant="success">ok</Badge>
|
|
82
|
+
<Badge variant="warning">warn</Badge>
|
|
83
|
+
<Badge variant="info">info</Badge>
|
|
84
|
+
<Badge variant="primary">primary</Badge>
|
|
85
|
+
<Badge variant="danger">danger</Badge>
|
|
86
|
+
</>
|
|
87
|
+
);
|
|
88
|
+
expect(screen.getByText('ok')).toHaveClass('bg-success');
|
|
89
|
+
expect(screen.getByText('warn')).toHaveClass('bg-warning');
|
|
90
|
+
expect(screen.getByText('info')).toHaveClass('bg-info');
|
|
91
|
+
expect(screen.getByText('primary')).toHaveClass('bg-primary');
|
|
92
|
+
expect(screen.getByText('danger')).toHaveClass('bg-destructive');
|
|
93
|
+
expect(container.querySelectorAll('div')).toHaveLength(5);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
+
import { Slot } from '@radix-ui/react-slot';
|
|
2
3
|
import { cva, type VariantProps } from 'class-variance-authority';
|
|
3
4
|
import { cn } from '../../utils/cn';
|
|
4
5
|
|
|
@@ -8,6 +9,12 @@ const badgeVariants = cva(
|
|
|
8
9
|
variants: {
|
|
9
10
|
variant: {
|
|
10
11
|
default: 'border-transparent bg-primary text-primary-foreground hover:bg-primary/80',
|
|
12
|
+
// Aliases used by shadcn/Bootstrap-style codebases.
|
|
13
|
+
primary: 'border-transparent bg-primary text-primary-foreground hover:bg-primary/80',
|
|
14
|
+
danger: 'border-transparent bg-destructive text-destructive-foreground hover:bg-destructive/80',
|
|
15
|
+
success: 'border-transparent bg-success text-success-foreground hover:bg-success/80',
|
|
16
|
+
warning: 'border-transparent bg-warning text-warning-foreground hover:bg-warning/80',
|
|
17
|
+
info: 'border-transparent bg-info text-info-foreground hover:bg-info/80',
|
|
11
18
|
secondary: 'border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80',
|
|
12
19
|
destructive: 'border-transparent bg-destructive text-destructive-foreground hover:bg-destructive/80',
|
|
13
20
|
outline: 'text-foreground',
|
|
@@ -19,12 +26,17 @@ const badgeVariants = cva(
|
|
|
19
26
|
}
|
|
20
27
|
);
|
|
21
28
|
|
|
22
|
-
export type BadgeProps = React.HTMLAttributes<HTMLDivElement> &
|
|
29
|
+
export type BadgeProps = React.HTMLAttributes<HTMLDivElement> &
|
|
30
|
+
VariantProps<typeof badgeVariants> & {
|
|
31
|
+
/** Render the child element (e.g. a router Link) with badge styling. */
|
|
32
|
+
asChild?: boolean;
|
|
33
|
+
};
|
|
23
34
|
|
|
24
35
|
const Badge = React.forwardRef<HTMLDivElement, BadgeProps>(
|
|
25
|
-
({ className, variant, ...props }, ref) =>
|
|
26
|
-
|
|
27
|
-
|
|
36
|
+
({ className, variant, asChild = false, ...props }, ref) => {
|
|
37
|
+
const Comp = asChild ? Slot : 'div';
|
|
38
|
+
return <Comp ref={ref} className={cn(badgeVariants({ variant }), className)} {...props} />;
|
|
39
|
+
}
|
|
28
40
|
);
|
|
29
41
|
Badge.displayName = 'Badge';
|
|
30
42
|
|
|
@@ -107,3 +107,19 @@ describe('Breadcrumb onItemClick', () => {
|
|
|
107
107
|
});
|
|
108
108
|
});
|
|
109
109
|
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
describe('Breadcrumb linkComponent', () => {
|
|
113
|
+
it('renders items with a custom router link', () => {
|
|
114
|
+
function RouterLink({ href, children, ...props }: React.ComponentProps<'a'>) {
|
|
115
|
+
return (
|
|
116
|
+
<a data-router href={href} {...props}>
|
|
117
|
+
{children}
|
|
118
|
+
</a>
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
render(<Breadcrumb linkComponent={RouterLink} items={[{ label: 'Home', href: '/' }, { label: 'Docs' }]} />);
|
|
122
|
+
expect(screen.getByRole('link', { name: 'Home' })).toHaveAttribute('data-router');
|
|
123
|
+
expect(screen.getByText('Docs')).toHaveAttribute('aria-current', 'page');
|
|
124
|
+
});
|
|
125
|
+
});
|
|
@@ -19,6 +19,8 @@ export type BreadcrumbProps = React.HTMLAttributes<HTMLElement> & {
|
|
|
19
19
|
* (e.g. via a router, state machine, or analytics wrapper).
|
|
20
20
|
*/
|
|
21
21
|
onItemClick?: (item: BreadcrumbItem, index: number) => void;
|
|
22
|
+
/** Custom link element for routers (Next Link, react-router Link). */
|
|
23
|
+
linkComponent?: React.ElementType;
|
|
22
24
|
};
|
|
23
25
|
|
|
24
26
|
/**
|
|
@@ -27,7 +29,9 @@ export type BreadcrumbProps = React.HTMLAttributes<HTMLElement> & {
|
|
|
27
29
|
* announce the navigation landmark and current location.
|
|
28
30
|
*/
|
|
29
31
|
const Breadcrumb = React.forwardRef<HTMLElement, BreadcrumbProps>(
|
|
30
|
-
({ items, separator = '/', onItemClick, className, ...props }, ref) =>
|
|
32
|
+
({ items, separator = '/', onItemClick, linkComponent, className, ...props }, ref) => {
|
|
33
|
+
const LinkEl = linkComponent ?? 'a';
|
|
34
|
+
return (
|
|
31
35
|
<nav
|
|
32
36
|
ref={ref}
|
|
33
37
|
aria-label="Breadcrumb"
|
|
@@ -45,12 +49,12 @@ const Breadcrumb = React.forwardRef<HTMLElement, BreadcrumbProps>(
|
|
|
45
49
|
</span>
|
|
46
50
|
)}
|
|
47
51
|
{item.href && !isLast ? (
|
|
48
|
-
<
|
|
52
|
+
<LinkEl
|
|
49
53
|
href={item.href}
|
|
50
54
|
className="transition-colors hover:text-foreground"
|
|
51
55
|
onClick={
|
|
52
56
|
onItemClick
|
|
53
|
-
? (event) => {
|
|
57
|
+
? (event: React.MouseEvent<HTMLAnchorElement>) => {
|
|
54
58
|
event.preventDefault();
|
|
55
59
|
onItemClick(item, index);
|
|
56
60
|
}
|
|
@@ -58,7 +62,7 @@ const Breadcrumb = React.forwardRef<HTMLElement, BreadcrumbProps>(
|
|
|
58
62
|
}
|
|
59
63
|
>
|
|
60
64
|
{item.label}
|
|
61
|
-
</
|
|
65
|
+
</LinkEl>
|
|
62
66
|
) : (
|
|
63
67
|
<span
|
|
64
68
|
aria-current={isLast ? 'page' : undefined}
|
|
@@ -72,7 +76,8 @@ const Breadcrumb = React.forwardRef<HTMLElement, BreadcrumbProps>(
|
|
|
72
76
|
})}
|
|
73
77
|
</ol>
|
|
74
78
|
</nav>
|
|
75
|
-
|
|
79
|
+
);
|
|
80
|
+
}
|
|
76
81
|
);
|
|
77
82
|
Breadcrumb.displayName = 'Breadcrumb';
|
|
78
83
|
|
|
@@ -101,3 +101,70 @@ describe('Button semantic theme variants', () => {
|
|
|
101
101
|
);
|
|
102
102
|
});
|
|
103
103
|
});
|
|
104
|
+
|
|
105
|
+
describe('Button outline color variants', () => {
|
|
106
|
+
it('renders each colored outline with a semantic border and label', () => {
|
|
107
|
+
render(
|
|
108
|
+
<>
|
|
109
|
+
<Button variant="outline-destructive">Delete</Button>
|
|
110
|
+
<Button variant="outline-success">Approve</Button>
|
|
111
|
+
<Button variant="outline-warning">Careful</Button>
|
|
112
|
+
<Button variant="outline-info">Info</Button>
|
|
113
|
+
</>
|
|
114
|
+
);
|
|
115
|
+
const del = screen.getByRole('button', { name: 'Delete' });
|
|
116
|
+
expect(del).toHaveClass('border', 'border-destructive/50', 'text-destructive', 'bg-background');
|
|
117
|
+
expect(del.className).toContain('hover:bg-destructive/10');
|
|
118
|
+
expect(screen.getByRole('button', { name: 'Approve' })).toHaveClass('border-success/50', 'text-success');
|
|
119
|
+
expect(screen.getByRole('button', { name: 'Careful' })).toHaveClass('border-warning/50', 'text-warning-ink');
|
|
120
|
+
expect(screen.getByRole('button', { name: 'Info' })).toHaveClass('border-info/50', 'text-info-ink');
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('keeps colored outlines accessible as links via asChild', () => {
|
|
124
|
+
render(
|
|
125
|
+
<Button asChild variant="outline-success">
|
|
126
|
+
<a href="/ship">Ship</a>
|
|
127
|
+
</Button>
|
|
128
|
+
);
|
|
129
|
+
const link = screen.getByRole('link', { name: 'Ship' });
|
|
130
|
+
expect(link).toHaveClass('border-success/50', 'text-success');
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
describe('Button aliases and loading', () => {
|
|
136
|
+
it('accepts primary/danger aliases', () => {
|
|
137
|
+
render(
|
|
138
|
+
<>
|
|
139
|
+
<Button variant="primary">Go</Button>
|
|
140
|
+
<Button variant="danger">Delete</Button>
|
|
141
|
+
</>
|
|
142
|
+
);
|
|
143
|
+
expect(screen.getByRole('button', { name: 'Go' })).toHaveClass('bg-primary');
|
|
144
|
+
expect(screen.getByRole('button', { name: 'Delete' })).toHaveClass('bg-destructive');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('loading disables, sets aria-busy and announces', () => {
|
|
148
|
+
render(<Button loading>Save</Button>);
|
|
149
|
+
const button = screen.getByRole('button', { name: /Save/ });
|
|
150
|
+
expect(button).toBeDisabled();
|
|
151
|
+
expect(button).toHaveAttribute('aria-busy', 'true');
|
|
152
|
+
expect(screen.getByText('Loading')).toHaveClass('sr-only');
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('asChild ignores loading chrome', () => {
|
|
156
|
+
render(
|
|
157
|
+
<Button asChild loading>
|
|
158
|
+
<a href="/x">Link</a>
|
|
159
|
+
</Button>
|
|
160
|
+
);
|
|
161
|
+
expect(screen.getByRole('link', { name: 'Link' })).not.toHaveAttribute('aria-busy');
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
describe('localized labels', () => {
|
|
166
|
+
it('uses the loadingLabel override', () => {
|
|
167
|
+
render(<Button loading loadingLabel="Chargement">Save</Button>);
|
|
168
|
+
expect(screen.getByText('Chargement')).toBeInTheDocument();
|
|
169
|
+
});
|
|
170
|
+
});
|