@ncds/ui-admin 1.8.25-alpha.3 → 1.8.25
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/src/components/feedback-and-status/badge/Badge.js +1 -1
- package/dist/cjs/src/components/forms-and-input/combo-box/ComboBox.js +126 -16
- package/dist/cjs/src/components/forms-and-input/combo-box/__tests__/ComboBox.portal.test.js +3 -3
- package/dist/cjs/src/components/forms-and-input/combo-box/__tests__/ComboBox.test.js +455 -0
- package/dist/cjs/src/generated/scoped-css.js +1 -1
- package/dist/esm/src/components/feedback-and-status/badge/Badge.js +1 -1
- package/dist/esm/src/components/forms-and-input/combo-box/ComboBox.js +127 -17
- package/dist/esm/src/components/forms-and-input/combo-box/__tests__/ComboBox.portal.test.js +3 -3
- package/dist/esm/src/components/forms-and-input/combo-box/__tests__/ComboBox.test.js +452 -0
- package/dist/esm/src/generated/scoped-css.js +1 -1
- package/dist/temp/src/components/feedback-and-status/badge/Badge.d.ts +1 -1
- package/dist/temp/src/components/feedback-and-status/badge/Badge.js +1 -1
- package/dist/temp/src/components/forms-and-input/combo-box/ComboBox.d.ts +1 -1
- package/dist/temp/src/components/forms-and-input/combo-box/ComboBox.js +119 -13
- package/dist/temp/src/components/forms-and-input/combo-box/__tests__/ComboBox.portal.test.js +3 -3
- package/dist/temp/src/components/forms-and-input/combo-box/__tests__/ComboBox.test.d.ts +1 -0
- package/dist/temp/src/components/forms-and-input/combo-box/__tests__/ComboBox.test.js +293 -0
- package/dist/temp/src/generated/scoped-css.js +1 -1
- package/dist/types/src/components/feedback-and-status/badge/Badge.d.ts +1 -1
- package/dist/types/src/components/forms-and-input/combo-box/ComboBox.d.ts +1 -1
- package/dist/types/src/components/forms-and-input/combo-box/__tests__/ComboBox.test.d.ts +1 -0
- package/dist/ui-admin/assets/styles/style.css +14 -12
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
2
|
import { ChevronSelectorVertical, SearchLg } from '@ncds/ui-admin-icon';
|
|
3
3
|
import classNames from 'classnames';
|
|
4
|
-
import { forwardRef, useImperativeHandle, useRef, useState } from 'react';
|
|
4
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react';
|
|
5
5
|
import { useDropdown } from '../../../hooks/dropdown';
|
|
6
6
|
import { useFloatingPosition } from '../../../hooks/useFloatingPosition';
|
|
7
7
|
import { usePortalState } from '../../../hooks/usePortalState';
|
|
@@ -23,6 +23,112 @@ const notifyFormChange = (register, multiple, newValue) => {
|
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
|
+
/** '' · undefined · null 이면 빈 값. 숫자 0은 빈 값이 아니다 */
|
|
27
|
+
const isEmptyValue = value => value === '' || value == null;
|
|
28
|
+
/** react-hook-form hidden input 값 — Tag 와 같은 빈 값 규칙(0은 값)을 따른다 */
|
|
29
|
+
const toHiddenInputValue = (value, multiple) => {
|
|
30
|
+
if (multiple) return JSON.stringify(value || []);
|
|
31
|
+
return isEmptyValue(value) ? '' : String(value);
|
|
32
|
+
};
|
|
33
|
+
/** SelectDropdown 은 null 을 받지 않으므로 undefined 로 바꾼다 */
|
|
34
|
+
const toDropdownValue = value => value ?? undefined;
|
|
35
|
+
/** multiple 용 — 배열이 아니면(null · undefined 포함) 빈 배열 */
|
|
36
|
+
const toIdArray = value => Array.isArray(value) ? value : [];
|
|
37
|
+
/** 두 id 목록이 같은 id 들로 이루어졌는지 — 순서는 보지 않는다 (사용처가 onChange 값을 정렬해 돌려줘도 같은 값) */
|
|
38
|
+
const isSameIds = (currentIds, nextIds) => currentIds.length === nextIds.length && currentIds.every(id => nextIds.includes(id));
|
|
39
|
+
const isSameTags = (nextTags, prevTags) => nextTags.length === prevTags.length && nextTags.every((tag, index) => tag.id === prevTags[index].id && tag.label === prevTags[index].label);
|
|
40
|
+
const findOption = (optionItems, id) => optionItems.find(option => option.id === id);
|
|
41
|
+
/** 단일 모드 value 에 해당하는 옵션 — 빈 값·배열이면 없음 */
|
|
42
|
+
const findSingleOption = (value, optionItems) => {
|
|
43
|
+
if (isEmptyValue(value) || Array.isArray(value)) return undefined;
|
|
44
|
+
return findOption(optionItems, value);
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* 단일 모드 Tag — value 와 optionItems 에서 계산한다.
|
|
48
|
+
* 검색 필터로 목록에서 빠진 경우에만, 같은 id 로 기억해 둔 Tag 를 대신 쓴다.
|
|
49
|
+
*/
|
|
50
|
+
const resolveSingleTag = (value, optionItems, lastResolvedTag) => {
|
|
51
|
+
if (isEmptyValue(value) || Array.isArray(value)) return null;
|
|
52
|
+
const found = findOption(optionItems, value);
|
|
53
|
+
if (found) return {
|
|
54
|
+
id: found.id,
|
|
55
|
+
label: found.label
|
|
56
|
+
};
|
|
57
|
+
return lastResolvedTag?.id === value ? lastResolvedTag : null;
|
|
58
|
+
};
|
|
59
|
+
/** 단일 모드 Tag 목록 (0~1개). 목록에서 찾은 Tag 를 기억해 검색 필터 중에도 라벨을 유지한다 */
|
|
60
|
+
const useSingleSelectedTags = (value, optionItems, enabled) => {
|
|
61
|
+
const [lastResolvedTag, setLastResolvedTag] = useState(null);
|
|
62
|
+
const found = enabled ? findSingleOption(value, optionItems) : undefined;
|
|
63
|
+
const foundId = found?.id;
|
|
64
|
+
const foundLabel = found?.label;
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (foundId === undefined || foundLabel === undefined) return;
|
|
67
|
+
setLastResolvedTag({
|
|
68
|
+
id: foundId,
|
|
69
|
+
label: foundLabel
|
|
70
|
+
});
|
|
71
|
+
}, [foundId, foundLabel]);
|
|
72
|
+
const selectedTag = enabled ? resolveSingleTag(value, optionItems, lastResolvedTag) : null;
|
|
73
|
+
return selectedTag ? [selectedTag] : [];
|
|
74
|
+
};
|
|
75
|
+
/** multiple 모드 Tag — 목록에서 못 찾은 id 는 이미 표시 중인 Tag 의 라벨을 쓴다 */
|
|
76
|
+
const resolveMultipleTags = (ids, optionItems, prevTags) => {
|
|
77
|
+
const nextTags = ids.map(id => {
|
|
78
|
+
const found = findOption(optionItems, id);
|
|
79
|
+
return found ? {
|
|
80
|
+
id: found.id,
|
|
81
|
+
label: found.label
|
|
82
|
+
} : prevTags.find(tag => tag.id === id);
|
|
83
|
+
}).filter(tag => tag !== undefined);
|
|
84
|
+
// 결과가 같으면 이전 배열을 그대로 돌려준다 — 새 배열로 바꾸면 effect 가 다시 렌더를 일으킨다
|
|
85
|
+
return isSameTags(nextTags, prevTags) ? prevTags : nextTags;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* multiple 모드 Tag — 「선택 완료」·X 클릭·외부 value 변경 때만 확정 id 를 바꾼다.
|
|
89
|
+
* ComboBox 가 직접 onChange 로 내보낸 값은 markEmitted 로 기록해 두고, 그 값이 value 로 돌아오면 한 번만 무시한다(확정 아님).
|
|
90
|
+
* 바깥 값이 들어오면 남아 있던 기록도 지운다 — 오래된 기록 때문에 이후 같은 바깥 값이 무시되지 않게 하기 위함.
|
|
91
|
+
* optionItems 만 바뀌면 확정 id 는 그대로 두고 라벨만 다시 찾는다 — 「선택 완료」 전 검색으로 Tag 가 확정되지 않게 하기 위함.
|
|
92
|
+
*/
|
|
93
|
+
const useMultipleSelectedTags = (value, optionItems, enabled) => {
|
|
94
|
+
const [selectedTags, setSelectedTags] = useState([]);
|
|
95
|
+
const lastEmittedRef = useRef(null);
|
|
96
|
+
const committedIdsRef = useRef([]);
|
|
97
|
+
const prevValueIdsRef = useRef(null);
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
if (!enabled) return;
|
|
100
|
+
const ids = toIdArray(value);
|
|
101
|
+
const isEcho = lastEmittedRef.current !== null && isSameIds(ids, lastEmittedRef.current);
|
|
102
|
+
const isValueChanged = prevValueIdsRef.current === null || !isSameIds(ids, prevValueIdsRef.current);
|
|
103
|
+
prevValueIdsRef.current = ids;
|
|
104
|
+
if (isEcho) {
|
|
105
|
+
lastEmittedRef.current = null;
|
|
106
|
+
} else if (isValueChanged) {
|
|
107
|
+
committedIdsRef.current = ids;
|
|
108
|
+
lastEmittedRef.current = null;
|
|
109
|
+
}
|
|
110
|
+
setSelectedTags(prev => resolveMultipleTags(committedIdsRef.current, optionItems, prev));
|
|
111
|
+
}, [enabled, value, optionItems]);
|
|
112
|
+
/** 「선택 완료」로 Tag 를 확정한다 */
|
|
113
|
+
const commitTags = ids => {
|
|
114
|
+
committedIdsRef.current = ids;
|
|
115
|
+
setSelectedTags(prev => resolveMultipleTags(ids, optionItems, prev));
|
|
116
|
+
};
|
|
117
|
+
/** X 클릭 — 확정 id 에서 그 id 만 뺀다. value 기준으로 계산하면 「선택 완료」 전에 클릭한 항목까지 확정된다 */
|
|
118
|
+
const removeCommittedTag = tagId => {
|
|
119
|
+
commitTags(committedIdsRef.current.filter(id => id !== tagId));
|
|
120
|
+
};
|
|
121
|
+
/** ComboBox 가 onChange 로 내보낸 값을 기록한다 — value 로 돌아왔을 때 확정으로 보지 않기 위함 */
|
|
122
|
+
const markEmitted = ids => {
|
|
123
|
+
lastEmittedRef.current = ids;
|
|
124
|
+
};
|
|
125
|
+
return {
|
|
126
|
+
selectedTags,
|
|
127
|
+
commitTags,
|
|
128
|
+
removeCommittedTag,
|
|
129
|
+
markEmitted
|
|
130
|
+
};
|
|
131
|
+
};
|
|
26
132
|
const ComboBox = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
27
133
|
let {
|
|
28
134
|
placeholder = '검색하세요',
|
|
@@ -62,22 +168,24 @@ const ComboBox = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
62
168
|
} = usePortalState(usePortal);
|
|
63
169
|
const [inputValue, setInputValue] = useState(searchValue);
|
|
64
170
|
const [showAllItems, setShowAllItems] = useState(false);
|
|
65
|
-
const
|
|
171
|
+
const singleTags = useSingleSelectedTags(value, optionItems, !multiple);
|
|
172
|
+
const {
|
|
173
|
+
selectedTags,
|
|
174
|
+
commitTags,
|
|
175
|
+
removeCommittedTag,
|
|
176
|
+
markEmitted
|
|
177
|
+
} = useMultipleSelectedTags(value, optionItems, multiple);
|
|
66
178
|
const handleOptionSelect = option => {
|
|
67
179
|
if (disabled) return;
|
|
68
180
|
if (multiple) {
|
|
69
181
|
const newValue = tryToggle(option.id, Array.isArray(value) ? value : []);
|
|
70
182
|
if (newValue === null) return;
|
|
183
|
+
markEmitted(newValue);
|
|
71
184
|
onChange?.(newValue);
|
|
72
185
|
notifyFormChange(register, multiple, newValue);
|
|
73
186
|
return;
|
|
74
187
|
}
|
|
75
188
|
const newValue = option.id;
|
|
76
|
-
const tag = {
|
|
77
|
-
id: option.id,
|
|
78
|
-
label: option.label
|
|
79
|
-
};
|
|
80
|
-
setSelectedTags([tag]);
|
|
81
189
|
onChange?.(newValue);
|
|
82
190
|
notifyFormChange(register, multiple, newValue);
|
|
83
191
|
};
|
|
@@ -184,7 +292,6 @@ const ComboBox = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
184
292
|
const {
|
|
185
293
|
buttonText: selectAllButtonText,
|
|
186
294
|
toggleSelectAll,
|
|
187
|
-
getSelectedTagsData,
|
|
188
295
|
removeTag,
|
|
189
296
|
isMaxSelectionActive,
|
|
190
297
|
tryToggle
|
|
@@ -194,6 +301,7 @@ const ComboBox = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
194
301
|
const handleSelectAll = () => {
|
|
195
302
|
if (!multiple || !onChange) return;
|
|
196
303
|
const newSelectedValues = toggleSelectAll();
|
|
304
|
+
markEmitted(newSelectedValues);
|
|
197
305
|
onChange(newSelectedValues);
|
|
198
306
|
};
|
|
199
307
|
const handleEdit = () => {
|
|
@@ -201,30 +309,32 @@ const ComboBox = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
201
309
|
};
|
|
202
310
|
const handleComplete = completedValue => {
|
|
203
311
|
if (!multiple) return;
|
|
204
|
-
|
|
205
|
-
setSelectedTags(tags);
|
|
312
|
+
commitTags(toIdArray(completedValue));
|
|
206
313
|
setInputValue('');
|
|
207
314
|
closeDropdown();
|
|
208
315
|
onComplete?.(completedValue);
|
|
209
316
|
};
|
|
210
317
|
const handleRemoveTag = tagId => {
|
|
318
|
+
// X 버튼은 disabled 에서 숨기지만, 다른 호출 경로가 생겨도 값이 지워지지 않게 함수에서도 막는다
|
|
319
|
+
if (disabled) return;
|
|
211
320
|
if (!onChange) return;
|
|
212
321
|
if (multiple) {
|
|
213
322
|
const newSelectedValues = removeTag(tagId);
|
|
323
|
+
markEmitted(newSelectedValues);
|
|
214
324
|
onChange(newSelectedValues);
|
|
215
|
-
|
|
325
|
+
removeCommittedTag(tagId);
|
|
216
326
|
return;
|
|
217
327
|
}
|
|
218
328
|
onChange('');
|
|
219
|
-
setSelectedTags([]);
|
|
220
329
|
};
|
|
221
330
|
const comboboxId = `combobox-options-${id || 'default'}`;
|
|
222
331
|
const hasVisibleOptions = showAllItems || inputValue;
|
|
223
332
|
const displayedOptions = hasVisibleOptions ? optionItems : [];
|
|
224
333
|
const showNoResults = !hasVisibleOptions || optionItems.length === 0;
|
|
225
334
|
const showFooter = multiple || showFooterButtons;
|
|
226
|
-
const hiddenInputValue =
|
|
227
|
-
const
|
|
335
|
+
const hiddenInputValue = toHiddenInputValue(value, multiple);
|
|
336
|
+
const tags = multiple ? selectedTags : singleTags;
|
|
337
|
+
const hasTags = tags.length > 0;
|
|
228
338
|
const {
|
|
229
339
|
className: portalPanelClassName,
|
|
230
340
|
style: portalPanelStyle
|
|
@@ -235,7 +345,7 @@ const ComboBox = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
235
345
|
direction: dropdownDirection,
|
|
236
346
|
size: size,
|
|
237
347
|
options: displayedOptions,
|
|
238
|
-
value: value,
|
|
348
|
+
value: toDropdownValue(value),
|
|
239
349
|
focusedIndex: focusedIndex,
|
|
240
350
|
maxHeight: maxHeight,
|
|
241
351
|
listboxId: comboboxId,
|
|
@@ -314,10 +424,10 @@ const ComboBox = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
314
424
|
})]
|
|
315
425
|
}), hasTags && _jsx("div", {
|
|
316
426
|
className: "ncua-combobox__tags",
|
|
317
|
-
children:
|
|
427
|
+
children: tags.map(tag => _jsx(Tag, {
|
|
318
428
|
text: tag.label,
|
|
319
429
|
size: "sm",
|
|
320
|
-
close:
|
|
430
|
+
close: !disabled,
|
|
321
431
|
onButtonClick: () => handleRemoveTag(tag.id)
|
|
322
432
|
}, tag.id))
|
|
323
433
|
})]
|
|
@@ -473,7 +473,7 @@ describe('C-9 기본 경로 회귀 기준선', () => {
|
|
|
473
473
|
expect(onSearch).toHaveBeenCalledWith('사');
|
|
474
474
|
expect(view.content.querySelectorAll('.ncua-select-dropdown__option')).toHaveLength(createOptions().length);
|
|
475
475
|
});
|
|
476
|
-
it('옵션을 선택하면 onChange 가
|
|
476
|
+
it('옵션을 선택하면 onChange 가 호출된다', () => {
|
|
477
477
|
// Given: 옵션이 노출된 기본 모드 ComboBox 에서
|
|
478
478
|
const onChange = vi.fn();
|
|
479
479
|
const view = mountComboBox({
|
|
@@ -486,9 +486,9 @@ describe('C-9 기본 경로 회귀 기준선', () => {
|
|
|
486
486
|
act(() => {
|
|
487
487
|
requireElement(view.content, '.ncua-select-dropdown__option').click();
|
|
488
488
|
});
|
|
489
|
-
// Then: onChange 가 선택 값으로
|
|
489
|
+
// Then: onChange 가 선택 값으로 호출된다
|
|
490
|
+
// 선택 태그는 value 를 따르므로(#530) onChange 값을 value 로 돌려주는 경우의 태그 표시는 ComboBox.test.ts S-10 에서 검증한다
|
|
490
491
|
expect(onChange).toHaveBeenCalledWith('apple');
|
|
491
|
-
expect(view.container.querySelectorAll('.ncua-combobox__tags .ncua-tag')).toHaveLength(1);
|
|
492
492
|
});
|
|
493
493
|
it('Escape 를 누르면 인라인 패널이 닫힌다', () => {
|
|
494
494
|
// Given: 열려 있는 기본 모드 ComboBox 에서
|