@svadmin/ui 0.120.0 → 0.121.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/app.css +136 -0
- package/dist/app.theme.css +136 -0
- package/dist/components/AboutSettings.svelte +1 -1
- package/dist/components/ContextMenu.svelte +268 -0
- package/dist/components/ContextMenu.svelte.d.ts +22 -0
- package/dist/components/JsonViewer.svelte +323 -0
- package/dist/components/JsonViewer.svelte.d.ts +20 -0
- package/dist/components/KpiCard.svelte +250 -0
- package/dist/components/KpiCard.svelte.d.ts +44 -0
- package/dist/components/OtpInput.svelte +209 -0
- package/dist/components/OtpInput.svelte.d.ts +17 -0
- package/dist/components/RecordCompareModal.svelte +428 -0
- package/dist/components/RecordCompareModal.svelte.d.ts +42 -0
- package/dist/components/ResultPage.svelte +166 -0
- package/dist/components/ResultPage.svelte.d.ts +28 -0
- package/dist/components/SignaturePad.svelte +230 -66
- package/dist/components/SignaturePad.svelte.d.ts +6 -5
- package/dist/components/Tour.svelte +71 -13
- package/dist/components/Tour.svelte.d.ts +1 -1
- package/dist/components/TrendIndicator.svelte +21 -13
- package/dist/components/TrendIndicator.svelte.d.ts +4 -2
- package/dist/components/UnsavedChangesGuard.svelte +138 -0
- package/dist/components/UnsavedChangesGuard.svelte.d.ts +32 -0
- package/dist/components/WatermarkContainer.svelte +196 -0
- package/dist/components/WatermarkContainer.svelte.d.ts +20 -0
- package/dist/components/useUnsavedChanges.d.svelte.ts +1 -0
- package/dist/components/useUnsavedChanges.svelte.d.ts +22 -0
- package/dist/components/useUnsavedChanges.svelte.js +105 -0
- package/dist/index.d.ts +19 -19
- package/dist/index.js +14 -2
- package/package.json +1 -1
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
import { AlertTriangle, AlertCircle } from '@lucide/svelte';
|
|
4
|
+
import TrendIndicator from './TrendIndicator.svelte';
|
|
5
|
+
import { cn } from '../utils.js';
|
|
6
|
+
|
|
7
|
+
export interface KpiCardProps {
|
|
8
|
+
/** KPI 标题 / 指标名称,如 "季度总营收"、"API 响应成功率" */
|
|
9
|
+
title: string;
|
|
10
|
+
/** 主指标数值,如 1284500 或 "99.98%" */
|
|
11
|
+
value: number | string;
|
|
12
|
+
/** 数值前缀,如 "¥"、"$" */
|
|
13
|
+
prefix?: string | undefined;
|
|
14
|
+
/** 数值后缀,如 "元"、"次"、"ms" */
|
|
15
|
+
suffix?: string | undefined;
|
|
16
|
+
/** 辅助副标题或时间跨度,如 "近 30 天"、"统计周期: Q3" */
|
|
17
|
+
subtitle?: string | undefined;
|
|
18
|
+
/** 上一期数值,若提供则自动使用 TrendIndicator 计算并展示同环比 */
|
|
19
|
+
prevValue?: number | undefined;
|
|
20
|
+
/** 同环比前缀,如 "YoY" 或 "环比" */
|
|
21
|
+
trendPrefix?: string | undefined;
|
|
22
|
+
/** 是否反转同环比色彩极性(如报错率降低为优) */
|
|
23
|
+
reverseTrendColor?: boolean | undefined;
|
|
24
|
+
/** 目标达成率:当前达成数值与目标数值,如 { current: 85, target: 100 } */
|
|
25
|
+
targetProgress?: { current: number; target: number; label?: string } | undefined;
|
|
26
|
+
/** 走势微图数据点(如近 7 天或 12 个月的数值数组,自动渲染纯 SVG 平滑 Sparkline) */
|
|
27
|
+
sparklineData?: number[] | undefined;
|
|
28
|
+
/** 走势微图色调:'primary' | 'success' | 'warning' | 'destructive' */
|
|
29
|
+
sparklineTone?: 'primary' | 'success' | 'warning' | 'destructive' | undefined;
|
|
30
|
+
/** 加载中状态(骨架屏态) */
|
|
31
|
+
loading?: boolean | undefined;
|
|
32
|
+
/** 预警状态:'normal' | 'warning' | 'danger' */
|
|
33
|
+
alertStatus?: 'normal' | 'warning' | 'danger' | undefined;
|
|
34
|
+
/** 异常预警提示信息 */
|
|
35
|
+
alertMessage?: string | undefined;
|
|
36
|
+
/** 徽标标签文本 */
|
|
37
|
+
badge?: string | undefined;
|
|
38
|
+
/** 自定义卡片右上角操作区或底部插槽 */
|
|
39
|
+
extra?: Snippet | undefined;
|
|
40
|
+
children?: Snippet | undefined;
|
|
41
|
+
class?: string | undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let {
|
|
45
|
+
title,
|
|
46
|
+
value,
|
|
47
|
+
prefix = '',
|
|
48
|
+
suffix = '',
|
|
49
|
+
subtitle,
|
|
50
|
+
prevValue,
|
|
51
|
+
trendPrefix = '环比',
|
|
52
|
+
reverseTrendColor = false,
|
|
53
|
+
targetProgress,
|
|
54
|
+
sparklineData,
|
|
55
|
+
sparklineTone = 'primary',
|
|
56
|
+
loading = false,
|
|
57
|
+
alertStatus = 'normal',
|
|
58
|
+
alertMessage,
|
|
59
|
+
badge,
|
|
60
|
+
extra,
|
|
61
|
+
children,
|
|
62
|
+
class: className = '',
|
|
63
|
+
}: KpiCardProps = $props();
|
|
64
|
+
|
|
65
|
+
const formattedValue = $derived.by(() => {
|
|
66
|
+
if (typeof value === 'number') {
|
|
67
|
+
return value.toLocaleString('en-US');
|
|
68
|
+
}
|
|
69
|
+
return String(value);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const progressPercent = $derived.by(() => {
|
|
73
|
+
if (!targetProgress || targetProgress.target <= 0) return 0;
|
|
74
|
+
const pct = (targetProgress.current / targetProgress.target) * 100;
|
|
75
|
+
return Math.max(0, Math.min(100, Math.round(pct)));
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const sparklinePoints = $derived.by(() => {
|
|
79
|
+
if (!sparklineData || sparklineData.length < 2) return '';
|
|
80
|
+
const min = Math.min(...sparklineData);
|
|
81
|
+
const max = Math.max(...sparklineData);
|
|
82
|
+
const range = max - min || 1;
|
|
83
|
+
const width = 120;
|
|
84
|
+
const height = 36;
|
|
85
|
+
const padding = 2;
|
|
86
|
+
|
|
87
|
+
const points = sparklineData.map((val, idx) => {
|
|
88
|
+
const x = padding + (idx / (sparklineData.length - 1)) * (width - padding * 2);
|
|
89
|
+
const y = height - padding - ((val - min) / range) * (height - padding * 2);
|
|
90
|
+
return `${x.toFixed(1)},${y.toFixed(1)}`;
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
return points.join(' ');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const sparklineStroke = $derived.by(() => {
|
|
97
|
+
switch (sparklineTone) {
|
|
98
|
+
case 'success':
|
|
99
|
+
return 'var(--success)';
|
|
100
|
+
case 'warning':
|
|
101
|
+
return 'var(--warning)';
|
|
102
|
+
case 'destructive':
|
|
103
|
+
return 'var(--destructive)';
|
|
104
|
+
case 'primary':
|
|
105
|
+
default:
|
|
106
|
+
return 'var(--primary)';
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const alertClasses = $derived.by(() => {
|
|
111
|
+
if (alertStatus === 'danger') {
|
|
112
|
+
return 'border-destructive/40 bg-destructive/5 ring-1 ring-destructive/20';
|
|
113
|
+
}
|
|
114
|
+
if (alertStatus === 'warning') {
|
|
115
|
+
return 'border-warning/40 bg-warning/5 ring-1 ring-warning/20';
|
|
116
|
+
}
|
|
117
|
+
return 'border-border bg-card';
|
|
118
|
+
});
|
|
119
|
+
</script>
|
|
120
|
+
|
|
121
|
+
<div
|
|
122
|
+
data-svadmin-component="KpiCard"
|
|
123
|
+
class={cn(
|
|
124
|
+
'relative flex flex-col justify-between rounded-xl border p-5 shadow-xs transition-all hover:shadow-sm',
|
|
125
|
+
alertClasses,
|
|
126
|
+
className
|
|
127
|
+
)}
|
|
128
|
+
>
|
|
129
|
+
{#if loading}
|
|
130
|
+
<!-- Loading skeleton state -->
|
|
131
|
+
<div class="space-y-4 animate-pulse">
|
|
132
|
+
<div class="flex items-center justify-between">
|
|
133
|
+
<div class="h-4 w-28 bg-muted rounded-md"></div>
|
|
134
|
+
<div class="h-4 w-12 bg-muted rounded-md"></div>
|
|
135
|
+
</div>
|
|
136
|
+
<div class="h-8 w-44 bg-muted rounded-md"></div>
|
|
137
|
+
<div class="h-3 w-32 bg-muted rounded-md"></div>
|
|
138
|
+
</div>
|
|
139
|
+
{:else}
|
|
140
|
+
<!-- Header -->
|
|
141
|
+
<div class="flex items-start justify-between gap-3">
|
|
142
|
+
<div class="space-y-1">
|
|
143
|
+
<div class="flex items-center gap-2">
|
|
144
|
+
<span class="text-xs font-medium text-muted-foreground tracking-wide">{title}</span>
|
|
145
|
+
{#if badge}
|
|
146
|
+
<span class="rounded-full bg-primary/10 px-2 py-0.5 text-[10px] font-medium text-primary">
|
|
147
|
+
{badge}
|
|
148
|
+
</span>
|
|
149
|
+
{/if}
|
|
150
|
+
</div>
|
|
151
|
+
{#if subtitle}
|
|
152
|
+
<p class="text-[11px] text-muted-foreground/80">{subtitle}</p>
|
|
153
|
+
{/if}
|
|
154
|
+
</div>
|
|
155
|
+
|
|
156
|
+
{#if extra}
|
|
157
|
+
<div class="shrink-0 text-xs">
|
|
158
|
+
{@render extra()}
|
|
159
|
+
</div>
|
|
160
|
+
{/if}
|
|
161
|
+
</div>
|
|
162
|
+
|
|
163
|
+
<!-- Main Metric Value & Trend / Sparkline -->
|
|
164
|
+
<div class="my-3 flex items-baseline justify-between gap-4">
|
|
165
|
+
<div class="flex items-baseline gap-1 font-mono tracking-tight">
|
|
166
|
+
{#if prefix}
|
|
167
|
+
<span class="text-base font-semibold text-muted-foreground">{prefix}</span>
|
|
168
|
+
{/if}
|
|
169
|
+
<span class="text-3xl font-bold text-foreground">{formattedValue}</span>
|
|
170
|
+
{#if suffix}
|
|
171
|
+
<span class="text-xs font-medium text-muted-foreground">{suffix}</span>
|
|
172
|
+
{/if}
|
|
173
|
+
</div>
|
|
174
|
+
|
|
175
|
+
<!-- Sparkline Mini Chart -->
|
|
176
|
+
{#if sparklinePoints}
|
|
177
|
+
<div class="shrink-0 flex items-center" title="历史走势">
|
|
178
|
+
<svg class="h-9 w-28 overflow-visible" viewBox="0 0 120 36">
|
|
179
|
+
<polyline
|
|
180
|
+
fill="none"
|
|
181
|
+
stroke={sparklineStroke}
|
|
182
|
+
stroke-width="2"
|
|
183
|
+
stroke-linecap="round"
|
|
184
|
+
stroke-linejoin="round"
|
|
185
|
+
points={sparklinePoints}
|
|
186
|
+
/>
|
|
187
|
+
</svg>
|
|
188
|
+
</div>
|
|
189
|
+
{/if}
|
|
190
|
+
</div>
|
|
191
|
+
|
|
192
|
+
<!-- Trend, Progress & Alerts -->
|
|
193
|
+
<div class="space-y-2.5 pt-1">
|
|
194
|
+
{#if typeof prevValue === 'number' && typeof value === 'number'}
|
|
195
|
+
<div class="flex items-center gap-2 text-xs">
|
|
196
|
+
<TrendIndicator
|
|
197
|
+
{value}
|
|
198
|
+
{prevValue}
|
|
199
|
+
prefix={trendPrefix}
|
|
200
|
+
reverseColor={reverseTrendColor}
|
|
201
|
+
variant="badge"
|
|
202
|
+
size="sm"
|
|
203
|
+
/>
|
|
204
|
+
<span class="text-[11px] text-muted-foreground">对比上一周期</span>
|
|
205
|
+
</div>
|
|
206
|
+
{/if}
|
|
207
|
+
|
|
208
|
+
<!-- Target Completion Progress Bar -->
|
|
209
|
+
{#if targetProgress}
|
|
210
|
+
<div class="space-y-1">
|
|
211
|
+
<div class="flex items-center justify-between text-[11px] text-muted-foreground font-mono">
|
|
212
|
+
<span>{targetProgress.label ?? '目标达成'}</span>
|
|
213
|
+
<span class="font-semibold text-foreground">{progressPercent}%</span>
|
|
214
|
+
</div>
|
|
215
|
+
<div class="h-1.5 w-full overflow-hidden rounded-full bg-muted">
|
|
216
|
+
<div
|
|
217
|
+
class="h-full rounded-full bg-primary transition-all duration-500 ease-out"
|
|
218
|
+
style:width={`${progressPercent}%`}
|
|
219
|
+
></div>
|
|
220
|
+
</div>
|
|
221
|
+
</div>
|
|
222
|
+
{/if}
|
|
223
|
+
|
|
224
|
+
<!-- Alert warning banner -->
|
|
225
|
+
{#if alertMessage}
|
|
226
|
+
<div
|
|
227
|
+
class={cn(
|
|
228
|
+
'flex items-center gap-1.5 rounded-lg px-2.5 py-1.5 text-xs',
|
|
229
|
+
alertStatus === 'danger'
|
|
230
|
+
? 'bg-destructive/10 text-destructive'
|
|
231
|
+
: 'bg-warning/10 text-warning'
|
|
232
|
+
)}
|
|
233
|
+
>
|
|
234
|
+
{#if alertStatus === 'danger'}
|
|
235
|
+
<AlertCircle class="h-3.5 w-3.5 shrink-0" />
|
|
236
|
+
{:else}
|
|
237
|
+
<AlertTriangle class="h-3.5 w-3.5 shrink-0" />
|
|
238
|
+
{/if}
|
|
239
|
+
<span class="truncate">{alertMessage}</span>
|
|
240
|
+
</div>
|
|
241
|
+
{/if}
|
|
242
|
+
|
|
243
|
+
{#if children}
|
|
244
|
+
<div class="pt-2 border-t border-border/50 text-xs">
|
|
245
|
+
{@render children()}
|
|
246
|
+
</div>
|
|
247
|
+
{/if}
|
|
248
|
+
</div>
|
|
249
|
+
{/if}
|
|
250
|
+
</div>
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
export interface KpiCardProps {
|
|
3
|
+
/** KPI 标题 / 指标名称,如 "季度总营收"、"API 响应成功率" */
|
|
4
|
+
title: string;
|
|
5
|
+
/** 主指标数值,如 1284500 或 "99.98%" */
|
|
6
|
+
value: number | string;
|
|
7
|
+
/** 数值前缀,如 "¥"、"$" */
|
|
8
|
+
prefix?: string | undefined;
|
|
9
|
+
/** 数值后缀,如 "元"、"次"、"ms" */
|
|
10
|
+
suffix?: string | undefined;
|
|
11
|
+
/** 辅助副标题或时间跨度,如 "近 30 天"、"统计周期: Q3" */
|
|
12
|
+
subtitle?: string | undefined;
|
|
13
|
+
/** 上一期数值,若提供则自动使用 TrendIndicator 计算并展示同环比 */
|
|
14
|
+
prevValue?: number | undefined;
|
|
15
|
+
/** 同环比前缀,如 "YoY" 或 "环比" */
|
|
16
|
+
trendPrefix?: string | undefined;
|
|
17
|
+
/** 是否反转同环比色彩极性(如报错率降低为优) */
|
|
18
|
+
reverseTrendColor?: boolean | undefined;
|
|
19
|
+
/** 目标达成率:当前达成数值与目标数值,如 { current: 85, target: 100 } */
|
|
20
|
+
targetProgress?: {
|
|
21
|
+
current: number;
|
|
22
|
+
target: number;
|
|
23
|
+
label?: string;
|
|
24
|
+
} | undefined;
|
|
25
|
+
/** 走势微图数据点(如近 7 天或 12 个月的数值数组,自动渲染纯 SVG 平滑 Sparkline) */
|
|
26
|
+
sparklineData?: number[] | undefined;
|
|
27
|
+
/** 走势微图色调:'primary' | 'success' | 'warning' | 'destructive' */
|
|
28
|
+
sparklineTone?: 'primary' | 'success' | 'warning' | 'destructive' | undefined;
|
|
29
|
+
/** 加载中状态(骨架屏态) */
|
|
30
|
+
loading?: boolean | undefined;
|
|
31
|
+
/** 预警状态:'normal' | 'warning' | 'danger' */
|
|
32
|
+
alertStatus?: 'normal' | 'warning' | 'danger' | undefined;
|
|
33
|
+
/** 异常预警提示信息 */
|
|
34
|
+
alertMessage?: string | undefined;
|
|
35
|
+
/** 徽标标签文本 */
|
|
36
|
+
badge?: string | undefined;
|
|
37
|
+
/** 自定义卡片右上角操作区或底部插槽 */
|
|
38
|
+
extra?: Snippet | undefined;
|
|
39
|
+
children?: Snippet | undefined;
|
|
40
|
+
class?: string | undefined;
|
|
41
|
+
}
|
|
42
|
+
declare const KpiCard: import("svelte").Component<KpiCardProps, {}, "">;
|
|
43
|
+
type KpiCard = ReturnType<typeof KpiCard>;
|
|
44
|
+
export default KpiCard;
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { onMount } from 'svelte';
|
|
3
|
+
import { cn } from '../utils.js';
|
|
4
|
+
|
|
5
|
+
export interface OtpInputProps {
|
|
6
|
+
length?: number;
|
|
7
|
+
value?: string | string[];
|
|
8
|
+
type?: 'number' | 'text' | 'alphanumeric';
|
|
9
|
+
mask?: boolean;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
error?: boolean;
|
|
12
|
+
autoFocus?: boolean;
|
|
13
|
+
size?: 'sm' | 'md' | 'lg';
|
|
14
|
+
class?: string;
|
|
15
|
+
'aria-label'?: string;
|
|
16
|
+
onchange?: (value: string) => void;
|
|
17
|
+
oncomplete?: (value: string) => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let {
|
|
21
|
+
length = 6,
|
|
22
|
+
value = $bindable(''),
|
|
23
|
+
type = 'alphanumeric',
|
|
24
|
+
mask = false,
|
|
25
|
+
disabled = false,
|
|
26
|
+
error = false,
|
|
27
|
+
autoFocus = false,
|
|
28
|
+
size = 'md',
|
|
29
|
+
class: className = '',
|
|
30
|
+
'aria-label': groupAriaLabel = 'OTP verification code input',
|
|
31
|
+
onchange,
|
|
32
|
+
oncomplete,
|
|
33
|
+
}: OtpInputProps = $props();
|
|
34
|
+
|
|
35
|
+
let digits = $state<string[]>([]);
|
|
36
|
+
const inputRefs: (HTMLInputElement | null)[] = [];
|
|
37
|
+
|
|
38
|
+
function registerInput(node: HTMLInputElement, index: number) {
|
|
39
|
+
inputRefs[index] = node;
|
|
40
|
+
return {
|
|
41
|
+
destroy() {
|
|
42
|
+
if (inputRefs[index] === node) {
|
|
43
|
+
inputRefs[index] = null;
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function isValidChar(char: string): boolean {
|
|
50
|
+
if (type === 'number') {
|
|
51
|
+
return /^[0-9]$/.test(char);
|
|
52
|
+
}
|
|
53
|
+
if (type === 'alphanumeric') {
|
|
54
|
+
return /^[a-zA-Z0-9]$/.test(char);
|
|
55
|
+
}
|
|
56
|
+
return char.length === 1;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function getRawStringValue(): string {
|
|
60
|
+
if (Array.isArray(value)) {
|
|
61
|
+
return value.join('');
|
|
62
|
+
}
|
|
63
|
+
return value || '';
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function syncFromValue() {
|
|
67
|
+
const raw = getRawStringValue();
|
|
68
|
+
const chars = raw.split('').slice(0, length);
|
|
69
|
+
const newDigits = Array.from({ length }, (_, i) => chars[i] || '');
|
|
70
|
+
digits = newDigits;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function notifyChange() {
|
|
74
|
+
const combined = digits.join('');
|
|
75
|
+
if (Array.isArray(value)) {
|
|
76
|
+
value = [...digits];
|
|
77
|
+
} else {
|
|
78
|
+
value = combined;
|
|
79
|
+
}
|
|
80
|
+
onchange?.(combined);
|
|
81
|
+
if (combined.length === length && digits.every((d) => d !== '')) {
|
|
82
|
+
oncomplete?.(combined);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function handleInput(e: Event, index: number) {
|
|
87
|
+
const input = e.target as HTMLInputElement;
|
|
88
|
+
const rawVal = input.value;
|
|
89
|
+
const char = rawVal.slice(-1);
|
|
90
|
+
|
|
91
|
+
if (char && isValidChar(char)) {
|
|
92
|
+
digits[index] = char;
|
|
93
|
+
if (index < length - 1) {
|
|
94
|
+
inputRefs[index + 1]?.focus();
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
digits[index] = '';
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
notifyChange();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function handleKeyDown(e: KeyboardEvent, index: number) {
|
|
104
|
+
if (e.key === 'Backspace') {
|
|
105
|
+
if (digits[index] !== '') {
|
|
106
|
+
digits[index] = '';
|
|
107
|
+
notifyChange();
|
|
108
|
+
} else if (index > 0) {
|
|
109
|
+
digits[index - 1] = '';
|
|
110
|
+
inputRefs[index - 1]?.focus();
|
|
111
|
+
notifyChange();
|
|
112
|
+
}
|
|
113
|
+
e.preventDefault();
|
|
114
|
+
} else if (e.key === 'ArrowLeft') {
|
|
115
|
+
if (index > 0) {
|
|
116
|
+
inputRefs[index - 1]?.focus();
|
|
117
|
+
e.preventDefault();
|
|
118
|
+
}
|
|
119
|
+
} else if (e.key === 'ArrowRight') {
|
|
120
|
+
if (index < length - 1) {
|
|
121
|
+
inputRefs[index + 1]?.focus();
|
|
122
|
+
e.preventDefault();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function handlePaste(e: ClipboardEvent) {
|
|
128
|
+
e.preventDefault();
|
|
129
|
+
const pastedText = e.clipboardData?.getData('text') || '';
|
|
130
|
+
if (!pastedText) return;
|
|
131
|
+
|
|
132
|
+
let validChars = '';
|
|
133
|
+
for (const char of pastedText) {
|
|
134
|
+
if (isValidChar(char)) {
|
|
135
|
+
validChars += char;
|
|
136
|
+
if (validChars.length === length) break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (!validChars) return;
|
|
141
|
+
|
|
142
|
+
const newDigits = [...digits];
|
|
143
|
+
for (let i = 0; i < length; i++) {
|
|
144
|
+
const char = validChars[i];
|
|
145
|
+
if (char) {
|
|
146
|
+
newDigits[i] = char;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
digits = newDigits;
|
|
150
|
+
notifyChange();
|
|
151
|
+
|
|
152
|
+
const nextIndex = Math.min(validChars.length, length - 1);
|
|
153
|
+
inputRefs[nextIndex]?.focus();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
$effect(() => {
|
|
157
|
+
const raw = getRawStringValue();
|
|
158
|
+
if (digits.length !== length || raw !== digits.join('')) {
|
|
159
|
+
syncFromValue();
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
onMount(() => {
|
|
164
|
+
syncFromValue();
|
|
165
|
+
if (autoFocus && inputRefs[0]) {
|
|
166
|
+
inputRefs[0].focus();
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
const sizeClasses = {
|
|
171
|
+
sm: 'h-8 w-8 text-sm',
|
|
172
|
+
md: 'h-10 w-10 text-base',
|
|
173
|
+
lg: 'h-12 w-12 text-lg',
|
|
174
|
+
};
|
|
175
|
+
</script>
|
|
176
|
+
|
|
177
|
+
<div
|
|
178
|
+
class={cn('inline-flex items-center gap-2', className)}
|
|
179
|
+
data-svadmin-component="OtpInput"
|
|
180
|
+
role="group"
|
|
181
|
+
aria-label={groupAriaLabel}
|
|
182
|
+
>
|
|
183
|
+
{#each digits as digit, index (index)}
|
|
184
|
+
<input
|
|
185
|
+
use:registerInput={index}
|
|
186
|
+
type={mask ? 'password' : 'text'}
|
|
187
|
+
inputmode={type === 'number' ? 'numeric' : 'text'}
|
|
188
|
+
pattern={type === 'number' ? '[0-9]*' : undefined}
|
|
189
|
+
maxlength={1}
|
|
190
|
+
value={digit}
|
|
191
|
+
{disabled}
|
|
192
|
+
aria-label={`Digit ${index + 1} of ${length}`}
|
|
193
|
+
aria-invalid={error ? 'true' : undefined}
|
|
194
|
+
class={cn(
|
|
195
|
+
'rounded-md border text-center font-mono font-semibold transition-all outline-none',
|
|
196
|
+
'focus:ring-2 focus:ring-primary focus:ring-offset-2',
|
|
197
|
+
sizeClasses[size],
|
|
198
|
+
error
|
|
199
|
+
? 'border-destructive text-destructive focus:ring-destructive'
|
|
200
|
+
: 'border-input bg-background text-foreground',
|
|
201
|
+
disabled ? 'cursor-not-allowed bg-muted opacity-50' : 'cursor-text'
|
|
202
|
+
)}
|
|
203
|
+
oninput={(e) => handleInput(e, index)}
|
|
204
|
+
onkeydown={(e) => handleKeyDown(e, index)}
|
|
205
|
+
onpaste={handlePaste}
|
|
206
|
+
onfocus={(e) => (e.target as HTMLInputElement).select()}
|
|
207
|
+
/>
|
|
208
|
+
{/each}
|
|
209
|
+
</div>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface OtpInputProps {
|
|
2
|
+
length?: number;
|
|
3
|
+
value?: string | string[];
|
|
4
|
+
type?: 'number' | 'text' | 'alphanumeric';
|
|
5
|
+
mask?: boolean;
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
error?: boolean;
|
|
8
|
+
autoFocus?: boolean;
|
|
9
|
+
size?: 'sm' | 'md' | 'lg';
|
|
10
|
+
class?: string;
|
|
11
|
+
'aria-label'?: string;
|
|
12
|
+
onchange?: (value: string) => void;
|
|
13
|
+
oncomplete?: (value: string) => void;
|
|
14
|
+
}
|
|
15
|
+
declare const OtpInput: import("svelte").Component<OtpInputProps, {}, "value">;
|
|
16
|
+
type OtpInput = ReturnType<typeof OtpInput>;
|
|
17
|
+
export default OtpInput;
|