@svadmin/ui 0.108.2 → 0.108.4

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.
Files changed (47) hide show
  1. package/dist/components/AboutSettings.svelte +1 -1
  2. package/dist/components/AdminApp.svelte +20 -1
  3. package/dist/components/AdminApp.svelte.d.ts +6 -0
  4. package/dist/components/Calendar.svelte +28 -8
  5. package/dist/components/ConfigProvider.svelte +101 -10
  6. package/dist/components/ConfigProvider.svelte.d.ts +39 -0
  7. package/dist/components/DatePicker.svelte +32 -9
  8. package/dist/components/DatePicker.svelte.d.ts +3 -0
  9. package/dist/components/DevTools.svelte +198 -0
  10. package/dist/components/EditableTable.svelte +59 -6
  11. package/dist/components/EditableTable.svelte.d.ts +13 -0
  12. package/dist/components/EmptyState.svelte +19 -6
  13. package/dist/components/ListPagination.svelte +59 -11
  14. package/dist/components/NotificationHost.svelte +5 -4
  15. package/dist/components/RouteAccessGuard.svelte +42 -0
  16. package/dist/components/RouteAccessGuard.svelte.d.ts +11 -0
  17. package/dist/components/component-messages.d.ts +59 -0
  18. package/dist/components/component-messages.js +47 -0
  19. package/dist/components/config-provider-defaults.reader.svelte +8 -0
  20. package/dist/components/config-provider-defaults.reader.svelte.d.ts +3 -0
  21. package/dist/components/custom-route-access.d.ts +28 -0
  22. package/dist/components/custom-route-access.js +57 -0
  23. package/dist/components/notification-scope-probe.svelte +9 -0
  24. package/dist/components/notification-scope-probe.svelte.d.ts +6 -0
  25. package/dist/components/popup-portal-host.svelte +41 -0
  26. package/dist/components/popup-portal-host.svelte.d.ts +7 -0
  27. package/dist/components/ui/alert-dialog/alert-dialog-portal.svelte +3 -1
  28. package/dist/components/ui/button/button.svelte +15 -2
  29. package/dist/components/ui/dialog/dialog-portal.svelte +2 -1
  30. package/dist/components/ui/dropdown-menu/dropdown-menu-content.svelte +5 -2
  31. package/dist/components/ui/dropdown-menu/dropdown-menu-content.svelte.d.ts +1 -1
  32. package/dist/components/ui/popover/popover-content.svelte +6 -2
  33. package/dist/components/ui/popup-anchor.d.ts +6 -0
  34. package/dist/components/ui/popup-anchor.js +65 -0
  35. package/dist/components/ui/popup-portal.svelte +42 -0
  36. package/dist/components/ui/popup-portal.svelte.d.ts +11 -0
  37. package/dist/components/ui/sheet/sheet.svelte +3 -3
  38. package/dist/components/ui/tooltip/tooltip-content.svelte +5 -1
  39. package/dist/components/ui/tooltip/tooltip-portal.svelte +6 -2
  40. package/dist/components/ui/tooltip/tooltip-portal.svelte.d.ts +4 -1
  41. package/dist/devtools-inspector.d.ts +37 -0
  42. package/dist/devtools-inspector.js +56 -0
  43. package/dist/index.d.ts +7 -5
  44. package/dist/index.js +3 -2
  45. package/dist/notifications.svelte.d.ts +23 -12
  46. package/dist/notifications.svelte.js +118 -39
  47. package/package.json +4 -4
@@ -11,7 +11,7 @@
11
11
  const resources = $derived(adminContext.resources.filter((resource) => resource.meta?.['aboutVisible'] !== false));
12
12
  const providerNames = $derived(adminContext.getDataProviderNames());
13
13
  let { version: suppliedVersion, headingLevel = 'h1' }: { version?: string; headingLevel?: 'h1' | 'h2' } = $props();
14
- const buildVersion = "0.108.2";
14
+ const buildVersion = "0.108.4";
15
15
  const version = $derived(suppliedVersion ?? (
16
16
  buildVersion.startsWith('__')
17
17
  ? (i18n.locale === 'zh-CN' ? '开发构建(未提供版本)' : 'Development build (version unavailable)')
@@ -1,10 +1,16 @@
1
1
  <script module lang="ts">
2
2
  import type { Component as PageComponent } from 'svelte';
3
+ import type { CustomRouteAccessConfig } from './custom-route-access.js';
3
4
 
4
5
  export interface AdminPageRoute {
5
6
  readonly path: string;
6
7
  readonly component: PageComponent<{ route: string; params: Record<string, string | undefined> }>;
7
8
  readonly title?: string;
9
+ /**
10
+ * 直接 URL 的权限。省略时继承同路径菜单的 `meta.resource` / `meta.action`。
11
+ * `false` 保持公开,即使菜单项受保护。
12
+ */
13
+ readonly access?: CustomRouteAccessConfig | false;
8
14
  }
9
15
 
10
16
  const loadPublicProfilePage = () => import('./profile/PublicProfilePage.svelte');
@@ -92,6 +98,9 @@
92
98
  import SystemPageShell from './SystemPageShell.svelte';
93
99
  import LazyPage from './LazyPage.svelte';
94
100
  import ResourceAccessGuard from './ResourceAccessGuard.svelte';
101
+ import RouteAccessGuard from './RouteAccessGuard.svelte';
102
+ import { resolveCustomRouteAccess } from './custom-route-access.js';
103
+ import { bindNotificationRoot, provideNotificationController } from '../notifications.svelte.js';
95
104
  import { Button } from './ui/button/index.js';
96
105
  import { Input } from './ui/input/index.js';
97
106
  import { Badge } from './ui/badge/index.js';
@@ -352,6 +361,7 @@
352
361
  get tenant() { return tenant; },
353
362
  }));
354
363
  const adminContext = captureAdminContext();
364
+ const appNotifications = provideNotificationController();
355
365
 
356
366
  // Context must be available synchronously to descendants during their first render.
357
367
  syncComponentRegistry();
@@ -576,6 +586,7 @@
576
586
  </script>
577
587
 
578
588
  <QueryClientProvider client={queryClient}>
589
+ <div class="svadmin-admin-app" style="display: contents" use:bindNotificationRoot={appNotifications}>
579
590
  {#if !resolvedDataProvider}
580
591
  <ConfigErrorScreen title="{title} — DataProvider is required" />
581
592
  {:else if !authChecked || authProviderPending}
@@ -665,7 +676,14 @@
665
676
  <SettingsPage {...notificationPreferencesProvider ? { preferencesProvider: notificationPreferencesProvider } : {}} />
666
677
  {:else if customRouteMatch}
667
678
  {@const CustomPage = customRouteMatch.candidate.component}
668
- <CustomPage route={customRouteMatch.candidate.path} params={customRouteMatch.params} />
679
+ {@const routeAccess = resolveCustomRouteAccess(customRouteMatch.candidate, path, menu, customRouteMatch.params)}
680
+ {#if routeAccess}
681
+ <RouteAccessGuard resource={routeAccess.resource} action={routeAccess.action} {...routeAccess.id === undefined ? {} : { id: routeAccess.id }}>
682
+ <CustomPage route={customRouteMatch.candidate.path} params={customRouteMatch.params} />
683
+ </RouteAccessGuard>
684
+ {:else}
685
+ <CustomPage route={customRouteMatch.candidate.path} params={customRouteMatch.params} />
686
+ {/if}
669
687
  {:else if renderedRoute === '/' || renderedRoute === '' || renderedRoute === '/inventory-dashboard'}
670
688
  {#if dashboard}
671
689
  {@render dashboard()}
@@ -725,4 +743,5 @@
725
743
  {/if}
726
744
  <Toast />
727
745
  <NotificationHost />
746
+ </div>
728
747
  </QueryClientProvider>
@@ -1,4 +1,5 @@
1
1
  import type { Component as PageComponent } from 'svelte';
2
+ import type { CustomRouteAccessConfig } from './custom-route-access.js';
2
3
  export interface AdminPageRoute {
3
4
  readonly path: string;
4
5
  readonly component: PageComponent<{
@@ -6,6 +7,11 @@ export interface AdminPageRoute {
6
7
  params: Record<string, string | undefined>;
7
8
  }>;
8
9
  readonly title?: string;
10
+ /**
11
+ * 直接 URL 的权限。省略时继承同路径菜单的 `meta.resource` / `meta.action`。
12
+ * `false` 保持公开,即使菜单项受保护。
13
+ */
14
+ readonly access?: CustomRouteAccessConfig | false;
9
15
  }
10
16
  import '../app.css';
11
17
  import { type Component, type Snippet } from 'svelte';
@@ -1,8 +1,11 @@
1
1
  <script lang="ts">
2
2
  import { untrack } from 'svelte';
3
3
  import { ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight } from '@lucide/svelte';
4
+ import { useTranslation } from '@svadmin/core/i18n';
4
5
  import { Button } from './ui/button/index.js';
5
6
  import { cn } from '../utils.js';
7
+ import { hasComponentConfig, useComponentConfig } from './ConfigProvider.svelte';
8
+ import { componentText, resolveComponentLocale } from './component-messages.js';
6
9
 
7
10
  export interface CalendarEvent {
8
11
  id: string;
@@ -39,12 +42,29 @@
39
42
  let viewYear = $state(initial.getFullYear());
40
43
  let viewMonth = $state(initial.getMonth());
41
44
 
45
+ const config = useComponentConfig();
46
+ const i18n = useTranslation();
47
+ const provided = hasComponentConfig();
48
+ // 显式 locale 仍优先。未传入时与 DatePicker 共用 ConfigProvider / i18n 解析。
49
+ const activeLocale = $derived(resolveComponentLocale({
50
+ explicit: locale,
51
+ configLocale: provided ? config.locale : undefined,
52
+ i18nLocale: i18n.locale,
53
+ }));
54
+ const copy = $derived({
55
+ previousMonth: componentText(i18n, activeLocale, 'datePicker.previousMonth'),
56
+ nextMonth: componentText(i18n, activeLocale, 'datePicker.nextMonth'),
57
+ previousYear: componentText(i18n, activeLocale, 'datePicker.previousYear'),
58
+ nextYear: componentText(i18n, activeLocale, 'datePicker.nextYear'),
59
+ calendarDates: componentText(i18n, activeLocale, 'datePicker.calendarDates'),
60
+ });
61
+
42
62
  const weekdays = $derived.by(() => {
43
- const format = new Intl.DateTimeFormat(locale, { weekday: 'short' });
44
- return Array.from({ length: 7 }, (_, index) => format.format(new Date(2024, 0, 7 + ((index + weekStartsOn) % 7))));
63
+ const format = new Intl.DateTimeFormat(activeLocale, { weekday: 'short' });
64
+ return Array.from({ length: 7 }, (_, index) => format.format(new Date(2024, 0, 7 + ((index + weekStartsOn) % 7), 12)));
45
65
  });
46
66
 
47
- const title = $derived(new Intl.DateTimeFormat(locale, { month: 'long', year: 'numeric' }).format(new Date(viewYear, viewMonth, 1)));
67
+ const title = $derived(new Intl.DateTimeFormat(activeLocale, { month: 'long', year: 'numeric' }).format(new Date(viewYear, viewMonth, 1, 12)));
48
68
 
49
69
  const days = $derived.by(() => {
50
70
  const first = new Date(viewYear, viewMonth, 1);
@@ -107,17 +127,17 @@
107
127
  <header class="svadmin-calendar__header">
108
128
  <strong>{title}</strong>
109
129
  <div class="svadmin-calendar__nav">
110
- <Button variant="outline" size="icon-sm" aria-label="Previous year" onclick={() => moveYear(-1)}><ChevronsLeft size={16} /></Button>
111
- <Button variant="outline" size="icon-sm" aria-label="Previous month" onclick={() => move(-1)}><ChevronLeft size={16} /></Button>
112
- <Button variant="outline" size="icon-sm" aria-label="Next month" onclick={() => move(1)}><ChevronRight size={16} /></Button>
113
- <Button variant="outline" size="icon-sm" aria-label="Next year" onclick={() => moveYear(1)}><ChevronsRight size={16} /></Button>
130
+ <Button variant="outline" size="icon-sm" aria-label={copy.previousYear} onclick={() => moveYear(-1)}><ChevronsLeft size={16} /></Button>
131
+ <Button variant="outline" size="icon-sm" aria-label={copy.previousMonth} onclick={() => move(-1)}><ChevronLeft size={16} /></Button>
132
+ <Button variant="outline" size="icon-sm" aria-label={copy.nextMonth} onclick={() => move(1)}><ChevronRight size={16} /></Button>
133
+ <Button variant="outline" size="icon-sm" aria-label={copy.nextYear} onclick={() => moveYear(1)}><ChevronsRight size={16} /></Button>
114
134
  </div>
115
135
  </header>
116
136
  <div class="svadmin-calendar__weekdays" aria-hidden="true">
117
137
  {#each weekdays as weekday (weekday)}<span>{weekday}</span>{/each}
118
138
  </div>
119
139
  <!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
120
- <div class="svadmin-calendar__grid" role="group" aria-label="Calendar dates" onkeydown={onGridKeydown}>
140
+ <div class="svadmin-calendar__grid" role="group" aria-label={copy.calendarDates} onkeydown={onGridKeydown}>
121
141
  {#each days as day (day.iso)}
122
142
  <button
123
143
  type="button"
@@ -1,30 +1,106 @@
1
1
  <script module lang="ts">
2
2
  import { getContext } from 'svelte';
3
3
 
4
+ export type ComponentPropDefaults = Readonly<Record<string, Readonly<Record<string, unknown>>>>;
5
+
6
+ export interface EmptyStateDefaults {
7
+ readonly title?: string;
8
+ readonly description?: string;
9
+ readonly image?: string;
10
+ readonly imageAlt?: string;
11
+ }
12
+
13
+ export interface PaginationDefaults {
14
+ readonly pageSize?: number;
15
+ readonly pageSizeOptions?: readonly number[];
16
+ readonly showSummary?: boolean;
17
+ readonly simple?: boolean;
18
+ readonly showJumper?: boolean;
19
+ }
20
+
21
+ /**
22
+ * 弹出层挂载点。`node` 是触发器或锚点,不是已经挂到 body 上的浮层。
23
+ * 返回元素、选择器,或空值以回退到 document.body。
24
+ */
25
+ export type GetPopupContainer = (node?: HTMLElement) => HTMLElement | string | null | undefined;
26
+
27
+ /** Node SSR 没有 HTMLElement。typeof 守卫避免 `instanceof` 触发 ReferenceError。 */
28
+ function isHtmlElement(value: unknown): value is HTMLElement {
29
+ return typeof HTMLElement !== 'undefined' && value instanceof HTMLElement;
30
+ }
31
+
4
32
  export interface ComponentConfig {
5
33
  theme?: 'light' | 'dark' | 'system';
6
34
  density?: 'default' | 'compact';
7
35
  direction?: 'ltr' | 'rtl';
8
36
  locale?: string;
9
37
  componentSize?: 'sm' | 'md' | 'lg';
38
+ /**
39
+ * 按组件名设置的默认参数,例如 `{ Button: { variant: 'outline' } }`。显式 props 优先。
40
+ * 目前只有 Button、Empty(EmptyState)和 Pagination(ListPagination)会读取。
41
+ */
42
+ componentDefaults?: ComponentPropDefaults;
43
+ /** 空状态默认文案和插图。 */
44
+ empty?: EmptyStateDefaults;
45
+ /** 分页默认参数。 */
46
+ pagination?: PaginationDefaults;
47
+ /** 弹出层与浮层的挂载容器,语义对齐 getPopupContainer。 */
48
+ getPopupContainer?: GetPopupContainer;
10
49
  }
11
50
 
12
51
  export const CONFIG_CONTEXT_KEY = Symbol('svadmin-component-config');
13
52
 
53
+ export const DEFAULT_PAGE_SIZE_OPTIONS = Object.freeze([10, 20, 50]);
54
+
55
+ export const DEFAULT_COMPONENT_DEFAULTS: ComponentPropDefaults = Object.freeze({});
56
+
57
+ export const DEFAULT_EMPTY_STATE: EmptyStateDefaults = Object.freeze({});
58
+
59
+ export const DEFAULT_PAGINATION: PaginationDefaults = Object.freeze({});
60
+
61
+ export function defaultGetPopupContainer(node?: HTMLElement): HTMLElement | undefined {
62
+ const doc = node?.ownerDocument ?? (typeof document === 'undefined' ? undefined : document);
63
+ return doc?.body;
64
+ }
65
+
66
+ export function resolvePopupContainer(getPopupContainer: GetPopupContainer | undefined, node?: HTMLElement): HTMLElement | undefined {
67
+ const fallback = defaultGetPopupContainer(node);
68
+ if (!getPopupContainer) return fallback;
69
+ const resolved = getPopupContainer(node);
70
+ if (isHtmlElement(resolved)) return resolved;
71
+ if (typeof resolved === 'string' && resolved.length > 0) {
72
+ const doc = node?.ownerDocument ?? (typeof document === 'undefined' ? undefined : document);
73
+ const found = doc?.querySelector(resolved);
74
+ if (isHtmlElement(found)) return found;
75
+ }
76
+ return fallback;
77
+ }
78
+
79
+ export const DEFAULT_COMPONENT_CONFIG: Required<ComponentConfig> = {
80
+ theme: 'system',
81
+ density: 'default',
82
+ direction: 'ltr',
83
+ locale: 'en',
84
+ componentSize: 'md',
85
+ componentDefaults: DEFAULT_COMPONENT_DEFAULTS,
86
+ empty: DEFAULT_EMPTY_STATE,
87
+ pagination: DEFAULT_PAGINATION,
88
+ getPopupContainer: defaultGetPopupContainer,
89
+ };
90
+
91
+ /** 当前组件是否位于 ConfigProvider 内。只能在组件初始化时调用。 */
92
+ export function hasComponentConfig(): boolean {
93
+ return getContext<Required<ComponentConfig> | undefined>(CONFIG_CONTEXT_KEY) !== undefined;
94
+ }
95
+
14
96
  export function useComponentConfig(): Required<ComponentConfig> {
15
- return getContext<Required<ComponentConfig>>(CONFIG_CONTEXT_KEY) ?? {
16
- theme: 'system',
17
- density: 'default',
18
- direction: 'ltr',
19
- locale: 'en',
20
- componentSize: 'md',
21
- };
97
+ return getContext<Required<ComponentConfig>>(CONFIG_CONTEXT_KEY) ?? DEFAULT_COMPONENT_CONFIG;
22
98
  }
23
99
  </script>
24
100
 
25
101
  <script lang="ts">
26
102
  import type { Snippet } from 'svelte';
27
- import { setContext, untrack } from 'svelte';
103
+ import { setContext } from 'svelte';
28
104
  import { cn } from '../utils.js';
29
105
 
30
106
  interface Props extends ComponentConfig {
@@ -38,11 +114,26 @@
38
114
  direction = 'ltr',
39
115
  locale = 'en',
40
116
  componentSize = 'md',
117
+ componentDefaults = DEFAULT_COMPONENT_DEFAULTS,
118
+ empty = DEFAULT_EMPTY_STATE,
119
+ pagination = DEFAULT_PAGINATION,
120
+ getPopupContainer = defaultGetPopupContainer,
41
121
  class: className = '',
42
122
  children,
43
123
  }: Props = $props();
44
124
 
45
- setContext(CONFIG_CONTEXT_KEY, untrack(() => ({ theme, density, direction, locale, componentSize })));
125
+ // 保存读取器而不是初始化快照。子组件在渲染或 $derived 中读取字段时,会跟踪对应 prop。
126
+ setContext(CONFIG_CONTEXT_KEY, {
127
+ get theme() { return theme; },
128
+ get density() { return density; },
129
+ get direction() { return direction; },
130
+ get locale() { return locale; },
131
+ get componentSize() { return componentSize; },
132
+ get componentDefaults() { return componentDefaults; },
133
+ get empty() { return empty; },
134
+ get pagination() { return pagination; },
135
+ get getPopupContainer() { return getPopupContainer; },
136
+ } satisfies Required<ComponentConfig>);
46
137
  </script>
47
138
 
48
139
  <div
@@ -58,4 +149,4 @@
58
149
 
59
150
  <style>
60
151
  .svadmin-config-provider { display: contents; }
61
- </style>
152
+ </style>
@@ -1,11 +1,50 @@
1
+ export type ComponentPropDefaults = Readonly<Record<string, Readonly<Record<string, unknown>>>>;
2
+ export interface EmptyStateDefaults {
3
+ readonly title?: string;
4
+ readonly description?: string;
5
+ readonly image?: string;
6
+ readonly imageAlt?: string;
7
+ }
8
+ export interface PaginationDefaults {
9
+ readonly pageSize?: number;
10
+ readonly pageSizeOptions?: readonly number[];
11
+ readonly showSummary?: boolean;
12
+ readonly simple?: boolean;
13
+ readonly showJumper?: boolean;
14
+ }
15
+ /**
16
+ * 弹出层挂载点。`node` 是触发器或锚点,不是已经挂到 body 上的浮层。
17
+ * 返回元素、选择器,或空值以回退到 document.body。
18
+ */
19
+ export type GetPopupContainer = (node?: HTMLElement) => HTMLElement | string | null | undefined;
1
20
  export interface ComponentConfig {
2
21
  theme?: 'light' | 'dark' | 'system';
3
22
  density?: 'default' | 'compact';
4
23
  direction?: 'ltr' | 'rtl';
5
24
  locale?: string;
6
25
  componentSize?: 'sm' | 'md' | 'lg';
26
+ /**
27
+ * 按组件名设置的默认参数,例如 `{ Button: { variant: 'outline' } }`。显式 props 优先。
28
+ * 目前只有 Button、Empty(EmptyState)和 Pagination(ListPagination)会读取。
29
+ */
30
+ componentDefaults?: ComponentPropDefaults;
31
+ /** 空状态默认文案和插图。 */
32
+ empty?: EmptyStateDefaults;
33
+ /** 分页默认参数。 */
34
+ pagination?: PaginationDefaults;
35
+ /** 弹出层与浮层的挂载容器,语义对齐 getPopupContainer。 */
36
+ getPopupContainer?: GetPopupContainer;
7
37
  }
8
38
  export declare const CONFIG_CONTEXT_KEY: unique symbol;
39
+ export declare const DEFAULT_PAGE_SIZE_OPTIONS: readonly number[];
40
+ export declare const DEFAULT_COMPONENT_DEFAULTS: ComponentPropDefaults;
41
+ export declare const DEFAULT_EMPTY_STATE: EmptyStateDefaults;
42
+ export declare const DEFAULT_PAGINATION: PaginationDefaults;
43
+ export declare function defaultGetPopupContainer(node?: HTMLElement): HTMLElement | undefined;
44
+ export declare function resolvePopupContainer(getPopupContainer: GetPopupContainer | undefined, node?: HTMLElement): HTMLElement | undefined;
45
+ export declare const DEFAULT_COMPONENT_CONFIG: Required<ComponentConfig>;
46
+ /** 当前组件是否位于 ConfigProvider 内。只能在组件初始化时调用。 */
47
+ export declare function hasComponentConfig(): boolean;
9
48
  export declare function useComponentConfig(): Required<ComponentConfig>;
10
49
  import type { Snippet } from 'svelte';
11
50
  interface Props extends ComponentConfig {
@@ -1,10 +1,13 @@
1
1
  <script lang="ts">
2
2
  import { untrack, type ComponentProps } from 'svelte';
3
3
  import { ChevronLeft, ChevronRight } from '@lucide/svelte';
4
+ import { useTranslation } from '@svadmin/core/i18n';
4
5
  import DateTimeInput from './DateTimeInput.svelte';
5
6
  import { Button } from './ui/button/index.js';
6
7
  import * as Popover from './ui/popover/index.js';
7
8
  import { cn } from '../utils.js';
9
+ import { hasComponentConfig, useComponentConfig } from './ConfigProvider.svelte';
10
+ import { componentText, monthLabel, resolveComponentLocale, weekdayLabels } from './component-messages.js';
8
11
 
9
12
  export type DatePickerMode = 'date' | 'month' | 'year' | 'quarter';
10
13
  export interface DatePreset { label: string; value: string; }
@@ -17,7 +20,10 @@
17
20
  panel?: boolean;
18
21
  picker?: DatePickerMode;
19
22
  presets?: DatePreset[];
23
+ /** 未传入时跟随已解析 locale,英文为 Select date,中文为选择日期。 */
20
24
  placeholder?: string;
25
+ /** 覆盖 ConfigProvider 与 i18n 的面板语言。不传则保持原有默认路径。 */
26
+ locale?: string;
21
27
  onchange?: (value: string | null) => void;
22
28
  }
23
29
 
@@ -28,14 +34,31 @@
28
34
  panel = false,
29
35
  picker = 'date',
30
36
  presets = [],
31
- placeholder = 'Select date',
37
+ placeholder,
38
+ locale: localeProp,
32
39
  disabled = false,
33
40
  class: className,
34
41
  onchange,
35
42
  ...nativeProps
36
43
  }: Props = $props();
37
44
 
38
- const weekdays = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'];
45
+ const config = useComponentConfig();
46
+ const i18n = useTranslation();
47
+ const provided = hasComponentConfig();
48
+ // 组件属性 > ConfigProvider(仅存在时)> i18n > en。无 Provider 时不拿默认 en 盖住 i18n。
49
+ const activeLocale = $derived(resolveComponentLocale({
50
+ explicit: localeProp,
51
+ configLocale: provided ? config.locale : undefined,
52
+ i18nLocale: i18n.locale,
53
+ }));
54
+ const copy = $derived({
55
+ previous: componentText(i18n, activeLocale, 'datePicker.previous'),
56
+ next: componentText(i18n, activeLocale, 'datePicker.next'),
57
+ clear: componentText(i18n, activeLocale, 'datePicker.clear'),
58
+ selectDate: componentText(i18n, activeLocale, 'datePicker.selectDate'),
59
+ });
60
+ const resolvedPlaceholder = $derived(placeholder ?? copy.selectDate);
61
+ const weekdays = $derived(weekdayLabels(activeLocale));
39
62
  let open = $state(false);
40
63
  let viewMode = $state<'day' | 'month' | 'year'>(untrack(() => picker === 'year' ? 'year' : picker === 'month' ? 'month' : 'day'));
41
64
  let viewDate = $state(initialView(value));
@@ -92,7 +115,7 @@
92
115
 
93
116
  const months = $derived(Array.from({ length: 12 }, (_, month) => {
94
117
  const key = `${viewDate.getFullYear()}-${pad(month + 1)}`;
95
- return { month, key, label: new Intl.DateTimeFormat(undefined, { month: 'short' }).format(new Date(viewDate.getFullYear(), month, 1)), selected: value === key, disabled: false };
118
+ return { month, key, label: monthLabel(activeLocale, viewDate.getFullYear(), month), selected: value === key, disabled: false };
96
119
  }));
97
120
 
98
121
  const years = $derived.by(() => {
@@ -180,22 +203,22 @@
180
203
  aria-label={nativeProps.ariaLabel}
181
204
  class="flex min-h-9 w-full items-center justify-between rounded-md border border-input bg-background px-3 py-1.5 text-left text-sm focus:outline-hidden focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
182
205
  >
183
- <span class={value ? 'text-foreground' : 'text-muted-foreground'}>{formatValue(value) || placeholder}</span>
206
+ <span class={value ? 'text-foreground' : 'text-muted-foreground'}>{formatValue(value) || resolvedPlaceholder}</span>
184
207
  <ChevronRight class="h-4 w-4 rotate-90 opacity-50" />
185
208
  </button>
186
209
  {/snippet}
187
210
  </Popover.Trigger>
188
211
  <Popover.Content class="svadmin-date-picker-panel w-auto p-0" align="start">
189
- <div class="w-64 p-2">
212
+ <div class="w-64 p-2" lang={activeLocale}>
190
213
  <div class="mb-2 flex items-center justify-between">
191
- <Button type="button" variant="ghost" size="icon-sm" aria-label="Previous" onclick={() => move(-1)}><ChevronLeft size={16} /></Button>
214
+ <Button type="button" variant="ghost" size="icon-sm" aria-label={copy.previous} onclick={() => move(-1)}><ChevronLeft size={16} /></Button>
192
215
  <button type="button" class="rounded px-2 py-1 text-sm font-medium hover:bg-muted" onclick={toggleTitle}>{title}</button>
193
- <Button type="button" variant="ghost" size="icon-sm" aria-label="Next" onclick={() => move(1)}><ChevronRight size={16} /></Button>
216
+ <Button type="button" variant="ghost" size="icon-sm" aria-label={copy.next} onclick={() => move(1)}><ChevronRight size={16} /></Button>
194
217
  </div>
195
218
 
196
219
  {#if viewMode === 'day'}
197
220
  <div class="grid grid-cols-7 gap-0.5 text-center text-[0.6875rem] text-muted-foreground">
198
- {#each weekdays as weekday (weekday)}<span class="py-1">{weekday}</span>{/each}
221
+ {#each weekdays as weekday, index (index)}<span class="py-1">{weekday}</span>{/each}
199
222
  </div>
200
223
  <div class="grid grid-cols-7 gap-0.5">
201
224
  {#each days as day (day.iso)}
@@ -242,7 +265,7 @@
242
265
  {/if}
243
266
  {#if value}
244
267
  <div class="mt-1 flex justify-end border-t border-border pt-1">
245
- <Button type="button" variant="ghost" size="sm" onclick={clear}>Clear</Button>
268
+ <Button type="button" variant="ghost" size="sm" onclick={clear}>{copy.clear}</Button>
246
269
  </div>
247
270
  {/if}
248
271
  </div>
@@ -12,7 +12,10 @@ interface Props extends NativeProps {
12
12
  panel?: boolean;
13
13
  picker?: DatePickerMode;
14
14
  presets?: DatePreset[];
15
+ /** 未传入时跟随已解析 locale,英文为 Select date,中文为选择日期。 */
15
16
  placeholder?: string;
17
+ /** 覆盖 ConfigProvider 与 i18n 的面板语言。不传则保持原有默认路径。 */
18
+ locale?: string;
16
19
  onchange?: (value: string | null) => void;
17
20
  }
18
21
  export type DatePickerProps = Props;