@cocso-ui/react 1.10.1 → 1.12.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/cjs/components/breadcrumb/breadcrumb.d.ts +24 -3
- package/dist/cjs/components/breadcrumb/breadcrumb.js +44 -2
- package/dist/cjs/components/breadcrumb/breadcrumb.module.css.js +2 -2
- package/dist/cjs/components/card/card.d.ts +49 -2
- package/dist/cjs/components/card/card.js +76 -10
- package/dist/cjs/components/card/card.module.css.js +2 -2
- package/dist/cjs/components/copy-button/copy-button.d.ts +29 -0
- package/dist/cjs/components/copy-button/copy-button.js +67 -0
- package/dist/cjs/components/date-time-picker/date-time-picker.js +99 -76
- package/dist/cjs/components/day-picker/day-picker.js +24 -137
- package/dist/cjs/components/day-picker/day-picker.module.css.js +2 -2
- package/dist/cjs/components/empty-state/empty-state.d.ts +30 -0
- package/dist/cjs/components/empty-state/empty-state.js +50 -0
- package/dist/cjs/components/empty-state/empty-state.module.css.js +9 -0
- package/dist/cjs/components/file-uploader/file-uploader.d.ts +80 -0
- package/dist/cjs/components/file-uploader/file-uploader.js +258 -0
- package/dist/cjs/components/file-uploader/file-uploader.module.css.js +9 -0
- package/dist/cjs/components/form-field/form-field-context.d.ts +31 -0
- package/dist/cjs/components/form-field/form-field-context.js +53 -0
- package/dist/cjs/components/form-field/form-field-parts.d.ts +52 -0
- package/dist/cjs/components/form-field/form-field-parts.js +109 -0
- package/dist/cjs/components/form-field/form-field.d.ts +44 -0
- package/dist/cjs/components/form-field/form-field.js +61 -0
- package/dist/cjs/components/form-field/form-field.module.css.js +9 -0
- package/dist/cjs/components/info-popover/info-popover.d.ts +43 -0
- package/dist/cjs/components/info-popover/info-popover.js +57 -0
- package/dist/cjs/components/info-popover/info-popover.module.css.js +9 -0
- package/dist/cjs/components/loading-overlay/loading-overlay.d.ts +33 -0
- package/dist/cjs/components/loading-overlay/loading-overlay.js +68 -0
- package/dist/cjs/components/loading-overlay/loading-overlay.module.css.js +9 -0
- package/dist/cjs/components/number-input/number-input.d.ts +39 -0
- package/dist/cjs/components/number-input/number-input.js +167 -0
- package/dist/cjs/components/picker/year-month-view.js +216 -0
- package/dist/cjs/components/picker/year-month-view.module.css.js +9 -0
- package/dist/cjs/components/section-header/section-header.d.ts +48 -0
- package/dist/cjs/components/section-header/section-header.js +210 -0
- package/dist/cjs/components/section-header/section-header.module.css.js +9 -0
- package/dist/cjs/components/segmented-control/segmented-control.d.ts +55 -0
- package/dist/cjs/components/segmented-control/segmented-control.js +119 -0
- package/dist/cjs/components/segmented-control/segmented-control.module.css.js +9 -0
- package/dist/cjs/components/selection-chip/selection-chip.d.ts +22 -0
- package/dist/cjs/components/selection-chip/selection-chip.js +32 -0
- package/dist/cjs/components/selection-chip/selection-chip.module.css.js +9 -0
- package/dist/cjs/components/skeleton/skeleton.d.ts +7 -1
- package/dist/cjs/components/skeleton/skeleton.js +20 -2
- package/dist/cjs/components/skeleton/skeleton.module.css.js +1 -1
- package/dist/cjs/components/summary-card/summary-card.d.ts +47 -0
- package/dist/cjs/components/summary-card/summary-card.js +89 -0
- package/dist/cjs/components/summary-card/summary-card.module.css.js +9 -0
- package/dist/cjs/index.d.ts +15 -2
- package/dist/cjs/index.js +24 -0
- package/dist/esm/components/breadcrumb/breadcrumb.d.mts +24 -3
- package/dist/esm/components/breadcrumb/breadcrumb.mjs +44 -2
- package/dist/esm/components/breadcrumb/breadcrumb.module.css.mjs +1 -1
- package/dist/esm/components/card/card.d.mts +49 -2
- package/dist/esm/components/card/card.mjs +76 -10
- package/dist/esm/components/card/card.module.css.mjs +1 -1
- package/dist/esm/components/copy-button/copy-button.d.mts +29 -0
- package/dist/esm/components/copy-button/copy-button.mjs +65 -0
- package/dist/esm/components/date-time-picker/date-time-picker.mjs +99 -76
- package/dist/esm/components/day-picker/day-picker.mjs +27 -140
- package/dist/esm/components/day-picker/day-picker.module.css.mjs +1 -1
- package/dist/esm/components/empty-state/empty-state.d.mts +30 -0
- package/dist/esm/components/empty-state/empty-state.mjs +48 -0
- package/dist/esm/components/empty-state/empty-state.module.css.mjs +3 -0
- package/dist/esm/components/file-uploader/file-uploader.d.mts +80 -0
- package/dist/esm/components/file-uploader/file-uploader.mjs +256 -0
- package/dist/esm/components/file-uploader/file-uploader.module.css.mjs +3 -0
- package/dist/esm/components/form-field/form-field-context.d.mts +31 -0
- package/dist/esm/components/form-field/form-field-context.mjs +49 -0
- package/dist/esm/components/form-field/form-field-parts.d.mts +52 -0
- package/dist/esm/components/form-field/form-field-parts.mjs +105 -0
- package/dist/esm/components/form-field/form-field.d.mts +44 -0
- package/dist/esm/components/form-field/form-field.mjs +59 -0
- package/dist/esm/components/form-field/form-field.module.css.mjs +3 -0
- package/dist/esm/components/info-popover/info-popover.d.mts +43 -0
- package/dist/esm/components/info-popover/info-popover.mjs +55 -0
- package/dist/esm/components/info-popover/info-popover.module.css.mjs +3 -0
- package/dist/esm/components/loading-overlay/loading-overlay.d.mts +33 -0
- package/dist/esm/components/loading-overlay/loading-overlay.mjs +66 -0
- package/dist/esm/components/loading-overlay/loading-overlay.module.css.mjs +3 -0
- package/dist/esm/components/number-input/number-input.d.mts +39 -0
- package/dist/esm/components/number-input/number-input.mjs +165 -0
- package/dist/esm/components/picker/year-month-view.mjs +212 -0
- package/dist/esm/components/picker/year-month-view.module.css.mjs +3 -0
- package/dist/esm/components/section-header/section-header.d.mts +48 -0
- package/dist/esm/components/section-header/section-header.mjs +208 -0
- package/dist/esm/components/section-header/section-header.module.css.mjs +3 -0
- package/dist/esm/components/segmented-control/segmented-control.d.mts +55 -0
- package/dist/esm/components/segmented-control/segmented-control.mjs +117 -0
- package/dist/esm/components/segmented-control/segmented-control.module.css.mjs +3 -0
- package/dist/esm/components/selection-chip/selection-chip.d.mts +22 -0
- package/dist/esm/components/selection-chip/selection-chip.mjs +30 -0
- package/dist/esm/components/selection-chip/selection-chip.module.css.mjs +3 -0
- package/dist/esm/components/skeleton/skeleton.d.mts +7 -1
- package/dist/esm/components/skeleton/skeleton.mjs +20 -2
- package/dist/esm/components/summary-card/summary-card.d.mts +47 -0
- package/dist/esm/components/summary-card/summary-card.mjs +87 -0
- package/dist/esm/components/summary-card/summary-card.module.css.mjs +3 -0
- package/dist/esm/index.css +2 -2
- package/dist/esm/index.d.mts +15 -2
- package/dist/esm/index.mjs +12 -0
- package/package.json +1 -1
|
@@ -1,19 +1,15 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
3
|
-
import { format
|
|
2
|
+
import { ArrowIOSBackwardIcon, ArrowIOSForwardIcon } from '@cocso-ui/react-icons';
|
|
3
|
+
import { format } from 'date-fns';
|
|
4
4
|
import { ko } from 'date-fns/locale';
|
|
5
|
-
import { useState
|
|
5
|
+
import { useState } from 'react';
|
|
6
6
|
import DatePicker from 'react-datepicker';
|
|
7
7
|
import { cn } from '../../cn.mjs';
|
|
8
|
+
import { useYearMonthView, YearMonthView, CalendarTitle } from '../picker/year-month-view.mjs';
|
|
8
9
|
import styles from './day-picker.module.css.mjs';
|
|
9
10
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
10
11
|
import { Popover } from '../popover/popover.mjs';
|
|
11
12
|
import { Button } from '../button/button.mjs';
|
|
12
|
-
import { Typography } from '../typography/typography.mjs';
|
|
13
|
-
|
|
14
|
-
const MONTHS = Array.from({
|
|
15
|
-
length: 12
|
|
16
|
-
}, (_, month) => month);
|
|
17
13
|
|
|
18
14
|
// Where the calendar opens: the value's month, else today's, pulled inside
|
|
19
15
|
// the bounds so an empty picker with a future `minDate` does not open on a
|
|
@@ -47,54 +43,17 @@ function DayPicker({
|
|
|
47
43
|
const [open, setOpen] = useState(false);
|
|
48
44
|
// The header's month opens a year-and-month grid in the same panel, so a
|
|
49
45
|
// date years away is a few clicks instead of one click per month.
|
|
50
|
-
const
|
|
51
|
-
const [viewDate, setViewDate] = useState(() => initialViewDate(value, minDate, maxDate));
|
|
52
|
-
// Remounts the calendar after a jump. A live calendar follows its keyboard
|
|
53
|
-
// cursor before `openToDate`, so a fresh mount is the reliable way to land
|
|
54
|
-
// on the picked month.
|
|
55
|
-
const [calendarKey, setCalendarKey] = useState(0);
|
|
56
|
-
// The day grid's height, held by the month grid so the panel does not
|
|
57
|
-
// shrink under the pointer — a panel opened above its trigger would jump.
|
|
58
|
-
const [dayViewHeight, setDayViewHeight] = useState();
|
|
59
|
-
const focusAfterViewChange = useRef(false);
|
|
60
|
-
const titleRef = useRef(null);
|
|
61
|
-
const currentMonthRef = useRef(null);
|
|
62
|
-
|
|
63
|
-
// A view change unmounts the control that had focus. Hand it to the
|
|
64
|
-
// matching control in the new view instead of dropping it on the body.
|
|
65
|
-
useEffect(() => {
|
|
66
|
-
if (!focusAfterViewChange.current) {
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
focusAfterViewChange.current = false;
|
|
70
|
-
(view === "month" ? currentMonthRef : titleRef).current?.focus();
|
|
71
|
-
}, [view]);
|
|
46
|
+
const calendar = useYearMonthView(() => initialViewDate(value, minDate, maxDate));
|
|
72
47
|
const handleOpenChange = next => {
|
|
73
48
|
setOpen(next);
|
|
74
49
|
if (next) {
|
|
75
|
-
|
|
76
|
-
setViewDate(initialViewDate(value, minDate, maxDate));
|
|
77
|
-
setCalendarKey(key => key + 1);
|
|
50
|
+
calendar.reset(initialViewDate(value, minDate, maxDate));
|
|
78
51
|
}
|
|
79
52
|
};
|
|
80
53
|
const handleChange = date => {
|
|
81
54
|
onValueChange?.(date);
|
|
82
55
|
setOpen(false);
|
|
83
56
|
};
|
|
84
|
-
const changeView = next => {
|
|
85
|
-
focusAfterViewChange.current = true;
|
|
86
|
-
setView(next);
|
|
87
|
-
};
|
|
88
|
-
const jumpTo = (year, month) => {
|
|
89
|
-
setViewDate(new Date(year, month, 1));
|
|
90
|
-
setCalendarKey(key => key + 1);
|
|
91
|
-
changeView("day");
|
|
92
|
-
};
|
|
93
|
-
const year = viewDate.getFullYear();
|
|
94
|
-
const monthDisabled = month => {
|
|
95
|
-
const first = startOfMonth(new Date(year, month, 1));
|
|
96
|
-
return minDate !== undefined && endOfMonth(first) < minDate || maxDate !== undefined && first > maxDate;
|
|
97
|
-
};
|
|
98
57
|
return /*#__PURE__*/jsx("div", {
|
|
99
58
|
className: cn(styles.root, className),
|
|
100
59
|
ref: ref,
|
|
@@ -114,75 +73,20 @@ function DayPicker({
|
|
|
114
73
|
}), /*#__PURE__*/jsxs(Popover.Content, {
|
|
115
74
|
"aria-label": "Select date",
|
|
116
75
|
className: styles.content,
|
|
117
|
-
children: [/*#__PURE__*/
|
|
118
|
-
|
|
119
|
-
hidden: view !== "month",
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
type: "button",
|
|
130
|
-
children: [/*#__PURE__*/jsx(Typography, {
|
|
131
|
-
size: "small",
|
|
132
|
-
type: "body",
|
|
133
|
-
weight: "semibold",
|
|
134
|
-
children: viewDate.toLocaleDateString(locale.code ?? "ko-KR", {
|
|
135
|
-
year: "numeric"
|
|
136
|
-
})
|
|
137
|
-
}), /*#__PURE__*/jsx(ArrowDropUpIcon, {
|
|
138
|
-
"aria-hidden": true,
|
|
139
|
-
size: 16
|
|
140
|
-
})]
|
|
141
|
-
}), /*#__PURE__*/jsxs("div", {
|
|
142
|
-
className: styles.menu,
|
|
143
|
-
children: [/*#__PURE__*/jsx(Button, {
|
|
144
|
-
"aria-label": "Previous year",
|
|
145
|
-
className: styles.arrow,
|
|
146
|
-
disabled: minDate !== undefined && year - 1 < minDate.getFullYear(),
|
|
147
|
-
onClick: () => setViewDate(new Date(year - 1, 0, 1)),
|
|
148
|
-
size: "x-small",
|
|
149
|
-
svgOnly: true,
|
|
150
|
-
type: "button",
|
|
151
|
-
variant: "outline",
|
|
152
|
-
children: /*#__PURE__*/jsx(ArrowIOSBackwardIcon, {})
|
|
153
|
-
}), /*#__PURE__*/jsx(Button, {
|
|
154
|
-
"aria-label": "Next year",
|
|
155
|
-
className: styles.arrow,
|
|
156
|
-
disabled: maxDate !== undefined && year + 1 > maxDate.getFullYear(),
|
|
157
|
-
onClick: () => setViewDate(new Date(year + 1, 0, 1)),
|
|
158
|
-
size: "x-small",
|
|
159
|
-
svgOnly: true,
|
|
160
|
-
type: "button",
|
|
161
|
-
variant: "outline",
|
|
162
|
-
children: /*#__PURE__*/jsx(ArrowIOSForwardIcon, {})
|
|
163
|
-
})]
|
|
164
|
-
})]
|
|
165
|
-
}), /*#__PURE__*/jsx("div", {
|
|
166
|
-
className: styles.monthGrid,
|
|
167
|
-
children: MONTHS.map(month => {
|
|
168
|
-
const current = value !== undefined && value.getFullYear() === year && value.getMonth() === month;
|
|
169
|
-
const isViewed = viewDate.getMonth() === month;
|
|
170
|
-
return /*#__PURE__*/jsx("button", {
|
|
171
|
-
"aria-current": current ? "date" : undefined,
|
|
172
|
-
className: styles.monthCell,
|
|
173
|
-
"data-selected": current || undefined,
|
|
174
|
-
disabled: monthDisabled(month),
|
|
175
|
-
onClick: () => jumpTo(year, month),
|
|
176
|
-
ref: isViewed ? currentMonthRef : undefined,
|
|
177
|
-
type: "button",
|
|
178
|
-
children: format(new Date(year, month, 1), "LLL", {
|
|
179
|
-
locale
|
|
180
|
-
})
|
|
181
|
-
}, month);
|
|
182
|
-
})
|
|
183
|
-
})]
|
|
76
|
+
children: [/*#__PURE__*/jsx(YearMonthView, {
|
|
77
|
+
currentMonthRef: calendar.currentMonthRef,
|
|
78
|
+
hidden: calendar.view !== "month",
|
|
79
|
+
locale: locale,
|
|
80
|
+
maxDate: maxDate,
|
|
81
|
+
minDate: minDate,
|
|
82
|
+
minHeight: calendar.dayViewHeight,
|
|
83
|
+
onBack: () => calendar.changeView("day"),
|
|
84
|
+
onPick: calendar.jumpTo,
|
|
85
|
+
onYearChange: calendar.setViewDate,
|
|
86
|
+
selected: value,
|
|
87
|
+
viewDate: calendar.viewDate
|
|
184
88
|
}), /*#__PURE__*/jsx("div", {
|
|
185
|
-
hidden: view !== "day",
|
|
89
|
+
hidden: calendar.view !== "day",
|
|
186
90
|
children: /*#__PURE__*/jsx(DatePicker, {
|
|
187
91
|
dateFormat: dateFormat,
|
|
188
92
|
dayClassName: date => {
|
|
@@ -207,8 +111,8 @@ function DayPicker({
|
|
|
207
111
|
maxDate: maxDate,
|
|
208
112
|
minDate: minDate,
|
|
209
113
|
onChange: handleChange,
|
|
210
|
-
onMonthChange: setViewDate,
|
|
211
|
-
openToDate: viewDate,
|
|
114
|
+
onMonthChange: calendar.setViewDate,
|
|
115
|
+
openToDate: calendar.viewDate,
|
|
212
116
|
renderCustomHeader: ({
|
|
213
117
|
date,
|
|
214
118
|
decreaseMonth,
|
|
@@ -216,28 +120,11 @@ function DayPicker({
|
|
|
216
120
|
prevMonthButtonDisabled,
|
|
217
121
|
nextMonthButtonDisabled
|
|
218
122
|
}) => /*#__PURE__*/jsxs(Fragment, {
|
|
219
|
-
children: [/*#__PURE__*/
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
setViewDate(date);
|
|
225
|
-
changeView("month");
|
|
226
|
-
},
|
|
227
|
-
ref: titleRef,
|
|
228
|
-
type: "button",
|
|
229
|
-
children: [/*#__PURE__*/jsx(Typography, {
|
|
230
|
-
size: "small",
|
|
231
|
-
type: "body",
|
|
232
|
-
weight: "semibold",
|
|
233
|
-
children: date.toLocaleDateString(locale.code ?? "ko-KR", {
|
|
234
|
-
year: "numeric",
|
|
235
|
-
month: "long"
|
|
236
|
-
})
|
|
237
|
-
}), /*#__PURE__*/jsx(ArrowDropDownIcon, {
|
|
238
|
-
"aria-hidden": true,
|
|
239
|
-
size: 16
|
|
240
|
-
})]
|
|
123
|
+
children: [/*#__PURE__*/jsx(CalendarTitle, {
|
|
124
|
+
date: date,
|
|
125
|
+
locale: locale,
|
|
126
|
+
onPress: panel => calendar.openMonths(date, panel),
|
|
127
|
+
ref: calendar.titleRef
|
|
241
128
|
}), /*#__PURE__*/jsxs("div", {
|
|
242
129
|
className: styles.menu,
|
|
243
130
|
children: [/*#__PURE__*/jsx(Button, {
|
|
@@ -265,7 +152,7 @@ function DayPicker({
|
|
|
265
152
|
}),
|
|
266
153
|
selected: value,
|
|
267
154
|
showPopperArrow: false
|
|
268
|
-
}, calendarKey)
|
|
155
|
+
}, calendar.calendarKey)
|
|
269
156
|
})]
|
|
270
157
|
})]
|
|
271
158
|
})
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var styles = {"root":"day-picker-module_root__QVrbb","content":"day-picker-module_content__34qUu","menu":"day-picker-module_menu__2eMt0","arrow":"day-picker-module_arrow__p9QLq","
|
|
1
|
+
var styles = {"root":"day-picker-module_root__QVrbb","content":"day-picker-module_content__34qUu","menu":"day-picker-module_menu__2eMt0","arrow":"day-picker-module_arrow__p9QLq","sunday":"day-picker-module_sunday__nVavs","saturday":"day-picker-module_saturday__ZYoB9"};
|
|
2
2
|
|
|
3
3
|
export { styles as default };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ComponentProps, ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
interface EmptyStateProps extends Omit<ComponentProps<"div">, "title"> {
|
|
5
|
+
/** A button placed under the text. Usually a single "add" action. */
|
|
6
|
+
action?: ReactNode;
|
|
7
|
+
/**
|
|
8
|
+
* Rendered above the title, in the same frame — a `Spinner` while loading,
|
|
9
|
+
* or an illustration. Loading, failure and empty share one frame so the
|
|
10
|
+
* three states stand in the same place at the same size.
|
|
11
|
+
*/
|
|
12
|
+
children?: ReactNode;
|
|
13
|
+
/** Why being empty is expected here, or what to do next. */
|
|
14
|
+
description?: ReactNode;
|
|
15
|
+
/** One bold line that says what is missing. */
|
|
16
|
+
title?: ReactNode;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The empty state of a region that is not a table.
|
|
20
|
+
*
|
|
21
|
+
* Inside a table, use the table's own empty row — it has to span the columns.
|
|
22
|
+
* Outside one (a list inside a card, a section of a popup), bare text floating
|
|
23
|
+
* in the layout reads as "something went wrong". The dashed frame marks out
|
|
24
|
+
* the place the content would occupy, so the screen says first that being
|
|
25
|
+
* empty is normal.
|
|
26
|
+
*/
|
|
27
|
+
declare function EmptyState({ ref, className, title, description, action, children, ...props }: EmptyStateProps): react_jsx_runtime.JSX.Element;
|
|
28
|
+
|
|
29
|
+
export { EmptyState };
|
|
30
|
+
export type { EmptyStateProps };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { cn } from '../../cn.mjs';
|
|
2
|
+
import styles from './empty-state.module.css.mjs';
|
|
3
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
4
|
+
import { Typography } from '../typography/typography.mjs';
|
|
5
|
+
import { colors } from '../../token/color.mjs';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The empty state of a region that is not a table.
|
|
9
|
+
*
|
|
10
|
+
* Inside a table, use the table's own empty row — it has to span the columns.
|
|
11
|
+
* Outside one (a list inside a card, a section of a popup), bare text floating
|
|
12
|
+
* in the layout reads as "something went wrong". The dashed frame marks out
|
|
13
|
+
* the place the content would occupy, so the screen says first that being
|
|
14
|
+
* empty is normal.
|
|
15
|
+
*/
|
|
16
|
+
function EmptyState({
|
|
17
|
+
ref,
|
|
18
|
+
className,
|
|
19
|
+
title,
|
|
20
|
+
description,
|
|
21
|
+
action,
|
|
22
|
+
children,
|
|
23
|
+
...props
|
|
24
|
+
}) {
|
|
25
|
+
return /*#__PURE__*/jsxs("div", {
|
|
26
|
+
className: cn(styles.root, className),
|
|
27
|
+
"data-cocso-component": "empty-state",
|
|
28
|
+
ref: ref,
|
|
29
|
+
...props,
|
|
30
|
+
children: [children, title && /*#__PURE__*/jsx(Typography, {
|
|
31
|
+
size: "small",
|
|
32
|
+
type: "body",
|
|
33
|
+
weight: "medium",
|
|
34
|
+
children: title
|
|
35
|
+
}), description && /*#__PURE__*/jsx(Typography, {
|
|
36
|
+
className: styles.description,
|
|
37
|
+
color: colors.textSecondary,
|
|
38
|
+
size: "x-small",
|
|
39
|
+
type: "body",
|
|
40
|
+
children: description
|
|
41
|
+
}), action && /*#__PURE__*/jsx("div", {
|
|
42
|
+
className: styles.action,
|
|
43
|
+
children: action
|
|
44
|
+
})]
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { EmptyState };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ComponentProps, ReactNode, Ref } from 'react';
|
|
3
|
+
|
|
4
|
+
/** One chosen file. The `File` is kept so the caller can upload it later. */
|
|
5
|
+
interface FileItem {
|
|
6
|
+
file: File;
|
|
7
|
+
name: string;
|
|
8
|
+
size: number;
|
|
9
|
+
}
|
|
10
|
+
/** Why a file was turned away. */
|
|
11
|
+
type FileUploaderRejectionReason = "type" | "size" | "count";
|
|
12
|
+
interface FileUploaderRejection {
|
|
13
|
+
/** The file that was turned away. */
|
|
14
|
+
file: File;
|
|
15
|
+
/** The message that describes it, already resolved from `messages`. */
|
|
16
|
+
message: string;
|
|
17
|
+
reason: FileUploaderRejectionReason;
|
|
18
|
+
}
|
|
19
|
+
/** Every word the component says. Each one is optional; the rest keep the default. */
|
|
20
|
+
interface FileUploaderMessages {
|
|
21
|
+
/** The prompt in the one-line (`compact`) drop zone. */
|
|
22
|
+
compactPrompt?: ReactNode;
|
|
23
|
+
/** The `(max N MB)` hint shown when no `sizeHint` is given. */
|
|
24
|
+
maxSizeHint?: (maxSizeMb: number) => ReactNode;
|
|
25
|
+
/** The prompt in the default drop zone. */
|
|
26
|
+
prompt?: ReactNode;
|
|
27
|
+
/** A file arrived after `maxFiles` was reached. */
|
|
28
|
+
rejectCount?: (maxFiles: number) => string;
|
|
29
|
+
/** A file is larger than `maxSize`. */
|
|
30
|
+
rejectSize?: (file: File, maxSizeMb: number) => string;
|
|
31
|
+
/** A file does not match `accept`. */
|
|
32
|
+
rejectType?: (file: File) => string;
|
|
33
|
+
/** Accessible name of the remove button on each row. */
|
|
34
|
+
remove?: (fileName: string) => string;
|
|
35
|
+
}
|
|
36
|
+
interface FileUploaderProps extends Omit<ComponentProps<"input">, "type" | "size" | "ref"> {
|
|
37
|
+
/**
|
|
38
|
+
* One-line drop zone. A table row that holds an uploader grows by the full
|
|
39
|
+
* zone's height otherwise; this lays the prompt out on a single line.
|
|
40
|
+
*/
|
|
41
|
+
compact?: boolean;
|
|
42
|
+
/** The chosen files (controlled). */
|
|
43
|
+
files: FileItem[];
|
|
44
|
+
/** Most files that may be chosen in total. Default 2. */
|
|
45
|
+
maxFiles?: number;
|
|
46
|
+
/** Largest single file in bytes. Default 3 MB. */
|
|
47
|
+
maxSize?: number;
|
|
48
|
+
/** Override any of the default (Korean) words. */
|
|
49
|
+
messages?: FileUploaderMessages;
|
|
50
|
+
/** Called with the next list whenever files are added or removed. */
|
|
51
|
+
onFilesChange: (files: FileItem[]) => void;
|
|
52
|
+
/**
|
|
53
|
+
* Called with every file turned away by one pick or drop. When given, the
|
|
54
|
+
* default error toasts are not shown — the caller reports them instead.
|
|
55
|
+
*/
|
|
56
|
+
onReject?: (rejections: FileUploaderRejection[]) => void;
|
|
57
|
+
/** Called after a row's remove button takes a file out. */
|
|
58
|
+
onRemove?: (fileName: string) => void;
|
|
59
|
+
/** Forwarded to the underlying `<input type="file">`. */
|
|
60
|
+
ref?: Ref<HTMLInputElement>;
|
|
61
|
+
/** Replaces the `(max N MB)` hint. Still shown while disabled. */
|
|
62
|
+
sizeHint?: ReactNode;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Drag-and-drop or pick files, listed underneath as `FileRow`s.
|
|
66
|
+
*
|
|
67
|
+
* The drop zone is a `<label>` around a real, visually hidden
|
|
68
|
+
* `<input type="file">`, so Tab reaches the input, Enter and Space open the
|
|
69
|
+
* picker natively, and the zone's prompt is the input's accessible name. The
|
|
70
|
+
* zone is drawn focused while the input is. It disappears once `maxFiles` is
|
|
71
|
+
* reached (or one file is chosen when `multiple` is false).
|
|
72
|
+
*
|
|
73
|
+
* Files that fail `accept`, `maxSize` or `maxFiles` are turned away and
|
|
74
|
+
* reported — through `onReject` when given, otherwise as error toasts (which
|
|
75
|
+
* need a `Toaster` mounted).
|
|
76
|
+
*/
|
|
77
|
+
declare function FileUploader({ ref, className, style, files, onFilesChange, onRemove, onReject, maxFiles, maxSize, sizeHint, compact, messages: messagesProp, multiple, accept, disabled, onChange, ...props }: FileUploaderProps): react_jsx_runtime.JSX.Element;
|
|
78
|
+
|
|
79
|
+
export { FileUploader };
|
|
80
|
+
export type { FileItem, FileUploaderMessages, FileUploaderProps, FileUploaderRejection, FileUploaderRejectionReason };
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { PlusIcon, CloseIcon } from '@cocso-ui/react-icons';
|
|
3
|
+
import { useState, useRef } from 'react';
|
|
4
|
+
import { cn } from '../../cn.mjs';
|
|
5
|
+
import styles from './file-uploader.module.css.mjs';
|
|
6
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
7
|
+
import { Typography } from '../typography/typography.mjs';
|
|
8
|
+
import { FileRow } from '../file-row/file-row.mjs';
|
|
9
|
+
import { Button } from '../button/button.mjs';
|
|
10
|
+
import { toast } from 'sonner';
|
|
11
|
+
|
|
12
|
+
const DEFAULT_MESSAGES = {
|
|
13
|
+
compactPrompt: "끌어다 놓거나 클릭해 등록",
|
|
14
|
+
prompt: "파일을 여기에 끌어다 놓거나, 클릭하여 파일을 선택해주세요",
|
|
15
|
+
maxSizeHint: maxSizeMb => `(최대 ${maxSizeMb}MB)`,
|
|
16
|
+
remove: fileName => `${fileName} 삭제`,
|
|
17
|
+
rejectCount: maxFiles => `최대 ${maxFiles}개의 파일만 업로드할 수 있습니다.`,
|
|
18
|
+
rejectSize: (file, maxSizeMb) => `${file.name}은(는) ${maxSizeMb}MB를 초과합니다.`,
|
|
19
|
+
rejectType: file => `${file.name}은(는) 허용되지 않는 파일 타입입니다.`
|
|
20
|
+
};
|
|
21
|
+
const BYTES_PER_MB = 1024 * 1024;
|
|
22
|
+
const DEFAULT_MAX_SIZE = 3 * BYTES_PER_MB;
|
|
23
|
+
function matchesAccept(file, accept) {
|
|
24
|
+
if (!accept) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
return accept.split(",").map(type => type.trim()).filter(Boolean).some(type => {
|
|
28
|
+
if (type.endsWith("/*")) {
|
|
29
|
+
return file.type.startsWith(type.slice(0, -1));
|
|
30
|
+
}
|
|
31
|
+
return file.type === type || file.name.toLowerCase().endsWith(type.toLowerCase());
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Drag-and-drop or pick files, listed underneath as `FileRow`s.
|
|
37
|
+
*
|
|
38
|
+
* The drop zone is a `<label>` around a real, visually hidden
|
|
39
|
+
* `<input type="file">`, so Tab reaches the input, Enter and Space open the
|
|
40
|
+
* picker natively, and the zone's prompt is the input's accessible name. The
|
|
41
|
+
* zone is drawn focused while the input is. It disappears once `maxFiles` is
|
|
42
|
+
* reached (or one file is chosen when `multiple` is false).
|
|
43
|
+
*
|
|
44
|
+
* Files that fail `accept`, `maxSize` or `maxFiles` are turned away and
|
|
45
|
+
* reported — through `onReject` when given, otherwise as error toasts (which
|
|
46
|
+
* need a `Toaster` mounted).
|
|
47
|
+
*/
|
|
48
|
+
function FileUploader({
|
|
49
|
+
ref,
|
|
50
|
+
className,
|
|
51
|
+
style,
|
|
52
|
+
files,
|
|
53
|
+
onFilesChange,
|
|
54
|
+
onRemove,
|
|
55
|
+
onReject,
|
|
56
|
+
maxFiles = 2,
|
|
57
|
+
maxSize = DEFAULT_MAX_SIZE,
|
|
58
|
+
sizeHint,
|
|
59
|
+
compact = false,
|
|
60
|
+
messages: messagesProp,
|
|
61
|
+
multiple = true,
|
|
62
|
+
accept,
|
|
63
|
+
disabled = false,
|
|
64
|
+
onChange,
|
|
65
|
+
...props
|
|
66
|
+
}) {
|
|
67
|
+
const messages = {
|
|
68
|
+
...DEFAULT_MESSAGES,
|
|
69
|
+
...messagesProp
|
|
70
|
+
};
|
|
71
|
+
const [isDragActive, setIsDragActive] = useState(false);
|
|
72
|
+
const zoneRef = useRef(null);
|
|
73
|
+
const maxSizeMb = Math.round(maxSize / BYTES_PER_MB);
|
|
74
|
+
|
|
75
|
+
// Names and sizes can repeat, so each `File` gets an id the first time it
|
|
76
|
+
// is seen. The caller keeps the same `File` across renders, so the id holds.
|
|
77
|
+
const keys = useRef(new WeakMap());
|
|
78
|
+
const nextKey = useRef(0);
|
|
79
|
+
const keyFor = file => {
|
|
80
|
+
let key = keys.current.get(file);
|
|
81
|
+
if (key === undefined) {
|
|
82
|
+
key = nextKey.current++;
|
|
83
|
+
keys.current.set(file, key);
|
|
84
|
+
}
|
|
85
|
+
return key;
|
|
86
|
+
};
|
|
87
|
+
const addFiles = incoming => {
|
|
88
|
+
const rejections = [];
|
|
89
|
+
const accepted = [];
|
|
90
|
+
for (const file of Array.from(incoming)) {
|
|
91
|
+
if (!matchesAccept(file, accept)) {
|
|
92
|
+
rejections.push({
|
|
93
|
+
file,
|
|
94
|
+
reason: "type",
|
|
95
|
+
message: messages.rejectType(file)
|
|
96
|
+
});
|
|
97
|
+
} else if (file.size > maxSize) {
|
|
98
|
+
rejections.push({
|
|
99
|
+
file,
|
|
100
|
+
reason: "size",
|
|
101
|
+
message: messages.rejectSize(file, maxSizeMb)
|
|
102
|
+
});
|
|
103
|
+
} else {
|
|
104
|
+
accepted.push(file);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const remaining = Math.max(0, maxFiles - files.length);
|
|
108
|
+
const added = accepted.slice(0, remaining);
|
|
109
|
+
for (const file of accepted.slice(remaining)) {
|
|
110
|
+
rejections.push({
|
|
111
|
+
file,
|
|
112
|
+
reason: "count",
|
|
113
|
+
message: messages.rejectCount(maxFiles)
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
if (rejections.length > 0) {
|
|
117
|
+
if (onReject) {
|
|
118
|
+
onReject(rejections);
|
|
119
|
+
} else {
|
|
120
|
+
// One toast per distinct message: ten files over the count limit
|
|
121
|
+
// say so once, not ten times.
|
|
122
|
+
for (const message of new Set(rejections.map(r => r.message))) {
|
|
123
|
+
toast.error(message);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (added.length > 0) {
|
|
128
|
+
onFilesChange([...files, ...added.map(file => ({
|
|
129
|
+
file,
|
|
130
|
+
name: file.name,
|
|
131
|
+
size: file.size
|
|
132
|
+
}))]);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
const handleDragEnter = event => {
|
|
136
|
+
event.preventDefault();
|
|
137
|
+
event.stopPropagation();
|
|
138
|
+
if (!disabled) {
|
|
139
|
+
setIsDragActive(true);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
const handleDragOver = event => {
|
|
143
|
+
event.preventDefault();
|
|
144
|
+
event.stopPropagation();
|
|
145
|
+
};
|
|
146
|
+
const handleDragLeave = event => {
|
|
147
|
+
event.preventDefault();
|
|
148
|
+
event.stopPropagation();
|
|
149
|
+
const next = event.relatedTarget;
|
|
150
|
+
if (!(next && zoneRef.current?.contains(next))) {
|
|
151
|
+
setIsDragActive(false);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
const handleDrop = event => {
|
|
155
|
+
event.preventDefault();
|
|
156
|
+
event.stopPropagation();
|
|
157
|
+
setIsDragActive(false);
|
|
158
|
+
if (disabled) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (event.dataTransfer?.files) {
|
|
162
|
+
addFiles(event.dataTransfer.files);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
const handleRemove = index => {
|
|
166
|
+
const removed = files[index];
|
|
167
|
+
onFilesChange(files.filter((_, i) => i !== index));
|
|
168
|
+
if (removed) {
|
|
169
|
+
onRemove?.(removed.name);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
const hideZone = files.length >= maxFiles || !multiple && files.length >= 1;
|
|
173
|
+
const hint = sizeHint ?? messages.maxSizeHint(maxSizeMb);
|
|
174
|
+
const prompt = compact ? messages.compactPrompt : messages.prompt;
|
|
175
|
+
return /*#__PURE__*/jsxs("div", {
|
|
176
|
+
className: cn(styles.uploader, className),
|
|
177
|
+
"data-cocso-component": "file-uploader",
|
|
178
|
+
style: style,
|
|
179
|
+
children: [!hideZone &&
|
|
180
|
+
/*#__PURE__*/
|
|
181
|
+
// biome-ignore lint/a11y/noNoninteractiveElementInteractions: drag and drop has no keyboard equivalent; the wrapped input is the keyboard path
|
|
182
|
+
jsxs("label", {
|
|
183
|
+
className: cn(styles.zone, compact && styles.compact),
|
|
184
|
+
"data-cocso-component": "file-uploader-dropzone",
|
|
185
|
+
"data-compact": compact || undefined,
|
|
186
|
+
"data-disabled": disabled || undefined,
|
|
187
|
+
"data-drag-active": isDragActive || undefined,
|
|
188
|
+
onDragEnter: handleDragEnter,
|
|
189
|
+
onDragLeave: handleDragLeave,
|
|
190
|
+
onDragOver: handleDragOver,
|
|
191
|
+
onDrop: handleDrop,
|
|
192
|
+
ref: zoneRef,
|
|
193
|
+
children: [/*#__PURE__*/jsx("input", {
|
|
194
|
+
accept: accept,
|
|
195
|
+
className: styles.input,
|
|
196
|
+
disabled: disabled,
|
|
197
|
+
multiple: multiple,
|
|
198
|
+
ref: ref,
|
|
199
|
+
type: "file",
|
|
200
|
+
...props,
|
|
201
|
+
onChange: event => {
|
|
202
|
+
onChange?.(event);
|
|
203
|
+
if (event.target.files) {
|
|
204
|
+
addFiles(event.target.files);
|
|
205
|
+
}
|
|
206
|
+
event.target.value = "";
|
|
207
|
+
}
|
|
208
|
+
}), compact && !disabled && /*#__PURE__*/jsx(PlusIcon, {
|
|
209
|
+
"aria-hidden": true,
|
|
210
|
+
className: styles.icon,
|
|
211
|
+
size: 20
|
|
212
|
+
}), /*#__PURE__*/jsxs(Typography, {
|
|
213
|
+
className: styles.text,
|
|
214
|
+
render: /*#__PURE__*/jsx("span", {}),
|
|
215
|
+
size: "x-small",
|
|
216
|
+
type: "body",
|
|
217
|
+
children: [!disabled && /*#__PURE__*/jsx("span", {
|
|
218
|
+
className: styles.prompt,
|
|
219
|
+
children: prompt
|
|
220
|
+
}), !disabled && (compact ? " · " : " "), /*#__PURE__*/jsx("span", {
|
|
221
|
+
className: styles.hint,
|
|
222
|
+
children: hint
|
|
223
|
+
})]
|
|
224
|
+
}), !(compact || disabled) && /*#__PURE__*/jsx(PlusIcon, {
|
|
225
|
+
"aria-hidden": true,
|
|
226
|
+
className: styles.icon,
|
|
227
|
+
size: 20
|
|
228
|
+
})]
|
|
229
|
+
}), files.length > 0 && /*#__PURE__*/jsx("ul", {
|
|
230
|
+
className: styles.list,
|
|
231
|
+
"data-cocso-component": "file-uploader-list",
|
|
232
|
+
children: files.map((item, index) => /*#__PURE__*/jsx("li", {
|
|
233
|
+
className: styles.item,
|
|
234
|
+
children: /*#__PURE__*/jsx(FileRow, {
|
|
235
|
+
actions: /*#__PURE__*/jsx(Button, {
|
|
236
|
+
"aria-label": messages.remove(item.name),
|
|
237
|
+
disabled: disabled,
|
|
238
|
+
onClick: () => handleRemove(index),
|
|
239
|
+
size: "x-small",
|
|
240
|
+
svgOnly: true,
|
|
241
|
+
type: "button",
|
|
242
|
+
variant: "ghost",
|
|
243
|
+
children: /*#__PURE__*/jsx(CloseIcon, {
|
|
244
|
+
size: 14
|
|
245
|
+
})
|
|
246
|
+
}),
|
|
247
|
+
disabled: disabled,
|
|
248
|
+
name: item.name,
|
|
249
|
+
stretch: true
|
|
250
|
+
})
|
|
251
|
+
}, keyFor(item.file)))
|
|
252
|
+
})]
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export { FileUploader };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
var styles = {"uploader":"file-uploader-module_uploader__H6Mi3","zone":"file-uploader-module_zone__-lnp-","input":"file-uploader-module_input__wft8r","compact":"file-uploader-module_compact__s-Ynq","text":"file-uploader-module_text__hTQbR","prompt":"file-uploader-module_prompt__bvvKg","icon":"file-uploader-module_icon__7Pt-y","list":"file-uploader-module_list__neAsl","item":"file-uploader-module_item__p8Jns"};
|
|
2
|
+
|
|
3
|
+
export { styles as default };
|