@sonamu-kit/react-components 0.4.7 → 0.4.9
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/components/ui/calendar.js +12 -6
- package/dist/components/ui/calendar.js.map +1 -1
- package/dist/components/ui/carousel.js +12 -16
- package/dist/components/ui/carousel.js.map +1 -1
- package/dist/components/ui/common-modal.d.ts +6 -6
- package/dist/components/ui/common-modal.js.map +1 -1
- package/dist/components/ui/date-input.js +6 -5
- package/dist/components/ui/date-input.js.map +1 -1
- package/dist/components/ui/file-input.js +131 -130
- package/dist/components/ui/file-input.js.map +1 -1
- package/dist/components/ui/input.js +21 -20
- package/dist/components/ui/input.js.map +1 -1
- package/dist/components/ui/month-picker-multiple.js +31 -19
- package/dist/components/ui/month-picker-multiple.js.map +1 -1
- package/dist/components/ui/pagination.js +18 -17
- package/dist/components/ui/pagination.js.map +1 -1
- package/dist/components/ui/select/enum-select.js +4 -2
- package/dist/components/ui/select/enum-select.js.map +1 -1
- package/dist/components/ui/select/id-async-select.d.ts +32 -14
- package/dist/components/ui/select/id-async-select.js +185 -144
- package/dist/components/ui/select/id-async-select.js.map +1 -1
- package/dist/components/ui/select/select.js +199 -158
- package/dist/components/ui/select/select.js.map +1 -1
- package/dist/components/ui/sidebar.d.ts +2 -1
- package/dist/components/ui/sidebar.js +54 -45
- package/dist/components/ui/sidebar.js.map +1 -1
- package/dist/components/ui/sonamu-filter/constants.d.ts +27 -3
- package/dist/components/ui/sonamu-filter/constants.js.map +1 -1
- package/dist/components/ui/sonamu-filter/rule-row-input.js +18 -15
- package/dist/components/ui/sonamu-filter/rule-row-input.js.map +1 -1
- package/dist/components/ui/sonamu-filter/sonamu-filter-modal.js +34 -37
- package/dist/components/ui/sonamu-filter/sonamu-filter-modal.js.map +1 -1
- package/dist/components/ui/sonamu-filter/sonamu-filter-popover.js +17 -12
- package/dist/components/ui/sonamu-filter/sonamu-filter-popover.js.map +1 -1
- package/dist/components/ui/sonamu-filter/types.d.ts +14 -9
- package/dist/components/ui/sonamu-filter/utils.d.ts +9 -7
- package/dist/components/ui/sonamu-filter/utils.js +39 -20
- package/dist/components/ui/sonamu-filter/utils.js.map +1 -1
- package/dist/components/ui/sonamu-filter/value-input.js +65 -50
- package/dist/components/ui/sonamu-filter/value-input.js.map +1 -1
- package/dist/contexts/sonamu-context.js +34 -10
- package/dist/contexts/sonamu-context.js.map +1 -1
- package/dist/demo/components/FormDebugPanel.d.ts +1 -1
- package/dist/hooks/use-toast.d.ts +1 -1
- package/dist/hooks/use-toast.js.map +1 -1
- package/dist/lib/base-helpers.d.ts +1 -1
- package/dist/lib/base-helpers.js +7 -7
- package/dist/lib/base-helpers.js.map +1 -1
- package/dist/lib/caster.d.ts +3 -2
- package/dist/lib/caster.js +37 -19
- package/dist/lib/caster.js.map +1 -1
- package/dist/lib/form-helpers.js +38 -35
- package/dist/lib/form-helpers.js.map +1 -1
- package/dist/lib/list-helpers.d.ts +2 -2
- package/dist/lib/list-helpers.js +33 -36
- package/dist/lib/list-helpers.js.map +1 -1
- package/dist/lib/use-mobile.js +5 -7
- package/dist/lib/use-mobile.js.map +1 -1
- package/dist/lib/utils.d.ts +0 -14
- package/dist/lib/utils.js +24 -14
- package/dist/lib/utils.js.map +1 -1
- package/dist/styles/globals.css +38 -106
- package/package.json +5 -3
|
@@ -5,257 +5,258 @@ import i from "../../_virtual/~icons/lucide/x.js";
|
|
|
5
5
|
import a from "../../_virtual/~icons/lucide/image.js";
|
|
6
6
|
import o from "../../_virtual/~icons/lucide/loader2.js";
|
|
7
7
|
import s from "../../_virtual/~icons/lucide/upload.js";
|
|
8
|
-
import { useCallback as c,
|
|
9
|
-
import { Fragment as
|
|
10
|
-
import {
|
|
8
|
+
import { useCallback as c, useMemo as l, useRef as u, useState as d } from "react";
|
|
9
|
+
import { Fragment as f, jsx as p, jsxs as m } from "react/jsx-runtime";
|
|
10
|
+
import { isFunction as h } from "radashi";
|
|
11
|
+
import { FileIcon as g, defaultStyles as _ } from "react-file-icon";
|
|
11
12
|
//#region src/components/ui/file-input.tsx
|
|
12
|
-
function
|
|
13
|
+
function v(e) {
|
|
14
|
+
return Object.prototype.toString.call(e) === "[object File]";
|
|
15
|
+
}
|
|
16
|
+
function y(e) {
|
|
13
17
|
let t = e.split(".");
|
|
14
18
|
return t.length < 2 ? "" : t[t.length - 1].toLowerCase();
|
|
15
19
|
}
|
|
16
|
-
var
|
|
20
|
+
var b = {
|
|
17
21
|
sm: 14,
|
|
18
22
|
md: 18,
|
|
19
23
|
lg: 22,
|
|
20
24
|
xl: 26
|
|
21
|
-
};
|
|
22
|
-
function
|
|
23
|
-
let n =
|
|
24
|
-
return /* @__PURE__ */
|
|
25
|
+
}, x = new Map(Object.entries(_));
|
|
26
|
+
function S({ filename: e, size: t = "md" }) {
|
|
27
|
+
let n = y(e), r = x.get(n) ?? {};
|
|
28
|
+
return /* @__PURE__ */ p("div", {
|
|
25
29
|
className: "shrink-0 self-center",
|
|
26
30
|
style: {
|
|
27
|
-
width:
|
|
31
|
+
width: b[t],
|
|
28
32
|
marginTop: -2
|
|
29
33
|
},
|
|
30
|
-
children: /* @__PURE__ */
|
|
34
|
+
children: /* @__PURE__ */ p(g, {
|
|
31
35
|
extension: n || void 0,
|
|
32
36
|
...r
|
|
33
37
|
})
|
|
34
38
|
});
|
|
35
39
|
}
|
|
36
|
-
var
|
|
40
|
+
var C = {
|
|
37
41
|
sm: "w-20 h-20",
|
|
38
42
|
md: "w-32 h-32",
|
|
39
43
|
lg: "w-40 h-40",
|
|
40
44
|
xl: "w-48 h-48"
|
|
41
|
-
},
|
|
45
|
+
}, w = {
|
|
42
46
|
sm: "w-48 h-12",
|
|
43
47
|
md: "w-80 h-16",
|
|
44
48
|
lg: "w-96 h-20",
|
|
45
49
|
xl: "w-[28rem] h-24"
|
|
46
50
|
};
|
|
47
|
-
function
|
|
48
|
-
let { placeholder:
|
|
49
|
-
if (!
|
|
50
|
-
let e =
|
|
51
|
+
function T(g) {
|
|
52
|
+
let { placeholder: _, disabled: y = !1, className: b, previewSize: x = "md", uploadMode: T, viewMode: E, uploadParams: D, onBlur: O } = g, { uploader: k, SD: A } = r(), j = g.multiple ?? !1, M = E === "image", N = g.accept ?? (M ? "image/*" : "*/*"), P = u(null), [F, I] = d(!1), [L, R] = d(!1), z = _ ?? `${A(M ? "rc.fileInput.imagePlaceholder" : "rc.fileInput.filePlaceholder")} URL${j ? "S" : ""}`, B = l(() => {
|
|
53
|
+
if (g.multiple !== !0) {
|
|
54
|
+
let e = g.value;
|
|
51
55
|
return e ? [e] : [];
|
|
52
56
|
}
|
|
53
|
-
return
|
|
54
|
-
})
|
|
55
|
-
url:
|
|
57
|
+
return g.value ?? [];
|
|
58
|
+
}, [g.multiple, g.value]), V = l(() => B.filter(v), [B]), H = t(V), U = B.map((e) => v(e) ? {
|
|
59
|
+
url: H[V.indexOf(e)],
|
|
56
60
|
name: e.name,
|
|
57
61
|
isPending: !0
|
|
58
62
|
} : {
|
|
59
63
|
url: e.url,
|
|
60
64
|
name: e.name,
|
|
61
65
|
isPending: !1
|
|
62
|
-
}),
|
|
63
|
-
if (e.length === 0 ||
|
|
64
|
-
let t =
|
|
66
|
+
}), W = g.multiple === !0 ? g.maxFiles ?? 10 : 1, G = B.length, K = G < W, q = c(async (e) => {
|
|
67
|
+
if (e.length === 0 || y) return;
|
|
68
|
+
let t = W - G;
|
|
65
69
|
if (t <= 0) {
|
|
66
|
-
|
|
70
|
+
let e = A("rc.fileInput.maxFilesExceeded");
|
|
71
|
+
alert(h(e) ? e(W) : e);
|
|
67
72
|
return;
|
|
68
73
|
}
|
|
69
74
|
let n = e.slice(0, t);
|
|
70
|
-
if (
|
|
71
|
-
|
|
75
|
+
if (T === "eager") {
|
|
76
|
+
I(!0);
|
|
72
77
|
try {
|
|
73
|
-
let e = await
|
|
74
|
-
if (
|
|
75
|
-
let t = [...
|
|
76
|
-
|
|
77
|
-
} else
|
|
78
|
+
let e = await k(n, D);
|
|
79
|
+
if (g.multiple === !0) {
|
|
80
|
+
let t = [...B, ...e];
|
|
81
|
+
g.onValueChange?.(t);
|
|
82
|
+
} else g.onValueChange?.(e[0] ?? null);
|
|
78
83
|
} catch (e) {
|
|
79
|
-
console.error("Upload failed:", e), alert(
|
|
84
|
+
console.error("Upload failed:", e), alert(A("rc.fileInput.uploadFailed"));
|
|
80
85
|
} finally {
|
|
81
|
-
|
|
86
|
+
I(!1);
|
|
82
87
|
}
|
|
83
|
-
} else if (
|
|
84
|
-
let e = [...
|
|
85
|
-
|
|
86
|
-
} else
|
|
87
|
-
}, [
|
|
88
|
-
g,
|
|
89
|
-
H,
|
|
90
|
-
V,
|
|
91
|
-
O,
|
|
92
|
-
S,
|
|
93
|
-
w,
|
|
94
|
-
L,
|
|
95
|
-
m,
|
|
96
|
-
E
|
|
97
|
-
]), G = c((e) => {
|
|
98
|
-
W(Array.from(e.target.files || [])), j.current && (j.current.value = "");
|
|
99
|
-
}, [W]), K = c((e) => {
|
|
100
|
-
e.preventDefault(), F(!1), !(g || M) && W(Array.from(e.dataTransfer.files));
|
|
88
|
+
} else if (g.multiple === !0) {
|
|
89
|
+
let e = [...B, ...n];
|
|
90
|
+
g.onValueChange?.(e);
|
|
91
|
+
} else g.onValueChange?.(n[0] ?? null);
|
|
101
92
|
}, [
|
|
93
|
+
y,
|
|
94
|
+
G,
|
|
102
95
|
W,
|
|
96
|
+
T,
|
|
97
|
+
D,
|
|
98
|
+
B,
|
|
103
99
|
g,
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
100
|
+
k,
|
|
101
|
+
A
|
|
102
|
+
]), J = c((e) => {
|
|
103
|
+
q(Array.from(e.target.files || [])), P.current && (P.current.value = "");
|
|
104
|
+
}, [q]), Y = c((e) => {
|
|
105
|
+
e.preventDefault(), R(!1), !(y || F) && q(Array.from(e.dataTransfer.files));
|
|
106
|
+
}, [
|
|
107
|
+
q,
|
|
108
|
+
y,
|
|
109
|
+
F
|
|
110
|
+
]), X = c((e) => (t) => {
|
|
111
|
+
if (t.stopPropagation(), g.multiple !== !0) g.onValueChange?.(null);
|
|
107
112
|
else {
|
|
108
|
-
let t =
|
|
109
|
-
|
|
113
|
+
let t = B.filter((t, n) => n !== e);
|
|
114
|
+
g.onValueChange?.(t);
|
|
110
115
|
}
|
|
111
|
-
}, [
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
return /* @__PURE__ */ p("div", {
|
|
121
|
-
className: e("relative inline-block", _),
|
|
122
|
-
children: [/* @__PURE__ */ f("input", {
|
|
123
|
-
ref: j,
|
|
116
|
+
}, [B, g]), Z = () => {
|
|
117
|
+
!y && !F && P.current?.click();
|
|
118
|
+
}, Q = M ? C : w;
|
|
119
|
+
if (!j && U.length > 0) {
|
|
120
|
+
let t = U[0];
|
|
121
|
+
return /* @__PURE__ */ m("div", {
|
|
122
|
+
className: e("relative inline-block", b),
|
|
123
|
+
children: [/* @__PURE__ */ p("input", {
|
|
124
|
+
ref: P,
|
|
124
125
|
type: "file",
|
|
125
|
-
accept:
|
|
126
|
-
onChange:
|
|
127
|
-
onBlur:
|
|
128
|
-
disabled:
|
|
126
|
+
accept: N,
|
|
127
|
+
onChange: J,
|
|
128
|
+
onBlur: O,
|
|
129
|
+
disabled: y || F,
|
|
129
130
|
className: "sr-only"
|
|
130
|
-
}), /* @__PURE__ */
|
|
131
|
-
onClick:
|
|
132
|
-
onDrop:
|
|
131
|
+
}), /* @__PURE__ */ p("div", {
|
|
132
|
+
onClick: Z,
|
|
133
|
+
onDrop: Y,
|
|
133
134
|
onDragOver: (e) => {
|
|
134
|
-
e.preventDefault(), !
|
|
135
|
+
e.preventDefault(), !y && !F && R(!0);
|
|
135
136
|
},
|
|
136
137
|
onDragLeave: (e) => {
|
|
137
|
-
e.preventDefault(),
|
|
138
|
+
e.preventDefault(), R(!1);
|
|
138
139
|
},
|
|
139
|
-
className: e("relative flex items-center justify-center rounded-lg border-2 cursor-pointer transition-all",
|
|
140
|
-
children:
|
|
140
|
+
className: e("relative flex items-center justify-center rounded-lg border-2 cursor-pointer transition-all", Q[x], M ? "border-transparent" : "border-dashed border-muted-foreground/25 hover:border-muted-foreground/50", L && "border-primary bg-primary/5", y && "opacity-50 cursor-not-allowed", F && "cursor-wait"),
|
|
141
|
+
children: F ? /* @__PURE__ */ m("div", {
|
|
141
142
|
className: "flex flex-col items-center gap-2 text-muted-foreground",
|
|
142
|
-
children: [/* @__PURE__ */
|
|
143
|
+
children: [/* @__PURE__ */ p(o, { className: "h-6 w-6 animate-spin" }), /* @__PURE__ */ p("span", {
|
|
143
144
|
className: "text-xs",
|
|
144
|
-
children:
|
|
145
|
+
children: A("rc.fileInput.uploading")
|
|
145
146
|
})]
|
|
146
|
-
}) : /* @__PURE__ */
|
|
147
|
-
|
|
147
|
+
}) : /* @__PURE__ */ m(f, { children: [
|
|
148
|
+
M ? /* @__PURE__ */ p("img", {
|
|
148
149
|
src: t.url,
|
|
149
150
|
alt: "Preview",
|
|
150
151
|
className: "h-full w-full object-cover rounded-lg overflow-hidden"
|
|
151
|
-
}) : /* @__PURE__ */
|
|
152
|
+
}) : /* @__PURE__ */ m("div", {
|
|
152
153
|
className: "flex items-center gap-2 px-3 h-full w-full min-w-0 overflow-hidden",
|
|
153
154
|
children: [
|
|
154
|
-
/* @__PURE__ */
|
|
155
|
+
/* @__PURE__ */ p(S, {
|
|
155
156
|
filename: t.name,
|
|
156
157
|
size: x
|
|
157
158
|
}),
|
|
158
|
-
/* @__PURE__ */
|
|
159
|
+
/* @__PURE__ */ p("span", {
|
|
159
160
|
className: e("text-xs truncate min-w-0", t.isPending ? "w-[140px] shrink-0" : "flex-1"),
|
|
160
161
|
children: t.name
|
|
161
162
|
}),
|
|
162
|
-
t.isPending && /* @__PURE__ */
|
|
163
|
+
t.isPending && /* @__PURE__ */ p("span", {
|
|
163
164
|
className: "px-2 py-0.5 bg-yellow-500/90 text-white text-xs rounded whitespace-nowrap shrink-0",
|
|
164
|
-
children:
|
|
165
|
+
children: A("rc.fileInput.pending")
|
|
165
166
|
})
|
|
166
167
|
]
|
|
167
168
|
}),
|
|
168
|
-
!
|
|
169
|
+
!y && /* @__PURE__ */ p(n, {
|
|
169
170
|
type: "button",
|
|
170
171
|
variant: "destructive",
|
|
171
172
|
size: "xs",
|
|
172
173
|
className: "absolute -top-2 -right-2 h-6 w-6 rounded-full p-0",
|
|
173
|
-
onClick:
|
|
174
|
-
icon: /* @__PURE__ */
|
|
174
|
+
onClick: X(0),
|
|
175
|
+
icon: /* @__PURE__ */ p(i, {})
|
|
175
176
|
}),
|
|
176
|
-
|
|
177
|
+
M && t.isPending && /* @__PURE__ */ p("div", {
|
|
177
178
|
className: "absolute bottom-2 left-2 right-2 mx-auto max-w-[calc(100%-1rem)] px-2 py-1 bg-yellow-500/90 text-white text-xs rounded text-center truncate",
|
|
178
|
-
children:
|
|
179
|
+
children: A("rc.fileInput.pending")
|
|
179
180
|
})
|
|
180
181
|
] })
|
|
181
182
|
})]
|
|
182
183
|
});
|
|
183
184
|
}
|
|
184
|
-
return /* @__PURE__ */
|
|
185
|
-
className: e("flex flex-wrap gap-3",
|
|
185
|
+
return /* @__PURE__ */ m("div", {
|
|
186
|
+
className: e("flex flex-wrap gap-3", b),
|
|
186
187
|
children: [
|
|
187
|
-
/* @__PURE__ */
|
|
188
|
-
ref:
|
|
188
|
+
/* @__PURE__ */ p("input", {
|
|
189
|
+
ref: P,
|
|
189
190
|
type: "file",
|
|
190
|
-
accept:
|
|
191
|
-
multiple:
|
|
192
|
-
onChange:
|
|
193
|
-
onBlur:
|
|
194
|
-
disabled:
|
|
191
|
+
accept: N,
|
|
192
|
+
multiple: j,
|
|
193
|
+
onChange: J,
|
|
194
|
+
onBlur: O,
|
|
195
|
+
disabled: y || F,
|
|
195
196
|
className: "sr-only"
|
|
196
197
|
}),
|
|
197
|
-
|
|
198
|
-
className: e("relative",
|
|
198
|
+
U.map((t, r) => /* @__PURE__ */ m("div", {
|
|
199
|
+
className: e("relative", Q[x]),
|
|
199
200
|
children: [
|
|
200
|
-
|
|
201
|
+
M ? /* @__PURE__ */ p("div", {
|
|
201
202
|
className: "h-full w-full rounded-lg border overflow-hidden",
|
|
202
|
-
children: /* @__PURE__ */
|
|
203
|
+
children: /* @__PURE__ */ p("img", {
|
|
203
204
|
src: t.url,
|
|
204
205
|
alt: `Uploaded ${r + 1}`,
|
|
205
206
|
className: "h-full w-full object-cover"
|
|
206
207
|
})
|
|
207
|
-
}) : /* @__PURE__ */
|
|
208
|
+
}) : /* @__PURE__ */ m("div", {
|
|
208
209
|
className: e("relative flex items-center gap-2 px-3 rounded-lg border-2 min-w-0 overflow-hidden", "border-dashed border-muted-foreground/25", "h-full w-full"),
|
|
209
210
|
children: [
|
|
210
|
-
/* @__PURE__ */
|
|
211
|
+
/* @__PURE__ */ p(S, {
|
|
211
212
|
filename: t.name,
|
|
212
213
|
size: x
|
|
213
214
|
}),
|
|
214
|
-
/* @__PURE__ */
|
|
215
|
+
/* @__PURE__ */ p("span", {
|
|
215
216
|
className: e("text-xs truncate min-w-0", t.isPending ? "w-[140px] shrink-0" : "flex-1"),
|
|
216
217
|
children: t.name
|
|
217
218
|
}),
|
|
218
|
-
t.isPending && /* @__PURE__ */
|
|
219
|
+
t.isPending && /* @__PURE__ */ p("span", {
|
|
219
220
|
className: "px-2 py-0.5 bg-yellow-500/90 text-white text-xs rounded whitespace-nowrap shrink-0",
|
|
220
|
-
children:
|
|
221
|
+
children: A("rc.fileInput.pending")
|
|
221
222
|
})
|
|
222
223
|
]
|
|
223
224
|
}),
|
|
224
|
-
!
|
|
225
|
+
!y && /* @__PURE__ */ p(n, {
|
|
225
226
|
type: "button",
|
|
226
227
|
variant: "destructive",
|
|
227
228
|
size: "xs",
|
|
228
229
|
className: "absolute -top-2 -right-2 h-6 w-6 rounded-full p-0",
|
|
229
|
-
onClick:
|
|
230
|
-
icon: /* @__PURE__ */
|
|
230
|
+
onClick: X(r),
|
|
231
|
+
icon: /* @__PURE__ */ p(i, {})
|
|
231
232
|
}),
|
|
232
|
-
|
|
233
|
+
M && t.isPending && /* @__PURE__ */ p("div", {
|
|
233
234
|
className: "absolute bottom-2 left-2 right-2 mx-auto max-w-[calc(100%-1rem)] px-2 py-1 bg-yellow-500/90 text-white text-xs rounded text-center truncate",
|
|
234
|
-
children:
|
|
235
|
+
children: A("rc.fileInput.pending")
|
|
235
236
|
})
|
|
236
237
|
]
|
|
237
238
|
}, `${t.url}-${r}`)),
|
|
238
|
-
|
|
239
|
-
onClick:
|
|
240
|
-
onDrop:
|
|
239
|
+
K && /* @__PURE__ */ p("div", {
|
|
240
|
+
onClick: Z,
|
|
241
|
+
onDrop: Y,
|
|
241
242
|
onDragOver: (e) => {
|
|
242
|
-
e.preventDefault(), !
|
|
243
|
+
e.preventDefault(), !y && !F && R(!0);
|
|
243
244
|
},
|
|
244
245
|
onDragLeave: (e) => {
|
|
245
|
-
e.preventDefault(),
|
|
246
|
+
e.preventDefault(), R(!1);
|
|
246
247
|
},
|
|
247
|
-
className: e("flex items-center justify-center rounded-lg border-2 border-dashed cursor-pointer transition-all",
|
|
248
|
-
children:
|
|
248
|
+
className: e("flex items-center justify-center rounded-lg border-2 border-dashed cursor-pointer transition-all", Q[x], L ? "border-primary bg-primary/5" : "border-muted-foreground/25 hover:border-muted-foreground/50", y && "opacity-50 cursor-not-allowed", F && "cursor-wait"),
|
|
249
|
+
children: F ? /* @__PURE__ */ m("div", {
|
|
249
250
|
className: "flex flex-col items-center gap-2 text-muted-foreground",
|
|
250
|
-
children: [/* @__PURE__ */
|
|
251
|
+
children: [/* @__PURE__ */ p(o, { className: "h-6 w-6 animate-spin" }), /* @__PURE__ */ p("span", {
|
|
251
252
|
className: "text-xs",
|
|
252
|
-
children:
|
|
253
|
+
children: A("rc.fileInput.uploading")
|
|
253
254
|
})]
|
|
254
|
-
}) : /* @__PURE__ */
|
|
255
|
+
}) : /* @__PURE__ */ m("div", {
|
|
255
256
|
className: "flex flex-col items-center gap-2 text-muted-foreground p-2",
|
|
256
|
-
children: [
|
|
257
|
+
children: [p(M ? a : s, { className: "h-6 w-6" }), /* @__PURE__ */ p("span", {
|
|
257
258
|
className: "text-xs text-center",
|
|
258
|
-
children:
|
|
259
|
+
children: z
|
|
259
260
|
})]
|
|
260
261
|
})
|
|
261
262
|
})
|
|
@@ -263,6 +264,6 @@ function x(m) {
|
|
|
263
264
|
});
|
|
264
265
|
}
|
|
265
266
|
//#endregion
|
|
266
|
-
export {
|
|
267
|
+
export { T as FileInput };
|
|
267
268
|
|
|
268
269
|
//# sourceMappingURL=file-input.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-input.js","names":[],"sources":["../../../src/components/ui/file-input.tsx"],"sourcesContent":["import type React from \"react\";\nimport { useCallback, useRef, useState } from \"react\";\nimport { FileIcon as ReactFileIcon, defaultStyles } from \"react-file-icon\";\nimport ImageIcon from \"~icons/lucide/image\";\nimport Loader2Icon from \"~icons/lucide/loader2\";\nimport UploadIcon from \"~icons/lucide/upload\";\nimport XIcon from \"~icons/lucide/x\";\n\nimport { type SonamuFile, type UploadParams } from \"@/contexts\";\nimport { useSonamuBaseContext } from \"@/contexts\";\nimport { cn, useObjectUrls } from \"@/lib/utils\";\n\nimport { Button } from \"./button\";\n\nexport type PreviewSize = \"sm\" | \"md\" | \"lg\" | \"xl\";\n\nfunction getFileExtension(filename: string): string {\n const parts = filename.split(\".\");\n if (parts.length < 2) return \"\";\n return parts[parts.length - 1].toLowerCase();\n}\n\nconst fileIconSizes: Record<PreviewSize, number> = {\n sm: 14,\n md: 18,\n lg: 22,\n xl: 26,\n};\n\nfunction FileTypeIcon({ filename, size = \"md\" }: { filename: string; size?: PreviewSize }) {\n const ext = getFileExtension(filename);\n const styleProps =\n ext && ext in defaultStyles ? defaultStyles[ext as keyof typeof defaultStyles] : {};\n\n return (\n <div className=\"shrink-0 self-center\" style={{ width: fileIconSizes[size], marginTop: -2 }}>\n <ReactFileIcon extension={ext || undefined} {...styleProps} />\n </div>\n );\n}\n\n// 이미지용: 정사각형\nconst imageSizeClasses: Record<PreviewSize, string> = {\n sm: \"w-20 h-20\",\n md: \"w-32 h-32\",\n lg: \"w-40 h-40\",\n xl: \"w-48 h-48\",\n};\n\n// 파일용: 가로로 긴 직사각형\nconst fileSizeClasses: Record<PreviewSize, string> = {\n sm: \"w-48 h-12\",\n md: \"w-80 h-16\",\n lg: \"w-96 h-20\",\n xl: \"w-[28rem] h-24\",\n};\n\ntype BaseProps = {\n uploadMode: \"eager\" | \"lazy\";\n viewMode: \"image\" | \"file\";\n placeholder?: string;\n accept?: string;\n disabled?: boolean;\n className?: string;\n previewSize?: PreviewSize;\n uploadParams?: UploadParams;\n onBlur?: React.FocusEventHandler<HTMLInputElement>;\n};\n\ntype SingleModeProps = BaseProps & {\n multiple?: false;\n value: SonamuFile | File | null;\n onValueChange?: (value: SonamuFile | File | null) => void;\n};\n\ntype MultipleModeProps = BaseProps & {\n multiple: true;\n value: (SonamuFile | File)[] | null;\n onValueChange?: (value: (SonamuFile | File)[]) => void;\n maxFiles?: number;\n};\n\nexport type FileInputProps = SingleModeProps | MultipleModeProps;\n\nexport function FileInput(props: FileInputProps) {\n const {\n placeholder,\n disabled = false,\n className,\n previewSize = \"md\",\n uploadMode,\n viewMode,\n uploadParams,\n onBlur,\n } = props;\n\n const { uploader, SD } = useSonamuBaseContext();\n\n const isMultiple = props.multiple ?? false;\n const isImageView = viewMode === \"image\";\n\n // viewMode에 따라 accept 자동 설정\n const accept = props.accept ?? (isImageView ? \"image/*\" : \"*/*\");\n\n const inputRef = useRef<HTMLInputElement>(null);\n const [isUploading, setIsUploading] = useState(false);\n const [dragOver, setDragOver] = useState(false);\n\n // 기본 placeholder 설정\n const defaultPlaceholder =\n placeholder ??\n `${isImageView ? SD(\"rc.fileInput.imagePlaceholder\") : SD(\"rc.fileInput.filePlaceholder\")} URL${isMultiple ? \"S\" : \"\"}`;\n\n // 입력 정규화: 내부적으로 배열로 통일\n const values = (() => {\n if (!isMultiple) {\n const singleValue = (props as SingleModeProps).value;\n return singleValue ? [singleValue] : [];\n }\n\n const multiValue = (props as MultipleModeProps).value || [];\n\n return multiValue;\n })();\n\n // File 객체만 추출\n const fileObjects = values.filter((v): v is File => v instanceof File);\n\n // File 객체는 useObjectUrls로 안전하게 blob URL 생성 (자동 메모리 해제)\n const blobUrls = useObjectUrls(fileObjects);\n\n // 최종 display 데이터: 원래 순서를 유지하면서 File은 blob URL로, SonamuFile은 그대로\n const displayItems = values.map((v) => {\n if (v instanceof File) {\n const index = fileObjects.indexOf(v);\n return {\n url: blobUrls[index],\n name: v.name,\n isPending: true,\n };\n }\n // SonamuFile\n return {\n url: v.url,\n name: v.name,\n isPending: false,\n };\n });\n\n const maxFiles = isMultiple ? ((props as MultipleModeProps).maxFiles ?? 10) : 1;\n const totalCount = values.length;\n const canAddMore = totalCount < maxFiles;\n\n const handleFilesChange = useCallback(\n async (files: File[]) => {\n if (files.length === 0 || disabled) return;\n\n // maxFiles 체크\n const remainingSlots = maxFiles - totalCount;\n if (remainingSlots <= 0) {\n alert(\n (SD(\"rc.fileInput.maxFilesExceeded\") as unknown as (maxFiles: number) => string)(\n maxFiles,\n ),\n );\n return;\n }\n\n const filesToAdd = files.slice(0, remainingSlots);\n\n // Eager 모드: 즉시 업로드\n if (uploadMode === \"eager\") {\n setIsUploading(true);\n try {\n const uploadedFiles = await uploader(filesToAdd, uploadParams);\n\n if (isMultiple) {\n // Multiple 파일 업로드\n const finalValues = [...values, ...uploadedFiles];\n (props.onValueChange as ((v: (SonamuFile | File)[]) => void) | undefined)?.(\n finalValues,\n );\n } else {\n // Single 파일 업로드\n (props.onValueChange as ((v: SonamuFile | File) => void) | undefined)?.(\n uploadedFiles[0],\n );\n }\n } catch (error) {\n console.error(\"Upload failed:\", error);\n alert(SD(\"rc.fileInput.uploadFailed\"));\n } finally {\n setIsUploading(false);\n }\n } else {\n // Lazy 모드: File 객체 그대로 전달\n if (isMultiple) {\n const finalValues = [...values, ...filesToAdd];\n (props.onValueChange as ((v: (SonamuFile | File)[]) => void) | undefined)?.(finalValues);\n } else {\n (props.onValueChange as ((v: SonamuFile | File) => void) | undefined)?.(filesToAdd[0]);\n }\n }\n },\n [disabled, totalCount, maxFiles, isMultiple, uploadMode, uploadParams, values, props, uploader],\n );\n\n const handleInputChange = useCallback(\n (e: React.ChangeEvent<HTMLInputElement>) => {\n const files = Array.from(e.target.files || []);\n handleFilesChange(files);\n // input 초기화\n if (inputRef.current) {\n inputRef.current.value = \"\";\n }\n },\n [handleFilesChange],\n );\n\n const handleDrop = useCallback(\n (e: React.DragEvent) => {\n e.preventDefault();\n setDragOver(false);\n if (disabled || isUploading) return;\n\n const files = Array.from(e.dataTransfer.files);\n handleFilesChange(files);\n },\n [handleFilesChange, disabled, isUploading],\n );\n\n const handleRemove = useCallback(\n (index: number) => (e: React.MouseEvent) => {\n e.stopPropagation();\n\n if (!isMultiple) {\n (props.onValueChange as ((v: SonamuFile | File | null) => void) | undefined)?.(null);\n } else {\n const newValue = values.filter((_, i) => i !== index);\n (props.onValueChange as ((v: (SonamuFile | File)[]) => void) | undefined)?.(newValue);\n }\n },\n [values, isMultiple, props.onValueChange],\n );\n\n const handleClick = () => {\n if (!disabled && !isUploading) {\n inputRef.current?.click();\n }\n };\n\n // viewMode에 따라 적절한 사이즈 클래스 선택\n const sizeClasses = isImageView ? imageSizeClasses : fileSizeClasses;\n\n // Single 모드 렌더링\n if (!isMultiple && displayItems.length > 0) {\n const item = displayItems[0];\n return (\n <div className={cn(\"relative inline-block\", className)}>\n <input\n ref={inputRef}\n type=\"file\"\n accept={accept}\n onChange={handleInputChange}\n onBlur={onBlur}\n disabled={disabled || isUploading}\n className=\"sr-only\"\n />\n\n <div\n onClick={handleClick}\n onDrop={handleDrop}\n onDragOver={(e) => {\n e.preventDefault();\n !disabled && !isUploading && setDragOver(true);\n }}\n onDragLeave={(e) => {\n e.preventDefault();\n setDragOver(false);\n }}\n className={cn(\n \"relative flex items-center justify-center rounded-lg border-2 cursor-pointer transition-all\",\n sizeClasses[previewSize],\n isImageView\n ? \"border-transparent\"\n : \"border-dashed border-muted-foreground/25 hover:border-muted-foreground/50\",\n dragOver && \"border-primary bg-primary/5\",\n disabled && \"opacity-50 cursor-not-allowed\",\n isUploading && \"cursor-wait\",\n )}\n >\n {isUploading ? (\n <div className=\"flex flex-col items-center gap-2 text-muted-foreground\">\n <Loader2Icon className=\"h-6 w-6 animate-spin\" />\n <span className=\"text-xs\">{SD(\"rc.fileInput.uploading\")}</span>\n </div>\n ) : (\n <>\n {isImageView ? (\n <img\n src={item.url}\n alt=\"Preview\"\n className=\"h-full w-full object-cover rounded-lg overflow-hidden\"\n />\n ) : (\n <div className=\"flex items-center gap-2 px-3 h-full w-full min-w-0 overflow-hidden\">\n <FileTypeIcon filename={item.name} size={previewSize} />\n <span\n className={cn(\n \"text-xs truncate min-w-0\",\n item.isPending ? \"w-[140px] shrink-0\" : \"flex-1\",\n )}\n >\n {item.name}\n </span>\n {item.isPending && (\n <span className=\"px-2 py-0.5 bg-yellow-500/90 text-white text-xs rounded whitespace-nowrap shrink-0\">\n {SD(\"rc.fileInput.pending\")}\n </span>\n )}\n </div>\n )}\n {!disabled && (\n <Button\n type=\"button\"\n variant=\"destructive\"\n size=\"xs\"\n className=\"absolute -top-2 -right-2 h-6 w-6 rounded-full p-0\"\n onClick={handleRemove(0)}\n icon={<XIcon />}\n />\n )}\n {/* 이미지 모드일 때만 하단에 대기중 배지 표시 */}\n {isImageView && item.isPending && (\n <div className=\"absolute bottom-2 left-2 right-2 mx-auto max-w-[calc(100%-1rem)] px-2 py-1 bg-yellow-500/90 text-white text-xs rounded text-center truncate\">\n {SD(\"rc.fileInput.pending\")}\n </div>\n )}\n </>\n )}\n </div>\n </div>\n );\n }\n\n // Multiple 모드 또는 빈 Single 모드 렌더링\n return (\n <div className={cn(\"flex flex-wrap gap-3\", className)}>\n <input\n ref={inputRef}\n type=\"file\"\n accept={accept}\n multiple={isMultiple}\n onChange={handleInputChange}\n onBlur={onBlur}\n disabled={disabled || isUploading}\n className=\"sr-only\"\n />\n\n {/* 업로드된 파일들 */}\n {displayItems.map((item, index) => (\n <div key={`${item.url}-${index}`} className={cn(\"relative\", sizeClasses[previewSize])}>\n {isImageView ? (\n <div className=\"h-full w-full rounded-lg border overflow-hidden\">\n <img\n src={item.url}\n alt={`Uploaded ${index + 1}`}\n className=\"h-full w-full object-cover\"\n />\n </div>\n ) : (\n <div\n className={cn(\n \"relative flex items-center gap-2 px-3 rounded-lg border-2 min-w-0 overflow-hidden\",\n \"border-dashed border-muted-foreground/25\",\n \"h-full w-full\",\n )}\n >\n <FileTypeIcon filename={item.name} size={previewSize} />\n <span\n className={cn(\n \"text-xs truncate min-w-0\",\n item.isPending ? \"w-[140px] shrink-0\" : \"flex-1\",\n )}\n >\n {item.name}\n </span>\n {item.isPending && (\n <span className=\"px-2 py-0.5 bg-yellow-500/90 text-white text-xs rounded whitespace-nowrap shrink-0\">\n {SD(\"rc.fileInput.pending\")}\n </span>\n )}\n </div>\n )}\n {!disabled && (\n <Button\n type=\"button\"\n variant=\"destructive\"\n size=\"xs\"\n className=\"absolute -top-2 -right-2 h-6 w-6 rounded-full p-0\"\n onClick={handleRemove(index)}\n icon={<XIcon />}\n />\n )}\n {/* 이미지 모드일 때만 하단에 대기중 배지 표시 */}\n {isImageView && item.isPending && (\n <div className=\"absolute bottom-2 left-2 right-2 mx-auto max-w-[calc(100%-1rem)] px-2 py-1 bg-yellow-500/90 text-white text-xs rounded text-center truncate\">\n {SD(\"rc.fileInput.pending\")}\n </div>\n )}\n </div>\n ))}\n\n {/* 추가 버튼 */}\n {canAddMore && (\n <div\n onClick={handleClick}\n onDrop={handleDrop}\n onDragOver={(e) => {\n e.preventDefault();\n !disabled && !isUploading && setDragOver(true);\n }}\n onDragLeave={(e) => {\n e.preventDefault();\n setDragOver(false);\n }}\n className={cn(\n \"flex items-center justify-center rounded-lg border-2 border-dashed cursor-pointer transition-all\",\n sizeClasses[previewSize],\n dragOver\n ? \"border-primary bg-primary/5\"\n : \"border-muted-foreground/25 hover:border-muted-foreground/50\",\n disabled && \"opacity-50 cursor-not-allowed\",\n isUploading && \"cursor-wait\",\n )}\n >\n {isUploading ? (\n <div className=\"flex flex-col items-center gap-2 text-muted-foreground\">\n <Loader2Icon className=\"h-6 w-6 animate-spin\" />\n <span className=\"text-xs\">{SD(\"rc.fileInput.uploading\")}</span>\n </div>\n ) : (\n <div className=\"flex flex-col items-center gap-2 text-muted-foreground p-2\">\n {isImageView ? <ImageIcon className=\"h-6 w-6\" /> : <UploadIcon className=\"h-6 w-6\" />}\n <span className=\"text-xs text-center\">{defaultPlaceholder}</span>\n </div>\n )}\n </div>\n )}\n </div>\n );\n}\n"],"mappings":";;;;;;;;;;;AAgBA,SAAS,EAAiB,GAA0B;CAClD,IAAM,IAAQ,EAAS,MAAM,GAAG;CAEhC,OADI,EAAM,SAAS,IAAU,KACtB,EAAM,EAAM,SAAS,GAAG,YAAY;AAC7C;AAEA,IAAM,IAA6C;CACjD,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;AACN;AAEA,SAAS,EAAa,EAAE,aAAU,UAAO,QAAkD;CACzF,IAAM,IAAM,EAAiB,CAAQ,GAC/B,IACJ,KAAO,KAAO,IAAgB,EAAc,KAAqC,CAAC;CAEpF,OACE,kBAAC,OAAD;EAAK,WAAU;EAAuB,OAAO;GAAE,OAAO,EAAc;GAAO,WAAW;EAAG;YACvF,kBAAC,GAAD;GAAe,WAAW,KAAO,KAAA;GAAW,GAAI;EAAa,CAAA;CAC1D,CAAA;AAET;AAGA,IAAM,IAAgD;CACpD,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;AACN,GAGM,IAA+C;CACnD,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;AACN;AA6BA,SAAgB,EAAU,GAAuB;CAC/C,IAAM,EACJ,gBACA,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C,QAAD;MAAM,WAAU;gBAAW,EAAG,wBAAwB;KAAQ,CAAA,CAC3D;SAEL,kBAAC,OAAD;KAAK,WAAU;eAAf,CACiB,EAAd,IAAe,IAAoC,GAArC,EAAW,WAAU,UAAW,CAAqC,GACpF,kBAAC,QAAD;MAAM,WAAU;gBAAuB;KAAyB,CAAA,CAC7D;;GAEJ,CAAA;EAEJ;;AAET"}
|
|
1
|
+
{"version":3,"file":"file-input.js","names":[],"sources":["../../../src/components/ui/file-input.tsx"],"sourcesContent":["import { isFunction } from \"radashi\";\nimport type React from \"react\";\nimport { useCallback, useMemo, useRef, useState } from \"react\";\nimport { FileIcon as ReactFileIcon, defaultStyles } from \"react-file-icon\";\nimport ImageIcon from \"~icons/lucide/image\";\nimport Loader2Icon from \"~icons/lucide/loader2\";\nimport UploadIcon from \"~icons/lucide/upload\";\nimport XIcon from \"~icons/lucide/x\";\n\nimport { type SonamuFile, type UploadParams } from \"@/contexts\";\nimport { useSonamuBaseContext } from \"@/contexts\";\nimport { type RCKeys } from \"@/i18n/rc-keys\";\nimport { cn, useObjectUrls } from \"@/lib/utils\";\n\nimport { Button } from \"./button\";\n\nexport type PreviewSize = \"sm\" | \"md\" | \"lg\" | \"xl\";\n\nfunction isFile(value: SonamuFile | File): value is File {\n return Object.prototype.toString.call(value) === \"[object File]\";\n}\n\nfunction getFileExtension(filename: string): string {\n const parts = filename.split(\".\");\n if (parts.length < 2) return \"\";\n return parts[parts.length - 1].toLowerCase();\n}\n\nconst fileIconSizes = {\n sm: 14,\n md: 18,\n lg: 22,\n xl: 26,\n};\n\nconst fileIconStyles = new Map<string, Partial<React.ComponentProps<typeof ReactFileIcon>>>(\n Object.entries(defaultStyles),\n);\n\nfunction FileTypeIcon({ filename, size = \"md\" }: { filename: string; size?: PreviewSize }) {\n const ext = getFileExtension(filename);\n const styleProps = fileIconStyles.get(ext) ?? {};\n\n return (\n <div className=\"shrink-0 self-center\" style={{ width: fileIconSizes[size], marginTop: -2 }}>\n <ReactFileIcon extension={ext || undefined} {...styleProps} />\n </div>\n );\n}\n\n// 이미지용: 정사각형\nconst imageSizeClasses = {\n sm: \"w-20 h-20\",\n md: \"w-32 h-32\",\n lg: \"w-40 h-40\",\n xl: \"w-48 h-48\",\n};\n\n// 파일용: 가로로 긴 직사각형\nconst fileSizeClasses = {\n sm: \"w-48 h-12\",\n md: \"w-80 h-16\",\n lg: \"w-96 h-20\",\n xl: \"w-[28rem] h-24\",\n};\n\ntype BaseProps = {\n uploadMode: \"eager\" | \"lazy\";\n viewMode: \"image\" | \"file\";\n placeholder?: string;\n accept?: string;\n disabled?: boolean;\n className?: string;\n previewSize?: PreviewSize;\n uploadParams?: UploadParams;\n onBlur?: React.FocusEventHandler<HTMLInputElement>;\n};\n\ntype SingleModeProps = BaseProps & {\n multiple?: false;\n value: SonamuFile | File | null;\n onValueChange?: (value: SonamuFile | File | null) => void;\n};\n\ntype MultipleModeProps = BaseProps & {\n multiple: true;\n value: (SonamuFile | File)[] | null;\n onValueChange?: (value: (SonamuFile | File)[]) => void;\n maxFiles?: number;\n};\n\nexport type FileInputProps = SingleModeProps | MultipleModeProps;\n\nexport function FileInput(props: FileInputProps) {\n const {\n placeholder,\n disabled = false,\n className,\n previewSize = \"md\",\n uploadMode,\n viewMode,\n uploadParams,\n onBlur,\n } = props;\n\n const { uploader, SD } = useSonamuBaseContext<RCKeys>();\n\n const isMultiple = props.multiple ?? false;\n const isImageView = viewMode === \"image\";\n\n // viewMode에 따라 accept 자동 설정\n const accept = props.accept ?? (isImageView ? \"image/*\" : \"*/*\");\n\n const inputRef = useRef<HTMLInputElement>(null);\n const [isUploading, setIsUploading] = useState(false);\n const [dragOver, setDragOver] = useState(false);\n\n // 기본 placeholder 설정\n const defaultPlaceholder =\n placeholder ??\n `${isImageView ? SD(\"rc.fileInput.imagePlaceholder\") : SD(\"rc.fileInput.filePlaceholder\")} URL${isMultiple ? \"S\" : \"\"}`;\n\n // 입력 정규화: 내부적으로 배열로 통일\n const values: Array<SonamuFile | File> = useMemo(() => {\n if (props.multiple !== true) {\n const singleValue = props.value;\n return singleValue ? [singleValue] : [];\n }\n return props.value ?? [];\n }, [props.multiple, props.value]);\n\n // File 객체만 추출\n const fileObjects = useMemo(() => values.filter(isFile), [values]);\n\n // File 객체는 useObjectUrls로 안전하게 blob URL 생성 (자동 메모리 해제)\n const blobUrls = useObjectUrls(fileObjects);\n\n // 최종 display 데이터: 원래 순서를 유지하면서 File은 blob URL로, SonamuFile은 그대로\n const displayItems = values.map((v) => {\n if (isFile(v)) {\n const index = fileObjects.indexOf(v);\n return {\n url: blobUrls[index],\n name: v.name,\n isPending: true,\n };\n }\n // SonamuFile\n return {\n url: v.url,\n name: v.name,\n isPending: false,\n };\n });\n\n const maxFiles = props.multiple === true ? (props.maxFiles ?? 10) : 1;\n const totalCount = values.length;\n const canAddMore = totalCount < maxFiles;\n\n const handleFilesChange = useCallback(\n async (files: File[]) => {\n if (files.length === 0 || disabled) return;\n\n // maxFiles 체크\n const remainingSlots = maxFiles - totalCount;\n if (remainingSlots <= 0) {\n const maxFilesTranslation = SD(\"rc.fileInput.maxFilesExceeded\");\n alert(\n isFunction(maxFilesTranslation) ? maxFilesTranslation(maxFiles) : maxFilesTranslation,\n );\n return;\n }\n\n const filesToAdd = files.slice(0, remainingSlots);\n\n // Eager 모드: 즉시 업로드\n if (uploadMode === \"eager\") {\n setIsUploading(true);\n try {\n const uploadedFiles = await uploader(filesToAdd, uploadParams);\n\n if (props.multiple === true) {\n // Multiple 파일 업로드\n const finalValues = [...values, ...uploadedFiles];\n props.onValueChange?.(finalValues);\n } else {\n // Single 파일 업로드\n props.onValueChange?.(uploadedFiles[0] ?? null);\n }\n } catch (error) {\n console.error(\"Upload failed:\", error);\n alert(SD(\"rc.fileInput.uploadFailed\"));\n } finally {\n setIsUploading(false);\n }\n } else {\n // Lazy 모드: File 객체 그대로 전달\n if (props.multiple === true) {\n const finalValues = [...values, ...filesToAdd];\n props.onValueChange?.(finalValues);\n } else {\n props.onValueChange?.(filesToAdd[0] ?? null);\n }\n }\n },\n [disabled, totalCount, maxFiles, uploadMode, uploadParams, values, props, uploader, SD],\n );\n\n const handleInputChange = useCallback(\n (e: React.ChangeEvent<HTMLInputElement>) => {\n const files = Array.from(e.target.files || []);\n handleFilesChange(files);\n // input 초기화\n if (inputRef.current) {\n inputRef.current.value = \"\";\n }\n },\n [handleFilesChange],\n );\n\n const handleDrop = useCallback(\n (e: React.DragEvent) => {\n e.preventDefault();\n setDragOver(false);\n if (disabled || isUploading) return;\n\n const files = Array.from(e.dataTransfer.files);\n handleFilesChange(files);\n },\n [handleFilesChange, disabled, isUploading],\n );\n\n const handleRemove = useCallback(\n (index: number) => (e: React.MouseEvent) => {\n e.stopPropagation();\n\n if (props.multiple !== true) {\n props.onValueChange?.(null);\n } else {\n const newValue = values.filter((_, i) => i !== index);\n props.onValueChange?.(newValue);\n }\n },\n [values, props],\n );\n\n const handleClick = () => {\n if (!disabled && !isUploading) {\n inputRef.current?.click();\n }\n };\n\n // viewMode에 따라 적절한 사이즈 클래스 선택\n const sizeClasses = isImageView ? imageSizeClasses : fileSizeClasses;\n\n // Single 모드 렌더링\n if (!isMultiple && displayItems.length > 0) {\n const item = displayItems[0];\n return (\n <div className={cn(\"relative inline-block\", className)}>\n <input\n ref={inputRef}\n type=\"file\"\n accept={accept}\n onChange={handleInputChange}\n onBlur={onBlur}\n disabled={disabled || isUploading}\n className=\"sr-only\"\n />\n\n <div\n onClick={handleClick}\n onDrop={handleDrop}\n onDragOver={(e) => {\n e.preventDefault();\n !disabled && !isUploading && setDragOver(true);\n }}\n onDragLeave={(e) => {\n e.preventDefault();\n setDragOver(false);\n }}\n className={cn(\n \"relative flex items-center justify-center rounded-lg border-2 cursor-pointer transition-all\",\n sizeClasses[previewSize],\n isImageView\n ? \"border-transparent\"\n : \"border-dashed border-muted-foreground/25 hover:border-muted-foreground/50\",\n dragOver && \"border-primary bg-primary/5\",\n disabled && \"opacity-50 cursor-not-allowed\",\n isUploading && \"cursor-wait\",\n )}\n >\n {isUploading ? (\n <div className=\"flex flex-col items-center gap-2 text-muted-foreground\">\n <Loader2Icon className=\"h-6 w-6 animate-spin\" />\n <span className=\"text-xs\">{SD(\"rc.fileInput.uploading\")}</span>\n </div>\n ) : (\n <>\n {isImageView ? (\n <img\n src={item.url}\n alt=\"Preview\"\n className=\"h-full w-full object-cover rounded-lg overflow-hidden\"\n />\n ) : (\n <div className=\"flex items-center gap-2 px-3 h-full w-full min-w-0 overflow-hidden\">\n <FileTypeIcon filename={item.name} size={previewSize} />\n <span\n className={cn(\n \"text-xs truncate min-w-0\",\n item.isPending ? \"w-[140px] shrink-0\" : \"flex-1\",\n )}\n >\n {item.name}\n </span>\n {item.isPending && (\n <span className=\"px-2 py-0.5 bg-yellow-500/90 text-white text-xs rounded whitespace-nowrap shrink-0\">\n {SD(\"rc.fileInput.pending\")}\n </span>\n )}\n </div>\n )}\n {!disabled && (\n <Button\n type=\"button\"\n variant=\"destructive\"\n size=\"xs\"\n className=\"absolute -top-2 -right-2 h-6 w-6 rounded-full p-0\"\n onClick={handleRemove(0)}\n icon={<XIcon />}\n />\n )}\n {/* 이미지 모드일 때만 하단에 대기중 배지 표시 */}\n {isImageView && item.isPending && (\n <div className=\"absolute bottom-2 left-2 right-2 mx-auto max-w-[calc(100%-1rem)] px-2 py-1 bg-yellow-500/90 text-white text-xs rounded text-center truncate\">\n {SD(\"rc.fileInput.pending\")}\n </div>\n )}\n </>\n )}\n </div>\n </div>\n );\n }\n\n // Multiple 모드 또는 빈 Single 모드 렌더링\n return (\n <div className={cn(\"flex flex-wrap gap-3\", className)}>\n <input\n ref={inputRef}\n type=\"file\"\n accept={accept}\n multiple={isMultiple}\n onChange={handleInputChange}\n onBlur={onBlur}\n disabled={disabled || isUploading}\n className=\"sr-only\"\n />\n\n {/* 업로드된 파일들 */}\n {displayItems.map((item, index) => (\n <div key={`${item.url}-${index}`} className={cn(\"relative\", sizeClasses[previewSize])}>\n {isImageView ? (\n <div className=\"h-full w-full rounded-lg border overflow-hidden\">\n <img\n src={item.url}\n alt={`Uploaded ${index + 1}`}\n className=\"h-full w-full object-cover\"\n />\n </div>\n ) : (\n <div\n className={cn(\n \"relative flex items-center gap-2 px-3 rounded-lg border-2 min-w-0 overflow-hidden\",\n \"border-dashed border-muted-foreground/25\",\n \"h-full w-full\",\n )}\n >\n <FileTypeIcon filename={item.name} size={previewSize} />\n <span\n className={cn(\n \"text-xs truncate min-w-0\",\n item.isPending ? \"w-[140px] shrink-0\" : \"flex-1\",\n )}\n >\n {item.name}\n </span>\n {item.isPending && (\n <span className=\"px-2 py-0.5 bg-yellow-500/90 text-white text-xs rounded whitespace-nowrap shrink-0\">\n {SD(\"rc.fileInput.pending\")}\n </span>\n )}\n </div>\n )}\n {!disabled && (\n <Button\n type=\"button\"\n variant=\"destructive\"\n size=\"xs\"\n className=\"absolute -top-2 -right-2 h-6 w-6 rounded-full p-0\"\n onClick={handleRemove(index)}\n icon={<XIcon />}\n />\n )}\n {/* 이미지 모드일 때만 하단에 대기중 배지 표시 */}\n {isImageView && item.isPending && (\n <div className=\"absolute bottom-2 left-2 right-2 mx-auto max-w-[calc(100%-1rem)] px-2 py-1 bg-yellow-500/90 text-white text-xs rounded text-center truncate\">\n {SD(\"rc.fileInput.pending\")}\n </div>\n )}\n </div>\n ))}\n\n {/* 추가 버튼 */}\n {canAddMore && (\n <div\n onClick={handleClick}\n onDrop={handleDrop}\n onDragOver={(e) => {\n e.preventDefault();\n !disabled && !isUploading && setDragOver(true);\n }}\n onDragLeave={(e) => {\n e.preventDefault();\n setDragOver(false);\n }}\n className={cn(\n \"flex items-center justify-center rounded-lg border-2 border-dashed cursor-pointer transition-all\",\n sizeClasses[previewSize],\n dragOver\n ? \"border-primary bg-primary/5\"\n : \"border-muted-foreground/25 hover:border-muted-foreground/50\",\n disabled && \"opacity-50 cursor-not-allowed\",\n isUploading && \"cursor-wait\",\n )}\n >\n {isUploading ? (\n <div className=\"flex flex-col items-center gap-2 text-muted-foreground\">\n <Loader2Icon className=\"h-6 w-6 animate-spin\" />\n <span className=\"text-xs\">{SD(\"rc.fileInput.uploading\")}</span>\n </div>\n ) : (\n <div className=\"flex flex-col items-center gap-2 text-muted-foreground p-2\">\n {isImageView ? <ImageIcon className=\"h-6 w-6\" /> : <UploadIcon className=\"h-6 w-6\" />}\n <span className=\"text-xs text-center\">{defaultPlaceholder}</span>\n </div>\n )}\n </div>\n )}\n </div>\n );\n}\n"],"mappings":";;;;;;;;;;;;AAkBA,SAAS,EAAO,GAAyC;CACvD,OAAO,OAAO,UAAU,SAAS,KAAK,CAAK,MAAM;AACnD;AAEA,SAAS,EAAiB,GAA0B;CAClD,IAAM,IAAQ,EAAS,MAAM,GAAG;CAEhC,OADI,EAAM,SAAS,IAAU,KACtB,EAAM,EAAM,SAAS,GAAG,YAAY;AAC7C;AAEA,IAAM,IAAgB;CACpB,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;AACN,GAEM,IAAiB,IAAI,IACzB,OAAO,QAAQ,CAAa,CAC9B;AAEA,SAAS,EAAa,EAAE,aAAU,UAAO,QAAkD;CACzF,IAAM,IAAM,EAAiB,CAAQ,GAC/B,IAAa,EAAe,IAAI,CAAG,KAAK,CAAC;CAE/C,OACE,kBAAC,OAAD;EAAK,WAAU;EAAuB,OAAO;GAAE,OAAO,EAAc;GAAO,WAAW;EAAG;YACvF,kBAAC,GAAD;GAAe,WAAW,KAAO,KAAA;GAAW,GAAI;EAAa,CAAA;CAC1D,CAAA;AAET;AAGA,IAAM,IAAmB;CACvB,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;AACN,GAGM,IAAkB;CACtB,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;AACN;AA6BA,SAAgB,EAAU,GAAuB;CAC/C,IAAM,EACJ,gBACA,cAAW,IACX,cACA,iBAAc,MACd,eACA,aACA,iBACA,cACE,GAEE,EAAE,aAAU,UAAO,EAA6B,GAEhD,IAAa,EAAM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AAK,WAAU;eAAf,CACE,kBAAC,GAAD,EAAa,WAAU,uBAAwB,CAAA,GAC/C,kBAAC,QAAD;MAAM,WAAU;gBAAW,EAAG,wBAAwB;KAAQ,CAAA,CAC3D;SAEL,kBAAC,OAAD;KAAK,WAAU;eAAf,CACiB,EAAd,IAAe,IAAoC,GAArC,EAAW,WAAU,UAAW,CAAqC,GACpF,kBAAC,QAAD;MAAM,WAAU;gBAAuB;KAAyB,CAAA,CAC7D;;GAEJ,CAAA;EAEJ;;AAET"}
|
|
@@ -2,38 +2,39 @@ import { cn as e } from "../../lib/utils.js";
|
|
|
2
2
|
import t from "../../_virtual/~icons/lucide/x.js";
|
|
3
3
|
import * as n from "react";
|
|
4
4
|
import { jsx as r, jsxs as i } from "react/jsx-runtime";
|
|
5
|
+
import { isFunction as a } from "radashi";
|
|
5
6
|
//#region src/components/ui/input.tsx
|
|
6
|
-
var
|
|
7
|
-
let
|
|
8
|
-
|
|
9
|
-
}, [
|
|
10
|
-
|
|
11
|
-
},
|
|
12
|
-
let e =
|
|
13
|
-
e && ((Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value")?.set)?.call(e, ""), e.dispatchEvent(new Event("input", { bubbles: !0 })), e.focus()),
|
|
14
|
-
},
|
|
15
|
-
type:
|
|
7
|
+
var o = n.forwardRef(({ className: o, type: s, onValueChange: c, onChange: l, clearable: u = !1, ...d }, f) => {
|
|
8
|
+
let p = n.useRef(null), m = n.useCallback((e) => {
|
|
9
|
+
p.current = e, a(f) ? f(e) : f && (f.current = e);
|
|
10
|
+
}, [f]), h = d.value !== void 0, [g, _] = n.useState(() => String(d.defaultValue ?? "").length > 0), v = h ? String(d.value ?? "").length > 0 : g, y = (e) => {
|
|
11
|
+
h || _(e.target.value.length > 0), c?.(e.target.value), l?.(e);
|
|
12
|
+
}, b = () => {
|
|
13
|
+
let e = p.current;
|
|
14
|
+
e && ((Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value")?.set)?.call(e, ""), e.dispatchEvent(new Event("input", { bubbles: !0 })), e.focus()), h || _(!1);
|
|
15
|
+
}, x = u && v && !d.disabled && !d.readOnly, S = /* @__PURE__ */ r("input", {
|
|
16
|
+
type: s,
|
|
16
17
|
"data-slot": "input",
|
|
17
|
-
className: e("file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 border-input flex h-9 w-full min-w-0 rounded-md border px-3 py-1 text-base bg-input-background transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm", "focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]", "aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
|
|
18
|
-
ref:
|
|
19
|
-
...
|
|
20
|
-
onChange:
|
|
18
|
+
className: e("file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 border-input flex h-9 w-full min-w-0 rounded-md border px-3 py-1 text-base bg-input-background transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm", "focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]", "aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive", u && "pr-8", o),
|
|
19
|
+
ref: m,
|
|
20
|
+
...d,
|
|
21
|
+
onChange: y
|
|
21
22
|
});
|
|
22
|
-
return
|
|
23
|
+
return u ? /* @__PURE__ */ i("div", {
|
|
23
24
|
className: "relative w-full",
|
|
24
|
-
children: [
|
|
25
|
+
children: [S, x && /* @__PURE__ */ r("button", {
|
|
25
26
|
type: "button",
|
|
26
27
|
tabIndex: -1,
|
|
27
28
|
"aria-label": "Clear",
|
|
28
29
|
onMouseDown: (e) => e.preventDefault(),
|
|
29
|
-
onClick:
|
|
30
|
+
onClick: b,
|
|
30
31
|
className: "absolute right-2 top-1/2 -translate-y-1/2 flex items-center justify-center text-muted-foreground opacity-60 hover:opacity-100 transition-opacity cursor-pointer",
|
|
31
32
|
children: /* @__PURE__ */ r(t, { className: "h-4 w-4" })
|
|
32
33
|
})]
|
|
33
|
-
}) :
|
|
34
|
+
}) : S;
|
|
34
35
|
});
|
|
35
|
-
|
|
36
|
+
o.displayName = "Input";
|
|
36
37
|
//#endregion
|
|
37
|
-
export {
|
|
38
|
+
export { o as Input };
|
|
38
39
|
|
|
39
40
|
//# sourceMappingURL=input.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"input.js","names":[],"sources":["../../../src/components/ui/input.tsx"],"sourcesContent":["import * as React from \"react\";\nimport XIcon from \"~icons/lucide/x\";\n\nimport { type Override } from \"../../lib/types\";\nimport { cn } from \"../../lib/utils\";\n\nexport type InputProps = Override<\n React.ComponentProps<\"input\">,\n {\n onValueChange?: (value: string) => void;\n /**\n * 활성화 시 내용이 채워지면 오른쪽에 X 아이콘이 뜨고, 누르면 내용을 초기화한다.\n * controlled/uncontrolled 입력 모두 지원한다.\n */\n clearable?: boolean;\n }\n>;\n\nconst Input = React.forwardRef<HTMLInputElement, InputProps>(\n ({ className, type, onValueChange, onChange, clearable = false, ...props }, ref) => {\n const innerRef = React.useRef<HTMLInputElement>(null);\n\n // 전달된 ref와 내부 ref를 함께 채우는 콜백 ref\n const setRefs = React.useCallback(\n (node: HTMLInputElement | null) => {\n innerRef.current = node;\n if (
|
|
1
|
+
{"version":3,"file":"input.js","names":[],"sources":["../../../src/components/ui/input.tsx"],"sourcesContent":["import { isFunction } from \"radashi\";\nimport * as React from \"react\";\nimport XIcon from \"~icons/lucide/x\";\n\nimport { type Override } from \"../../lib/types\";\nimport { cn } from \"../../lib/utils\";\n\nexport type InputProps = Override<\n React.ComponentProps<\"input\">,\n {\n onValueChange?: (value: string) => void;\n /**\n * 활성화 시 내용이 채워지면 오른쪽에 X 아이콘이 뜨고, 누르면 내용을 초기화한다.\n * controlled/uncontrolled 입력 모두 지원한다.\n */\n clearable?: boolean;\n }\n>;\n\nconst Input = React.forwardRef<HTMLInputElement, InputProps>(\n ({ className, type, onValueChange, onChange, clearable = false, ...props }, ref) => {\n const innerRef = React.useRef<HTMLInputElement>(null);\n\n // 전달된 ref와 내부 ref를 함께 채우는 콜백 ref\n const setRefs = React.useCallback(\n (node: HTMLInputElement | null) => {\n innerRef.current = node;\n if (isFunction(ref)) {\n ref(node);\n } else if (ref) {\n ref.current = node;\n }\n },\n [ref],\n );\n\n // controlled면 value로, uncontrolled면 내부 상태로 내용 유무를 판단한다.\n const isControlled = props.value !== undefined;\n const [uncontrolledHasValue, setUncontrolledHasValue] = React.useState(\n () => String(props.defaultValue ?? \"\").length > 0,\n );\n const hasValue = isControlled ? String(props.value ?? \"\").length > 0 : uncontrolledHasValue;\n\n const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {\n if (!isControlled) {\n setUncontrolledHasValue(e.target.value.length > 0);\n }\n onValueChange?.(e.target.value);\n onChange?.(e);\n };\n\n const handleClear = () => {\n const node = innerRef.current;\n if (node) {\n // native value setter로 값을 비우고 input 이벤트를 dispatch하면\n // React의 value tracker가 변화를 인식해 controlled/uncontrolled 모두에서 onChange가 발화된다.\n const nativeInputValueSetter = Object.getOwnPropertyDescriptor(\n window.HTMLInputElement.prototype,\n \"value\",\n )?.set;\n nativeInputValueSetter?.call(node, \"\");\n node.dispatchEvent(new Event(\"input\", { bubbles: true }));\n node.focus();\n }\n if (!isControlled) {\n setUncontrolledHasValue(false);\n }\n };\n\n const showClear = clearable && hasValue && !props.disabled && !props.readOnly;\n\n const inputEl = (\n <input\n type={type}\n data-slot=\"input\"\n className={cn(\n \"file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 border-input flex h-9 w-full min-w-0 rounded-md border px-3 py-1 text-base bg-input-background transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm\",\n \"focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]\",\n \"aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive\",\n clearable && \"pr-8\",\n className,\n )}\n ref={setRefs}\n {...props}\n onChange={handleChange}\n />\n );\n\n if (!clearable) {\n return inputEl;\n }\n\n return (\n <div className=\"relative w-full\">\n {inputEl}\n {showClear && (\n <button\n type=\"button\"\n tabIndex={-1}\n aria-label=\"Clear\"\n onMouseDown={(e) => e.preventDefault()}\n onClick={handleClear}\n className=\"absolute right-2 top-1/2 -translate-y-1/2 flex items-center justify-center text-muted-foreground opacity-60 hover:opacity-100 transition-opacity cursor-pointer\"\n >\n <XIcon className=\"h-4 w-4\" />\n </button>\n )}\n </div>\n );\n },\n);\nInput.displayName = \"Input\";\n\nexport { Input };\n"],"mappings":";;;;;;AAmBA,IAAM,IAAQ,EAAM,YACjB,EAAE,cAAW,SAAM,kBAAe,aAAU,eAAY,IAAO,GAAG,KAAS,MAAQ;CAClF,IAAM,IAAW,EAAM,OAAyB,IAAI,GAG9C,IAAU,EAAM,aACnB,MAAkC;EAEjC,AADA,EAAS,UAAU,GACf,EAAW,CAAG,IAChB,EAAI,CAAI,IACC,MACT,EAAI,UAAU;CAElB,GACA,CAAC,CAAG,CACN,GAGM,IAAe,EAAM,UAAU,KAAA,GAC/B,CAAC,GAAsB,KAA2B,EAAM,eACtD,OAAO,EAAM,gBAAgB,EAAE,EAAE,SAAS,CAClD,GACM,IAAW,IAAe,OAAO,EAAM,SAAS,EAAE,EAAE,SAAS,IAAI,GAEjE,KAAgB,MAA2C;EAK/D,AAJK,KACH,EAAwB,EAAE,OAAO,MAAM,SAAS,CAAC,GAEnD,IAAgB,EAAE,OAAO,KAAK,GAC9B,IAAW,CAAC;CACd,GAEM,UAAoB;EACxB,IAAM,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