@tide-design/components 0.24.0 → 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/DatePicker/DatePicker.d.ts +43 -0
- package/dist/DatePicker/DatePicker.js +20 -0
- package/dist/DatePicker/components/Calendar.js +152 -0
- package/dist/DatePicker/components/CalendarCell.js +33 -0
- package/dist/DatePicker/components/CalendarGrid.js +28 -0
- package/dist/DatePicker/components/CalendarMonthView.js +82 -0
- package/dist/DatePicker/components/CalendarNavButton.js +24 -0
- package/dist/DatePicker/components/CalendarPickerCell.js +39 -0
- package/dist/DatePicker/components/CalendarPickerGrid.js +46 -0
- package/dist/DatePicker/components/CalendarTitle.js +33 -0
- package/dist/DatePicker/components/CalendarYearView.js +64 -0
- package/dist/DatePicker/components/DatePickerGroup.js +121 -0
- package/dist/DatePicker/components/DatePickerPopover.js +18 -0
- package/dist/DatePicker/components/DatePickerPopoverInner.js +57 -0
- package/dist/DatePicker/components/DateSegment.js +26 -0
- package/dist/DatePicker/components/DateSegments.js +62 -0
- package/dist/DatePicker/constants/calendarClass.js +41 -0
- package/dist/DatePicker/constants/calendarPicker.js +4 -0
- package/dist/DatePicker/constants/dateFormat.js +5 -0
- package/dist/DatePicker/constants/datePickerClass.js +27 -0
- package/dist/DatePicker/hooks/useOpenOnFocus.js +69 -0
- package/dist/DatePicker/hooks/useSegmentEntry.js +53 -0
- package/dist/DatePicker/index.d.ts +2 -0
- package/dist/DatePicker/libs/calendarPicker.js +64 -0
- package/dist/DatePicker/libs/dateFormat.js +45 -0
- package/dist/DatePicker/libs/isoDate.js +23 -0
- package/dist/DatePicker/libs/segmentEntry.js +19 -0
- package/dist/DatePicker/libs/warnOnce.js +17 -0
- package/dist/Field/Field.d.ts +1 -1
- package/dist/Field/Field.js +4 -12
- package/dist/Field/components/FieldLabelledControl.js +40 -0
- package/dist/NumberFormatInput/NumberFormatInput.d.ts +5 -5
- package/dist/NumberFormatInput/NumberFormatInput.js +4 -2
- package/dist/NumberFormatInput/libs/numericFormatter.js +9 -2
- package/dist/NumberFormatInput/types.d.ts +10 -0
- package/dist/_internal/components/OverlayPortal.js +2 -1
- package/dist/_internal/constants/inputClass.js +6 -1
- package/dist/_internal/hooks/useFieldGroupControl.js +30 -0
- package/dist/_internal/hooks/usePopoverOverlay.js +3 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +2 -1
- package/package.json +4 -3
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { inputGroupClass, inputSizeClass } from "../../_internal/constants/inputClass.js";
|
|
4
|
+
import { useFieldGroupControl } from "../../_internal/hooks/useFieldGroupControl.js";
|
|
5
|
+
import { cn } from "../../_internal/libs/cn.js";
|
|
6
|
+
import { defaultDateFormat } from "../constants/dateFormat.js";
|
|
7
|
+
import { calendarIconClass, groupClass } from "../constants/datePickerClass.js";
|
|
8
|
+
import { resolveDateFormat } from "../libs/dateFormat.js";
|
|
9
|
+
import { useOpenOnFocus } from "../hooks/useOpenOnFocus.js";
|
|
10
|
+
import { parseIsoDate, toIsoDate } from "../libs/isoDate.js";
|
|
11
|
+
import { Calendar } from "./Calendar.js";
|
|
12
|
+
import { DatePickerPopover } from "./DatePickerPopover.js";
|
|
13
|
+
import { DateSegments } from "./DateSegments.js";
|
|
14
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
15
|
+
import { useMemo, useRef, useState } from "react";
|
|
16
|
+
import { CalendarIcon } from "@tide-design/icons";
|
|
17
|
+
import { mergeProps, mergeRefs, useDatePicker } from "react-aria";
|
|
18
|
+
import { useDatePickerState } from "react-stately";
|
|
19
|
+
import { toCalendarDate } from "@internationalized/date";
|
|
20
|
+
|
|
21
|
+
// src/DatePicker/components/DatePickerGroup.tsx
|
|
22
|
+
const toIso = (date) => toIsoDate(toCalendarDate(date));
|
|
23
|
+
/** `I18nProvider` 안에서 그리는 DatePicker 본체. group(칸 + 장식 달력 아이콘)과 Popover 달력을 조립한다. */
|
|
24
|
+
function DatePickerGroup({ value, defaultValue, onValueChange, onOpenChange, name, form, size = "md", invalid, disabled, readOnly = false, required, min, max, isDateUnavailable, format = defaultDateFormat, id, ref, className, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, ...props }) {
|
|
25
|
+
const groupRef = useRef(null);
|
|
26
|
+
const popoverRef = useRef(null);
|
|
27
|
+
const control = useFieldGroupControl({
|
|
28
|
+
id,
|
|
29
|
+
invalid,
|
|
30
|
+
disabled,
|
|
31
|
+
required,
|
|
32
|
+
"aria-labelledby": ariaLabelledBy,
|
|
33
|
+
"aria-describedby": ariaDescribedBy
|
|
34
|
+
}, () => groupRef.current?.querySelector("[data-segment]")?.focus());
|
|
35
|
+
const controlled = useMemo(() => value === void 0 ? void 0 : parseIsoDate(value), [value]);
|
|
36
|
+
const [initial] = useState(() => parseIsoDate(defaultValue));
|
|
37
|
+
const minValue = useMemo(() => parseIsoDate(min), [min]);
|
|
38
|
+
const maxValue = useMemo(() => parseIsoDate(max), [max]);
|
|
39
|
+
const unavailable = useMemo(() => isDateUnavailable ? (date) => isDateUnavailable(toIso(date)) : void 0, [isDateUnavailable]);
|
|
40
|
+
const parts = useMemo(() => resolveDateFormat(format), [format]);
|
|
41
|
+
const pickerProps = {
|
|
42
|
+
id: control.id,
|
|
43
|
+
"aria-label": ariaLabel,
|
|
44
|
+
"aria-labelledby": control["aria-labelledby"],
|
|
45
|
+
"aria-describedby": control["aria-describedby"],
|
|
46
|
+
value: controlled,
|
|
47
|
+
defaultValue: initial,
|
|
48
|
+
onChange: (date) => onValueChange?.(date ? toIso(date) : null),
|
|
49
|
+
onOpenChange,
|
|
50
|
+
minValue,
|
|
51
|
+
maxValue,
|
|
52
|
+
isDateUnavailable: unavailable,
|
|
53
|
+
isDisabled: control.disabled,
|
|
54
|
+
isReadOnly: readOnly,
|
|
55
|
+
isRequired: control.required,
|
|
56
|
+
name,
|
|
57
|
+
form,
|
|
58
|
+
validationBehavior: "native",
|
|
59
|
+
shouldForceLeadingZeros: true
|
|
60
|
+
};
|
|
61
|
+
const state = useDatePickerState(pickerProps);
|
|
62
|
+
const aria = useDatePicker(pickerProps, state, groupRef);
|
|
63
|
+
const openOnFocus = useOpenOnFocus({
|
|
64
|
+
state,
|
|
65
|
+
groupRef,
|
|
66
|
+
popoverRef,
|
|
67
|
+
enabled: !control.disabled && !readOnly
|
|
68
|
+
});
|
|
69
|
+
const dialogProps = {
|
|
70
|
+
id: aria.dialogProps.id,
|
|
71
|
+
"aria-label": aria.buttonProps["aria-label"],
|
|
72
|
+
"aria-labelledby": `${aria.dialogProps.id} ${aria.groupProps["aria-labelledby"] ?? ""}`.trim()
|
|
73
|
+
};
|
|
74
|
+
const [entering, setEntering] = useState(false);
|
|
75
|
+
const fieldValue = state.value ? toIso(state.value) : null;
|
|
76
|
+
const [calendarValue, setCalendarValue] = useState(fieldValue);
|
|
77
|
+
if (!entering && calendarValue !== fieldValue) setCalendarValue(fieldValue);
|
|
78
|
+
const mergedRef = useMemo(() => mergeRefs(ref, groupRef), [ref, groupRef]);
|
|
79
|
+
const showInvalid = control.invalid || aria.isInvalid;
|
|
80
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("div", {
|
|
81
|
+
...mergeProps(props, aria.groupProps, openOnFocus.groupProps),
|
|
82
|
+
ref: mergedRef,
|
|
83
|
+
"data-invalid": showInvalid ? "" : void 0,
|
|
84
|
+
"data-disabled": control.disabled ? "" : void 0,
|
|
85
|
+
"data-open": state.isOpen ? "" : void 0,
|
|
86
|
+
"aria-controls": state.isOpen ? dialogProps.id : void 0,
|
|
87
|
+
className: cn(inputSizeClass[size], inputGroupClass, groupClass, className),
|
|
88
|
+
children: [/* @__PURE__ */ jsx(DateSegments, {
|
|
89
|
+
fieldProps: aria.fieldProps,
|
|
90
|
+
parts,
|
|
91
|
+
invalid: showInvalid,
|
|
92
|
+
onEntryChange: setEntering
|
|
93
|
+
}), /* @__PURE__ */ jsx(CalendarIcon, { className: calendarIconClass[size] })]
|
|
94
|
+
}), /* @__PURE__ */ jsx(DatePickerPopover, {
|
|
95
|
+
state,
|
|
96
|
+
groupRef,
|
|
97
|
+
popoverRef,
|
|
98
|
+
dialogProps,
|
|
99
|
+
fieldMode: openOnFocus.fieldMode,
|
|
100
|
+
children: /* @__PURE__ */ jsx(Calendar, {
|
|
101
|
+
value: calendarValue,
|
|
102
|
+
onValueChange: (next) => {
|
|
103
|
+
const date = parseIsoDate(next);
|
|
104
|
+
if (date) state.setDateValue(date);
|
|
105
|
+
},
|
|
106
|
+
onPickerValueChange: (next) => {
|
|
107
|
+
const date = parseIsoDate(next);
|
|
108
|
+
if (!date) return;
|
|
109
|
+
state.setValue(date);
|
|
110
|
+
state.commitValidation();
|
|
111
|
+
},
|
|
112
|
+
readOnly,
|
|
113
|
+
disabled: control.disabled,
|
|
114
|
+
min,
|
|
115
|
+
max,
|
|
116
|
+
isDateUnavailable
|
|
117
|
+
})
|
|
118
|
+
})] });
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export { DatePickerGroup };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { OverlayPortal } from "../../_internal/components/OverlayPortal.js";
|
|
4
|
+
import { useOverlayPresence } from "../../_internal/hooks/useOverlayPresence.js";
|
|
5
|
+
import { DatePickerPopoverInner } from "./DatePickerPopoverInner.js";
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
|
|
8
|
+
// src/DatePicker/components/DatePickerPopover.tsx
|
|
9
|
+
function DatePickerPopover(props) {
|
|
10
|
+
if (!useOverlayPresence(props.state.isOpen, props.popoverRef)) return null;
|
|
11
|
+
return /* @__PURE__ */ jsx(OverlayPortal, {
|
|
12
|
+
isExiting: !props.state.isOpen,
|
|
13
|
+
containFocus: !props.fieldMode,
|
|
14
|
+
children: /* @__PURE__ */ jsx(DatePickerPopoverInner, { ...props })
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { DatePickerPopover };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useOverlayTheme } from "../../_internal/hooks/useOverlayTheme.js";
|
|
4
|
+
import { usePopoverOverlay } from "../../_internal/hooks/usePopoverOverlay.js";
|
|
5
|
+
import { popoverClass } from "../constants/datePickerClass.js";
|
|
6
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
+
import { useEffect } from "react";
|
|
8
|
+
import { DismissButton, useInteractOutside } from "react-aria";
|
|
9
|
+
|
|
10
|
+
// src/DatePicker/components/DatePickerPopoverInner.tsx
|
|
11
|
+
/** DOM에 있는 동안만 portal 안에서 렌더한다. 칸 포커스로 열렸으면(`fieldMode`) 포커스를 칸에 둔다. */
|
|
12
|
+
function DatePickerPopoverInner({ state, groupRef, popoverRef, dialogProps, fieldMode, children }) {
|
|
13
|
+
useOverlayTheme(groupRef, popoverRef);
|
|
14
|
+
const isInGroup = (element) => groupRef.current?.contains(element) === true;
|
|
15
|
+
const { overlayProps } = usePopoverOverlay({
|
|
16
|
+
triggerRef: groupRef,
|
|
17
|
+
popoverRef,
|
|
18
|
+
placement: "bottom start",
|
|
19
|
+
offset: 4,
|
|
20
|
+
nonModal: fieldMode,
|
|
21
|
+
shouldCloseOnInteractOutside: fieldMode ? (element) => !isInGroup(element) : void 0
|
|
22
|
+
}, state);
|
|
23
|
+
useInteractOutside({
|
|
24
|
+
ref: popoverRef,
|
|
25
|
+
isDisabled: !fieldMode || !state.isOpen,
|
|
26
|
+
onInteractOutside: (event) => {
|
|
27
|
+
if (!(event.target instanceof Element && isInGroup(event.target))) state.close();
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
const popover = popoverRef.current;
|
|
32
|
+
if (fieldMode || !state.isOpen || !popover || popover.contains(document.activeElement)) return;
|
|
33
|
+
popover.querySelector("[role=\"gridcell\"] [tabindex=\"0\"]")?.focus();
|
|
34
|
+
}, [
|
|
35
|
+
fieldMode,
|
|
36
|
+
state.isOpen,
|
|
37
|
+
popoverRef
|
|
38
|
+
]);
|
|
39
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
40
|
+
...overlayProps,
|
|
41
|
+
id: dialogProps.id,
|
|
42
|
+
role: "dialog",
|
|
43
|
+
tabIndex: -1,
|
|
44
|
+
"aria-label": dialogProps["aria-label"],
|
|
45
|
+
"aria-labelledby": dialogProps["aria-labelledby"],
|
|
46
|
+
ref: popoverRef,
|
|
47
|
+
"data-open": state.isOpen ? "" : void 0,
|
|
48
|
+
className: popoverClass,
|
|
49
|
+
children: [
|
|
50
|
+
/* @__PURE__ */ jsx(DismissButton, { onDismiss: state.close }),
|
|
51
|
+
children,
|
|
52
|
+
/* @__PURE__ */ jsx(DismissButton, { onDismiss: state.close })
|
|
53
|
+
]
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { DatePickerPopoverInner };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { cn } from "../../_internal/libs/cn.js";
|
|
4
|
+
import { segmentCaretClass, segmentClass, yearSegmentClass } from "../constants/datePickerClass.js";
|
|
5
|
+
import { useSegmentEntry } from "../hooks/useSegmentEntry.js";
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
import { useRef } from "react";
|
|
8
|
+
import { mergeProps, useDateSegment } from "react-aria";
|
|
9
|
+
|
|
10
|
+
// src/DatePicker/components/DateSegment.tsx
|
|
11
|
+
/** 연·월·일 중 한 칸(`spinbutton`). 이름은 React Aria가 locale 문구(`년` 등)와 label로 붙인다. */
|
|
12
|
+
function DateSegment({ segment, state, placeholder, invalid, onEntryChange }) {
|
|
13
|
+
const ref = useRef(null);
|
|
14
|
+
const { segmentProps } = useDateSegment(segment, state, ref);
|
|
15
|
+
const { entryProps, entered } = useSegmentEntry(segment, state, ref, onEntryChange);
|
|
16
|
+
return /* @__PURE__ */ jsx("div", {
|
|
17
|
+
...mergeProps(segmentProps, entryProps),
|
|
18
|
+
"aria-invalid": invalid || segmentProps["aria-invalid"] === "true" ? "true" : void 0,
|
|
19
|
+
ref,
|
|
20
|
+
"data-segment": "",
|
|
21
|
+
className: cn(segmentClass, segmentCaretClass, segment.type === "year" && "min-w-[calc(var(--spacing)*6+2ch)] data-placeholder:min-w-6"),
|
|
22
|
+
children: entered || (segment.isPlaceholder ? placeholder : segment.text)
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { DateSegment };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { separatorClass } from "../constants/datePickerClass.js";
|
|
4
|
+
import { DateSegment } from "./DateSegment.js";
|
|
5
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
import { useRef } from "react";
|
|
7
|
+
import { useDateField, useLocale } from "react-aria";
|
|
8
|
+
import { useDateFieldState } from "react-stately";
|
|
9
|
+
import { GregorianCalendar } from "@internationalized/date";
|
|
10
|
+
|
|
11
|
+
// src/DatePicker/components/DateSegments.tsx
|
|
12
|
+
const createCalendar = () => new GregorianCalendar();
|
|
13
|
+
const placeholders = {
|
|
14
|
+
year: "yyyy",
|
|
15
|
+
month: "MM",
|
|
16
|
+
day: "dd"
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* 연·월·일 칸과 폼 제출용 숨은 input. locale이 만든 칸 중 literal은 버리고 `format` 순서로 그린다(ADR 0045).
|
|
20
|
+
* 구분자는 스크린리더에서 숨긴다. 칸 이름(`년`·`월`·`일`)이 대신 읽힌다.
|
|
21
|
+
*/
|
|
22
|
+
function DateSegments({ fieldProps, parts, invalid, onEntryChange }) {
|
|
23
|
+
const { locale } = useLocale();
|
|
24
|
+
const state = useDateFieldState({
|
|
25
|
+
...fieldProps,
|
|
26
|
+
locale,
|
|
27
|
+
createCalendar,
|
|
28
|
+
shouldForceLeadingZeros: true
|
|
29
|
+
});
|
|
30
|
+
const ref = useRef(null);
|
|
31
|
+
const inputRef = useRef(null);
|
|
32
|
+
const { fieldProps: segmentsProps, inputProps } = useDateField({
|
|
33
|
+
...fieldProps,
|
|
34
|
+
inputRef
|
|
35
|
+
}, state, ref);
|
|
36
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
37
|
+
...segmentsProps,
|
|
38
|
+
ref,
|
|
39
|
+
className: "flex items-center",
|
|
40
|
+
children: [parts.map((part, index) => {
|
|
41
|
+
if (part.type === "literal") return /* @__PURE__ */ jsx("span", {
|
|
42
|
+
"aria-hidden": "true",
|
|
43
|
+
className: separatorClass,
|
|
44
|
+
children: part.text
|
|
45
|
+
}, index);
|
|
46
|
+
const segment = state.segments.find((candidate) => candidate.type === part.type);
|
|
47
|
+
if (!segment) return null;
|
|
48
|
+
return /* @__PURE__ */ jsx(DateSegment, {
|
|
49
|
+
segment,
|
|
50
|
+
state,
|
|
51
|
+
placeholder: placeholders[part.type],
|
|
52
|
+
invalid,
|
|
53
|
+
onEntryChange
|
|
54
|
+
}, index);
|
|
55
|
+
}), /* @__PURE__ */ jsx("input", {
|
|
56
|
+
...inputProps,
|
|
57
|
+
ref: inputRef
|
|
58
|
+
})]
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { DateSegments };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// src/DatePicker/constants/calendarClass.ts
|
|
2
|
+
const calendarClass = "flex w-max flex-col gap-2 text-default";
|
|
3
|
+
const calendarHeaderClass = "flex items-center justify-between gap-2";
|
|
4
|
+
const calendarTitleClass = "text-label-md text-default";
|
|
5
|
+
/** 날짜 화면 제목. 연·월 버튼을 나란히 둔다. */
|
|
6
|
+
const calendarTitleButtonsClass = "flex items-center text-label-md text-default";
|
|
7
|
+
/** 제목의 연·월 버튼. 보이는 글자가 제목 그대로라 옆 여백을 줄인다. */
|
|
8
|
+
const calendarTitleButtonClass = "px-1.5";
|
|
9
|
+
const calendarGridClass = "border-collapse";
|
|
10
|
+
const calendarWeekdayClass = "size-9 p-0 text-center text-label-sm font-normal text-muted";
|
|
11
|
+
const calendarCellClass = "p-0.5 text-center";
|
|
12
|
+
const calendarItemStateClass = [
|
|
13
|
+
"cursor-pointer rounded-md outline-none select-none transition-colors",
|
|
14
|
+
"focus-visible:focus-ring",
|
|
15
|
+
"hover:not-data-selected:not-data-disabled:not-data-unavailable:bg-hover",
|
|
16
|
+
"data-selected:bg-primary-solid data-selected:text-on-solid data-selected:hover:bg-primary-solid-hover",
|
|
17
|
+
"data-selected:focus-visible:inset-ring-2 data-selected:focus-visible:inset-ring-(color:--tide-bg-canvas)",
|
|
18
|
+
"data-disabled:cursor-not-allowed data-disabled:text-disabled data-disabled:line-through",
|
|
19
|
+
"forced-colors:data-selected:forced-color-adjust-none forced-colors:data-selected:bg-[Highlight] forced-colors:data-selected:text-[HighlightText]"
|
|
20
|
+
].join(" ");
|
|
21
|
+
/**
|
|
22
|
+
* 날짜 버튼. 상태는 boolean `data-*`로 받고 `not-data-*`로 서로 빼 CSS 순서에 기대지 않는다.
|
|
23
|
+
* 색만으로 구분하지 않게 오늘은 굵기+점, 고른 날은 채움, 막힌 날은 취소선을 함께 쓴다.
|
|
24
|
+
*/
|
|
25
|
+
const calendarDayClass = [
|
|
26
|
+
"relative mx-auto flex size-8 items-center justify-center text-body-sm tabular-nums",
|
|
27
|
+
calendarItemStateClass,
|
|
28
|
+
"data-today:font-semibold data-today:after:absolute data-today:after:bottom-1 data-today:after:size-1 data-today:after:rounded-full data-today:after:bg-current",
|
|
29
|
+
"data-unavailable:cursor-not-allowed data-unavailable:text-muted data-unavailable:line-through",
|
|
30
|
+
"forced-colors:after:forced-color-adjust-none"
|
|
31
|
+
].join(" ");
|
|
32
|
+
/** 연도·월 화면 grid. 날짜 화면(요일 칸 7개)과 같은 폭에 3열로 나눈다. */
|
|
33
|
+
const calendarPickerGridClass = "w-63 table-fixed border-collapse";
|
|
34
|
+
/** 연도·월 칸. 10년 묶음 밖 앞뒤 해(`data-outside`)는 흐린 글자다. */
|
|
35
|
+
const calendarPickerItemClass = [
|
|
36
|
+
"flex h-12 w-full items-center justify-center text-body-sm tabular-nums",
|
|
37
|
+
calendarItemStateClass,
|
|
38
|
+
"data-outside:not-data-selected:not-data-disabled:text-muted"
|
|
39
|
+
].join(" ");
|
|
40
|
+
|
|
41
|
+
export { calendarCellClass, calendarClass, calendarDayClass, calendarGridClass, calendarHeaderClass, calendarPickerGridClass, calendarPickerItemClass, calendarTitleButtonClass, calendarTitleButtonsClass, calendarTitleClass, calendarWeekdayClass };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { overlaySurfaceClass, popoverMotionClass } from "../../_internal/constants/overlayClass.js";
|
|
2
|
+
|
|
3
|
+
// src/DatePicker/constants/datePickerClass.ts
|
|
4
|
+
/** 칸을 누르지 않고 group 빈 곳·아이콘을 눌러도 React Aria가 칸으로 포커스를 옮긴다. */
|
|
5
|
+
const groupClass = "cursor-text tabular-nums";
|
|
6
|
+
/**
|
|
7
|
+
* 고른 칸은 옅은 primary 바탕에 글자색을 그대로 둔다. 칸이 비면 자리표시(`yyyy` 등)를 흐린 글자로 보인다.
|
|
8
|
+
* 자리표시 글자는 숫자와 폭이 달라, 값이 있을 때의 좌우 여백을 padding으로 준다.
|
|
9
|
+
*/
|
|
10
|
+
const segmentClass = "inline-flex min-h-6 min-w-6 items-center justify-center rounded-sm outline-none data-placeholder:px-[calc((var(--spacing)*6-2ch)/2)] data-placeholder:text-muted focus:bg-primary-subtle in-data-disabled:text-disabled forced-colors:focus:bg-[Highlight] forced-colors:focus:text-[HighlightText]";
|
|
11
|
+
/**
|
|
12
|
+
* 포커스된 칸의 깜빡이는 커서. React Aria가 선택을 칸 앞에 붙이고 caret을 숨기므로 직접 그린다.
|
|
13
|
+
* 값 끝(비었으면 자리표시 앞)에 두고, 폭을 음수 margin으로 상쇄해 글자 위치를 바꾸지 않는다.
|
|
14
|
+
*/
|
|
15
|
+
const segmentCaretClass = "focus:after:-me-px focus:after:h-[1.2em] focus:after:w-px focus:after:bg-current focus:motion-safe:after:animate-caret-blink data-placeholder:focus:after:order-first forced-colors:focus:after:bg-[HighlightText]";
|
|
16
|
+
/** 두 자리 칸이 24px 안에 남기는 여백을 연도 칸도 같게 둔다. 숫자 폭은 group의 `tabular-nums`로 `1ch`와 같다. */
|
|
17
|
+
const yearSegmentClass = "min-w-[calc(var(--spacing)*6+2ch)] data-placeholder:min-w-6";
|
|
18
|
+
const separatorClass = "px-px text-muted in-data-disabled:text-disabled";
|
|
19
|
+
/** 장식 달력 아이콘. 누름이 group에 닿아야 React Aria가 칸에 포커스를 준다. */
|
|
20
|
+
const calendarIconClass = {
|
|
21
|
+
sm: "pointer-events-none ms-auto -me-0.5 size-4 shrink-0 text-muted in-data-disabled:text-disabled",
|
|
22
|
+
md: "pointer-events-none ms-auto size-4 shrink-0 text-muted in-data-disabled:text-disabled",
|
|
23
|
+
lg: "pointer-events-none ms-auto -me-0.5 size-5 shrink-0 text-muted in-data-disabled:text-disabled"
|
|
24
|
+
};
|
|
25
|
+
const popoverClass = `isolate z-popover p-3 outline-none ${overlaySurfaceClass} ${popoverMotionClass}`;
|
|
26
|
+
|
|
27
|
+
export { calendarIconClass, groupClass, popoverClass, segmentCaretClass, segmentClass, separatorClass, yearSegmentClass };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useRef, useState } from "react";
|
|
4
|
+
|
|
5
|
+
// src/DatePicker/hooks/useOpenOnFocus.ts
|
|
6
|
+
const isInside = (ref, target) => target instanceof Node && ref.current?.contains(target) === true;
|
|
7
|
+
/** 세그먼트에 포커스가 새로 들어오거나 닫힌 입력 칸(빈 곳·아이콘 포함)을 누르면 달력을 연다. 포커스는 칸에 그대로 둔다(ADR 0045). */
|
|
8
|
+
function useOpenOnFocus({ state, groupRef, popoverRef, enabled }) {
|
|
9
|
+
const [fieldMode, setFieldMode] = useState(false);
|
|
10
|
+
if (fieldMode && !state.isOpen) setFieldMode(false);
|
|
11
|
+
const focusInPopoverRef = useRef(false);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
const track = (event) => {
|
|
14
|
+
focusInPopoverRef.current = isInside(popoverRef, event.target);
|
|
15
|
+
};
|
|
16
|
+
document.addEventListener("focusin", track, true);
|
|
17
|
+
return () => document.removeEventListener("focusin", track, true);
|
|
18
|
+
}, [popoverRef]);
|
|
19
|
+
const restoringRef = useRef(false);
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
if (state.isOpen || !focusInPopoverRef.current) return;
|
|
22
|
+
focusInPopoverRef.current = false;
|
|
23
|
+
restoringRef.current = true;
|
|
24
|
+
const clear = () => {
|
|
25
|
+
restoringRef.current = false;
|
|
26
|
+
};
|
|
27
|
+
document.addEventListener("focusin", clear, { once: true });
|
|
28
|
+
return () => document.removeEventListener("focusin", clear);
|
|
29
|
+
}, [state.isOpen, popoverRef]);
|
|
30
|
+
const openInField = () => {
|
|
31
|
+
setFieldMode(true);
|
|
32
|
+
state.setOpen(true);
|
|
33
|
+
};
|
|
34
|
+
return {
|
|
35
|
+
fieldMode,
|
|
36
|
+
groupProps: {
|
|
37
|
+
onFocus: (event) => {
|
|
38
|
+
if (restoringRef.current) {
|
|
39
|
+
restoringRef.current = false;
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (!enabled || state.isOpen || !event.target.hasAttribute("data-segment")) return;
|
|
43
|
+
if (isInside(groupRef, event.relatedTarget) || isInside(popoverRef, event.relatedTarget)) return;
|
|
44
|
+
openInField();
|
|
45
|
+
},
|
|
46
|
+
onPointerDownCapture: (event) => {
|
|
47
|
+
if (!enabled || state.isOpen || event.button !== 0) return;
|
|
48
|
+
if (!isInside(groupRef, document.activeElement)) return;
|
|
49
|
+
openInField();
|
|
50
|
+
},
|
|
51
|
+
onBlur: (event) => {
|
|
52
|
+
const next = event.relatedTarget;
|
|
53
|
+
if (!fieldMode || !state.isOpen || next === null) return;
|
|
54
|
+
if (isInside(groupRef, next) || isInside(popoverRef, next)) return;
|
|
55
|
+
state.close();
|
|
56
|
+
},
|
|
57
|
+
onKeyDown: (event) => {
|
|
58
|
+
if (!fieldMode || !state.isOpen) return;
|
|
59
|
+
if (event.key === "Escape") {
|
|
60
|
+
event.preventDefault();
|
|
61
|
+
state.close();
|
|
62
|
+
}
|
|
63
|
+
if (event.altKey && (event.key === "ArrowDown" || event.key === "ArrowUp")) setFieldMode(false);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { useOpenOnFocus };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { enterSegmentDigit, eraseSegmentDigit } from "../libs/segmentEntry.js";
|
|
4
|
+
import { useEffect, useEffectEvent, useRef, useState } from "react";
|
|
5
|
+
|
|
6
|
+
// src/DatePicker/hooks/useSegmentEntry.ts
|
|
7
|
+
const spinKeys = /* @__PURE__ */ new Set([
|
|
8
|
+
"ArrowUp",
|
|
9
|
+
"ArrowDown",
|
|
10
|
+
"PageUp",
|
|
11
|
+
"PageDown",
|
|
12
|
+
"Home",
|
|
13
|
+
"End"
|
|
14
|
+
]);
|
|
15
|
+
/** 칸에 숫자를 치는 중인지 알린다. 칸 입력이 끝나거나 포커스가 칸을 떠나면 `false`로 부른다. */
|
|
16
|
+
function useSegmentEntry(segment, state, ref, onEntryChange) {
|
|
17
|
+
const enteredRef = useRef("");
|
|
18
|
+
const [entered, setEntered] = useState("");
|
|
19
|
+
const update = (next) => {
|
|
20
|
+
const was = enteredRef.current !== "";
|
|
21
|
+
enteredRef.current = next;
|
|
22
|
+
setEntered(next);
|
|
23
|
+
if (was !== (next !== "")) onEntryChange(next !== "");
|
|
24
|
+
};
|
|
25
|
+
const onBeforeInput = useEffectEvent((event) => {
|
|
26
|
+
if (state.isDisabled || state.isReadOnly || event.data == null) return;
|
|
27
|
+
if (event.inputType.startsWith("delete") || event.inputType === "insertCompositionText") return;
|
|
28
|
+
const next = enterSegmentDigit(enteredRef.current, event.data, segment.maxValue);
|
|
29
|
+
if (next !== null) update(next);
|
|
30
|
+
});
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
const element = ref.current;
|
|
33
|
+
if (!element) return;
|
|
34
|
+
const listener = (event) => onBeforeInput(event);
|
|
35
|
+
element.addEventListener("beforeinput", listener, { capture: true });
|
|
36
|
+
return () => element.removeEventListener("beforeinput", listener, { capture: true });
|
|
37
|
+
}, [ref]);
|
|
38
|
+
return {
|
|
39
|
+
entered,
|
|
40
|
+
entryProps: {
|
|
41
|
+
onFocus: () => update(""),
|
|
42
|
+
onBlur: () => update(""),
|
|
43
|
+
onKeyDown: (event) => {
|
|
44
|
+
if (event.key === "Backspace" || event.key === "Delete") {
|
|
45
|
+
const editable = !state.isReadOnly && !segment.isPlaceholder;
|
|
46
|
+
update(editable ? eraseSegmentDigit(enteredRef.current || segment.text) : "");
|
|
47
|
+
} else if (spinKeys.has(event.key)) update("");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { useSegmentEntry };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// src/DatePicker/libs/calendarPicker.ts
|
|
2
|
+
const decadeStart = (year) => Math.floor(year / 10) * 10;
|
|
3
|
+
/** 월 화면 칸의 키. 연도 화면 칸의 키는 해 숫자다. 둘 다 ±1·±3이 이웃 칸이 된다. */
|
|
4
|
+
const monthKey = ({ year, month }) => year * 12 + month - 1;
|
|
5
|
+
const fromMonthKey = (key) => ({
|
|
6
|
+
year: Math.floor(key / 12),
|
|
7
|
+
month: key % 12 + 1
|
|
8
|
+
});
|
|
9
|
+
const clampKey = (key, min, max) => Math.min(Math.max(key, min), max);
|
|
10
|
+
/**
|
|
11
|
+
* 값의 해·달만 바꾼 날. 일이 그 달을 넘으면 끝날로 줄이고 min·max 밖이면 경계로 맞춘다.
|
|
12
|
+
* 맞춘 날이 막힌 날이면 `null`.
|
|
13
|
+
*/
|
|
14
|
+
function shiftYearMonth(date, fields, { min, max, isUnavailable } = {}) {
|
|
15
|
+
let next = date.set(fields);
|
|
16
|
+
if (min && next.compare(min) < 0) next = min;
|
|
17
|
+
if (max && next.compare(max) > 0) next = max;
|
|
18
|
+
return isUnavailable?.(next) ? null : next;
|
|
19
|
+
}
|
|
20
|
+
/** 연도·월 grid에서 키 하나가 옮길 칸. 처리하지 않는 키면 `null`. */
|
|
21
|
+
function nextPickerKey(key, focused, range, isRtl = false) {
|
|
22
|
+
const step = isRtl ? -1 : 1;
|
|
23
|
+
const target = {
|
|
24
|
+
ArrowLeft: focused - step,
|
|
25
|
+
ArrowRight: focused + step,
|
|
26
|
+
ArrowUp: focused - 3,
|
|
27
|
+
ArrowDown: focused + 3,
|
|
28
|
+
PageUp: focused - range.page,
|
|
29
|
+
PageDown: focused + range.page,
|
|
30
|
+
Home: range.blockStart,
|
|
31
|
+
End: range.blockStart + range.page - 1
|
|
32
|
+
}[key];
|
|
33
|
+
return target === void 0 ? null : clampKey(target, range.min, range.max);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 「연 월」 제목을 연·월 버튼으로 나눌 조각으로 바꾼다. 순서는 locale을 따른다.
|
|
37
|
+
* 공백이 아닌 구분 글자(`년`, `年`)는 앞 조각에 붙여 버튼 글자가 보이는 제목과 같게 한다.
|
|
38
|
+
*/
|
|
39
|
+
function splitYearMonthTitle(parts) {
|
|
40
|
+
const result = [];
|
|
41
|
+
for (const part of parts) {
|
|
42
|
+
if (part.type === "year" || part.type === "month") {
|
|
43
|
+
result.push({
|
|
44
|
+
type: part.type,
|
|
45
|
+
text: part.value
|
|
46
|
+
});
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const [, attached = "", rest = ""] = /^(\S*)([\s\S]*)$/.exec(part.value) ?? [];
|
|
50
|
+
const previous = result.at(-1);
|
|
51
|
+
if (attached && previous && previous.type !== "literal") previous.text += attached;
|
|
52
|
+
else if (attached) result.push({
|
|
53
|
+
type: "literal",
|
|
54
|
+
text: attached
|
|
55
|
+
});
|
|
56
|
+
if (rest) result.push({
|
|
57
|
+
type: "literal",
|
|
58
|
+
text: rest
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { clampKey, decadeStart, fromMonthKey, monthKey, nextPickerKey, shiftYearMonth, splitYearMonthTitle };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { defaultDateFormat } from "../constants/dateFormat.js";
|
|
2
|
+
import { warnOnce } from "./warnOnce.js";
|
|
3
|
+
|
|
4
|
+
// src/DatePicker/libs/dateFormat.ts
|
|
5
|
+
const tokens = [
|
|
6
|
+
["yyyy", "year"],
|
|
7
|
+
["MM", "month"],
|
|
8
|
+
["dd", "day"]
|
|
9
|
+
];
|
|
10
|
+
/** `yyyy`·`MM`·`dd`를 하나씩 담은 형식을 조각으로 나눈다. 토큰이 빠졌거나 겹치거나 모르는 글자 토큰이면 `null`이다. */
|
|
11
|
+
function parseDateFormat(format) {
|
|
12
|
+
const parts = [];
|
|
13
|
+
const seen = /* @__PURE__ */ new Set();
|
|
14
|
+
let index = 0;
|
|
15
|
+
while (index < format.length) {
|
|
16
|
+
const token = tokens.find(([text]) => format.startsWith(text, index));
|
|
17
|
+
if (token) {
|
|
18
|
+
const [text, type] = token;
|
|
19
|
+
if (seen.has(type)) return null;
|
|
20
|
+
seen.add(type);
|
|
21
|
+
parts.push({ type });
|
|
22
|
+
index += text.length;
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
const char = format.charAt(index);
|
|
26
|
+
if (/\p{L}/u.test(char)) return null;
|
|
27
|
+
const last = parts.at(-1);
|
|
28
|
+
if (last?.type === "literal") last.text += char;
|
|
29
|
+
else parts.push({
|
|
30
|
+
type: "literal",
|
|
31
|
+
text: char
|
|
32
|
+
});
|
|
33
|
+
index += 1;
|
|
34
|
+
}
|
|
35
|
+
return seen.size === tokens.length ? parts : null;
|
|
36
|
+
}
|
|
37
|
+
/** `parseDateFormat`과 같지만 읽지 못하면 개발 모드에서 경고하고 기본 형식(`yyyy-MM-dd`)을 쓴다. */
|
|
38
|
+
function resolveDateFormat(format) {
|
|
39
|
+
const parts = parseDateFormat(format);
|
|
40
|
+
if (parts) return parts;
|
|
41
|
+
warnOnce(`format:${format}`, `[Tide] DatePicker format "${format}"을 읽지 못해 "${defaultDateFormat}"을 씁니다. yyyy·MM·dd를 하나씩 쓰고(대소문자 구분) 사이에는 구분자만 넣으세요.`);
|
|
42
|
+
return parseDateFormat("yyyy-MM-dd") ?? [];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { parseDateFormat, resolveDateFormat };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { warnOnce } from "./warnOnce.js";
|
|
2
|
+
import { parseDate } from "@internationalized/date";
|
|
3
|
+
|
|
4
|
+
// src/DatePicker/libs/isoDate.ts
|
|
5
|
+
/**
|
|
6
|
+
* ISO `YYYY-MM-DD` 문자열을 `CalendarDate`로 바꾼다. 빈 값은 `null`이다.
|
|
7
|
+
* 형식이 틀리거나 없는 날짜면 `null`을 돌려주고 개발 모드에서 값마다 한 번 경고한다(ADR 0045).
|
|
8
|
+
*/
|
|
9
|
+
function parseIsoDate(value) {
|
|
10
|
+
if (value == null || value === "") return null;
|
|
11
|
+
try {
|
|
12
|
+
return parseDate(value);
|
|
13
|
+
} catch {
|
|
14
|
+
warnOnce(`date:${value}`, `[Tide] 날짜 "${value}"를 읽지 못해 빈 값으로 둡니다. "YYYY-MM-DD" 형식의 있는 날짜를 넘기세요.`);
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** `CalendarDate`를 ISO `YYYY-MM-DD` 문자열로 바꾼다. */
|
|
19
|
+
function toIsoDate(date) {
|
|
20
|
+
return date.toString();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { parseIsoDate, toIsoDate };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// src/DatePicker/libs/segmentEntry.ts
|
|
2
|
+
/**
|
|
3
|
+
* 숫자 칸에 `key`를 친 뒤 이어 칠 자릿수. 칸 입력이 끝났으면 `""`, 받지 않는 키면 `null`.
|
|
4
|
+
* 자릿수가 최댓값 자릿수에 닿거나, 숫자를 하나 더 붙이면 최댓값을 넘을 때 끝난다.
|
|
5
|
+
*/
|
|
6
|
+
function enterSegmentDigit(entered, key, maxValue) {
|
|
7
|
+
const next = entered + key;
|
|
8
|
+
if (!/^\d+$/.test(next)) return null;
|
|
9
|
+
if (maxValue === void 0) return next;
|
|
10
|
+
return Number(`${Number(next)}0`) > maxValue || next.length >= String(maxValue).length ? "" : next;
|
|
11
|
+
}
|
|
12
|
+
/** Backspace·Delete 뒤 이어 칠 자릿수. 남은 값이 없거나 0이면 `""`. */
|
|
13
|
+
function eraseSegmentDigit(text) {
|
|
14
|
+
if (!/^\d+$/.test(text)) return "";
|
|
15
|
+
const next = text.slice(0, -1);
|
|
16
|
+
return Number(next) === 0 ? "" : next;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { enterSegmentDigit, eraseSegmentDigit };
|