@ncds/ui-admin 1.8.26-alpha.0 → 1.8.26-alpha.2
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/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/components/forms-and-input/input-base/__tests__/inputAutofill.test.js +83 -0
- package/dist/cjs/src/generated/scoped-css.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/components/forms-and-input/input-base/__tests__/inputAutofill.test.js +80 -0
- package/dist/esm/src/generated/scoped-css.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/components/forms-and-input/input-base/__tests__/inputAutofill.test.d.ts +1 -0
- package/dist/temp/src/components/forms-and-input/input-base/__tests__/inputAutofill.test.js +88 -0
- package/dist/temp/src/generated/scoped-css.js +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/types/src/components/forms-and-input/input-base/__tests__/inputAutofill.test.d.ts +1 -0
- package/dist/ui-admin/assets/styles/style.css +7 -0
- package/package.json +1 -1
|
@@ -30,6 +30,112 @@ const notifyFormChange = (register, multiple, newValue) => {
|
|
|
30
30
|
});
|
|
31
31
|
}
|
|
32
32
|
};
|
|
33
|
+
/** '' · undefined · null 이면 빈 값. 숫자 0은 빈 값이 아니다 */
|
|
34
|
+
const isEmptyValue = value => value === '' || value == null;
|
|
35
|
+
/** react-hook-form hidden input 값 — Tag 와 같은 빈 값 규칙(0은 값)을 따른다 */
|
|
36
|
+
const toHiddenInputValue = (value, multiple) => {
|
|
37
|
+
if (multiple) return JSON.stringify(value || []);
|
|
38
|
+
return isEmptyValue(value) ? '' : String(value);
|
|
39
|
+
};
|
|
40
|
+
/** SelectDropdown 은 null 을 받지 않으므로 undefined 로 바꾼다 */
|
|
41
|
+
const toDropdownValue = value => value ?? undefined;
|
|
42
|
+
/** multiple 용 — 배열이 아니면(null · undefined 포함) 빈 배열 */
|
|
43
|
+
const toIdArray = value => Array.isArray(value) ? value : [];
|
|
44
|
+
/** 두 id 목록이 같은 id 들로 이루어졌는지 — 순서는 보지 않는다 (사용처가 onChange 값을 정렬해 돌려줘도 같은 값) */
|
|
45
|
+
const isSameIds = (currentIds, nextIds) => currentIds.length === nextIds.length && currentIds.every(id => nextIds.includes(id));
|
|
46
|
+
const isSameTags = (nextTags, prevTags) => nextTags.length === prevTags.length && nextTags.every((tag, index) => tag.id === prevTags[index].id && tag.label === prevTags[index].label);
|
|
47
|
+
const findOption = (optionItems, id) => optionItems.find(option => option.id === id);
|
|
48
|
+
/** 단일 모드 value 에 해당하는 옵션 — 빈 값·배열이면 없음 */
|
|
49
|
+
const findSingleOption = (value, optionItems) => {
|
|
50
|
+
if (isEmptyValue(value) || Array.isArray(value)) return undefined;
|
|
51
|
+
return findOption(optionItems, value);
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* 단일 모드 Tag — value 와 optionItems 에서 계산한다.
|
|
55
|
+
* 검색 필터로 목록에서 빠진 경우에만, 같은 id 로 기억해 둔 Tag 를 대신 쓴다.
|
|
56
|
+
*/
|
|
57
|
+
const resolveSingleTag = (value, optionItems, lastResolvedTag) => {
|
|
58
|
+
if (isEmptyValue(value) || Array.isArray(value)) return null;
|
|
59
|
+
const found = findOption(optionItems, value);
|
|
60
|
+
if (found) return {
|
|
61
|
+
id: found.id,
|
|
62
|
+
label: found.label
|
|
63
|
+
};
|
|
64
|
+
return lastResolvedTag?.id === value ? lastResolvedTag : null;
|
|
65
|
+
};
|
|
66
|
+
/** 단일 모드 Tag 목록 (0~1개). 목록에서 찾은 Tag 를 기억해 검색 필터 중에도 라벨을 유지한다 */
|
|
67
|
+
const useSingleSelectedTags = (value, optionItems, enabled) => {
|
|
68
|
+
const [lastResolvedTag, setLastResolvedTag] = (0, _react.useState)(null);
|
|
69
|
+
const found = enabled ? findSingleOption(value, optionItems) : undefined;
|
|
70
|
+
const foundId = found?.id;
|
|
71
|
+
const foundLabel = found?.label;
|
|
72
|
+
(0, _react.useEffect)(() => {
|
|
73
|
+
if (foundId === undefined || foundLabel === undefined) return;
|
|
74
|
+
setLastResolvedTag({
|
|
75
|
+
id: foundId,
|
|
76
|
+
label: foundLabel
|
|
77
|
+
});
|
|
78
|
+
}, [foundId, foundLabel]);
|
|
79
|
+
const selectedTag = enabled ? resolveSingleTag(value, optionItems, lastResolvedTag) : null;
|
|
80
|
+
return selectedTag ? [selectedTag] : [];
|
|
81
|
+
};
|
|
82
|
+
/** multiple 모드 Tag — 목록에서 못 찾은 id 는 이미 표시 중인 Tag 의 라벨을 쓴다 */
|
|
83
|
+
const resolveMultipleTags = (ids, optionItems, prevTags) => {
|
|
84
|
+
const nextTags = ids.map(id => {
|
|
85
|
+
const found = findOption(optionItems, id);
|
|
86
|
+
return found ? {
|
|
87
|
+
id: found.id,
|
|
88
|
+
label: found.label
|
|
89
|
+
} : prevTags.find(tag => tag.id === id);
|
|
90
|
+
}).filter(tag => tag !== undefined);
|
|
91
|
+
// 결과가 같으면 이전 배열을 그대로 돌려준다 — 새 배열로 바꾸면 effect 가 다시 렌더를 일으킨다
|
|
92
|
+
return isSameTags(nextTags, prevTags) ? prevTags : nextTags;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* multiple 모드 Tag — 「선택 완료」·X 클릭·외부 value 변경 때만 확정 id 를 바꾼다.
|
|
96
|
+
* ComboBox 가 직접 onChange 로 내보낸 값은 markEmitted 로 기록해 두고, 그 값이 value 로 돌아오면 한 번만 무시한다(확정 아님).
|
|
97
|
+
* 바깥 값이 들어오면 남아 있던 기록도 지운다 — 오래된 기록 때문에 이후 같은 바깥 값이 무시되지 않게 하기 위함.
|
|
98
|
+
* optionItems 만 바뀌면 확정 id 는 그대로 두고 라벨만 다시 찾는다 — 「선택 완료」 전 검색으로 Tag 가 확정되지 않게 하기 위함.
|
|
99
|
+
*/
|
|
100
|
+
const useMultipleSelectedTags = (value, optionItems, enabled) => {
|
|
101
|
+
const [selectedTags, setSelectedTags] = (0, _react.useState)([]);
|
|
102
|
+
const lastEmittedRef = (0, _react.useRef)(null);
|
|
103
|
+
const committedIdsRef = (0, _react.useRef)([]);
|
|
104
|
+
const prevValueIdsRef = (0, _react.useRef)(null);
|
|
105
|
+
(0, _react.useEffect)(() => {
|
|
106
|
+
if (!enabled) return;
|
|
107
|
+
const ids = toIdArray(value);
|
|
108
|
+
const isEcho = lastEmittedRef.current !== null && isSameIds(ids, lastEmittedRef.current);
|
|
109
|
+
const isValueChanged = prevValueIdsRef.current === null || !isSameIds(ids, prevValueIdsRef.current);
|
|
110
|
+
prevValueIdsRef.current = ids;
|
|
111
|
+
if (isEcho) {
|
|
112
|
+
lastEmittedRef.current = null;
|
|
113
|
+
} else if (isValueChanged) {
|
|
114
|
+
committedIdsRef.current = ids;
|
|
115
|
+
lastEmittedRef.current = null;
|
|
116
|
+
}
|
|
117
|
+
setSelectedTags(prev => resolveMultipleTags(committedIdsRef.current, optionItems, prev));
|
|
118
|
+
}, [enabled, value, optionItems]);
|
|
119
|
+
/** 「선택 완료」로 Tag 를 확정한다 */
|
|
120
|
+
const commitTags = ids => {
|
|
121
|
+
committedIdsRef.current = ids;
|
|
122
|
+
setSelectedTags(prev => resolveMultipleTags(ids, optionItems, prev));
|
|
123
|
+
};
|
|
124
|
+
/** X 클릭 — 확정 id 에서 그 id 만 뺀다. value 기준으로 계산하면 「선택 완료」 전에 클릭한 항목까지 확정된다 */
|
|
125
|
+
const removeCommittedTag = tagId => {
|
|
126
|
+
commitTags(committedIdsRef.current.filter(id => id !== tagId));
|
|
127
|
+
};
|
|
128
|
+
/** ComboBox 가 onChange 로 내보낸 값을 기록한다 — value 로 돌아왔을 때 확정으로 보지 않기 위함 */
|
|
129
|
+
const markEmitted = ids => {
|
|
130
|
+
lastEmittedRef.current = ids;
|
|
131
|
+
};
|
|
132
|
+
return {
|
|
133
|
+
selectedTags,
|
|
134
|
+
commitTags,
|
|
135
|
+
removeCommittedTag,
|
|
136
|
+
markEmitted
|
|
137
|
+
};
|
|
138
|
+
};
|
|
33
139
|
const ComboBox = exports.ComboBox = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => {
|
|
34
140
|
let {
|
|
35
141
|
placeholder = '검색하세요',
|
|
@@ -69,22 +175,24 @@ const ComboBox = exports.ComboBox = /*#__PURE__*/(0, _react.forwardRef)((_ref, r
|
|
|
69
175
|
} = (0, _usePortalState.usePortalState)(usePortal);
|
|
70
176
|
const [inputValue, setInputValue] = (0, _react.useState)(searchValue);
|
|
71
177
|
const [showAllItems, setShowAllItems] = (0, _react.useState)(false);
|
|
72
|
-
const
|
|
178
|
+
const singleTags = useSingleSelectedTags(value, optionItems, !multiple);
|
|
179
|
+
const {
|
|
180
|
+
selectedTags,
|
|
181
|
+
commitTags,
|
|
182
|
+
removeCommittedTag,
|
|
183
|
+
markEmitted
|
|
184
|
+
} = useMultipleSelectedTags(value, optionItems, multiple);
|
|
73
185
|
const handleOptionSelect = option => {
|
|
74
186
|
if (disabled) return;
|
|
75
187
|
if (multiple) {
|
|
76
188
|
const newValue = tryToggle(option.id, Array.isArray(value) ? value : []);
|
|
77
189
|
if (newValue === null) return;
|
|
190
|
+
markEmitted(newValue);
|
|
78
191
|
onChange?.(newValue);
|
|
79
192
|
notifyFormChange(register, multiple, newValue);
|
|
80
193
|
return;
|
|
81
194
|
}
|
|
82
195
|
const newValue = option.id;
|
|
83
|
-
const tag = {
|
|
84
|
-
id: option.id,
|
|
85
|
-
label: option.label
|
|
86
|
-
};
|
|
87
|
-
setSelectedTags([tag]);
|
|
88
196
|
onChange?.(newValue);
|
|
89
197
|
notifyFormChange(register, multiple, newValue);
|
|
90
198
|
};
|
|
@@ -191,7 +299,6 @@ const ComboBox = exports.ComboBox = /*#__PURE__*/(0, _react.forwardRef)((_ref, r
|
|
|
191
299
|
const {
|
|
192
300
|
buttonText: selectAllButtonText,
|
|
193
301
|
toggleSelectAll,
|
|
194
|
-
getSelectedTagsData,
|
|
195
302
|
removeTag,
|
|
196
303
|
isMaxSelectionActive,
|
|
197
304
|
tryToggle
|
|
@@ -201,6 +308,7 @@ const ComboBox = exports.ComboBox = /*#__PURE__*/(0, _react.forwardRef)((_ref, r
|
|
|
201
308
|
const handleSelectAll = () => {
|
|
202
309
|
if (!multiple || !onChange) return;
|
|
203
310
|
const newSelectedValues = toggleSelectAll();
|
|
311
|
+
markEmitted(newSelectedValues);
|
|
204
312
|
onChange(newSelectedValues);
|
|
205
313
|
};
|
|
206
314
|
const handleEdit = () => {
|
|
@@ -208,30 +316,32 @@ const ComboBox = exports.ComboBox = /*#__PURE__*/(0, _react.forwardRef)((_ref, r
|
|
|
208
316
|
};
|
|
209
317
|
const handleComplete = completedValue => {
|
|
210
318
|
if (!multiple) return;
|
|
211
|
-
|
|
212
|
-
setSelectedTags(tags);
|
|
319
|
+
commitTags(toIdArray(completedValue));
|
|
213
320
|
setInputValue('');
|
|
214
321
|
closeDropdown();
|
|
215
322
|
onComplete?.(completedValue);
|
|
216
323
|
};
|
|
217
324
|
const handleRemoveTag = tagId => {
|
|
325
|
+
// X 버튼은 disabled 에서 숨기지만, 다른 호출 경로가 생겨도 값이 지워지지 않게 함수에서도 막는다
|
|
326
|
+
if (disabled) return;
|
|
218
327
|
if (!onChange) return;
|
|
219
328
|
if (multiple) {
|
|
220
329
|
const newSelectedValues = removeTag(tagId);
|
|
330
|
+
markEmitted(newSelectedValues);
|
|
221
331
|
onChange(newSelectedValues);
|
|
222
|
-
|
|
332
|
+
removeCommittedTag(tagId);
|
|
223
333
|
return;
|
|
224
334
|
}
|
|
225
335
|
onChange('');
|
|
226
|
-
setSelectedTags([]);
|
|
227
336
|
};
|
|
228
337
|
const comboboxId = `combobox-options-${id || 'default'}`;
|
|
229
338
|
const hasVisibleOptions = showAllItems || inputValue;
|
|
230
339
|
const displayedOptions = hasVisibleOptions ? optionItems : [];
|
|
231
340
|
const showNoResults = !hasVisibleOptions || optionItems.length === 0;
|
|
232
341
|
const showFooter = multiple || showFooterButtons;
|
|
233
|
-
const hiddenInputValue =
|
|
234
|
-
const
|
|
342
|
+
const hiddenInputValue = toHiddenInputValue(value, multiple);
|
|
343
|
+
const tags = multiple ? selectedTags : singleTags;
|
|
344
|
+
const hasTags = tags.length > 0;
|
|
235
345
|
const {
|
|
236
346
|
className: portalPanelClassName,
|
|
237
347
|
style: portalPanelStyle
|
|
@@ -242,7 +352,7 @@ const ComboBox = exports.ComboBox = /*#__PURE__*/(0, _react.forwardRef)((_ref, r
|
|
|
242
352
|
direction: dropdownDirection,
|
|
243
353
|
size: size,
|
|
244
354
|
options: displayedOptions,
|
|
245
|
-
value: value,
|
|
355
|
+
value: toDropdownValue(value),
|
|
246
356
|
focusedIndex: focusedIndex,
|
|
247
357
|
maxHeight: maxHeight,
|
|
248
358
|
listboxId: comboboxId,
|
|
@@ -321,10 +431,10 @@ const ComboBox = exports.ComboBox = /*#__PURE__*/(0, _react.forwardRef)((_ref, r
|
|
|
321
431
|
})]
|
|
322
432
|
}), hasTags && (0, _jsxRuntime.jsx)("div", {
|
|
323
433
|
className: "ncua-combobox__tags",
|
|
324
|
-
children:
|
|
434
|
+
children: tags.map(tag => (0, _jsxRuntime.jsx)(_tag.Tag, {
|
|
325
435
|
text: tag.label,
|
|
326
436
|
size: "sm",
|
|
327
|
-
close:
|
|
437
|
+
close: !disabled,
|
|
328
438
|
onButtonClick: () => handleRemoveTag(tag.id)
|
|
329
439
|
}, tag.id))
|
|
330
440
|
})]
|
|
@@ -476,7 +476,7 @@ const clickButtonByLabel = (scope, label) => {
|
|
|
476
476
|
(0, _vitest.expect)(onSearch).toHaveBeenCalledWith('사');
|
|
477
477
|
(0, _vitest.expect)(view.content.querySelectorAll('.ncua-select-dropdown__option')).toHaveLength(createOptions().length);
|
|
478
478
|
});
|
|
479
|
-
(0, _vitest.it)('옵션을 선택하면 onChange 가
|
|
479
|
+
(0, _vitest.it)('옵션을 선택하면 onChange 가 호출된다', () => {
|
|
480
480
|
// Given: 옵션이 노출된 기본 모드 ComboBox 에서
|
|
481
481
|
const onChange = _vitest.vi.fn();
|
|
482
482
|
const view = mountComboBox({
|
|
@@ -489,9 +489,9 @@ const clickButtonByLabel = (scope, label) => {
|
|
|
489
489
|
(0, _testUtils.act)(() => {
|
|
490
490
|
requireElement(view.content, '.ncua-select-dropdown__option').click();
|
|
491
491
|
});
|
|
492
|
-
// Then: onChange 가 선택 값으로
|
|
492
|
+
// Then: onChange 가 선택 값으로 호출된다
|
|
493
|
+
// 선택 태그는 value 를 따르므로(#530) onChange 값을 value 로 돌려주는 경우의 태그 표시는 ComboBox.test.ts S-10 에서 검증한다
|
|
493
494
|
(0, _vitest.expect)(onChange).toHaveBeenCalledWith('apple');
|
|
494
|
-
(0, _vitest.expect)(view.container.querySelectorAll('.ncua-combobox__tags .ncua-tag')).toHaveLength(1);
|
|
495
495
|
});
|
|
496
496
|
(0, _vitest.it)('Escape 를 누르면 인라인 패널이 닫힌다', () => {
|
|
497
497
|
// Given: 열려 있는 기본 모드 ComboBox 에서
|
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _react = require("react");
|
|
4
|
+
var _client = require("react-dom/client");
|
|
5
|
+
var _testUtils = require("react-dom/test-utils");
|
|
6
|
+
var _vitest = require("vitest");
|
|
7
|
+
var _ComboBox = require("../ComboBox");
|
|
8
|
+
// @vitest-environment jsdom
|
|
9
|
+
|
|
10
|
+
// React 18에서 act() 사용 시 필요한 플래그 (미설정 시 console.error 경고 발생)
|
|
11
|
+
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
|
|
12
|
+
const OPTIONS = [{
|
|
13
|
+
id: 1,
|
|
14
|
+
label: '사과'
|
|
15
|
+
}, {
|
|
16
|
+
id: 2,
|
|
17
|
+
label: '바나나'
|
|
18
|
+
}, {
|
|
19
|
+
id: 3,
|
|
20
|
+
label: '체리'
|
|
21
|
+
}];
|
|
22
|
+
/** id 1·2가 빠진 목록 — 검색 필터로 optionItems가 줄어든 상황 */
|
|
23
|
+
const FILTERED_OPTIONS = [{
|
|
24
|
+
id: 3,
|
|
25
|
+
label: '체리'
|
|
26
|
+
}];
|
|
27
|
+
/** optionItems에 없는 id */
|
|
28
|
+
const MISSING_ID = 99;
|
|
29
|
+
/** 바깥에서 reset 으로 넣는 값 — 기존 선택(1·2)과 겹치지 않는 id */
|
|
30
|
+
const RESET_ID = 3;
|
|
31
|
+
/** 「전체 선택」 결과 — OPTIONS 의 모든 id */
|
|
32
|
+
const ALL_IDS = OPTIONS.map(option => option.id);
|
|
33
|
+
let root = null;
|
|
34
|
+
let container = null;
|
|
35
|
+
/** 같은 root에 다시 render하면 props 변경(rerender)이 된다. */
|
|
36
|
+
function render(props) {
|
|
37
|
+
if (!container) {
|
|
38
|
+
container = document.createElement('div');
|
|
39
|
+
document.body.appendChild(container);
|
|
40
|
+
root = (0, _client.createRoot)(container);
|
|
41
|
+
}
|
|
42
|
+
(0, _testUtils.act)(() => {
|
|
43
|
+
root?.render(/*#__PURE__*/(0, _react.createElement)(_ComboBox.ComboBox, props));
|
|
44
|
+
});
|
|
45
|
+
return container;
|
|
46
|
+
}
|
|
47
|
+
/** onChange로 받은 값을 value로 되돌려주는 사용처 흉내 (controlled) */
|
|
48
|
+
function Harness(_ref) {
|
|
49
|
+
let {
|
|
50
|
+
initialValue,
|
|
51
|
+
onChangeSpy,
|
|
52
|
+
transform,
|
|
53
|
+
...rest
|
|
54
|
+
} = _ref;
|
|
55
|
+
const [value, setValue] = (0, _react.useState)(initialValue);
|
|
56
|
+
return /*#__PURE__*/(0, _react.createElement)(_ComboBox.ComboBox, {
|
|
57
|
+
...rest,
|
|
58
|
+
value,
|
|
59
|
+
onChange: next => {
|
|
60
|
+
onChangeSpy(next);
|
|
61
|
+
setValue(transform ? transform(next) : next);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
function renderHarness(props) {
|
|
66
|
+
if (!container) {
|
|
67
|
+
container = document.createElement('div');
|
|
68
|
+
document.body.appendChild(container);
|
|
69
|
+
root = (0, _client.createRoot)(container);
|
|
70
|
+
}
|
|
71
|
+
(0, _testUtils.act)(() => {
|
|
72
|
+
root?.render(/*#__PURE__*/(0, _react.createElement)(Harness, props));
|
|
73
|
+
});
|
|
74
|
+
return container;
|
|
75
|
+
}
|
|
76
|
+
function getTagTexts(el) {
|
|
77
|
+
return Array.from(el.querySelectorAll('.ncua-combobox__tags .ncua-tag .ncua-tag__text')).map(node => node.textContent ?? '');
|
|
78
|
+
}
|
|
79
|
+
function getCloseButtons(el) {
|
|
80
|
+
return Array.from(el.querySelectorAll('.ncua-combobox__tags .ncua-tag__close'));
|
|
81
|
+
}
|
|
82
|
+
function click(target) {
|
|
83
|
+
if (!target) throw new Error('클릭 대상을 찾을 수 없습니다');
|
|
84
|
+
(0, _testUtils.act)(() => {
|
|
85
|
+
target.click();
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/** 「전체」 버튼으로 드롭다운을 연다 (입력값이 없어도 전체 목록 표시). */
|
|
89
|
+
function openDropdown(el) {
|
|
90
|
+
click(el.querySelector('.ncua-input__right-icon'));
|
|
91
|
+
}
|
|
92
|
+
function clickOption(el, label) {
|
|
93
|
+
const option = Array.from(el.querySelectorAll('.ncua-select-dropdown__option')).find(node => node.textContent === label);
|
|
94
|
+
click(option);
|
|
95
|
+
}
|
|
96
|
+
function clickButton(el, label) {
|
|
97
|
+
const button = Array.from(el.querySelectorAll('button')).find(node => node.textContent?.includes(label));
|
|
98
|
+
click(button);
|
|
99
|
+
}
|
|
100
|
+
(0, _vitest.afterEach)(() => {
|
|
101
|
+
(0, _testUtils.act)(() => {
|
|
102
|
+
root?.unmount();
|
|
103
|
+
});
|
|
104
|
+
container?.remove();
|
|
105
|
+
root = null;
|
|
106
|
+
container = null;
|
|
107
|
+
});
|
|
108
|
+
(0, _vitest.describe)('ComboBox — 단일 모드 외부 value Tag', () => {
|
|
109
|
+
(0, _vitest.it)('S-01: value에 해당하는 옵션이 있으면 Tag 1개를 라벨로 표시한다', () => {
|
|
110
|
+
const el = render({
|
|
111
|
+
value: 1,
|
|
112
|
+
optionItems: OPTIONS
|
|
113
|
+
});
|
|
114
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과']);
|
|
115
|
+
});
|
|
116
|
+
(0, _vitest.it)('S-02: optionItems가 늦게 도착하면 도착 시 Tag를 표시한다', () => {
|
|
117
|
+
const el = render({
|
|
118
|
+
value: 1,
|
|
119
|
+
optionItems: []
|
|
120
|
+
});
|
|
121
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual([]);
|
|
122
|
+
render({
|
|
123
|
+
value: 1,
|
|
124
|
+
optionItems: OPTIONS
|
|
125
|
+
});
|
|
126
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과']);
|
|
127
|
+
});
|
|
128
|
+
_vitest.it.each([['빈 문자열', ''], ['undefined', undefined], ['null', null]])('S-03: value가 %s이면 Tag를 표시하지 않는다', (_, value) => {
|
|
129
|
+
const el = render({
|
|
130
|
+
value: value,
|
|
131
|
+
optionItems: OPTIONS
|
|
132
|
+
});
|
|
133
|
+
(0, _vitest.expect)(el.querySelector('.ncua-combobox__tags')).toBeNull();
|
|
134
|
+
});
|
|
135
|
+
_vitest.it.each([['빈 문자열', ''], ['undefined', undefined]])('S-04: Tag 표시 중 value가 %s로 바뀌면 Tag가 사라진다', (_, value) => {
|
|
136
|
+
const el = render({
|
|
137
|
+
value: 1,
|
|
138
|
+
optionItems: OPTIONS
|
|
139
|
+
});
|
|
140
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과']);
|
|
141
|
+
render({
|
|
142
|
+
value,
|
|
143
|
+
optionItems: OPTIONS
|
|
144
|
+
});
|
|
145
|
+
(0, _vitest.expect)(el.querySelector('.ncua-combobox__tags')).toBeNull();
|
|
146
|
+
});
|
|
147
|
+
(0, _vitest.it)('S-05: 검색 필터로 optionItems에서 선택 항목이 빠져도 같은 라벨로 Tag를 유지한다', () => {
|
|
148
|
+
const el = render({
|
|
149
|
+
value: 1,
|
|
150
|
+
optionItems: OPTIONS
|
|
151
|
+
});
|
|
152
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과']);
|
|
153
|
+
render({
|
|
154
|
+
value: 1,
|
|
155
|
+
optionItems: FILTERED_OPTIONS
|
|
156
|
+
});
|
|
157
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과']);
|
|
158
|
+
});
|
|
159
|
+
(0, _vitest.it)('S-06: 목록에 없는 다른 id로 바뀌면 이전 라벨을 쓰지 않고 Tag를 표시하지 않는다', () => {
|
|
160
|
+
const el = render({
|
|
161
|
+
value: 1,
|
|
162
|
+
optionItems: OPTIONS
|
|
163
|
+
});
|
|
164
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과']);
|
|
165
|
+
render({
|
|
166
|
+
value: MISSING_ID,
|
|
167
|
+
optionItems: OPTIONS
|
|
168
|
+
});
|
|
169
|
+
(0, _vitest.expect)(el.querySelector('.ncua-combobox__tags')).toBeNull();
|
|
170
|
+
});
|
|
171
|
+
(0, _vitest.it)('S-07: value가 다른 id로 바뀌면 그 id의 라벨로 Tag를 바꾼다', () => {
|
|
172
|
+
const el = render({
|
|
173
|
+
value: 1,
|
|
174
|
+
optionItems: OPTIONS
|
|
175
|
+
});
|
|
176
|
+
render({
|
|
177
|
+
value: 2,
|
|
178
|
+
optionItems: OPTIONS
|
|
179
|
+
});
|
|
180
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['바나나']);
|
|
181
|
+
});
|
|
182
|
+
(0, _vitest.it)('S-08: disabled면 Tag는 표시하고 X 버튼은 표시하지 않는다', () => {
|
|
183
|
+
const el = render({
|
|
184
|
+
value: 1,
|
|
185
|
+
optionItems: OPTIONS,
|
|
186
|
+
disabled: true
|
|
187
|
+
});
|
|
188
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과']);
|
|
189
|
+
(0, _vitest.expect)(getCloseButtons(el)).toHaveLength(0);
|
|
190
|
+
});
|
|
191
|
+
(0, _vitest.it)('S-09: value가 0이어도 빈 값으로 보지 않고 Tag를 표시한다', () => {
|
|
192
|
+
const el = render({
|
|
193
|
+
value: 0,
|
|
194
|
+
optionItems: [{
|
|
195
|
+
id: 0,
|
|
196
|
+
label: '없음'
|
|
197
|
+
}, ...OPTIONS]
|
|
198
|
+
});
|
|
199
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['없음']);
|
|
200
|
+
});
|
|
201
|
+
_vitest.it.each([[0, '0'], [null, '']])('S-12: register 사용 시 value가 %s이면 hidden input 값은 %j이다 (Tag 표시 규칙과 같게 0은 값으로 본다)', (value, expected) => {
|
|
202
|
+
const register = {
|
|
203
|
+
name: 'item',
|
|
204
|
+
onChange: _vitest.vi.fn(),
|
|
205
|
+
onBlur: _vitest.vi.fn(),
|
|
206
|
+
ref: _vitest.vi.fn()
|
|
207
|
+
};
|
|
208
|
+
const el = render({
|
|
209
|
+
value,
|
|
210
|
+
register,
|
|
211
|
+
optionItems: [{
|
|
212
|
+
id: 0,
|
|
213
|
+
label: '없음'
|
|
214
|
+
}, ...OPTIONS]
|
|
215
|
+
});
|
|
216
|
+
(0, _vitest.expect)(el.querySelector('input[type="hidden"]')?.value).toBe(expected);
|
|
217
|
+
});
|
|
218
|
+
(0, _vitest.it)('S-10 (회귀): 옵션을 클릭하면 onChange(id)를 호출하고 Tag를 표시한다', () => {
|
|
219
|
+
const onChangeSpy = _vitest.vi.fn();
|
|
220
|
+
const el = renderHarness({
|
|
221
|
+
initialValue: '',
|
|
222
|
+
onChangeSpy,
|
|
223
|
+
optionItems: OPTIONS
|
|
224
|
+
});
|
|
225
|
+
openDropdown(el);
|
|
226
|
+
clickOption(el, '바나나');
|
|
227
|
+
(0, _vitest.expect)(onChangeSpy).toHaveBeenCalledWith(2);
|
|
228
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['바나나']);
|
|
229
|
+
});
|
|
230
|
+
(0, _vitest.it)('S-11 (회귀): X를 클릭하면 onChange("")를 호출하고 Tag가 사라진다', () => {
|
|
231
|
+
const onChangeSpy = _vitest.vi.fn();
|
|
232
|
+
const el = renderHarness({
|
|
233
|
+
initialValue: 1,
|
|
234
|
+
onChangeSpy,
|
|
235
|
+
optionItems: OPTIONS
|
|
236
|
+
});
|
|
237
|
+
click(getCloseButtons(el)[0]);
|
|
238
|
+
(0, _vitest.expect)(onChangeSpy).toHaveBeenCalledWith('');
|
|
239
|
+
(0, _vitest.expect)(el.querySelector('.ncua-combobox__tags')).toBeNull();
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
(0, _vitest.describe)('ComboBox — multiple 모드 외부 value Tag', () => {
|
|
243
|
+
(0, _vitest.it)('M-01: value 배열에 해당하는 Tag를 모두 표시한다', () => {
|
|
244
|
+
const el = render({
|
|
245
|
+
multiple: true,
|
|
246
|
+
value: [1, 2],
|
|
247
|
+
optionItems: OPTIONS
|
|
248
|
+
});
|
|
249
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과', '바나나']);
|
|
250
|
+
});
|
|
251
|
+
(0, _vitest.it)('M-02: optionItems가 늦게 도착하면 도착 시 Tag를 표시한다', () => {
|
|
252
|
+
const el = render({
|
|
253
|
+
multiple: true,
|
|
254
|
+
value: [1, 2],
|
|
255
|
+
optionItems: []
|
|
256
|
+
});
|
|
257
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual([]);
|
|
258
|
+
render({
|
|
259
|
+
multiple: true,
|
|
260
|
+
value: [1, 2],
|
|
261
|
+
optionItems: OPTIONS
|
|
262
|
+
});
|
|
263
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과', '바나나']);
|
|
264
|
+
});
|
|
265
|
+
_vitest.it.each([['빈 배열', []], ['undefined', undefined], ['null', null]])('M-03: value가 %s이면 Tag를 표시하지 않는다', (_, value) => {
|
|
266
|
+
const el = render({
|
|
267
|
+
multiple: true,
|
|
268
|
+
value: value,
|
|
269
|
+
optionItems: OPTIONS
|
|
270
|
+
});
|
|
271
|
+
(0, _vitest.expect)(el.querySelector('.ncua-combobox__tags')).toBeNull();
|
|
272
|
+
});
|
|
273
|
+
_vitest.it.each([['빈 배열', []], ['undefined', undefined]])('M-04: Tag 표시 중 value가 %s로 바뀌면 Tag가 사라진다', (_, value) => {
|
|
274
|
+
const el = render({
|
|
275
|
+
multiple: true,
|
|
276
|
+
value: [1, 2],
|
|
277
|
+
optionItems: OPTIONS
|
|
278
|
+
});
|
|
279
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과', '바나나']);
|
|
280
|
+
render({
|
|
281
|
+
multiple: true,
|
|
282
|
+
value,
|
|
283
|
+
optionItems: OPTIONS
|
|
284
|
+
});
|
|
285
|
+
(0, _vitest.expect)(el.querySelector('.ncua-combobox__tags')).toBeNull();
|
|
286
|
+
});
|
|
287
|
+
(0, _vitest.it)('M-05: 바깥에서 value를 바꾸면(reset) 새 value로 Tag를 다시 그린다', () => {
|
|
288
|
+
const el = render({
|
|
289
|
+
multiple: true,
|
|
290
|
+
value: [1, 2],
|
|
291
|
+
optionItems: OPTIONS
|
|
292
|
+
});
|
|
293
|
+
render({
|
|
294
|
+
multiple: true,
|
|
295
|
+
value: [RESET_ID],
|
|
296
|
+
optionItems: OPTIONS
|
|
297
|
+
});
|
|
298
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['체리']);
|
|
299
|
+
});
|
|
300
|
+
(0, _vitest.it)('M-06: 검색 필터로 optionItems에서 선택 항목이 빠져도 Tag를 유지한다', () => {
|
|
301
|
+
const el = render({
|
|
302
|
+
multiple: true,
|
|
303
|
+
value: [1, 2],
|
|
304
|
+
optionItems: OPTIONS
|
|
305
|
+
});
|
|
306
|
+
render({
|
|
307
|
+
multiple: true,
|
|
308
|
+
value: [1, 2],
|
|
309
|
+
optionItems: FILTERED_OPTIONS
|
|
310
|
+
});
|
|
311
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과', '바나나']);
|
|
312
|
+
});
|
|
313
|
+
(0, _vitest.it)('M-07: disabled면 Tag는 표시하고 X 버튼은 표시하지 않는다', () => {
|
|
314
|
+
const el = render({
|
|
315
|
+
multiple: true,
|
|
316
|
+
value: [1],
|
|
317
|
+
optionItems: OPTIONS,
|
|
318
|
+
disabled: true
|
|
319
|
+
});
|
|
320
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과']);
|
|
321
|
+
(0, _vitest.expect)(getCloseButtons(el)).toHaveLength(0);
|
|
322
|
+
});
|
|
323
|
+
(0, _vitest.it)('M-08 (회귀): 옵션을 클릭하면 onChange만 호출하고 Tag는 「선택 완료」 전까지 그대로다', () => {
|
|
324
|
+
const onChangeSpy = _vitest.vi.fn();
|
|
325
|
+
const el = renderHarness({
|
|
326
|
+
multiple: true,
|
|
327
|
+
initialValue: [1],
|
|
328
|
+
onChangeSpy,
|
|
329
|
+
optionItems: OPTIONS
|
|
330
|
+
});
|
|
331
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과']);
|
|
332
|
+
openDropdown(el);
|
|
333
|
+
clickOption(el, '바나나');
|
|
334
|
+
(0, _vitest.expect)(onChangeSpy).toHaveBeenCalledWith([1, 2]);
|
|
335
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과']);
|
|
336
|
+
});
|
|
337
|
+
(0, _vitest.it)('M-09 (회귀): 옵션 클릭 후 「선택 완료」를 누르면 Tag를 반영하고 onComplete를 호출한다', () => {
|
|
338
|
+
const onChangeSpy = _vitest.vi.fn();
|
|
339
|
+
const onComplete = _vitest.vi.fn();
|
|
340
|
+
const el = renderHarness({
|
|
341
|
+
multiple: true,
|
|
342
|
+
initialValue: [1],
|
|
343
|
+
onChangeSpy,
|
|
344
|
+
onComplete,
|
|
345
|
+
optionItems: OPTIONS
|
|
346
|
+
});
|
|
347
|
+
openDropdown(el);
|
|
348
|
+
clickOption(el, '바나나');
|
|
349
|
+
clickButton(el, '선택 완료');
|
|
350
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과', '바나나']);
|
|
351
|
+
(0, _vitest.expect)(onComplete).toHaveBeenCalledWith([1, 2]);
|
|
352
|
+
});
|
|
353
|
+
(0, _vitest.it)('M-10 (회귀): X를 클릭하면 onChange(남은 배열)를 호출하고 해당 Tag만 제거한다', () => {
|
|
354
|
+
const onChangeSpy = _vitest.vi.fn();
|
|
355
|
+
const el = renderHarness({
|
|
356
|
+
multiple: true,
|
|
357
|
+
initialValue: [1, 2],
|
|
358
|
+
onChangeSpy,
|
|
359
|
+
optionItems: OPTIONS
|
|
360
|
+
});
|
|
361
|
+
click(getCloseButtons(el)[0]);
|
|
362
|
+
(0, _vitest.expect)(onChangeSpy).toHaveBeenCalledWith([2]);
|
|
363
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['바나나']);
|
|
364
|
+
});
|
|
365
|
+
(0, _vitest.it)('M-11 (회귀): 「전체 선택」을 누르면 onChange(전체 id)만 호출하고 Tag는 그대로다', () => {
|
|
366
|
+
const onChangeSpy = _vitest.vi.fn();
|
|
367
|
+
const el = renderHarness({
|
|
368
|
+
multiple: true,
|
|
369
|
+
initialValue: [],
|
|
370
|
+
onChangeSpy,
|
|
371
|
+
optionItems: OPTIONS
|
|
372
|
+
});
|
|
373
|
+
openDropdown(el);
|
|
374
|
+
clickButton(el, '전체 선택');
|
|
375
|
+
(0, _vitest.expect)(onChangeSpy).toHaveBeenCalledWith(ALL_IDS);
|
|
376
|
+
(0, _vitest.expect)(el.querySelector('.ncua-combobox__tags')).toBeNull();
|
|
377
|
+
});
|
|
378
|
+
(0, _vitest.it)('M-12: 클릭으로 고른 값과 같은 값을 바깥에서 비웠다가 다시 넣어도 Tag를 표시한다', () => {
|
|
379
|
+
const props = {
|
|
380
|
+
multiple: true,
|
|
381
|
+
optionItems: OPTIONS,
|
|
382
|
+
onChange: _vitest.vi.fn()
|
|
383
|
+
};
|
|
384
|
+
const el = render({
|
|
385
|
+
...props,
|
|
386
|
+
value: []
|
|
387
|
+
});
|
|
388
|
+
openDropdown(el);
|
|
389
|
+
clickOption(el, '사과');
|
|
390
|
+
render({
|
|
391
|
+
...props,
|
|
392
|
+
value: [1]
|
|
393
|
+
});
|
|
394
|
+
clickButton(el, '선택 완료');
|
|
395
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과']);
|
|
396
|
+
render({
|
|
397
|
+
...props,
|
|
398
|
+
value: []
|
|
399
|
+
});
|
|
400
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual([]);
|
|
401
|
+
render({
|
|
402
|
+
...props,
|
|
403
|
+
value: [1]
|
|
404
|
+
});
|
|
405
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과']);
|
|
406
|
+
});
|
|
407
|
+
(0, _vitest.it)('M-13 (회귀): 클릭 후 「선택 완료」 전에 optionItems가 바뀌어도(검색) Tag는 그대로다', () => {
|
|
408
|
+
const onChangeSpy = _vitest.vi.fn();
|
|
409
|
+
const el = renderHarness({
|
|
410
|
+
multiple: true,
|
|
411
|
+
initialValue: [1],
|
|
412
|
+
onChangeSpy,
|
|
413
|
+
optionItems: OPTIONS
|
|
414
|
+
});
|
|
415
|
+
openDropdown(el);
|
|
416
|
+
clickOption(el, '바나나');
|
|
417
|
+
renderHarness({
|
|
418
|
+
multiple: true,
|
|
419
|
+
initialValue: [1],
|
|
420
|
+
onChangeSpy,
|
|
421
|
+
optionItems: FILTERED_OPTIONS
|
|
422
|
+
});
|
|
423
|
+
(0, _vitest.expect)(onChangeSpy).toHaveBeenCalledWith([1, 2]);
|
|
424
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['사과']);
|
|
425
|
+
});
|
|
426
|
+
(0, _vitest.it)('M-14 (회귀): 「선택 완료」 전에 클릭한 항목이 있을 때 X를 누르면 그 Tag만 빠지고 클릭한 항목은 확정되지 않는다', () => {
|
|
427
|
+
const onChangeSpy = _vitest.vi.fn();
|
|
428
|
+
const el = renderHarness({
|
|
429
|
+
multiple: true,
|
|
430
|
+
initialValue: [1],
|
|
431
|
+
onChangeSpy,
|
|
432
|
+
optionItems: OPTIONS
|
|
433
|
+
});
|
|
434
|
+
openDropdown(el);
|
|
435
|
+
clickOption(el, '바나나');
|
|
436
|
+
click(getCloseButtons(el)[0]);
|
|
437
|
+
(0, _vitest.expect)(onChangeSpy).toHaveBeenLastCalledWith([2]);
|
|
438
|
+
(0, _vitest.expect)(el.querySelector('.ncua-combobox__tags')).toBeNull();
|
|
439
|
+
});
|
|
440
|
+
(0, _vitest.it)('M-15 (회귀): 사용처가 onChange 값의 순서를 바꿔 돌려줘도 「선택 완료」 전에는 Tag가 그대로다', () => {
|
|
441
|
+
const onChangeSpy = _vitest.vi.fn();
|
|
442
|
+
const sortIds = value => Array.isArray(value) ? [...value].sort() : value;
|
|
443
|
+
const el = renderHarness({
|
|
444
|
+
multiple: true,
|
|
445
|
+
initialValue: [RESET_ID],
|
|
446
|
+
onChangeSpy,
|
|
447
|
+
transform: sortIds,
|
|
448
|
+
optionItems: OPTIONS
|
|
449
|
+
});
|
|
450
|
+
openDropdown(el);
|
|
451
|
+
clickOption(el, '사과');
|
|
452
|
+
(0, _vitest.expect)(onChangeSpy).toHaveBeenCalledWith([RESET_ID, 1]);
|
|
453
|
+
(0, _vitest.expect)(getTagTexts(el)).toEqual(['체리']);
|
|
454
|
+
});
|
|
455
|
+
});
|