jy-headless 0.3.6 → 0.3.11
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/README.md +62 -0
- package/dist/Autocomplete/Autocomplete.d.ts +8 -0
- package/dist/Autocomplete/Autocomplete.js +309 -0
- package/dist/Autocomplete/Autocomplete.type.d.ts +36 -0
- package/dist/Autocomplete/index.d.ts +1 -0
- package/{cjs → dist}/Input/TextInput.type.d.ts +2 -2
- package/dist/Input/index.d.ts +2 -0
- package/dist/Select/Select.js +176 -0
- package/dist/Select/index.d.ts +1 -0
- package/dist/cjs/Autocomplete/Autocomplete.d.ts +8 -0
- package/dist/cjs/Autocomplete/Autocomplete.js +311 -0
- package/dist/cjs/Autocomplete/Autocomplete.type.d.ts +36 -0
- package/dist/cjs/Autocomplete/index.d.ts +1 -0
- package/{Input → dist/cjs/Input}/TextInput.type.d.ts +2 -2
- package/dist/cjs/Input/index.d.ts +2 -0
- package/dist/cjs/Select/Select.js +178 -0
- package/dist/cjs/Select/index.d.ts +1 -0
- package/dist/cjs/index.d.ts +5 -0
- package/{cjs → dist/cjs}/index.js +3 -0
- package/dist/index.d.ts +5 -0
- package/{index.js → dist/index.js} +2 -0
- package/package.json +66 -45
- package/Popover/Popover.d.ts +0 -2
- package/Popover/Popover.js +0 -75
- package/Popover/Popover.type.d.ts +0 -9
- package/Popover/index.d.ts +0 -2
- package/cjs/Popover/Popover.d.ts +0 -2
- package/cjs/Popover/Popover.js +0 -77
- package/cjs/Popover/Popover.type.d.ts +0 -9
- package/cjs/Popover/index.d.ts +0 -2
- package/cjs/index.d.ts +0 -4
- package/index.d.ts +0 -4
- package/version.txt +0 -1
- /package/{Input → dist/Input}/NumberInput.d.ts +0 -0
- /package/{Input → dist/Input}/NumberInput.type.d.ts +0 -0
- /package/{Input → dist/Input}/TextInput.d.ts +0 -0
- /package/{Select → dist/Select}/Select.d.ts +0 -0
- /package/{Select → dist/Select}/Select.type.d.ts +0 -0
- /package/{Tooltip → dist/Tooltip}/Tooltip.d.ts +0 -0
- /package/{Tooltip → dist/Tooltip}/Tooltip.js +0 -0
- /package/{Tooltip → dist/Tooltip}/Tooltip.type.d.ts +0 -0
- /package/{Tooltip → dist/Tooltip}/index.d.ts +0 -0
- /package/{cjs → dist/cjs}/Input/NumberInput.d.ts +0 -0
- /package/{cjs → dist/cjs}/Input/NumberInput.type.d.ts +0 -0
- /package/{cjs → dist/cjs}/Input/TextInput.d.ts +0 -0
- /package/{cjs → dist/cjs}/Select/Select.d.ts +0 -0
- /package/{cjs → dist/cjs}/Select/Select.type.d.ts +0 -0
- /package/{cjs → dist/cjs}/Tooltip/Tooltip.d.ts +0 -0
- /package/{cjs → dist/cjs}/Tooltip/Tooltip.js +0 -0
- /package/{cjs → dist/cjs}/Tooltip/Tooltip.type.d.ts +0 -0
- /package/{cjs → dist/cjs}/Tooltip/index.d.ts +0 -0
- /package/{cjs → dist/cjs}/hooks/index.d.ts +0 -0
- /package/{cjs → dist/cjs}/hooks/useDebounce.d.ts +0 -0
- /package/{cjs → dist/cjs}/hooks/useDebounce.js +0 -0
- /package/{cjs → dist/cjs}/hooks/usePortal.d.ts +0 -0
- /package/{cjs → dist/cjs}/hooks/usePortal.js +0 -0
- /package/{cjs → dist/cjs}/hooks/useThrottle.d.ts +0 -0
- /package/{cjs → dist/cjs}/hooks/useThrottle.js +0 -0
- /package/{hooks → dist/hooks}/index.d.ts +0 -0
- /package/{hooks → dist/hooks}/useDebounce.d.ts +0 -0
- /package/{hooks → dist/hooks}/useDebounce.js +0 -0
- /package/{hooks → dist/hooks}/usePortal.d.ts +0 -0
- /package/{hooks → dist/hooks}/usePortal.js +0 -0
- /package/{hooks → dist/hooks}/useThrottle.d.ts +0 -0
- /package/{hooks → dist/hooks}/useThrottle.js +0 -0
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
+
var react = require('react');
|
|
5
|
+
var usePortal = require('../hooks/usePortal.js');
|
|
6
|
+
|
|
7
|
+
const AutocompleteContext = react.createContext(null);
|
|
8
|
+
const useAutocomplete = () => {
|
|
9
|
+
const ctx = react.useContext(AutocompleteContext);
|
|
10
|
+
if (!ctx)
|
|
11
|
+
throw new Error('Autocomplete components must be used within <Autocomplete>');
|
|
12
|
+
return ctx;
|
|
13
|
+
};
|
|
14
|
+
const defaultFilter = (item, query) => item.label.toLowerCase().includes(query.trim().toLowerCase());
|
|
15
|
+
/**
|
|
16
|
+
* Root
|
|
17
|
+
*/
|
|
18
|
+
const AutocompleteContainer = ({ value, onChange, inputValue, onInputChange, disabled, filterFn = defaultFilter, children, }) => {
|
|
19
|
+
const [open, setOpen] = react.useState(false);
|
|
20
|
+
const [activeIndex, setActiveIndex] = react.useState(-1);
|
|
21
|
+
const inputRef = react.useRef(null);
|
|
22
|
+
// controlled/uncontrolled query
|
|
23
|
+
const [internalQuery, setInternalQuery] = react.useState('');
|
|
24
|
+
const query = inputValue ?? internalQuery;
|
|
25
|
+
const setQuery = (v) => {
|
|
26
|
+
onInputChange?.(v);
|
|
27
|
+
if (inputValue === undefined)
|
|
28
|
+
setInternalQuery(v);
|
|
29
|
+
};
|
|
30
|
+
const listboxId = react.useId();
|
|
31
|
+
// NOTE:
|
|
32
|
+
// - virtualization을 제대로 하려면 options data(items)가 Root에 필요하지만,
|
|
33
|
+
// compound API 유지를 위해 Options에서 items를 주입받아 Root에서 filtered를 만들기 어렵다.
|
|
34
|
+
// 그래서 Root에서는 "filtered"를 Options에서 제공할 수 있게 설계하면 복잡해짐.
|
|
35
|
+
// ✅ 해결: Options에 items를 주면 Root가 접근할 수 있도록 "itemsRef"를 둔다.
|
|
36
|
+
const itemsRef = react.useRef([]);
|
|
37
|
+
const setItems = (items) => {
|
|
38
|
+
itemsRef.current = items;
|
|
39
|
+
};
|
|
40
|
+
const filtered = react.useMemo(() => {
|
|
41
|
+
const src = itemsRef.current ?? [];
|
|
42
|
+
if (!query.trim())
|
|
43
|
+
return src;
|
|
44
|
+
return src.filter((it) => filterFn(it, query));
|
|
45
|
+
}, [query, filterFn]);
|
|
46
|
+
const close = () => {
|
|
47
|
+
setOpen(false);
|
|
48
|
+
setActiveIndex(-1);
|
|
49
|
+
};
|
|
50
|
+
const commitByIndex = (index) => {
|
|
51
|
+
const item = filtered[index];
|
|
52
|
+
if (!item || item.disabled)
|
|
53
|
+
return;
|
|
54
|
+
onChange(item.value);
|
|
55
|
+
setQuery(item.label);
|
|
56
|
+
close();
|
|
57
|
+
};
|
|
58
|
+
// 외부 value 변경 시 input label 동기화
|
|
59
|
+
react.useEffect(() => {
|
|
60
|
+
if (!value)
|
|
61
|
+
return;
|
|
62
|
+
const found = itemsRef.current.find((i) => i.value === value);
|
|
63
|
+
if (found)
|
|
64
|
+
setQuery(found.label);
|
|
65
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
66
|
+
}, [value]);
|
|
67
|
+
const statusText = react.useMemo(() => {
|
|
68
|
+
if (!open)
|
|
69
|
+
return '';
|
|
70
|
+
const n = filtered.length;
|
|
71
|
+
if (n === 0)
|
|
72
|
+
return 'No results.';
|
|
73
|
+
if (n === 1)
|
|
74
|
+
return '1 result available.';
|
|
75
|
+
return `${n} results available.`;
|
|
76
|
+
}, [open, filtered.length]);
|
|
77
|
+
return (jsxRuntime.jsxs(AutocompleteContext.Provider, { value: {
|
|
78
|
+
open,
|
|
79
|
+
setOpen,
|
|
80
|
+
selectedValue: value,
|
|
81
|
+
setSelectedValue: onChange,
|
|
82
|
+
query,
|
|
83
|
+
setQuery,
|
|
84
|
+
disabled,
|
|
85
|
+
inputRef,
|
|
86
|
+
listboxId,
|
|
87
|
+
activeIndex,
|
|
88
|
+
setActiveIndex,
|
|
89
|
+
filtered,
|
|
90
|
+
statusText,
|
|
91
|
+
commitByIndex,
|
|
92
|
+
close,
|
|
93
|
+
}, children: [jsxRuntime.jsx(ItemsBridge, { onItems: setItems }), children] }));
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* Options에서 items를 주입해주기 위한 브릿지(보이지 않는 컴포넌트)
|
|
97
|
+
* - Options가 items prop을 받으면, 내부에서 window.__ 같은 걸 쓰지 않고 Root ref에 주입
|
|
98
|
+
*/
|
|
99
|
+
const ItemsBridgeContext = react.createContext(null);
|
|
100
|
+
const ItemsBridge = ({ onItems }) => {
|
|
101
|
+
return jsxRuntime.jsx(ItemsBridgeContext.Provider, { value: onItems });
|
|
102
|
+
};
|
|
103
|
+
const useItemsBridge = () => react.useContext(ItemsBridgeContext);
|
|
104
|
+
/**
|
|
105
|
+
* Input (Combobox Trigger)
|
|
106
|
+
* - 포커스는 input 유지
|
|
107
|
+
* - aria-activedescendant로 active option을 알려줌
|
|
108
|
+
*/
|
|
109
|
+
const Input = ({ onKeyDown, onFocus, onChange, ...props }) => {
|
|
110
|
+
const { open, setOpen, query, setQuery, disabled, listboxId, activeIndex, setActiveIndex, filtered, commitByIndex, close, inputRef, } = useAutocomplete();
|
|
111
|
+
const activeId = activeIndex >= 0 ? `${listboxId}-opt-${activeIndex}` : undefined;
|
|
112
|
+
const move = (delta) => {
|
|
113
|
+
if (!filtered.length)
|
|
114
|
+
return;
|
|
115
|
+
setOpen(true);
|
|
116
|
+
setActiveIndex(activeIndex < 0 ? 0 : (activeIndex + delta + filtered.length) % filtered.length);
|
|
117
|
+
};
|
|
118
|
+
const pageMove = (deltaPages) => {
|
|
119
|
+
if (!filtered.length)
|
|
120
|
+
return;
|
|
121
|
+
setOpen(true);
|
|
122
|
+
// 10개 단위 이동(관례). 필요하면 props로 빼도 됨
|
|
123
|
+
const jump = 10 * deltaPages;
|
|
124
|
+
setActiveIndex(Math.max(0, Math.min(filtered.length - 1, (activeIndex < 0 ? 0 : activeIndex) + jump)));
|
|
125
|
+
};
|
|
126
|
+
return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("input", { ref: inputRef, role: 'combobox', "aria-autocomplete": 'list', "aria-expanded": open, "aria-controls": listboxId, "aria-activedescendant": activeId, disabled: disabled, value: query, onFocus: (e) => {
|
|
127
|
+
if (!disabled) {
|
|
128
|
+
setOpen(true);
|
|
129
|
+
setActiveIndex(filtered.length ? 0 : -1);
|
|
130
|
+
}
|
|
131
|
+
onFocus?.(e);
|
|
132
|
+
}, onChange: (e) => {
|
|
133
|
+
if (disabled)
|
|
134
|
+
return;
|
|
135
|
+
setQuery(e.target.value);
|
|
136
|
+
setOpen(true);
|
|
137
|
+
setActiveIndex(0);
|
|
138
|
+
onChange?.(e);
|
|
139
|
+
}, onKeyDown: (e) => {
|
|
140
|
+
if (disabled)
|
|
141
|
+
return;
|
|
142
|
+
switch (e.key) {
|
|
143
|
+
case 'ArrowDown':
|
|
144
|
+
e.preventDefault();
|
|
145
|
+
move(1);
|
|
146
|
+
break;
|
|
147
|
+
case 'ArrowUp':
|
|
148
|
+
e.preventDefault();
|
|
149
|
+
move(-1);
|
|
150
|
+
break;
|
|
151
|
+
case 'Home':
|
|
152
|
+
e.preventDefault();
|
|
153
|
+
setOpen(true);
|
|
154
|
+
setActiveIndex(filtered.length ? 0 : -1);
|
|
155
|
+
break;
|
|
156
|
+
case 'End':
|
|
157
|
+
e.preventDefault();
|
|
158
|
+
setOpen(true);
|
|
159
|
+
setActiveIndex(filtered.length ? filtered.length - 1 : -1);
|
|
160
|
+
break;
|
|
161
|
+
case 'PageDown':
|
|
162
|
+
e.preventDefault();
|
|
163
|
+
pageMove(1);
|
|
164
|
+
break;
|
|
165
|
+
case 'PageUp':
|
|
166
|
+
e.preventDefault();
|
|
167
|
+
pageMove(-1);
|
|
168
|
+
break;
|
|
169
|
+
case 'Enter':
|
|
170
|
+
if (open && activeIndex >= 0) {
|
|
171
|
+
e.preventDefault();
|
|
172
|
+
commitByIndex(activeIndex);
|
|
173
|
+
}
|
|
174
|
+
break;
|
|
175
|
+
case 'Escape':
|
|
176
|
+
e.preventDefault();
|
|
177
|
+
close();
|
|
178
|
+
break;
|
|
179
|
+
case 'Tab':
|
|
180
|
+
// 관례: 탭 이동 시 팝오버 닫기
|
|
181
|
+
close();
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
onKeyDown?.(e);
|
|
185
|
+
}, ...props }), jsxRuntime.jsx("span", { "aria-live": 'polite', style: {
|
|
186
|
+
position: 'absolute',
|
|
187
|
+
width: 1,
|
|
188
|
+
height: 1,
|
|
189
|
+
overflow: 'hidden',
|
|
190
|
+
clip: 'rect(0 0 0 0)',
|
|
191
|
+
whiteSpace: 'nowrap',
|
|
192
|
+
}, children: open ? `${filtered.length} results.` : '' })] }));
|
|
193
|
+
};
|
|
194
|
+
/**
|
|
195
|
+
* Options
|
|
196
|
+
* - portal
|
|
197
|
+
* - outside click close
|
|
198
|
+
* - virtualization (items + renderItem provided)
|
|
199
|
+
*/
|
|
200
|
+
const Options = ({ items, renderItem, itemHeight = 36, maxVisibleItems = 8, overscan = 3, children, ...props }) => {
|
|
201
|
+
const bridge = useItemsBridge();
|
|
202
|
+
const { open, setOpen, close, inputRef, listboxId, filtered, activeIndex, setActiveIndex, commitByIndex, } = useAutocomplete();
|
|
203
|
+
// items 주입(가상화 모드)
|
|
204
|
+
react.useEffect(() => {
|
|
205
|
+
if (items && bridge)
|
|
206
|
+
bridge(items);
|
|
207
|
+
}, [items, bridge]);
|
|
208
|
+
const popoverRef = react.useRef(null);
|
|
209
|
+
const triggerWidth = inputRef.current?.getBoundingClientRect().width;
|
|
210
|
+
// outside click
|
|
211
|
+
react.useEffect(() => {
|
|
212
|
+
if (!open)
|
|
213
|
+
return;
|
|
214
|
+
const onDown = (e) => {
|
|
215
|
+
const t = e.target;
|
|
216
|
+
if (inputRef.current?.contains(t) || popoverRef.current?.contains(t))
|
|
217
|
+
return;
|
|
218
|
+
close();
|
|
219
|
+
};
|
|
220
|
+
document.addEventListener('mousedown', onDown);
|
|
221
|
+
return () => document.removeEventListener('mousedown', onDown);
|
|
222
|
+
}, [open, close, inputRef]);
|
|
223
|
+
// scroll container ref for virtualization
|
|
224
|
+
const scrollRef = react.useRef(null);
|
|
225
|
+
// active option이 항상 보이게 스크롤 보정
|
|
226
|
+
react.useEffect(() => {
|
|
227
|
+
if (!open)
|
|
228
|
+
return;
|
|
229
|
+
if (!scrollRef.current)
|
|
230
|
+
return;
|
|
231
|
+
if (activeIndex < 0)
|
|
232
|
+
return;
|
|
233
|
+
const top = activeIndex * itemHeight;
|
|
234
|
+
const bottom = top + itemHeight;
|
|
235
|
+
const viewTop = scrollRef.current.scrollTop;
|
|
236
|
+
const viewBottom = viewTop + scrollRef.current.clientHeight;
|
|
237
|
+
if (top < viewTop)
|
|
238
|
+
scrollRef.current.scrollTop = top;
|
|
239
|
+
else if (bottom > viewBottom)
|
|
240
|
+
scrollRef.current.scrollTop = bottom - scrollRef.current.clientHeight;
|
|
241
|
+
}, [open, activeIndex, itemHeight]);
|
|
242
|
+
// virtualization range
|
|
243
|
+
const total = filtered.length;
|
|
244
|
+
const viewportCount = Math.min(maxVisibleItems, Math.max(1, total));
|
|
245
|
+
const viewportHeight = viewportCount * itemHeight;
|
|
246
|
+
const [scrollTop, setScrollTop] = react.useState(0);
|
|
247
|
+
const startIndex = Math.max(0, Math.floor(scrollTop / itemHeight) - overscan);
|
|
248
|
+
const endIndex = Math.min(total, Math.ceil((scrollTop + viewportHeight) / itemHeight) + overscan);
|
|
249
|
+
const visible = filtered.slice(startIndex, endIndex);
|
|
250
|
+
const { portal } = usePortal({
|
|
251
|
+
visible: open,
|
|
252
|
+
targetRef: inputRef,
|
|
253
|
+
popoverRef,
|
|
254
|
+
direction: 'bottom',
|
|
255
|
+
gap: 4,
|
|
256
|
+
content: (jsxRuntime.jsx("div", { ref: popoverRef, style: { width: triggerWidth }, ...props, children: items && renderItem ? (jsxRuntime.jsx("div", { id: listboxId, role: 'listbox', ref: scrollRef, style: { maxHeight: viewportHeight, overflow: 'auto', position: 'relative' }, onScroll: (e) => setScrollTop(e.target.scrollTop), children: jsxRuntime.jsx("div", { style: { height: total * itemHeight, position: 'relative' }, children: visible.map((item, i) => {
|
|
257
|
+
const index = startIndex + i; // filtered index
|
|
258
|
+
const isActive = index === activeIndex;
|
|
259
|
+
const isSelected = item.value === undefined; // selection 표시를 더 강하게 원하면 ctx.selectedValue 비교해서 쓰면 됨
|
|
260
|
+
return (jsxRuntime.jsx("div", { id: `${listboxId}-opt-${index}`, role: 'option', "aria-selected": isSelected, "aria-disabled": item.disabled, "data-active": isActive, tabIndex: -1, onMouseEnter: () => setActiveIndex(index), onMouseDown: (e) => {
|
|
261
|
+
// 클릭 시 input blur 방지(중요)
|
|
262
|
+
e.preventDefault();
|
|
263
|
+
}, onClick: () => commitByIndex(index), style: {
|
|
264
|
+
position: 'absolute',
|
|
265
|
+
top: index * itemHeight,
|
|
266
|
+
left: 0,
|
|
267
|
+
right: 0,
|
|
268
|
+
height: itemHeight,
|
|
269
|
+
display: 'flex',
|
|
270
|
+
alignItems: 'center',
|
|
271
|
+
}, children: renderItem(item) }, item.value));
|
|
272
|
+
}) }) })) : (
|
|
273
|
+
/* ✅ children 모드(비가상화) */
|
|
274
|
+
jsxRuntime.jsx("div", { id: listboxId, role: 'listbox', children: children })) })),
|
|
275
|
+
});
|
|
276
|
+
return open ? portal : null;
|
|
277
|
+
};
|
|
278
|
+
/**
|
|
279
|
+
* Option (children 모드 전용 / small list)
|
|
280
|
+
* - a11y option role/ids만 최소 보장
|
|
281
|
+
* - 가상화는 여기엔 적용하지 않음
|
|
282
|
+
*/
|
|
283
|
+
const Option = ({ value, label, disabled, children, ...props }) => {
|
|
284
|
+
const { listboxId, filtered, query, open, setOpen, setQuery, setSelectedValue, activeIndex, setActiveIndex, } = useAutocomplete();
|
|
285
|
+
// children 모드에서 필터링은 “간단 버전”
|
|
286
|
+
const visible = react.useMemo(() => {
|
|
287
|
+
if (!query.trim())
|
|
288
|
+
return true;
|
|
289
|
+
return label.toLowerCase().includes(query.trim().toLowerCase());
|
|
290
|
+
}, [label, query]);
|
|
291
|
+
react.useMemo(() => {
|
|
292
|
+
// filtered는 items 모드에서만 의미있음.
|
|
293
|
+
// children 모드는 간단히 -1 처리(aria-activedescendant는 items 모드가 권장)
|
|
294
|
+
return -1;
|
|
295
|
+
}, []);
|
|
296
|
+
if (!visible)
|
|
297
|
+
return null;
|
|
298
|
+
return (jsxRuntime.jsx("div", { role: "option", "aria-disabled": disabled, "aria-selected": false, tabIndex: -1, onMouseDown: (e) => e.preventDefault(), onClick: () => {
|
|
299
|
+
if (disabled)
|
|
300
|
+
return;
|
|
301
|
+
setSelectedValue(value);
|
|
302
|
+
setQuery(label);
|
|
303
|
+
setOpen(false);
|
|
304
|
+
setActiveIndex(-1);
|
|
305
|
+
}, ...props, children: children ?? label }));
|
|
306
|
+
};
|
|
307
|
+
Object.assign(AutocompleteContainer, {
|
|
308
|
+
Input,
|
|
309
|
+
Options,
|
|
310
|
+
Option,
|
|
311
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { HTMLAttributes, InputHTMLAttributes, ReactNode } from 'react';
|
|
2
|
+
export type AutocompleteItem = {
|
|
3
|
+
value: string;
|
|
4
|
+
label: string;
|
|
5
|
+
disabled?: boolean;
|
|
6
|
+
};
|
|
7
|
+
export interface AutocompleteProps {
|
|
8
|
+
value: string | null;
|
|
9
|
+
onChange: (v: string | null) => void;
|
|
10
|
+
inputValue?: string;
|
|
11
|
+
onInputChange?: (v: string) => void;
|
|
12
|
+
disabled?: boolean;
|
|
13
|
+
/** 옵션 필터 커스터마이즈 */
|
|
14
|
+
filterFn?: (item: AutocompleteItem, query: string) => boolean;
|
|
15
|
+
children: ReactNode;
|
|
16
|
+
}
|
|
17
|
+
export interface AutocompleteInputProps extends InputHTMLAttributes<HTMLInputElement> {
|
|
18
|
+
/** label 연결용 (없으면 aria-label 필수) */
|
|
19
|
+
'aria-label'?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface AutocompleteOptionsProps extends HTMLAttributes<HTMLDivElement> {
|
|
22
|
+
/** ✅ 가상화 모드: 데이터 기반 */
|
|
23
|
+
items?: AutocompleteItem[];
|
|
24
|
+
/** 가상화 모드에서 항목 렌더 */
|
|
25
|
+
renderItem?: (item: AutocompleteItem) => ReactNode;
|
|
26
|
+
/** virtualization 옵션 */
|
|
27
|
+
itemHeight?: number;
|
|
28
|
+
maxVisibleItems?: number;
|
|
29
|
+
overscan?: number;
|
|
30
|
+
}
|
|
31
|
+
export interface AutocompleteOptionProps extends HTMLAttributes<HTMLDivElement> {
|
|
32
|
+
/** children 모드(비가상화) */
|
|
33
|
+
value: string;
|
|
34
|
+
label: string;
|
|
35
|
+
disabled?: boolean;
|
|
36
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Autocomplete';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ChangeEvent, CompositionEvent,
|
|
1
|
+
import { ChangeEvent, CompositionEvent, InputHTMLAttributes } from 'react';
|
|
2
2
|
/**
|
|
3
3
|
* 고급 기능을 제공하는 TextInput 컴포넌트 props
|
|
4
4
|
*
|
|
@@ -9,7 +9,7 @@ import { ChangeEvent, CompositionEvent, HTMLProps } from 'react';
|
|
|
9
9
|
* - IME(한글 입력) 대응
|
|
10
10
|
* - 공백 제어
|
|
11
11
|
*/
|
|
12
|
-
export interface TextInputProps extends
|
|
12
|
+
export interface TextInputProps extends InputHTMLAttributes<HTMLInputElement> {
|
|
13
13
|
/**
|
|
14
14
|
* 입력값 검증 함수
|
|
15
15
|
*
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
+
var react = require('react');
|
|
5
|
+
var usePortal = require('../hooks/usePortal.js');
|
|
6
|
+
|
|
7
|
+
const SelectContext = react.createContext(null);
|
|
8
|
+
/**
|
|
9
|
+
* Select Context 접근 훅
|
|
10
|
+
*
|
|
11
|
+
* @throws Select 외부에서 사용할 경우 에러
|
|
12
|
+
*/
|
|
13
|
+
const useSelectContext = () => {
|
|
14
|
+
const ctx = react.useContext(SelectContext);
|
|
15
|
+
if (!ctx)
|
|
16
|
+
throw new Error('Select components must be used within <Select>');
|
|
17
|
+
return ctx;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Select 루트 컨테이너
|
|
21
|
+
*
|
|
22
|
+
* - 상태 관리 담당
|
|
23
|
+
* - Context Provider 역할
|
|
24
|
+
*/
|
|
25
|
+
const SelectContainer = ({ value, onChange, multiple = false, children }) => {
|
|
26
|
+
const [open, setOpen] = react.useState(false);
|
|
27
|
+
const triggerRef = react.useRef(null);
|
|
28
|
+
const optionRefs = react.useRef([]);
|
|
29
|
+
const [focusedIndex, setFocusedIndex] = react.useState(-1);
|
|
30
|
+
const toggleValue = (v) => {
|
|
31
|
+
if (!multiple) {
|
|
32
|
+
onChange([v]);
|
|
33
|
+
setOpen(false);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
onChange(value.includes(v)
|
|
37
|
+
? value.filter(i => i !== v)
|
|
38
|
+
: [...value, v]);
|
|
39
|
+
};
|
|
40
|
+
return (jsxRuntime.jsx(SelectContext.Provider, { value: {
|
|
41
|
+
open,
|
|
42
|
+
setOpen,
|
|
43
|
+
value,
|
|
44
|
+
toggleValue,
|
|
45
|
+
multiple,
|
|
46
|
+
triggerRef,
|
|
47
|
+
optionRefs,
|
|
48
|
+
focusedIndex,
|
|
49
|
+
setFocusedIndex,
|
|
50
|
+
}, children: children }));
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Select 트리거 버튼
|
|
54
|
+
*
|
|
55
|
+
* - 클릭 시 Options 열림
|
|
56
|
+
* - 최초 포커스를 첫 옵션으로 이동
|
|
57
|
+
*/
|
|
58
|
+
const Trigger = (props) => {
|
|
59
|
+
const { open, setOpen, triggerRef, setFocusedIndex } = useSelectContext();
|
|
60
|
+
return (jsxRuntime.jsx("div", { ref: triggerRef, role: 'button', "aria-expanded": open, onClick: (e) => {
|
|
61
|
+
setOpen(!open);
|
|
62
|
+
setFocusedIndex(0);
|
|
63
|
+
props.onClick?.(e);
|
|
64
|
+
}, ...props }));
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Options 드롭다운 영역
|
|
68
|
+
*
|
|
69
|
+
* 기능:
|
|
70
|
+
* - 외부 클릭 시 닫힘
|
|
71
|
+
* - ESC 키 닫기
|
|
72
|
+
* - ↑ ↓ 키 포커스 이동
|
|
73
|
+
* - portal 렌더링
|
|
74
|
+
*/
|
|
75
|
+
const Options = ({ children, ...props }) => {
|
|
76
|
+
const { open, triggerRef, setOpen, setFocusedIndex, optionRefs } = useSelectContext();
|
|
77
|
+
const popoverRef = react.useRef(null);
|
|
78
|
+
const triggerWidth = triggerRef.current?.getBoundingClientRect().width;
|
|
79
|
+
react.useEffect(() => {
|
|
80
|
+
if (!open)
|
|
81
|
+
return;
|
|
82
|
+
const handleOutsideClick = (e) => {
|
|
83
|
+
const target = e.target;
|
|
84
|
+
if (triggerRef.current?.contains(target) ||
|
|
85
|
+
popoverRef.current?.contains(target)) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
setOpen(false);
|
|
89
|
+
};
|
|
90
|
+
document.addEventListener('mousedown', handleOutsideClick);
|
|
91
|
+
return () => {
|
|
92
|
+
document.removeEventListener('mousedown', handleOutsideClick);
|
|
93
|
+
};
|
|
94
|
+
}, [open, setOpen, triggerRef]);
|
|
95
|
+
react.useEffect(() => {
|
|
96
|
+
if (!open)
|
|
97
|
+
return;
|
|
98
|
+
const handleKeyDown = (e) => {
|
|
99
|
+
e.preventDefault();
|
|
100
|
+
if (e.key === 'Escape') {
|
|
101
|
+
setOpen(false);
|
|
102
|
+
setFocusedIndex(-1);
|
|
103
|
+
}
|
|
104
|
+
if (e.key === 'ArrowUp') {
|
|
105
|
+
setFocusedIndex(prev => prev - 1 < 0 ? optionRefs.current.length - 1 : prev - 1);
|
|
106
|
+
}
|
|
107
|
+
if (e.key === 'ArrowDown') {
|
|
108
|
+
setFocusedIndex(prev => prev + 1 >= optionRefs.current.length ? 0 : prev + 1);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
document.addEventListener('keydown', handleKeyDown);
|
|
112
|
+
return () => {
|
|
113
|
+
optionRefs.current = [];
|
|
114
|
+
document.removeEventListener('keydown', handleKeyDown);
|
|
115
|
+
};
|
|
116
|
+
}, [open]);
|
|
117
|
+
const { portal } = usePortal({
|
|
118
|
+
visible: open,
|
|
119
|
+
targetRef: triggerRef,
|
|
120
|
+
popoverRef,
|
|
121
|
+
direction: 'bottom',
|
|
122
|
+
gap: 4,
|
|
123
|
+
content: (jsxRuntime.jsx("div", { ref: popoverRef, style: { width: triggerWidth }, ...props, children: children })),
|
|
124
|
+
});
|
|
125
|
+
return open ? portal : null;
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* 개별 선택 옵션
|
|
129
|
+
*
|
|
130
|
+
* 기능:
|
|
131
|
+
* - 선택 상태 표시
|
|
132
|
+
* - 포커스 관리
|
|
133
|
+
* - disabled 지원
|
|
134
|
+
*/
|
|
135
|
+
const Option = ({ value, disabled, children, ...props }) => {
|
|
136
|
+
const { value: selected, toggleValue, optionRefs, focusedIndex } = useSelectContext();
|
|
137
|
+
const isSelected = selected.includes(value);
|
|
138
|
+
const [index, setIndex] = react.useState(null);
|
|
139
|
+
const isFocused = react.useMemo(() => focusedIndex === index, [focusedIndex, index]);
|
|
140
|
+
const ref = react.useRef(null);
|
|
141
|
+
react.useEffect(() => {
|
|
142
|
+
if (ref.current && index === null) {
|
|
143
|
+
setIndex(optionRefs.current.length);
|
|
144
|
+
optionRefs.current[optionRefs.current.length] = ref.current;
|
|
145
|
+
}
|
|
146
|
+
}, []);
|
|
147
|
+
react.useEffect(() => {
|
|
148
|
+
ref.current?.setAttribute('data-focused', String(focusedIndex === index));
|
|
149
|
+
if (focusedIndex === index) {
|
|
150
|
+
ref.current?.focus();
|
|
151
|
+
}
|
|
152
|
+
}, [focusedIndex, index]);
|
|
153
|
+
return (jsxRuntime.jsx("div", { ref: ref, role: 'option', "aria-selected": isSelected, "aria-disabled": disabled, "data-focused": isFocused, onClick: () => {
|
|
154
|
+
if (!disabled)
|
|
155
|
+
toggleValue(value);
|
|
156
|
+
}, ...props, children: children }));
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Compound Select 컴포넌트
|
|
160
|
+
*
|
|
161
|
+
* 사용 예시:
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* <Select value={value} onChange={setValue}>
|
|
165
|
+
* <Select.Trigger>열기</Select.Trigger>
|
|
166
|
+
* <Select.Options>
|
|
167
|
+
* <Select.Option value="a">A</Select.Option>
|
|
168
|
+
* <Select.Option value="b">B</Select.Option>
|
|
169
|
+
* </Select.Options>
|
|
170
|
+
* </Select>
|
|
171
|
+
*/
|
|
172
|
+
Object.assign(SelectContainer, {
|
|
173
|
+
Trigger,
|
|
174
|
+
Options,
|
|
175
|
+
Option,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
exports.useSelectContext = useSelectContext;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Select';
|
|
@@ -6,9 +6,12 @@ require('react-dom');
|
|
|
6
6
|
var useDebounce = require('./hooks/useDebounce.js');
|
|
7
7
|
var useThrottle = require('./hooks/useThrottle.js');
|
|
8
8
|
var Tooltip = require('./Tooltip/Tooltip.js');
|
|
9
|
+
var Select = require('./Select/Select.js');
|
|
10
|
+
require('./Autocomplete/Autocomplete.js');
|
|
9
11
|
|
|
10
12
|
|
|
11
13
|
|
|
12
14
|
exports.useDebounce = useDebounce.useDebounce;
|
|
13
15
|
exports.useThrottle = useThrottle.useThrottle;
|
|
14
16
|
exports.Tooltip = Tooltip.Tooltip;
|
|
17
|
+
exports.useSelectContext = Select.useSelectContext;
|
package/dist/index.d.ts
ADDED
|
@@ -4,3 +4,5 @@ import 'react-dom';
|
|
|
4
4
|
export { useDebounce } from './hooks/useDebounce.js';
|
|
5
5
|
export { useThrottle } from './hooks/useThrottle.js';
|
|
6
6
|
export { Tooltip } from './Tooltip/Tooltip.js';
|
|
7
|
+
export { useSelectContext } from './Select/Select.js';
|
|
8
|
+
import './Autocomplete/Autocomplete.js';
|