@ncds/ui-admin 1.8.13 → 1.8.14
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/date-picker/DatePicker.js +17 -20
- package/dist/cjs/src/components/forms-and-input/date-picker/__tests__/DatePicker.memo.test.js +53 -0
- package/dist/cjs/src/components/forms-and-input/date-picker/__tests__/DatePicker.test.js +129 -0
- package/dist/cjs/src/components/forms-and-input/select/Select.js +12 -1
- package/dist/cjs/src/components/forms-and-input/select-box/SelectBox.js +5 -2
- package/dist/cjs/src/utils/__tests__/date-picker.test.js +29 -0
- package/dist/cjs/src/utils/date-picker.js +15 -1
- package/dist/esm/src/components/forms-and-input/date-picker/DatePicker.js +18 -21
- package/dist/esm/src/components/forms-and-input/date-picker/__tests__/DatePicker.memo.test.js +50 -0
- package/dist/esm/src/components/forms-and-input/date-picker/__tests__/DatePicker.test.js +126 -0
- package/dist/esm/src/components/forms-and-input/select/Select.js +12 -1
- package/dist/esm/src/components/forms-and-input/select-box/SelectBox.js +5 -2
- package/dist/esm/src/utils/__tests__/date-picker.test.js +26 -0
- package/dist/esm/src/utils/date-picker.js +13 -0
- package/dist/temp/src/components/forms-and-input/date-picker/DatePicker.js +18 -28
- package/dist/temp/src/components/forms-and-input/date-picker/__tests__/DatePicker.memo.test.d.ts +1 -0
- package/dist/temp/src/components/forms-and-input/date-picker/__tests__/DatePicker.memo.test.js +43 -0
- package/dist/temp/src/components/forms-and-input/date-picker/__tests__/DatePicker.test.d.ts +1 -0
- package/dist/temp/src/components/forms-and-input/date-picker/__tests__/DatePicker.test.js +109 -0
- package/dist/temp/src/components/forms-and-input/select/Select.d.ts +1 -0
- package/dist/temp/src/components/forms-and-input/select/Select.js +10 -2
- package/dist/temp/src/components/forms-and-input/select-box/SelectBox.d.ts +1 -0
- package/dist/temp/src/components/forms-and-input/select-box/SelectBox.js +5 -4
- package/dist/temp/src/utils/__tests__/date-picker.test.d.ts +1 -0
- package/dist/temp/src/utils/__tests__/date-picker.test.js +26 -0
- package/dist/temp/src/utils/date-picker.d.ts +2 -0
- package/dist/temp/src/utils/date-picker.js +13 -0
- package/dist/types/src/components/forms-and-input/date-picker/__tests__/DatePicker.memo.test.d.ts +1 -0
- package/dist/types/src/components/forms-and-input/date-picker/__tests__/DatePicker.test.d.ts +1 -0
- package/dist/types/src/components/forms-and-input/select/Select.d.ts +1 -0
- package/dist/types/src/components/forms-and-input/select-box/SelectBox.d.ts +1 -0
- package/dist/types/src/utils/__tests__/date-picker.test.d.ts +1 -0
- package/dist/types/src/utils/date-picker.d.ts +2 -0
- package/dist/ui-admin/assets/styles/style.css +18 -2
- package/package.json +2 -1
|
@@ -183,6 +183,12 @@ const setupTimeInputHandlers = (instance, input, isPortal, onHourInput, onMinute
|
|
|
183
183
|
};
|
|
184
184
|
/** 시간 입력 필드 이벤트 리스너 정리 */
|
|
185
185
|
const cleanupTimeInputHandlers = (instance, input, isPortal, onHourInput, onMinuteInput) => {
|
|
186
|
+
// document 전역 리스너는 캘린더 DOM 조회 성공 여부와 무관하게 항상 먼저 제거한다 (누수 방지)
|
|
187
|
+
const handleMouseDown = instance._handleMouseDown;
|
|
188
|
+
if (handleMouseDown) {
|
|
189
|
+
document.removeEventListener('mousedown', handleMouseDown, true);
|
|
190
|
+
instance._handleMouseDown = undefined;
|
|
191
|
+
}
|
|
186
192
|
const timeContainer = isPortal ? instance.calendarContainer : input.parentElement;
|
|
187
193
|
const timeInputWrapper = timeContainer?.querySelector('.flatpickr-time');
|
|
188
194
|
if (!timeInputWrapper) return;
|
|
@@ -194,10 +200,6 @@ const cleanupTimeInputHandlers = (instance, input, isPortal, onHourInput, onMinu
|
|
|
194
200
|
if (minuteInput) {
|
|
195
201
|
minuteInput.removeEventListener('input', onMinuteInput);
|
|
196
202
|
}
|
|
197
|
-
const handleMouseDown = instance._handleMouseDown;
|
|
198
|
-
if (handleMouseDown) {
|
|
199
|
-
document.removeEventListener('mousedown', handleMouseDown, true);
|
|
200
|
-
}
|
|
201
203
|
};
|
|
202
204
|
// ══════════════════════════════════════════
|
|
203
205
|
// DatePicker 컴포넌트
|
|
@@ -227,7 +229,7 @@ const DatePicker = exports.DatePicker = /*#__PURE__*/(0, _react.forwardRef)((_re
|
|
|
227
229
|
const onValidationErrorRef = (0, _react.useRef)(onValidationError);
|
|
228
230
|
onValidationErrorRef.current = onValidationError;
|
|
229
231
|
/** portal 컨테이너를 lazily 생성 (이미 있으면 재사용), className은 매번 갱신 */
|
|
230
|
-
const getPortalContainer = () => {
|
|
232
|
+
const getPortalContainer = (0, _react.useCallback)(() => {
|
|
231
233
|
if (!portal || typeof document === 'undefined') return undefined;
|
|
232
234
|
const portalClassName = (0, _classnames.default)('ncua-date-picker', 'ncua-date-picker--portal', `ncua-date-picker--${size}`, {
|
|
233
235
|
'ncua-date-picker--has-time': hasTimeOption
|
|
@@ -243,7 +245,7 @@ const DatePicker = exports.DatePicker = /*#__PURE__*/(0, _react.forwardRef)((_re
|
|
|
243
245
|
document.body.appendChild(el);
|
|
244
246
|
portalContainerRef.current = el;
|
|
245
247
|
return el;
|
|
246
|
-
};
|
|
248
|
+
}, [portal, size, hasTimeOption]);
|
|
247
249
|
/** 컴포넌트 언마운트 시 포탈 컨테이너 및 스크롤 리스너 정리 */
|
|
248
250
|
(0, _react.useEffect)(() => {
|
|
249
251
|
return () => {
|
|
@@ -255,7 +257,7 @@ const DatePicker = exports.DatePicker = /*#__PURE__*/(0, _react.forwardRef)((_re
|
|
|
255
257
|
// 날짜 유효성 검사
|
|
256
258
|
// ──────────────────────────────────────────────
|
|
257
259
|
/** minDate/maxDate 범위를 벗어났는지 확인 */
|
|
258
|
-
const checkDateViolations = (date, minDate, maxDate) => {
|
|
260
|
+
const checkDateViolations = (0, _react.useCallback)((date, minDate, maxDate) => {
|
|
259
261
|
const violations = [];
|
|
260
262
|
const inputDate = (0, _moment.default)(date);
|
|
261
263
|
if (!inputDate.isValid()) return violations;
|
|
@@ -272,16 +274,16 @@ const DatePicker = exports.DatePicker = /*#__PURE__*/(0, _react.forwardRef)((_re
|
|
|
272
274
|
}
|
|
273
275
|
}
|
|
274
276
|
return violations;
|
|
275
|
-
};
|
|
277
|
+
}, []);
|
|
276
278
|
// ──────────────────────────────────────────────
|
|
277
279
|
// 이벤트 핸들러
|
|
278
280
|
// ──────────────────────────────────────────────
|
|
279
281
|
/** 유효하지 않은 입력 시 이전 날짜로 복원 */
|
|
280
|
-
const restorePreviousDate = (target, instance) => {
|
|
282
|
+
const restorePreviousDate = (0, _react.useCallback)((target, instance) => {
|
|
281
283
|
if (instance.selectedDates.length > 0) {
|
|
282
284
|
const prevDate = instance.selectedDates[0];
|
|
283
285
|
if (prevDate instanceof Date && !Number.isNaN(prevDate.getTime())) {
|
|
284
|
-
const momentFormat =
|
|
286
|
+
const momentFormat = (0, _datePicker.convertToMomentFormat)(dateFormatRef.current);
|
|
285
287
|
target.value = (0, _moment.default)(prevDate).format(momentFormat);
|
|
286
288
|
instance.setDate(prevDate, false);
|
|
287
289
|
return;
|
|
@@ -289,7 +291,7 @@ const DatePicker = exports.DatePicker = /*#__PURE__*/(0, _react.forwardRef)((_re
|
|
|
289
291
|
}
|
|
290
292
|
target.value = '';
|
|
291
293
|
instance.setDate('', false);
|
|
292
|
-
};
|
|
294
|
+
}, []);
|
|
293
295
|
/** flatpickr에서 날짜가 변경되었을 때 호출 */
|
|
294
296
|
const onChangeDateHandler = (0, _react.useCallback)((dateTimeStamp, dateStr, fpInstance) => {
|
|
295
297
|
const instance = fpInstance;
|
|
@@ -303,7 +305,9 @@ const DatePicker = exports.DatePicker = /*#__PURE__*/(0, _react.forwardRef)((_re
|
|
|
303
305
|
const maxDate = instance.config.maxDate;
|
|
304
306
|
const violations = checkDateViolations(selectedDate, minDate, maxDate);
|
|
305
307
|
if (violations.length > 0 && onValidationErrorRef.current) {
|
|
306
|
-
|
|
308
|
+
// flatpickr는 onChange 발화 전에 selectedDates를 위반된 새 날짜로 갱신하므로,
|
|
309
|
+
// 직전 유효 날짜를 보관해 둔 _previousDateBeforeInput을 previousDate로 사용한다
|
|
310
|
+
const prevDate = instance._previousDateBeforeInput;
|
|
307
311
|
const validPrevDate = prevDate instanceof Date && !Number.isNaN(prevDate.getTime()) ? prevDate : undefined;
|
|
308
312
|
onValidationErrorRef.current({
|
|
309
313
|
date: selectedDate,
|
|
@@ -319,7 +323,6 @@ const DatePicker = exports.DatePicker = /*#__PURE__*/(0, _react.forwardRef)((_re
|
|
|
319
323
|
isValidDate(formattedDate) ? onChangeDateRef.current(formattedDate) : onChangeDateRef.current(dateStr);
|
|
320
324
|
}, [checkDateViolations, restorePreviousDate]);
|
|
321
325
|
/** input에 직접 타이핑할 때 날짜 형식 자동 변환 및 유효성 검사 */
|
|
322
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
323
326
|
const onInputHandler = (0, _react.useCallback)(e => {
|
|
324
327
|
const target = e.target;
|
|
325
328
|
const input = target.value;
|
|
@@ -372,12 +375,6 @@ const DatePicker = exports.DatePicker = /*#__PURE__*/(0, _react.forwardRef)((_re
|
|
|
372
375
|
// ──────────────────────────────────────────────
|
|
373
376
|
// flatpickr 옵션
|
|
374
377
|
// ──────────────────────────────────────────────
|
|
375
|
-
// datePickerOptions는 부모에서 매 렌더 새 객체일 수 있으므로 값 기반 비교
|
|
376
|
-
// 주의: JSON.stringify는 함수를 무시하고 Date를 문자열로 변환하므로,
|
|
377
|
-
// datePickerOptions에 함수나 Date 객체를 넘기면 변경 감지가 누락될 수 있음
|
|
378
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
379
|
-
const _datePickerOptionsKey = JSON.stringify(datePickerOptions);
|
|
380
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
381
378
|
const options = (0, _react.useMemo)(() => ({
|
|
382
379
|
mode: 'single',
|
|
383
380
|
static: !portal,
|
|
@@ -398,7 +395,7 @@ const DatePicker = exports.DatePicker = /*#__PURE__*/(0, _react.forwardRef)((_re
|
|
|
398
395
|
if (!momentDate.isValid()) {
|
|
399
396
|
return '';
|
|
400
397
|
}
|
|
401
|
-
const momentFormat =
|
|
398
|
+
const momentFormat = (0, _datePicker.convertToMomentFormat)(format);
|
|
402
399
|
return momentDate.format(momentFormat);
|
|
403
400
|
} catch (_error) {
|
|
404
401
|
return '';
|
|
@@ -0,0 +1,53 @@
|
|
|
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 _DatePicker = require("../DatePicker");
|
|
8
|
+
// @vitest-environment jsdom
|
|
9
|
+
|
|
10
|
+
const {
|
|
11
|
+
captured
|
|
12
|
+
} = _vitest.vi.hoisted(() => ({
|
|
13
|
+
captured: []
|
|
14
|
+
}));
|
|
15
|
+
// react-flatpickr를 mock하여 DatePicker가 넘기는 options 참조를 렌더마다 수집한다.
|
|
16
|
+
// (vi.mock / vi.hoisted는 vitest가 import 위로 호이스팅하므로 DatePicker import보다 먼저 적용된다)
|
|
17
|
+
_vitest.vi.mock('react-flatpickr', () => ({
|
|
18
|
+
default: props => {
|
|
19
|
+
captured.push(props.options);
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
}));
|
|
23
|
+
(0, _vitest.describe)('#1 options useMemo 안정성', () => {
|
|
24
|
+
(0, _vitest.it)('부모가 같은 props로 강제 리렌더해도 options 참조가 동일하게 유지된다(재생성 안 됨)', () => {
|
|
25
|
+
let bump = () => undefined;
|
|
26
|
+
const Harness = () => {
|
|
27
|
+
const [, setN] = (0, _react.useState)(0);
|
|
28
|
+
bump = () => setN(x => x + 1);
|
|
29
|
+
return /*#__PURE__*/(0, _react.createElement)(_DatePicker.DatePicker, {
|
|
30
|
+
currentDate: '2024-03-20',
|
|
31
|
+
onChangeDate: () => undefined
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
const container = document.createElement('div');
|
|
35
|
+
document.body.appendChild(container);
|
|
36
|
+
const root = (0, _client.createRoot)(container);
|
|
37
|
+
(0, _testUtils.act)(() => {
|
|
38
|
+
root.render(/*#__PURE__*/(0, _react.createElement)(Harness));
|
|
39
|
+
});
|
|
40
|
+
const countAfterMount = captured.length;
|
|
41
|
+
const firstOptions = captured[captured.length - 1];
|
|
42
|
+
(0, _testUtils.act)(() => {
|
|
43
|
+
bump();
|
|
44
|
+
});
|
|
45
|
+
const lastOptions = captured[captured.length - 1];
|
|
46
|
+
(0, _vitest.expect)(captured.length).toBeGreaterThan(countAfterMount); // 리렌더가 실제로 일어났는지
|
|
47
|
+
(0, _vitest.expect)(Object.is(firstOptions, lastOptions)).toBe(true); // 동일 참조 = options 재생성 안 됨
|
|
48
|
+
(0, _testUtils.act)(() => {
|
|
49
|
+
root.unmount();
|
|
50
|
+
});
|
|
51
|
+
container.remove();
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -0,0 +1,129 @@
|
|
|
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 _DatePicker = require("../DatePicker");
|
|
8
|
+
// @vitest-environment jsdom
|
|
9
|
+
|
|
10
|
+
const mounted = [];
|
|
11
|
+
function mount(props) {
|
|
12
|
+
const container = document.createElement('div');
|
|
13
|
+
document.body.appendChild(container);
|
|
14
|
+
const root = (0, _client.createRoot)(container);
|
|
15
|
+
(0, _testUtils.act)(() => {
|
|
16
|
+
root.render(/*#__PURE__*/(0, _react.createElement)(_DatePicker.DatePicker, props));
|
|
17
|
+
});
|
|
18
|
+
mounted.push({
|
|
19
|
+
root,
|
|
20
|
+
container
|
|
21
|
+
});
|
|
22
|
+
return container;
|
|
23
|
+
}
|
|
24
|
+
(0, _vitest.afterEach)(() => {
|
|
25
|
+
for (const {
|
|
26
|
+
root,
|
|
27
|
+
container
|
|
28
|
+
} of mounted.splice(0)) {
|
|
29
|
+
(0, _testUtils.act)(() => {
|
|
30
|
+
root.unmount();
|
|
31
|
+
});
|
|
32
|
+
container.remove();
|
|
33
|
+
}
|
|
34
|
+
_vitest.vi.restoreAllMocks();
|
|
35
|
+
});
|
|
36
|
+
(0, _vitest.describe)('DatePicker 렌더 (스모크)', () => {
|
|
37
|
+
(0, _vitest.it)('기본 props로 input을 렌더하고 크래시하지 않는다', () => {
|
|
38
|
+
const container = mount({
|
|
39
|
+
currentDate: '2024-03-20',
|
|
40
|
+
onChangeDate: () => undefined
|
|
41
|
+
});
|
|
42
|
+
(0, _vitest.expect)(container.querySelector('input')).not.toBeNull();
|
|
43
|
+
});
|
|
44
|
+
(0, _vitest.it)('enableTime 옵션으로도 정상 렌더한다', () => {
|
|
45
|
+
const container = mount({
|
|
46
|
+
currentDate: '2024-03-20 14:30',
|
|
47
|
+
onChangeDate: () => undefined,
|
|
48
|
+
datePickerOptions: {
|
|
49
|
+
enableTime: true,
|
|
50
|
+
dateFormat: 'Y-m-d H:i'
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
(0, _vitest.expect)(container.querySelector('input')).not.toBeNull();
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
(0, _vitest.describe)('#4 document mousedown 리스너 정리', () => {
|
|
57
|
+
(0, _vitest.it)('enableTime 마운트 후 언마운트 시, 추가된 mousedown 리스너가 모두 제거된다(누수 없음)', () => {
|
|
58
|
+
const added = [];
|
|
59
|
+
const removed = [];
|
|
60
|
+
const origAdd = document.addEventListener.bind(document);
|
|
61
|
+
const origRemove = document.removeEventListener.bind(document);
|
|
62
|
+
_vitest.vi.spyOn(document, 'addEventListener').mockImplementation((type, fn, opts) => {
|
|
63
|
+
if (type === 'mousedown' && fn) added.push(fn);
|
|
64
|
+
return origAdd(type, fn, opts);
|
|
65
|
+
});
|
|
66
|
+
_vitest.vi.spyOn(document, 'removeEventListener').mockImplementation((type, fn, opts) => {
|
|
67
|
+
if (type === 'mousedown' && fn) removed.push(fn);
|
|
68
|
+
return origRemove(type, fn, opts);
|
|
69
|
+
});
|
|
70
|
+
const container = document.createElement('div');
|
|
71
|
+
document.body.appendChild(container);
|
|
72
|
+
const root = (0, _client.createRoot)(container);
|
|
73
|
+
(0, _testUtils.act)(() => {
|
|
74
|
+
root.render(/*#__PURE__*/(0, _react.createElement)(_DatePicker.DatePicker, {
|
|
75
|
+
currentDate: '2024-03-20 14:30',
|
|
76
|
+
onChangeDate: () => undefined,
|
|
77
|
+
datePickerOptions: {
|
|
78
|
+
enableTime: true,
|
|
79
|
+
dateFormat: 'Y-m-d H:i'
|
|
80
|
+
}
|
|
81
|
+
}));
|
|
82
|
+
});
|
|
83
|
+
(0, _vitest.expect)(added.length).toBeGreaterThanOrEqual(1);
|
|
84
|
+
(0, _testUtils.act)(() => {
|
|
85
|
+
root.unmount();
|
|
86
|
+
});
|
|
87
|
+
container.remove();
|
|
88
|
+
for (const fn of added) {
|
|
89
|
+
(0, _vitest.expect)(removed).toContain(fn);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
(0, _vitest.describe)('#3 onValidationError.previousDate', () => {
|
|
94
|
+
// 허용 범위(10~25일) 기준: 마지막 유효 선택일과 범위 밖 위반일
|
|
95
|
+
const LAST_VALID_DAY = 15;
|
|
96
|
+
const OUT_OF_RANGE_DAY = 30;
|
|
97
|
+
(0, _vitest.it)('min/max 위반 시 previousDate는 위반 날짜가 아니라 직전 유효 날짜다', () => {
|
|
98
|
+
const onValidationError = _vitest.vi.fn();
|
|
99
|
+
const container = mount({
|
|
100
|
+
currentDate: '2024-03-20',
|
|
101
|
+
onChangeDate: () => undefined,
|
|
102
|
+
datePickerOptions: {
|
|
103
|
+
minDate: '2024-03-10',
|
|
104
|
+
maxDate: '2024-03-25',
|
|
105
|
+
allowInput: true
|
|
106
|
+
},
|
|
107
|
+
onValidationError
|
|
108
|
+
});
|
|
109
|
+
// flatpickr 인스턴스는 초기화된 input 엘리먼트의 _flatpickr에 부착된다
|
|
110
|
+
const input = container.querySelector('input');
|
|
111
|
+
const instance = input?._flatpickr;
|
|
112
|
+
(0, _vitest.expect)(instance).toBeTruthy();
|
|
113
|
+
// 1) 유효 날짜를 먼저 선택 → 직전 유효값(_previousDateBeforeInput)이 확립됨
|
|
114
|
+
(0, _testUtils.act)(() => {
|
|
115
|
+
instance.setDate(`2024-03-${LAST_VALID_DAY}`, true);
|
|
116
|
+
});
|
|
117
|
+
(0, _vitest.expect)(onValidationError).not.toHaveBeenCalled();
|
|
118
|
+
// 2) 허용 범위 밖 날짜 선택 → 위반 분기 진입
|
|
119
|
+
(0, _testUtils.act)(() => {
|
|
120
|
+
instance.setDate(`2024-03-${OUT_OF_RANGE_DAY}`, true);
|
|
121
|
+
});
|
|
122
|
+
(0, _vitest.expect)(onValidationError).toHaveBeenCalledTimes(1);
|
|
123
|
+
const arg = onValidationError.mock.calls[0][0];
|
|
124
|
+
(0, _vitest.expect)(arg.violations).toContain('maxDate');
|
|
125
|
+
(0, _vitest.expect)(arg.date.getDate()).toBe(OUT_OF_RANGE_DAY); // 위반 날짜
|
|
126
|
+
(0, _vitest.expect)(arg.previousDate?.getDate()).toBe(LAST_VALID_DAY); // 직전 유효 날짜 (≠ 위반 날짜)
|
|
127
|
+
(0, _vitest.expect)(arg.previousDate?.getTime()).not.toBe(arg.date.getTime());
|
|
128
|
+
});
|
|
129
|
+
});
|
|
@@ -24,12 +24,14 @@ const Select = exports.Select = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref)
|
|
|
24
24
|
optionItems,
|
|
25
25
|
register,
|
|
26
26
|
disabled = false,
|
|
27
|
+
readOnly = false,
|
|
27
28
|
...props
|
|
28
29
|
} = _ref;
|
|
29
30
|
return (0, _jsxRuntime.jsxs)("span", {
|
|
30
31
|
className: (0, _classnames.default)('ncua-select', {
|
|
31
32
|
destructive: destructive,
|
|
32
|
-
'ncua-select--simple': type === 'simple'
|
|
33
|
+
'ncua-select--simple': type === 'simple',
|
|
34
|
+
'ncua-select--readonly': readOnly
|
|
33
35
|
}, className, `ncua-select--${size}`),
|
|
34
36
|
children: [(0, _jsxRuntime.jsx)("span", {
|
|
35
37
|
className: "ncua-select__content",
|
|
@@ -39,8 +41,17 @@ const Select = exports.Select = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref)
|
|
|
39
41
|
id: id,
|
|
40
42
|
className: (0, _classnames.default)('ncua-select__tag', className),
|
|
41
43
|
disabled: disabled,
|
|
44
|
+
"aria-readonly": readOnly,
|
|
42
45
|
...register,
|
|
43
46
|
...props,
|
|
47
|
+
onKeyDown: e => {
|
|
48
|
+
props.onKeyDown?.(e);
|
|
49
|
+
// native select에는 readonly 속성이 없어 키보드로 값이 바뀐다.
|
|
50
|
+
// 값 변경 키는 막고, Tab/Shift+Tab(포커스 이동)은 허용한다.
|
|
51
|
+
if (readOnly && e.key !== 'Tab') {
|
|
52
|
+
e.preventDefault();
|
|
53
|
+
}
|
|
54
|
+
},
|
|
44
55
|
children: [placeholder && (0, _jsxRuntime.jsx)("option", {
|
|
45
56
|
value: "",
|
|
46
57
|
disabled: disabledPlaceholder,
|
|
@@ -78,6 +78,7 @@ const SelectBox = exports.SelectBox = /*#__PURE__*/(0, _react.forwardRef)((_ref2
|
|
|
78
78
|
value,
|
|
79
79
|
optionItems = [],
|
|
80
80
|
disabled = false,
|
|
81
|
+
readOnly = false,
|
|
81
82
|
maxHeight = DEFAULT_MAX_HEIGHT,
|
|
82
83
|
multiple = false,
|
|
83
84
|
maxSelection,
|
|
@@ -110,7 +111,7 @@ const SelectBox = exports.SelectBox = /*#__PURE__*/(0, _react.forwardRef)((_ref2
|
|
|
110
111
|
return selectedOption ? selectedOption : placeholder;
|
|
111
112
|
}, [multiple, selectedOption, placeholder]);
|
|
112
113
|
const handleOptionSelect = option => {
|
|
113
|
-
if (disabled) return;
|
|
114
|
+
if (disabled || readOnly) return;
|
|
114
115
|
if (multiple) {
|
|
115
116
|
const newValue = tryToggle(option.id, Array.isArray(value) ? value : []);
|
|
116
117
|
if (newValue === null) return;
|
|
@@ -139,7 +140,7 @@ const SelectBox = exports.SelectBox = /*#__PURE__*/(0, _react.forwardRef)((_ref2
|
|
|
139
140
|
maxHeight,
|
|
140
141
|
itemCount: optionItems.length,
|
|
141
142
|
optionItems,
|
|
142
|
-
disabled,
|
|
143
|
+
disabled: disabled || readOnly,
|
|
143
144
|
multiple,
|
|
144
145
|
onSelect: handleOptionSelect
|
|
145
146
|
});
|
|
@@ -244,6 +245,7 @@ const SelectBox = exports.SelectBox = /*#__PURE__*/(0, _react.forwardRef)((_ref2
|
|
|
244
245
|
className: (0, _classnames.default)('ncua-selectbox', `ncua-selectbox--${size}`, {
|
|
245
246
|
'ncua-selectbox--open': isOpen,
|
|
246
247
|
'ncua-selectbox--disabled': disabled,
|
|
248
|
+
'ncua-selectbox--readonly': readOnly,
|
|
247
249
|
'ncua-selectbox--simple': type === 'simple',
|
|
248
250
|
'ncua-selectbox--multiple': multiple,
|
|
249
251
|
destructive: destructive
|
|
@@ -259,6 +261,7 @@ const SelectBox = exports.SelectBox = /*#__PURE__*/(0, _react.forwardRef)((_ref2
|
|
|
259
261
|
"aria-haspopup": "listbox",
|
|
260
262
|
"aria-controls": `selectbox-options-${id || 'default'}`,
|
|
261
263
|
"aria-disabled": disabled,
|
|
264
|
+
"aria-readonly": readOnly,
|
|
262
265
|
"aria-label": selectedOption ? selectedOption.label : placeholder,
|
|
263
266
|
"aria-activedescendant": activeDescendantId,
|
|
264
267
|
children: [(0, _jsxRuntime.jsxs)("div", {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _moment = _interopRequireDefault(require("moment"));
|
|
4
|
+
var _vitest = require("vitest");
|
|
5
|
+
var _datePicker = require("../date-picker");
|
|
6
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
|
+
(0, _vitest.describe)('convertToMomentFormat', () => {
|
|
8
|
+
(0, _vitest.it)('날짜 전용 포맷을 moment 토큰으로 변환한다 (기존 동작 보존)', () => {
|
|
9
|
+
(0, _vitest.expect)((0, _datePicker.convertToMomentFormat)('Y-m-d')).toBe('YYYY-MM-DD');
|
|
10
|
+
});
|
|
11
|
+
(0, _vitest.it)('2자리 연도(y)를 YY로 변환한다', () => {
|
|
12
|
+
(0, _vitest.expect)((0, _datePicker.convertToMomentFormat)('y-m-d')).toBe('YY-MM-DD');
|
|
13
|
+
});
|
|
14
|
+
(0, _vitest.it)('시간 포함 포맷의 H/i/S를 모두 변환한다 (버그 수정 지점)', () => {
|
|
15
|
+
(0, _vitest.expect)((0, _datePicker.convertToMomentFormat)('Y-m-d H:i')).toBe('YYYY-MM-DD HH:mm');
|
|
16
|
+
(0, _vitest.expect)((0, _datePicker.convertToMomentFormat)('Y-m-d H:i:S')).toBe('YYYY-MM-DD HH:mm:ss');
|
|
17
|
+
});
|
|
18
|
+
(0, _vitest.it)('월(m)과 분(i)이 함께 있어도 서로 침범하지 않는다', () => {
|
|
19
|
+
// m -> MM(월) 먼저, i -> mm(분) 나중 순서가 지켜져야 함
|
|
20
|
+
(0, _vitest.expect)((0, _datePicker.convertToMomentFormat)('Y-m-d H:i')).not.toContain('i');
|
|
21
|
+
(0, _vitest.expect)((0, _datePicker.convertToMomentFormat)('Y-m-d H:i')).toBe('YYYY-MM-DD HH:mm');
|
|
22
|
+
});
|
|
23
|
+
(0, _vitest.it)('실제 moment 출력이 의도한 문자열과 일치한다 (회귀 핵심)', () => {
|
|
24
|
+
const date = (0, _moment.default)('2024-03-20 14:30:05', 'YYYY-MM-DD HH:mm:ss').toDate();
|
|
25
|
+
(0, _vitest.expect)((0, _moment.default)(date).format((0, _datePicker.convertToMomentFormat)('Y-m-d'))).toBe('2024-03-20');
|
|
26
|
+
(0, _vitest.expect)((0, _moment.default)(date).format((0, _datePicker.convertToMomentFormat)('Y-m-d H:i'))).toBe('2024-03-20 14:30');
|
|
27
|
+
(0, _vitest.expect)((0, _moment.default)(date).format((0, _datePicker.convertToMomentFormat)('Y-m-d H:i:S'))).toBe('2024-03-20 14:30:05');
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.formatMinuteInput = exports.formatHourInput = exports.formatDateInput = void 0;
|
|
6
|
+
exports.formatMinuteInput = exports.formatHourInput = exports.formatDateInput = exports.convertToMomentFormat = void 0;
|
|
7
7
|
exports.getDateFormat = getDateFormat;
|
|
8
8
|
exports.getSubtractDate = getSubtractDate;
|
|
9
9
|
exports.getToday = getToday;
|
|
@@ -31,6 +31,20 @@ function getSubtractDate(_ref) {
|
|
|
31
31
|
} = _ref;
|
|
32
32
|
return (0, _moment.default)(date).subtract(period, unit).toDate();
|
|
33
33
|
}
|
|
34
|
+
/** flatpickr dateFormat 토큰을 moment 포맷 토큰으로 변환 (단일 패스 치환으로 토큰 간 간섭 방지) */
|
|
35
|
+
const convertToMomentFormat = format => {
|
|
36
|
+
const tokenMap = {
|
|
37
|
+
Y: 'YYYY',
|
|
38
|
+
y: 'YY',
|
|
39
|
+
m: 'MM',
|
|
40
|
+
d: 'DD',
|
|
41
|
+
H: 'HH',
|
|
42
|
+
i: 'mm',
|
|
43
|
+
S: 'ss'
|
|
44
|
+
};
|
|
45
|
+
return format.replace(/[YymdHiS]/g, matched => tokenMap[matched] || matched);
|
|
46
|
+
};
|
|
47
|
+
exports.convertToMomentFormat = convertToMomentFormat;
|
|
34
48
|
const formatDateInput = input => {
|
|
35
49
|
const dateRegex = /^(19|20)\d{2}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[0-1])$/;
|
|
36
50
|
if (dateRegex.test(input)) {
|
|
@@ -4,7 +4,7 @@ import { Korean } from 'flatpickr/dist/l10n/ko';
|
|
|
4
4
|
import moment from 'moment';
|
|
5
5
|
import { forwardRef, useCallback, useEffect, useId, useMemo, useRef } from 'react';
|
|
6
6
|
import Flatpickr from 'react-flatpickr';
|
|
7
|
-
import { formatDateInput, formatHourInput, formatMinuteInput } from '../../../utils/date-picker';
|
|
7
|
+
import { convertToMomentFormat, formatDateInput, formatHourInput, formatMinuteInput } from '../../../utils/date-picker';
|
|
8
8
|
import { CustomInput } from './CustomInput';
|
|
9
9
|
// ══════════════════════════════════════════
|
|
10
10
|
// 유틸리티 함수
|
|
@@ -176,6 +176,12 @@ const setupTimeInputHandlers = (instance, input, isPortal, onHourInput, onMinute
|
|
|
176
176
|
};
|
|
177
177
|
/** 시간 입력 필드 이벤트 리스너 정리 */
|
|
178
178
|
const cleanupTimeInputHandlers = (instance, input, isPortal, onHourInput, onMinuteInput) => {
|
|
179
|
+
// document 전역 리스너는 캘린더 DOM 조회 성공 여부와 무관하게 항상 먼저 제거한다 (누수 방지)
|
|
180
|
+
const handleMouseDown = instance._handleMouseDown;
|
|
181
|
+
if (handleMouseDown) {
|
|
182
|
+
document.removeEventListener('mousedown', handleMouseDown, true);
|
|
183
|
+
instance._handleMouseDown = undefined;
|
|
184
|
+
}
|
|
179
185
|
const timeContainer = isPortal ? instance.calendarContainer : input.parentElement;
|
|
180
186
|
const timeInputWrapper = timeContainer?.querySelector('.flatpickr-time');
|
|
181
187
|
if (!timeInputWrapper) return;
|
|
@@ -187,10 +193,6 @@ const cleanupTimeInputHandlers = (instance, input, isPortal, onHourInput, onMinu
|
|
|
187
193
|
if (minuteInput) {
|
|
188
194
|
minuteInput.removeEventListener('input', onMinuteInput);
|
|
189
195
|
}
|
|
190
|
-
const handleMouseDown = instance._handleMouseDown;
|
|
191
|
-
if (handleMouseDown) {
|
|
192
|
-
document.removeEventListener('mousedown', handleMouseDown, true);
|
|
193
|
-
}
|
|
194
196
|
};
|
|
195
197
|
// ══════════════════════════════════════════
|
|
196
198
|
// DatePicker 컴포넌트
|
|
@@ -220,7 +222,7 @@ export const DatePicker = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
220
222
|
const onValidationErrorRef = useRef(onValidationError);
|
|
221
223
|
onValidationErrorRef.current = onValidationError;
|
|
222
224
|
/** portal 컨테이너를 lazily 생성 (이미 있으면 재사용), className은 매번 갱신 */
|
|
223
|
-
const getPortalContainer = () => {
|
|
225
|
+
const getPortalContainer = useCallback(() => {
|
|
224
226
|
if (!portal || typeof document === 'undefined') return undefined;
|
|
225
227
|
const portalClassName = classNames('ncua-date-picker', 'ncua-date-picker--portal', `ncua-date-picker--${size}`, {
|
|
226
228
|
'ncua-date-picker--has-time': hasTimeOption
|
|
@@ -236,7 +238,7 @@ export const DatePicker = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
236
238
|
document.body.appendChild(el);
|
|
237
239
|
portalContainerRef.current = el;
|
|
238
240
|
return el;
|
|
239
|
-
};
|
|
241
|
+
}, [portal, size, hasTimeOption]);
|
|
240
242
|
/** 컴포넌트 언마운트 시 포탈 컨테이너 및 스크롤 리스너 정리 */
|
|
241
243
|
useEffect(() => {
|
|
242
244
|
return () => {
|
|
@@ -248,7 +250,7 @@ export const DatePicker = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
248
250
|
// 날짜 유효성 검사
|
|
249
251
|
// ──────────────────────────────────────────────
|
|
250
252
|
/** minDate/maxDate 범위를 벗어났는지 확인 */
|
|
251
|
-
const checkDateViolations = (date, minDate, maxDate) => {
|
|
253
|
+
const checkDateViolations = useCallback((date, minDate, maxDate) => {
|
|
252
254
|
const violations = [];
|
|
253
255
|
const inputDate = moment(date);
|
|
254
256
|
if (!inputDate.isValid()) return violations;
|
|
@@ -265,16 +267,16 @@ export const DatePicker = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
265
267
|
}
|
|
266
268
|
}
|
|
267
269
|
return violations;
|
|
268
|
-
};
|
|
270
|
+
}, []);
|
|
269
271
|
// ──────────────────────────────────────────────
|
|
270
272
|
// 이벤트 핸들러
|
|
271
273
|
// ──────────────────────────────────────────────
|
|
272
274
|
/** 유효하지 않은 입력 시 이전 날짜로 복원 */
|
|
273
|
-
const restorePreviousDate = (target, instance) => {
|
|
275
|
+
const restorePreviousDate = useCallback((target, instance) => {
|
|
274
276
|
if (instance.selectedDates.length > 0) {
|
|
275
277
|
const prevDate = instance.selectedDates[0];
|
|
276
278
|
if (prevDate instanceof Date && !Number.isNaN(prevDate.getTime())) {
|
|
277
|
-
const momentFormat = dateFormatRef.current
|
|
279
|
+
const momentFormat = convertToMomentFormat(dateFormatRef.current);
|
|
278
280
|
target.value = moment(prevDate).format(momentFormat);
|
|
279
281
|
instance.setDate(prevDate, false);
|
|
280
282
|
return;
|
|
@@ -282,7 +284,7 @@ export const DatePicker = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
282
284
|
}
|
|
283
285
|
target.value = '';
|
|
284
286
|
instance.setDate('', false);
|
|
285
|
-
};
|
|
287
|
+
}, []);
|
|
286
288
|
/** flatpickr에서 날짜가 변경되었을 때 호출 */
|
|
287
289
|
const onChangeDateHandler = useCallback((dateTimeStamp, dateStr, fpInstance) => {
|
|
288
290
|
const instance = fpInstance;
|
|
@@ -296,7 +298,9 @@ export const DatePicker = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
296
298
|
const maxDate = instance.config.maxDate;
|
|
297
299
|
const violations = checkDateViolations(selectedDate, minDate, maxDate);
|
|
298
300
|
if (violations.length > 0 && onValidationErrorRef.current) {
|
|
299
|
-
|
|
301
|
+
// flatpickr는 onChange 발화 전에 selectedDates를 위반된 새 날짜로 갱신하므로,
|
|
302
|
+
// 직전 유효 날짜를 보관해 둔 _previousDateBeforeInput을 previousDate로 사용한다
|
|
303
|
+
const prevDate = instance._previousDateBeforeInput;
|
|
300
304
|
const validPrevDate = prevDate instanceof Date && !Number.isNaN(prevDate.getTime()) ? prevDate : undefined;
|
|
301
305
|
onValidationErrorRef.current({
|
|
302
306
|
date: selectedDate,
|
|
@@ -312,7 +316,6 @@ export const DatePicker = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
312
316
|
isValidDate(formattedDate) ? onChangeDateRef.current(formattedDate) : onChangeDateRef.current(dateStr);
|
|
313
317
|
}, [checkDateViolations, restorePreviousDate]);
|
|
314
318
|
/** input에 직접 타이핑할 때 날짜 형식 자동 변환 및 유효성 검사 */
|
|
315
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
316
319
|
const onInputHandler = useCallback(e => {
|
|
317
320
|
const target = e.target;
|
|
318
321
|
const input = target.value;
|
|
@@ -365,12 +368,6 @@ export const DatePicker = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
365
368
|
// ──────────────────────────────────────────────
|
|
366
369
|
// flatpickr 옵션
|
|
367
370
|
// ──────────────────────────────────────────────
|
|
368
|
-
// datePickerOptions는 부모에서 매 렌더 새 객체일 수 있으므로 값 기반 비교
|
|
369
|
-
// 주의: JSON.stringify는 함수를 무시하고 Date를 문자열로 변환하므로,
|
|
370
|
-
// datePickerOptions에 함수나 Date 객체를 넘기면 변경 감지가 누락될 수 있음
|
|
371
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
372
|
-
const _datePickerOptionsKey = JSON.stringify(datePickerOptions);
|
|
373
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
374
371
|
const options = useMemo(() => ({
|
|
375
372
|
mode: 'single',
|
|
376
373
|
static: !portal,
|
|
@@ -391,7 +388,7 @@ export const DatePicker = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
391
388
|
if (!momentDate.isValid()) {
|
|
392
389
|
return '';
|
|
393
390
|
}
|
|
394
|
-
const momentFormat = format
|
|
391
|
+
const momentFormat = convertToMomentFormat(format);
|
|
395
392
|
return momentDate.format(momentFormat);
|
|
396
393
|
} catch (_error) {
|
|
397
394
|
return '';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { createElement, useState } from 'react';
|
|
3
|
+
import { createRoot } from 'react-dom/client';
|
|
4
|
+
import { act } from 'react-dom/test-utils';
|
|
5
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
6
|
+
import { DatePicker } from '../DatePicker';
|
|
7
|
+
const {
|
|
8
|
+
captured
|
|
9
|
+
} = vi.hoisted(() => ({
|
|
10
|
+
captured: []
|
|
11
|
+
}));
|
|
12
|
+
// react-flatpickr를 mock하여 DatePicker가 넘기는 options 참조를 렌더마다 수집한다.
|
|
13
|
+
// (vi.mock / vi.hoisted는 vitest가 import 위로 호이스팅하므로 DatePicker import보다 먼저 적용된다)
|
|
14
|
+
vi.mock('react-flatpickr', () => ({
|
|
15
|
+
default: props => {
|
|
16
|
+
captured.push(props.options);
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}));
|
|
20
|
+
describe('#1 options useMemo 안정성', () => {
|
|
21
|
+
it('부모가 같은 props로 강제 리렌더해도 options 참조가 동일하게 유지된다(재생성 안 됨)', () => {
|
|
22
|
+
let bump = () => undefined;
|
|
23
|
+
const Harness = () => {
|
|
24
|
+
const [, setN] = useState(0);
|
|
25
|
+
bump = () => setN(x => x + 1);
|
|
26
|
+
return /*#__PURE__*/createElement(DatePicker, {
|
|
27
|
+
currentDate: '2024-03-20',
|
|
28
|
+
onChangeDate: () => undefined
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
const container = document.createElement('div');
|
|
32
|
+
document.body.appendChild(container);
|
|
33
|
+
const root = createRoot(container);
|
|
34
|
+
act(() => {
|
|
35
|
+
root.render(/*#__PURE__*/createElement(Harness));
|
|
36
|
+
});
|
|
37
|
+
const countAfterMount = captured.length;
|
|
38
|
+
const firstOptions = captured[captured.length - 1];
|
|
39
|
+
act(() => {
|
|
40
|
+
bump();
|
|
41
|
+
});
|
|
42
|
+
const lastOptions = captured[captured.length - 1];
|
|
43
|
+
expect(captured.length).toBeGreaterThan(countAfterMount); // 리렌더가 실제로 일어났는지
|
|
44
|
+
expect(Object.is(firstOptions, lastOptions)).toBe(true); // 동일 참조 = options 재생성 안 됨
|
|
45
|
+
act(() => {
|
|
46
|
+
root.unmount();
|
|
47
|
+
});
|
|
48
|
+
container.remove();
|
|
49
|
+
});
|
|
50
|
+
});
|