@svadmin/ui 0.60.1 → 0.64.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.
Files changed (35) hide show
  1. package/dist/components/AutoTable.svelte +8 -0
  2. package/dist/components/AutoTable.svelte.d.ts +7 -1
  3. package/dist/components/Cascader.svelte +205 -0
  4. package/dist/components/Cascader.svelte.d.ts +21 -0
  5. package/dist/components/DynamicFormList.svelte +214 -0
  6. package/dist/components/DynamicFormList.svelte.d.ts +62 -0
  7. package/dist/components/FieldRenderer.svelte +32 -0
  8. package/dist/components/FilterBuilder.svelte +310 -0
  9. package/dist/components/FilterBuilder.svelte.d.ts +23 -0
  10. package/dist/components/InferencerPanel.svelte +66 -6
  11. package/dist/components/Transfer.svelte +352 -0
  12. package/dist/components/Transfer.svelte.d.ts +28 -0
  13. package/dist/components/TreeSelect.svelte +307 -0
  14. package/dist/components/TreeSelect.svelte.d.ts +22 -0
  15. package/dist/components/ui/navigation-menu/index.d.ts +11 -0
  16. package/dist/components/ui/navigation-menu/index.js +13 -0
  17. package/dist/components/ui/navigation-menu/navigation-menu-content.svelte +21 -0
  18. package/dist/components/ui/navigation-menu/navigation-menu-content.svelte.d.ts +4 -0
  19. package/dist/components/ui/navigation-menu/navigation-menu-indicator.svelte +22 -0
  20. package/dist/components/ui/navigation-menu/navigation-menu-indicator.svelte.d.ts +4 -0
  21. package/dist/components/ui/navigation-menu/navigation-menu-item.svelte +17 -0
  22. package/dist/components/ui/navigation-menu/navigation-menu-item.svelte.d.ts +4 -0
  23. package/dist/components/ui/navigation-menu/navigation-menu-link.svelte +20 -0
  24. package/dist/components/ui/navigation-menu/navigation-menu-link.svelte.d.ts +4 -0
  25. package/dist/components/ui/navigation-menu/navigation-menu-list.svelte +17 -0
  26. package/dist/components/ui/navigation-menu/navigation-menu-list.svelte.d.ts +4 -0
  27. package/dist/components/ui/navigation-menu/navigation-menu-trigger.svelte +28 -0
  28. package/dist/components/ui/navigation-menu/navigation-menu-trigger.svelte.d.ts +4 -0
  29. package/dist/components/ui/navigation-menu/navigation-menu-viewport.svelte +22 -0
  30. package/dist/components/ui/navigation-menu/navigation-menu-viewport.svelte.d.ts +4 -0
  31. package/dist/components/ui/navigation-menu/navigation-menu.svelte +24 -0
  32. package/dist/components/ui/navigation-menu/navigation-menu.svelte.d.ts +4 -0
  33. package/dist/index.d.ts +10 -0
  34. package/dist/index.js +7 -0
  35. package/package.json +16 -16
@@ -0,0 +1,310 @@
1
+ <script lang="ts">
2
+ import { Plus, Trash2, RotateCcw, Filter as FilterIcon, Check } from '@lucide/svelte';
3
+ import { cn } from '../utils.js';
4
+ import { Button } from './ui/button/index.js';
5
+ import { Input } from './ui/input/index.js';
6
+ import { Select } from './ui/select/index.js';
7
+ import { useTranslation } from '@svadmin/core/i18n';
8
+ import type { CrudOperator, FieldDefinition, FieldFilter, Filter, LogicalFilter } from '@svadmin/core';
9
+
10
+ export interface FilterRuleItem {
11
+ id: string;
12
+ field: string;
13
+ operator: CrudOperator;
14
+ value: unknown;
15
+ }
16
+
17
+ const i18n = useTranslation();
18
+
19
+ interface Props {
20
+ fields?: FieldDefinition[];
21
+ filters?: Filter[];
22
+ logicalOperator?: 'and' | 'or';
23
+ class?: string;
24
+ onApply?: (filters: Filter[]) => void;
25
+ onReset?: () => void;
26
+ }
27
+
28
+ let {
29
+ fields = [],
30
+ filters = $bindable([]),
31
+ logicalOperator = $bindable('and'),
32
+ class: className,
33
+ onApply,
34
+ onReset,
35
+ }: Props = $props();
36
+
37
+ const operatorOptions: { value: CrudOperator; label: string }[] = [
38
+ { value: 'eq', label: '等于 (eq)' },
39
+ { value: 'ne', label: '不等于 (ne)' },
40
+ { value: 'contains', label: '包含 (contains)' },
41
+ { value: 'ncontains', label: '不包含 (ncontains)' },
42
+ { value: 'gt', label: '大于 (>)' },
43
+ { value: 'gte', label: '大于等于 (>=)' },
44
+ { value: 'lt', label: '小于 (<)' },
45
+ { value: 'lte', label: '小于等于 (<=)' },
46
+ { value: 'null', label: '为空 (null)' },
47
+ { value: 'nnull', label: '非空 (not null)' },
48
+ ];
49
+
50
+ let rules = $state<FilterRuleItem[]>([]);
51
+
52
+ // Initialize rules from filters prop
53
+ $effect(() => {
54
+ if (filters && filters.length > 0) {
55
+ const parsedRules: FilterRuleItem[] = [];
56
+ let nextLogical: 'and' | 'or' = 'and';
57
+
58
+ for (let i = 0; i < filters.length; i++) {
59
+ const item = filters[i];
60
+ if ('field' in item) {
61
+ parsedRules.push({
62
+ id: `rule-${i}-${Date.now()}`,
63
+ field: item.field,
64
+ operator: item.operator,
65
+ value: item.value,
66
+ });
67
+ } else if (item.operator === 'and' || item.operator === 'or') {
68
+ nextLogical = item.operator;
69
+ for (let j = 0; j < item.value.length; j++) {
70
+ const sub = item.value[j];
71
+ if ('field' in sub) {
72
+ parsedRules.push({
73
+ id: `rule-${i}-${j}-${Date.now()}`,
74
+ field: sub.field,
75
+ operator: sub.operator,
76
+ value: sub.value,
77
+ });
78
+ }
79
+ }
80
+ }
81
+ }
82
+ rules = parsedRules;
83
+ logicalOperator = nextLogical;
84
+ } else {
85
+ rules = [];
86
+ }
87
+ });
88
+
89
+ const availableFields = $derived(
90
+ fields.filter((f) => f.filterable !== false)
91
+ );
92
+
93
+ export function addRule() {
94
+ const firstField = availableFields[0]?.key ?? 'id';
95
+ const firstFieldDef = availableFields.find((f) => f.key === firstField);
96
+ const defaultOp: CrudOperator = firstFieldDef?.type === 'number' ? 'eq' : 'contains';
97
+
98
+ rules = [
99
+ ...rules,
100
+ {
101
+ id: `rule-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`,
102
+ field: firstField,
103
+ operator: defaultOp,
104
+ value: '',
105
+ },
106
+ ];
107
+ }
108
+
109
+ export function removeRule(index: number) {
110
+ rules = rules.filter((_, i) => i !== index);
111
+ }
112
+
113
+ export function reset() {
114
+ rules = [];
115
+ filters = [];
116
+ onReset?.();
117
+ }
118
+
119
+ export function apply() {
120
+ const validFieldFilters: FieldFilter[] = rules
121
+ .filter((r) => r.field && (r.operator === 'null' || r.operator === 'nnull' || (r.value !== '' && r.value !== undefined)))
122
+ .map((r) => ({
123
+ field: r.field,
124
+ operator: r.operator,
125
+ value: r.operator === 'null' || r.operator === 'nnull' ? null : r.value,
126
+ }));
127
+
128
+ let compiledFilters: Filter[] = [];
129
+ if (validFieldFilters.length === 1 || logicalOperator === 'and') {
130
+ compiledFilters = validFieldFilters;
131
+ } else if (validFieldFilters.length > 1 && logicalOperator === 'or') {
132
+ const logical: LogicalFilter = {
133
+ operator: 'or',
134
+ value: validFieldFilters,
135
+ };
136
+ compiledFilters = [logical];
137
+ }
138
+
139
+ filters = compiledFilters;
140
+ onApply?.(compiledFilters);
141
+ }
142
+ </script>
143
+
144
+ <div class={cn('w-full rounded-lg border border-border bg-card p-4 shadow-xs space-y-3', className)} data-testid="filter-builder">
145
+ <div class="flex items-center justify-between gap-2 border-b border-border/60 pb-3">
146
+ <div class="flex items-center gap-2">
147
+ <FilterIcon class="h-4 w-4 text-muted-foreground" />
148
+ <span class="text-sm font-medium text-foreground">
149
+ {i18n.t('common.filterBuilder', undefined) ?? '高级筛选'}
150
+ </span>
151
+ {#if rules.length > 1}
152
+ <div class="flex items-center rounded-md border border-border bg-muted/40 p-0.5 text-xs">
153
+ <button
154
+ type="button"
155
+ class={cn(
156
+ 'rounded px-2 py-0.5 text-xs transition-colors',
157
+ logicalOperator === 'and' ? 'bg-background font-semibold text-foreground shadow-xs' : 'text-muted-foreground hover:text-foreground'
158
+ )}
159
+ onclick={() => (logicalOperator = 'and')}
160
+ >
161
+ AND (且)
162
+ </button>
163
+ <button
164
+ type="button"
165
+ class={cn(
166
+ 'rounded px-2 py-0.5 text-xs transition-colors',
167
+ logicalOperator === 'or' ? 'bg-background font-semibold text-foreground shadow-xs' : 'text-muted-foreground hover:text-foreground'
168
+ )}
169
+ onclick={() => (logicalOperator = 'or')}
170
+ >
171
+ OR (或)
172
+ </button>
173
+ </div>
174
+ {/if}
175
+ </div>
176
+
177
+ <div class="flex items-center gap-1.5">
178
+ <Button
179
+ variant="ghost"
180
+ size="sm"
181
+ class="h-8 gap-1 text-xs"
182
+ data-testid="filter-builder-reset"
183
+ onclick={reset}
184
+ >
185
+ <RotateCcw class="h-3.5 w-3.5" />
186
+ {i18n.t('common.reset', undefined) ?? '重置'}
187
+ </Button>
188
+ <Button
189
+ variant="default"
190
+ size="sm"
191
+ class="h-8 gap-1 text-xs"
192
+ data-testid="filter-builder-apply"
193
+ onclick={apply}
194
+ >
195
+ <Check class="h-3.5 w-3.5" />
196
+ {i18n.t('common.confirm', undefined) ?? '应用'}
197
+ </Button>
198
+ </div>
199
+ </div>
200
+
201
+ <div class="space-y-2">
202
+ {#each rules as rule, index (rule.id)}
203
+ {@const fieldDef = availableFields.find((f) => f.key === rule.field)}
204
+ <div class="flex flex-wrap sm:flex-nowrap items-center gap-2 rounded-md bg-muted/20 p-2 border border-border/40">
205
+ <!-- Field select -->
206
+ <div class="w-36 shrink-0">
207
+ <Select
208
+ class="h-8 text-xs"
209
+ value={rule.field}
210
+ onchange={(e: Event) => {
211
+ rule.field = (e.currentTarget as HTMLSelectElement).value;
212
+ }}
213
+ >
214
+ {#each availableFields as f (f.key)}
215
+ <option value={f.key}>{f.label || f.key}</option>
216
+ {/each}
217
+ </Select>
218
+ </div>
219
+
220
+ <!-- Operator select -->
221
+ <div class="w-36 shrink-0">
222
+ <Select
223
+ class="h-8 text-xs"
224
+ value={rule.operator}
225
+ onchange={(e: Event) => {
226
+ rule.operator = (e.currentTarget as HTMLSelectElement).value as CrudOperator;
227
+ }}
228
+ >
229
+ {#each operatorOptions as op (op.value)}
230
+ <option value={op.value}>{op.label}</option>
231
+ {/each}
232
+ </Select>
233
+ </div>
234
+
235
+ <!-- Value input -->
236
+ <div class="flex-1 min-w-[120px]">
237
+ {#if rule.operator === 'null' || rule.operator === 'nnull'}
238
+ <div class="h-8 flex items-center px-3 text-xs text-muted-foreground italic bg-muted/30 rounded-md">
239
+ 无需填值
240
+ </div>
241
+ {:else if fieldDef?.type === 'select' && fieldDef.options}
242
+ <Select
243
+ class="h-8 text-xs"
244
+ value={String(rule.value ?? '')}
245
+ onchange={(e: Event) => {
246
+ rule.value = (e.currentTarget as HTMLSelectElement).value;
247
+ }}
248
+ >
249
+ <option value="">{i18n.t('field.selectPlaceholder', undefined) ?? '请选择'}</option>
250
+ {#each fieldDef.options as opt (opt.value)}
251
+ <option value={String(opt.value)}>{opt.label}</option>
252
+ {/each}
253
+ </Select>
254
+ {:else if fieldDef?.type === 'boolean'}
255
+ <Select
256
+ class="h-8 text-xs"
257
+ value={String(rule.value ?? '')}
258
+ onchange={(e: Event) => {
259
+ const val = (e.currentTarget as HTMLSelectElement).value;
260
+ rule.value = val === 'true' ? true : val === 'false' ? false : '';
261
+ }}
262
+ >
263
+ <option value="">{i18n.t('field.selectPlaceholder', undefined) ?? '请选择'}</option>
264
+ <option value="true">{i18n.t('common.yes', undefined) ?? '是 (true)'}</option>
265
+ <option value="false">{i18n.t('common.no', undefined) ?? '否 (false)'}</option>
266
+ </Select>
267
+ {:else}
268
+ <Input
269
+ type={fieldDef?.type === 'number' ? 'number' : 'text'}
270
+ class="h-8 text-xs"
271
+ placeholder="输入筛选值..."
272
+ value={String(rule.value ?? '')}
273
+ oninput={(e: Event) => {
274
+ const val = (e.currentTarget as HTMLInputElement).value;
275
+ rule.value = fieldDef?.type === 'number' && val !== '' ? Number(val) : val;
276
+ }}
277
+ />
278
+ {/if}
279
+ </div>
280
+
281
+ <!-- Remove button -->
282
+ <Button
283
+ type="button"
284
+ variant="ghost"
285
+ size="icon-sm"
286
+ class="shrink-0 text-muted-foreground hover:text-destructive"
287
+ onclick={() => removeRule(index)}
288
+ >
289
+ <Trash2 class="h-3.5 w-3.5" />
290
+ </Button>
291
+ </div>
292
+ {:else}
293
+ <div class="rounded-md border border-dashed border-border/80 py-4 text-center text-xs text-muted-foreground">
294
+ 暂无筛选条件,点击下方按钮添加规则
295
+ </div>
296
+ {/each}
297
+ </div>
298
+
299
+ <Button
300
+ type="button"
301
+ variant="outline"
302
+ size="sm"
303
+ data-testid="filter-builder-add-rule"
304
+ class="w-full gap-1.5 border-dashed text-xs h-8"
305
+ onclick={addRule}
306
+ >
307
+ <Plus class="h-3.5 w-3.5" />
308
+ {i18n.t('common.addRule', undefined) ?? '添加筛选条件'}
309
+ </Button>
310
+ </div>
@@ -0,0 +1,23 @@
1
+ import type { CrudOperator, FieldDefinition, Filter } from '@svadmin/core';
2
+ export interface FilterRuleItem {
3
+ id: string;
4
+ field: string;
5
+ operator: CrudOperator;
6
+ value: unknown;
7
+ }
8
+ interface Props {
9
+ fields?: FieldDefinition[];
10
+ filters?: Filter[];
11
+ logicalOperator?: 'and' | 'or';
12
+ class?: string;
13
+ onApply?: (filters: Filter[]) => void;
14
+ onReset?: () => void;
15
+ }
16
+ declare const FilterBuilder: import("svelte").Component<Props, {
17
+ addRule: () => void;
18
+ removeRule: (index: number) => void;
19
+ reset: () => void;
20
+ apply: () => void;
21
+ }, "filters" | "logicalOperator">;
22
+ type FilterBuilder = ReturnType<typeof FilterBuilder>;
23
+ export default FilterBuilder;
@@ -18,6 +18,7 @@
18
18
 
19
19
  let selectedResource = $state('');
20
20
  let inferResult = $state<InferResult | null>(null);
21
+ let codeTarget = $state<'resource' | 'typebox' | 'list' | 'create' | 'edit' | 'show'>('resource');
21
22
  let loading = $state(false);
22
23
  let error = $state<string | null>(null);
23
24
  let copied = $state(false);
@@ -78,9 +79,19 @@
78
79
  }
79
80
  }
80
81
 
82
+ const activeCode = $derived.by(() => {
83
+ if (!inferResult) return '';
84
+ if (codeTarget === 'typebox') return inferResult.typeboxCode;
85
+ if (codeTarget === 'list') return inferResult.componentCode.list;
86
+ if (codeTarget === 'create') return inferResult.componentCode.create;
87
+ if (codeTarget === 'edit') return inferResult.componentCode.edit;
88
+ if (codeTarget === 'show') return inferResult.componentCode.show;
89
+ return inferResult.code;
90
+ });
91
+
81
92
  function copyCode() {
82
- if (!inferResult) return;
83
- navigator.clipboard.writeText(inferResult.code);
93
+ if (!activeCode) return;
94
+ navigator.clipboard.writeText(activeCode);
84
95
  copied = true;
85
96
  if (copyTimer) clearTimeout(copyTimer);
86
97
  copyTimer = setTimeout(() => { copied = false; }, 2000);
@@ -190,8 +201,57 @@
190
201
 
191
202
  <!-- Generated code -->
192
203
  <div class="rounded-lg border overflow-hidden">
193
- <div class="flex items-center justify-between bg-muted px-3 py-1.5 text-xs text-muted-foreground">
194
- <span>Generated ResourceDefinition</span>
204
+ <div class="flex flex-wrap items-center justify-between gap-1 bg-muted px-3 py-1.5 text-xs text-muted-foreground border-b">
205
+ <div class="flex items-center gap-1">
206
+ <Button
207
+ variant={codeTarget === 'resource' ? 'secondary' : 'ghost'}
208
+ size="sm"
209
+ class="h-6 text-xs px-2"
210
+ onclick={() => { codeTarget = 'resource'; }}
211
+ >
212
+ Resource (TS)
213
+ </Button>
214
+ <Button
215
+ variant={codeTarget === 'typebox' ? 'secondary' : 'ghost'}
216
+ size="sm"
217
+ class="h-6 text-xs px-2"
218
+ onclick={() => { codeTarget = 'typebox'; }}
219
+ >
220
+ TypeBox Schema
221
+ </Button>
222
+ <Button
223
+ variant={codeTarget === 'list' ? 'secondary' : 'ghost'}
224
+ size="sm"
225
+ class="h-6 text-xs px-2"
226
+ onclick={() => { codeTarget = 'list'; }}
227
+ >
228
+ List Page
229
+ </Button>
230
+ <Button
231
+ variant={codeTarget === 'create' ? 'secondary' : 'ghost'}
232
+ size="sm"
233
+ class="h-6 text-xs px-2"
234
+ onclick={() => { codeTarget = 'create'; }}
235
+ >
236
+ Create Form
237
+ </Button>
238
+ <Button
239
+ variant={codeTarget === 'edit' ? 'secondary' : 'ghost'}
240
+ size="sm"
241
+ class="h-6 text-xs px-2"
242
+ onclick={() => { codeTarget = 'edit'; }}
243
+ >
244
+ Edit Form
245
+ </Button>
246
+ <Button
247
+ variant={codeTarget === 'show' ? 'secondary' : 'ghost'}
248
+ size="sm"
249
+ class="h-6 text-xs px-2"
250
+ onclick={() => { codeTarget = 'show'; }}
251
+ >
252
+ Show Page
253
+ </Button>
254
+ </div>
195
255
  <Button variant="ghost" size="sm" class="h-6 text-xs gap-1" onclick={copyCode}>
196
256
  {#if copied}
197
257
  <Check class="h-3 w-3 text-success" />
@@ -202,11 +262,11 @@
202
262
  {/if}
203
263
  </Button>
204
264
  </div>
205
- <pre class="max-h-64 overflow-auto bg-muted/30 p-3 text-xs text-foreground font-mono">{inferResult.code}</pre>
265
+ <pre class="max-h-64 overflow-auto bg-muted/30 p-3 text-xs text-foreground font-mono">{activeCode}</pre>
206
266
  </div>
207
267
 
208
268
  <p class="text-xs text-muted-foreground">
209
- Inferred {inferResult.fields.length} fields from sample data. Copy the code above into your <code class="bg-muted px-1 rounded text-foreground">resources.ts</code> file.
269
+ Inferred {inferResult.fields.length} fields from sample data. Copy the code above into your Svelte 5 application files.
210
270
  </p>
211
271
  {/if}
212
272
  </Card.CardContent>