@tide-design/components 0.27.0 → 0.28.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.
@@ -32,11 +32,11 @@ export interface DatePickerOwnProps {
32
32
  readOnly?: boolean;
33
33
  /** 필수. 비운 채 제출하면 막고 첫 칸으로 포커스를 옮긴다(브라우저 말풍선 없음, 메시지는 Field `error`). */
34
34
  required?: boolean;
35
- /** 고를 수 있는 첫날(ISO). 밖의 날은 달력에서 막히고 입력하면 오류 상태가 된다. */
35
+ /** 고를 수 있는 첫날(ISO). 밖의 날은 달력에서 막히고 입력하면 오류 상태가 된다(Field 안이면 기본 오류 문구). */
36
36
  min?: string;
37
37
  /** 고를 수 있는 마지막 날(ISO). */
38
38
  max?: string;
39
- /** 고를 수 없는 날(포커스는 간다). `"weekend"`는 `locale`의 주말을 막는다. 함수는 {@link DateUnavailableInfo} 객체 하나를 받는다. */
39
+ /** 고를 수 없는 날(포커스는 간다). 입력하면 오류 상태(Field 안이면 기본 오류 문구). `"weekend"`는 `locale`의 주말을 막는다. 함수는 {@link DateUnavailableInfo} 객체 하나를 받는다. */
40
40
  isDateUnavailable?: "weekend" | ((info: DateUnavailableInfo) => boolean);
41
41
  /** 달력 제목·요일·주 시작과 스크린리더 문구의 언어. 기본값 `"ko-KR"`. 입력 칸 순서는 `format`이 정한다. */
42
42
  locale?: string;
@@ -6,6 +6,8 @@ import { cn } from "../../_internal/libs/cn.js";
6
6
  import { defaultDateFormat } from "../constants/dateFormat.js";
7
7
  import { calendarIconClass, groupClass } from "../constants/datePickerClass.js";
8
8
  import { resolveDateFormat } from "../libs/dateFormat.js";
9
+ import { getDefaultError } from "../libs/defaultError.js";
10
+ import { useFieldErrorReport } from "../hooks/useFieldErrorReport.js";
9
11
  import { useOpenOnFocus } from "../hooks/useOpenOnFocus.js";
10
12
  import { toUnavailableMatcher } from "../libs/dateUnavailable.js";
11
13
  import { parseIsoDate, toIsoDate } from "../libs/isoDate.js";
@@ -62,6 +64,13 @@ function DatePickerGroup({ value, defaultValue, onValueChange, onOpenChange, nam
62
64
  };
63
65
  const state = useDatePickerState(pickerProps);
64
66
  const aria = useDatePicker(pickerProps, state, groupRef);
67
+ useFieldErrorReport(getDefaultError({
68
+ validation: state.displayValidation,
69
+ min: minValue,
70
+ max: maxValue,
71
+ parts,
72
+ locale
73
+ }));
65
74
  const openOnFocus = useOpenOnFocus({
66
75
  state,
67
76
  groupRef,
@@ -0,0 +1,16 @@
1
+ // src/DatePicker/constants/defaultError.ts
2
+ /** 기본 오류 문구. 내용은 react-stately 3.51.0 날짜 검증 문구와 같다. 사전에 없는 언어는 `en`을 쓴다. */
3
+ const defaultErrorStrings = {
4
+ ko: {
5
+ rangeUnderflow: (min) => `값은 ${min} 이후여야 합니다.`,
6
+ rangeOverflow: (max) => `값은 ${max} 이전이어야 합니다.`,
7
+ unavailableDate: "선택한 날짜를 사용할 수 없습니다."
8
+ },
9
+ en: {
10
+ rangeUnderflow: (min) => `Value must be ${min} or later.`,
11
+ rangeOverflow: (max) => `Value must be ${max} or earlier.`,
12
+ unavailableDate: "Selected date unavailable."
13
+ }
14
+ };
15
+
16
+ export { defaultErrorStrings };
@@ -0,0 +1,17 @@
1
+ "use client";
2
+
3
+ import { FieldContext } from "../../_internal/hooks/useFieldControl.js";
4
+ import { use, useEffect } from "react";
5
+
6
+ // src/DatePicker/hooks/useFieldErrorReport.ts
7
+ /** 기본 오류 문구를 둘러싼 Field에 올리고, 바뀌거나 사라지면 지운다. Field 밖이면 하는 일이 없다. */
8
+ function useFieldErrorReport(message) {
9
+ const report = use(FieldContext)?.reportError;
10
+ useEffect(() => {
11
+ if (!report) return void 0;
12
+ report(message);
13
+ return () => report(void 0);
14
+ }, [report, message]);
15
+ }
16
+
17
+ export { useFieldErrorReport };
@@ -41,5 +41,14 @@ function resolveDateFormat(format) {
41
41
  warnOnce(`format:${format}`, `[Tide] DatePicker format "${format}"을 읽지 못해 "${defaultDateFormat}"을 씁니다. yyyy·MM·dd를 하나씩 쓰고(대소문자 구분) 사이에는 구분자만 넣으세요.`);
42
42
  return parseDateFormat("yyyy-MM-dd") ?? [];
43
43
  }
44
+ /** 날짜를 `format` 조각 순서대로 적는다. 연은 네 자리, 월·일은 두 자리로 채운다(칸 표시와 같다). */
45
+ function formatDate(date, parts) {
46
+ const fields = {
47
+ year: String(date.year).padStart(4, "0"),
48
+ month: String(date.month).padStart(2, "0"),
49
+ day: String(date.day).padStart(2, "0")
50
+ };
51
+ return parts.map((part) => part.type === "literal" ? part.text : fields[part.type]).join("");
52
+ }
44
53
 
45
- export { parseDateFormat, resolveDateFormat };
54
+ export { formatDate, parseDateFormat, resolveDateFormat };
@@ -0,0 +1,24 @@
1
+ import { defaultErrorStrings } from "../constants/defaultError.js";
2
+ import { formatDate } from "./dateFormat.js";
3
+
4
+ // src/DatePicker/libs/defaultError.ts
5
+ const stringsFor = (locale) => {
6
+ const { language } = new Intl.Locale(locale);
7
+ return language === "ko" ? defaultErrorStrings.ko : defaultErrorStrings.en;
8
+ };
9
+ /**
10
+ * 보이는 검증 결과에서 min·max 밖, 막힌 날 문구를 만든다. 문구 언어는 `locale`, 날짜는 `format`을 따른다.
11
+ * 빈 값(`valueMissing`) 등 그 밖의 오류는 문구를 만들지 않는다(`undefined`).
12
+ */
13
+ function getDefaultError({ validation, min, max, parts, locale }) {
14
+ if (!validation.isInvalid) return void 0;
15
+ const details = validation.validationDetails;
16
+ const strings = stringsFor(locale);
17
+ const messages = [];
18
+ if (details.rangeUnderflow && min) messages.push(strings.rangeUnderflow(formatDate(min, parts)));
19
+ if (details.rangeOverflow && max) messages.push(strings.rangeOverflow(formatDate(max, parts)));
20
+ if (details.badInput) messages.push(strings.unavailableDate);
21
+ return messages.length > 0 ? messages.join(" ") : void 0;
22
+ }
23
+
24
+ export { getDefaultError };
@@ -4,7 +4,7 @@ export interface FieldOwnProps {
4
4
  label: ReactNode;
5
5
  /** 입력창 아래에 보이는 설명. 입력창의 `aria-describedby`로 연결된다. */
6
6
  description?: ReactNode;
7
- /** 오류 메시지. 오류 상태일 때만 보이고 입력창의 `aria-describedby`로 연결된다. */
7
+ /** 오류 메시지. 오류 상태일 때만 보이고 입력창의 `aria-describedby`로 연결된다. 없으면 안쪽 입력의 기본 문구(DatePicker 등)를 보인다. */
8
8
  error?: ReactNode;
9
9
  /** 오류 상태. 기본값은 `error`가 있으면 `true`. `false`를 주면 `error`가 있어도 숨긴다. */
10
10
  invalid?: boolean;
@@ -1,8 +1,10 @@
1
1
  import { cn } from "../_internal/libs/cn.js";
2
+ import { FieldDefaultError } from "./components/FieldDefaultError.js";
3
+ import { FieldErrorMessage } from "./components/FieldErrorMessage.js";
2
4
  import { FieldLabelledControl } from "./components/FieldLabelledControl.js";
5
+ import { FieldRoot } from "./components/FieldRoot.js";
3
6
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
4
7
  import { useId } from "react";
5
- import { CircleExclamationPointIcon } from "@tide-design/icons";
6
8
 
7
9
  // src/Field/Field.tsx
8
10
  const hasContent = (node) => node !== void 0 && node !== null && node !== false && node !== "";
@@ -33,10 +35,12 @@ function Field({ label, description, error, invalid, required = false, disabled
33
35
  className: "ms-0.5 text-danger",
34
36
  children: "*"
35
37
  }) : null] });
36
- return /* @__PURE__ */ jsxs("div", {
38
+ return /* @__PURE__ */ jsxs(FieldRoot, {
37
39
  ...props,
38
- "data-invalid": isInvalid ? "" : void 0,
39
- "data-disabled": disabled ? "" : void 0,
40
+ invalid: isInvalid,
41
+ disabled,
42
+ showError,
43
+ errorId,
40
44
  className: cn("flex flex-col gap-1.5", className),
41
45
  children: [
42
46
  /* @__PURE__ */ jsx(FieldLabelledControl, {
@@ -50,14 +54,10 @@ function Field({ label, description, error, invalid, required = false, disabled
50
54
  className: "text-body-sm text-muted",
51
55
  children: description
52
56
  }) : null,
53
- showError ? /* @__PURE__ */ jsxs("p", {
57
+ showError ? /* @__PURE__ */ jsx(FieldErrorMessage, {
54
58
  id: errorId,
55
- className: "flex items-start gap-1 text-body-sm text-danger",
56
- children: [/* @__PURE__ */ jsx(CircleExclamationPointIcon, {
57
- size: 16,
58
- className: "mt-0.5 shrink-0"
59
- }), /* @__PURE__ */ jsx("span", { children: error })]
60
- }) : null
59
+ children: error
60
+ }) : /* @__PURE__ */ jsx(FieldDefaultError, {})
61
61
  ]
62
62
  });
63
63
  }
@@ -0,0 +1,18 @@
1
+ "use client";
2
+
3
+ import { useFieldErrorContext } from "../hooks/useFieldErrorContext.js";
4
+ import { FieldErrorMessage } from "./FieldErrorMessage.js";
5
+ import { jsx } from "react/jsx-runtime";
6
+
7
+ // src/Field/components/FieldDefaultError.tsx
8
+ /** 앱 `error`가 없을 때 오류 자리. 안쪽 입력이 올린 기본 문구를 보인다. 서버 HTML에는 없다. */
9
+ function FieldDefaultError() {
10
+ const context = useFieldErrorContext();
11
+ if (!context?.defaultError) return null;
12
+ return /* @__PURE__ */ jsx(FieldErrorMessage, {
13
+ id: context.errorId,
14
+ children: context.defaultError
15
+ });
16
+ }
17
+
18
+ export { FieldDefaultError };
@@ -0,0 +1,16 @@
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
+ import { CircleExclamationPointIcon } from "@tide-design/icons";
3
+
4
+ // src/Field/components/FieldErrorMessage.tsx
5
+ function FieldErrorMessage({ id, children }) {
6
+ return /* @__PURE__ */ jsxs("p", {
7
+ id,
8
+ className: "flex items-start gap-1 text-body-sm text-danger",
9
+ children: [/* @__PURE__ */ jsx(CircleExclamationPointIcon, {
10
+ size: 16,
11
+ className: "mt-0.5 shrink-0"
12
+ }), /* @__PURE__ */ jsx("span", { children })]
13
+ });
14
+ }
15
+
16
+ export { FieldErrorMessage };
@@ -1,11 +1,13 @@
1
1
  "use client";
2
2
 
3
3
  import { FieldContext } from "../../_internal/hooks/useFieldControl.js";
4
+ import { useFieldErrorContext } from "../hooks/useFieldErrorContext.js";
4
5
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
5
6
  import { useCallback, useMemo, useRef, useState } from "react";
6
7
 
7
8
  // src/Field/components/FieldLabelledControl.tsx
8
9
  const labelClass = "text-label-md text-default";
10
+ const noop = () => {};
9
11
  function FieldLabelledControl({ value, group, label, children }) {
10
12
  const [hasGroupInput, setHasGroupInput] = useState(false);
11
13
  const labelClickRef = useRef(void 0);
@@ -17,10 +19,20 @@ function FieldLabelledControl({ value, group, label, children }) {
17
19
  setHasGroupInput(false);
18
20
  };
19
21
  }, []);
22
+ const fieldError = useFieldErrorContext();
23
+ const describedBy = fieldError?.defaultError ? [value.describedBy, fieldError.errorId].filter(Boolean).join(" ") : value.describedBy;
24
+ const reportError = fieldError?.reportError ?? noop;
20
25
  const contextValue = useMemo(() => ({
21
26
  ...value,
22
- registerGroupInput
23
- }), [value, registerGroupInput]);
27
+ describedBy,
28
+ registerGroupInput,
29
+ reportError
30
+ }), [
31
+ value,
32
+ describedBy,
33
+ registerGroupInput,
34
+ reportError
35
+ ]);
24
36
  return /* @__PURE__ */ jsxs(Fragment, { children: [group || hasGroupInput ? /* @__PURE__ */ jsx("span", {
25
37
  id: value.labelId,
26
38
  onClick: hasGroupInput ? () => labelClickRef.current?.() : void 0,
@@ -0,0 +1,28 @@
1
+ "use client";
2
+
3
+ import { FieldErrorContext } from "../hooks/useFieldErrorContext.js";
4
+ import { jsx } from "react/jsx-runtime";
5
+ import { useMemo, useState } from "react";
6
+
7
+ // src/Field/components/FieldRoot.tsx
8
+ function FieldRoot({ invalid, disabled, showError, errorId, className, children, ...props }) {
9
+ const [reported, setReported] = useState(void 0);
10
+ const defaultError = showError ? void 0 : reported || void 0;
11
+ const contextValue = useMemo(() => ({
12
+ errorId,
13
+ defaultError,
14
+ reportError: setReported
15
+ }), [errorId, defaultError]);
16
+ return /* @__PURE__ */ jsx(FieldErrorContext, {
17
+ value: contextValue,
18
+ children: /* @__PURE__ */ jsx("div", {
19
+ ...props,
20
+ "data-invalid": invalid || defaultError ? "" : void 0,
21
+ "data-disabled": disabled ? "" : void 0,
22
+ className,
23
+ children
24
+ })
25
+ });
26
+ }
27
+
28
+ export { FieldRoot };
@@ -0,0 +1,11 @@
1
+ "use client";
2
+
3
+ import { createContext, use } from "react";
4
+
5
+ // src/Field/hooks/useFieldErrorContext.ts
6
+ const FieldErrorContext = createContext(null);
7
+ function useFieldErrorContext() {
8
+ return use(FieldErrorContext);
9
+ }
10
+
11
+ export { FieldErrorContext, useFieldErrorContext };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tide-design/components",
3
- "version": "0.27.0",
3
+ "version": "0.28.0",
4
4
  "description": "Tide 디자인 시스템의 React Component",
5
5
  "keywords": [
6
6
  "accessibility",
@@ -38,8 +38,8 @@
38
38
  "@maskito/core": "5.6.1",
39
39
  "@maskito/kit": "5.6.1",
40
40
  "@maskito/react": "5.6.1",
41
- "@tide-design/foundation": "0.27.0",
42
- "@tide-design/icons": "0.27.0",
41
+ "@tide-design/foundation": "0.28.0",
42
+ "@tide-design/icons": "0.28.0",
43
43
  "clsx": "^2.1.1",
44
44
  "react-aria": "^3.53.1",
45
45
  "react-number-format": "^5.4.5",