@kapiuara/ui 0.3.1 → 0.4.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.
@@ -129,10 +129,10 @@ var u = {
129
129
  "aria-expanded": $,
130
130
  "aria-controls": N,
131
131
  "aria-activedescendant": $ && Q >= 0 ? F(Q) : void 0,
132
- "aria-busy": X || void 0,
133
132
  ...t(A ? null : je, {
134
133
  id: M,
135
134
  describedBy: n(P, O["aria-describedby"]),
135
+ busy: X || void 0,
136
136
  invalid: x ? x === "err" : void 0
137
137
  }),
138
138
  required: C,
@@ -1,6 +1,10 @@
1
1
  import { type HTMLAttributes, type ReactNode } from "react";
2
2
  import { type KapiFieldContextValue } from "./fieldLogic";
3
- export type KapiFieldStatus = "ok" | "err";
3
+ /**
4
+ * `pending` es para mientras una validación asíncrona resuelve: el campo se marca ocupado pero NO
5
+ * inválido, porque pintarlo en rojo antes de saber el resultado le miente a quien lo está usando.
6
+ */
7
+ export type KapiFieldStatus = "ok" | "err" | "pending";
4
8
  export type { KapiFieldContextValue };
5
9
  /** Atributos nativos (`id`, `style`, `data-*`…) van al contenedor; el `id` del control es `htmlFor`. */
6
10
  export type KapiFieldProps = Omit<HTMLAttributes<HTMLDivElement>, "children"> & {
@@ -14,7 +18,11 @@ export type KapiFieldProps = Omit<HTMLAttributes<HTMLDivElement>, "children"> &
14
18
  help?: ReactNode | undefined;
15
19
  /** `id` del texto de ayuda. Opcional: sin él se deriva del id del control. */
16
20
  helpId?: string | undefined;
17
- /** Estado de validación: pinta la ayuda y le pone ícono; con `err` el control queda `aria-invalid`. */
21
+ /**
22
+ * Estado de validación: pinta la ayuda y le pone ícono. Con `err` el control queda `aria-invalid`
23
+ * y la ayuda interrumpe (`role="alert"`); con `pending` queda `aria-busy` y la ayuda se anuncia
24
+ * sin interrumpir (`aria-live="polite"`), que es lo que corresponde cuando todavía no pasó nada.
25
+ */
18
26
  status?: KapiFieldStatus | undefined;
19
27
  /** El control del campo (input, select…); recibe `id`, `aria-describedby` y estado del contexto. */
20
28
  children: ReactNode;
@@ -2,54 +2,61 @@
2
2
  import { fieldIds as e } from "./fieldLogic.js";
3
3
  import { jsx as t, jsxs as n } from "react/jsx-runtime";
4
4
  import { createContext as r, useContext as i, useId as a, useMemo as o } from "react";
5
- import { AlertTriangle as s, CheckCircle2 as c } from "lucide-react";
5
+ import { AlertTriangle as s, CheckCircle2 as c, LoaderCircle as l } from "lucide-react";
6
6
  //#region src/components/inputs/KapiField.tsx
7
- var l = r(null), u = () => i(l), d = (e = "") => ["kapi-field", e].filter(Boolean).join(" "), f = (e) => ["kapi-help", e && `kapi-help--${e}`].filter(Boolean).join(" "), p = ({ label: r, htmlFor: i, required: u = !1, help: p, helpId: m, status: h, className: g, children: _, ...v }) => {
8
- let y = a(), b = !!p, { controlId: x, labelId: S, helpId: C } = e({
7
+ var u = r(null), d = () => i(u), f = (e = "") => ["kapi-field", e].filter(Boolean).join(" "), p = (e) => ["kapi-help", e && `kapi-help--${e}`].filter(Boolean).join(" "), m = ({ label: r, htmlFor: i, required: d = !1, help: m, helpId: h, status: g, className: _, children: v, ...y }) => {
8
+ let b = a(), x = !!m, { controlId: S, labelId: C, helpId: w } = e({
9
9
  htmlFor: i,
10
- autoId: y,
11
- helpId: m,
12
- hasHelp: b
13
- }), w = o(() => ({
14
- controlId: x,
15
- labelId: r == null ? void 0 : S,
16
- helpId: C,
17
- required: u,
18
- invalid: h === "err"
10
+ autoId: b,
11
+ helpId: h,
12
+ hasHelp: x
13
+ }), T = o(() => ({
14
+ controlId: S,
15
+ labelId: r == null ? void 0 : C,
16
+ helpId: w,
17
+ required: d,
18
+ invalid: g === "err",
19
+ busy: g === "pending"
19
20
  }), [
20
- x,
21
21
  S,
22
- r,
23
22
  C,
24
- u,
25
- h
23
+ r,
24
+ w,
25
+ d,
26
+ g
26
27
  ]);
27
- return /* @__PURE__ */ t(l.Provider, {
28
- value: w,
28
+ return /* @__PURE__ */ t(u.Provider, {
29
+ value: T,
29
30
  children: /* @__PURE__ */ n("div", {
30
- ...v,
31
- className: d(g),
31
+ ...y,
32
+ className: f(_),
32
33
  children: [
33
34
  r != null && /* @__PURE__ */ n("label", {
34
- id: S,
35
- htmlFor: x,
36
- children: [r, u && " *"]
37
- }),
38
- _,
39
- b ? /* @__PURE__ */ n("span", {
40
35
  id: C,
41
- className: f(h),
42
- role: h === "err" ? "alert" : void 0,
36
+ htmlFor: S,
37
+ children: [r, d && " *"]
38
+ }),
39
+ v,
40
+ x ? /* @__PURE__ */ n("span", {
41
+ id: w,
42
+ className: p(g),
43
+ role: g === "err" ? "alert" : void 0,
44
+ "aria-live": g === "pending" ? "polite" : void 0,
43
45
  children: [
44
- h === "ok" && /* @__PURE__ */ t(c, {
46
+ g === "ok" && /* @__PURE__ */ t(c, {
45
47
  size: 13,
46
48
  strokeWidth: 2.2
47
49
  }),
48
- h === "err" && /* @__PURE__ */ t(s, {
50
+ g === "err" && /* @__PURE__ */ t(s, {
49
51
  size: 13,
50
52
  strokeWidth: 2.2
51
53
  }),
52
- p
54
+ g === "pending" && /* @__PURE__ */ t(l, {
55
+ size: 13,
56
+ strokeWidth: 2.2,
57
+ className: "kapi-spinner"
58
+ }),
59
+ m
53
60
  ]
54
61
  }) : null
55
62
  ]
@@ -57,4 +64,4 @@ var l = r(null), u = () => i(l), d = (e = "") => ["kapi-field", e].filter(Boolea
57
64
  });
58
65
  };
59
66
  //#endregion
60
- export { p as KapiField, d as fieldClasses, f as helpClasses, u as useKapiField };
67
+ export { m as KapiField, f as fieldClasses, p as helpClasses, d as useKapiField };
@@ -6,6 +6,8 @@ export type KapiFieldContextValue = {
6
6
  helpId?: string | undefined;
7
7
  required: boolean;
8
8
  invalid: boolean;
9
+ /** El campo está esperando una validación asíncrona: ocupado, pero todavía no inválido. */
10
+ busy: boolean;
9
11
  };
10
12
  /** Suma ids para `aria-describedby`/`aria-labelledby`: sin vacíos ni repetidos; `undefined` si no queda ninguno. */
11
13
  export declare const joinIds: (...ids: (string | undefined | null | false)[]) => string | undefined;
@@ -30,6 +32,7 @@ export declare const fieldControlProps: (ctx: KapiFieldContextValue | null, own:
30
32
  describedBy?: string | undefined;
31
33
  invalid?: boolean | undefined;
32
34
  required?: boolean | undefined;
35
+ busy?: boolean | undefined;
33
36
  labelable?: boolean | undefined;
34
37
  ariaLabel?: string | undefined;
35
38
  }) => {
@@ -37,6 +40,7 @@ export declare const fieldControlProps: (ctx: KapiFieldContextValue | null, own:
37
40
  "aria-describedby": string | undefined;
38
41
  "aria-invalid": true | undefined;
39
42
  "aria-required": true | undefined;
43
+ "aria-busy": true | undefined;
40
44
  "aria-labelledby": string | undefined;
41
45
  };
42
46
  /**
@@ -49,6 +53,7 @@ export declare const nativeFieldProps: (ctx: KapiFieldContextValue | null, own:
49
53
  describedBy?: string | undefined;
50
54
  invalid?: boolean | undefined;
51
55
  required?: boolean | undefined;
56
+ busy?: boolean | undefined;
52
57
  ariaInvalid?: AriaAttributes["aria-invalid"] | undefined;
53
58
  ariaRequired?: AriaAttributes["aria-required"] | undefined;
54
59
  takeId?: boolean | undefined;
@@ -57,6 +62,7 @@ export declare const nativeFieldProps: (ctx: KapiFieldContextValue | null, own:
57
62
  "aria-describedby": string | undefined;
58
63
  "aria-invalid": boolean | "true" | "false" | "grammar" | "spelling" | undefined;
59
64
  "aria-required": (boolean | "true" | "false") | undefined;
65
+ "aria-busy": true | undefined;
60
66
  };
61
67
  /**
62
68
  * ¿El foco sale del control? `next` es el `relatedTarget` del blur; `containers` son las partes del
@@ -14,19 +14,22 @@ var e = (...e) => {
14
14
  "aria-describedby": e(t?.helpId, n.describedBy),
15
15
  "aria-invalid": n.invalid ?? t?.invalid ? !0 : void 0,
16
16
  "aria-required": n.required ?? t?.required ? !0 : void 0,
17
+ "aria-busy": n.busy ?? t?.busy ? !0 : void 0,
17
18
  "aria-labelledby": n.labelable === !1 && !n.ariaLabel ? t?.labelId : void 0
18
19
  }), r = (e, t) => {
19
20
  let r = n(e, {
20
21
  id: t.id,
21
22
  describedBy: t.describedBy,
22
23
  invalid: t.invalid,
23
- required: t.required
24
+ required: t.required,
25
+ busy: t.busy
24
26
  });
25
27
  return {
26
28
  id: t.takeId === !1 ? t.id : r.id,
27
29
  "aria-describedby": r["aria-describedby"],
28
30
  "aria-invalid": t.ariaInvalid ?? r["aria-invalid"],
29
- "aria-required": t.ariaRequired ?? r["aria-required"]
31
+ "aria-required": t.ariaRequired ?? r["aria-required"],
32
+ "aria-busy": r["aria-busy"]
30
33
  };
31
34
  }, i = (e, ...t) => !(e instanceof Node && t.some((t) => t?.contains(e))), a = (e, t) => {
32
35
  typeof e == "function" ? e(t) : e && (e.current = t);
@@ -4,8 +4,13 @@ export type KapiWizardStep = {
4
4
  title: string;
5
5
  desc?: ReactNode | undefined;
6
6
  content: ReactNode;
7
- /** Si devuelve false, "Siguiente"/"Finalizar" NO avanza (validación por paso). */
8
- validate?: (() => boolean) | undefined;
7
+ /**
8
+ * Si devuelve `false`, "Siguiente"/"Finalizar" NO avanza. Puede devolver una promesa: mientras
9
+ * resuelve, el wizard marca los botones como ocupados y no deja volver a apretar. Cada paso
10
+ * decide por su cuenta, así que un wizard puede mezclar pasos sincrónicos con otros que
11
+ * consultan al servidor. Una promesa rechazada se trata como `false`: no avanza.
12
+ */
13
+ validate?: (() => boolean | Promise<boolean>) | undefined;
9
14
  };
10
15
  export type KapiWizardOrientation = "horizontal" | "vertical";
11
16
  export type KapiWizardLabels = {
@@ -13,6 +18,8 @@ export type KapiWizardLabels = {
13
18
  cancel: string;
14
19
  back: string;
15
20
  next: string;
21
+ /** Reemplaza al texto de avanzar mientras un `validate` asíncrono está resolviendo. */
22
+ validating: string;
16
23
  };
17
24
  export declare const DEFAULT_WIZARD_LABELS: KapiWizardLabels;
18
25
  /** Atributos nativos (`id`, `style`, `data-*`…) van al contenedor del wizard. */
@@ -2,103 +2,120 @@
2
2
  import { KapiButton as e } from "../inputs/KapiButton.js";
3
3
  import { useLabels as t } from "../locale/KapiLocaleProvider.js";
4
4
  import { joinClasses as n } from "../rootProps.js";
5
- import { canJumpTo as r, clampStep as i, resolveAdvance as a } from "./wizardLogic.js";
6
- import { Fragment as o, jsx as s, jsxs as c } from "react/jsx-runtime";
7
- import { useState as l } from "react";
8
- import { Check as u, ChevronLeft as d, ChevronRight as f } from "lucide-react";
5
+ import { canJumpTo as r, clampStep as i, isThenable as a, resolveAdvance as o } from "./wizardLogic.js";
6
+ import { Fragment as s, jsx as c, jsxs as l } from "react/jsx-runtime";
7
+ import { useRef as u, useState as d } from "react";
8
+ import { Check as f, ChevronLeft as p, ChevronRight as m } from "lucide-react";
9
9
  //#region src/components/navigation/KapiWizard.tsx
10
- var p = {
10
+ var h = {
11
11
  finish: "Finalizar",
12
12
  cancel: "Cancelar",
13
13
  back: "Atrás",
14
- next: "Siguiente"
15
- }, m = {
14
+ next: "Siguiente",
15
+ validating: "Validando…"
16
+ }, g = {
16
17
  size: 15,
17
18
  strokeWidth: 2.2
18
- }, h = (e, t) => e === void 0 ? t : e, g = ({ steps: g, orientation: _ = "horizontal", onFinish: v, onCancel: y, finishLabel: b, cancelLabel: x, backLabel: S, nextLabel: C, labels: w, step: T, defaultStep: E = 0, onStepChange: D, className: O, ...k }) => {
19
- let A = t("wizard", p, w), [j, M] = l(E), N = i(T ?? j, g.length), P = g[N];
20
- if (!P) return null;
21
- let F = N === g.length - 1, I = (e) => {
22
- let t = i(e, g.length);
23
- t !== N && (T === void 0 && M(t), D?.(t));
24
- }, L = (e) => {
25
- r(e, N) && I(e);
26
- }, R = () => {
27
- let e = a(g, N);
28
- e === "next" ? I(N + 1) : e === "finish" && v?.();
29
- }, z = /* @__PURE__ */ c("div", {
19
+ }, _ = (e, t) => e === void 0 ? t : e, v = ({ steps: v, orientation: y = "horizontal", onFinish: b, onCancel: x, finishLabel: S, cancelLabel: C, backLabel: w, nextLabel: T, labels: E, step: D, defaultStep: O = 0, onStepChange: k, className: A, ...j }) => {
20
+ let M = t("wizard", h, E), [N, P] = d(O), [F, I] = d(!1), L = u(0), R = u(0), z = i(D ?? N, v.length);
21
+ R.current = z;
22
+ let B = v[z];
23
+ if (!B) return null;
24
+ let V = z === v.length - 1, H = (e) => {
25
+ let t = i(e, v.length);
26
+ t !== z && (D === void 0 && P(t), k?.(t));
27
+ }, U = (e) => {
28
+ r(e, z) && H(e);
29
+ }, W = (e, t) => {
30
+ let n = o(v, e, t);
31
+ n === "next" ? H(e + 1) : n === "finish" && b?.();
32
+ }, G = (e, t, n) => {
33
+ e === L.current && (I(!1), t === R.current && W(t, n));
34
+ }, K = () => {
35
+ if (F) return;
36
+ let e = !B.validate || B.validate();
37
+ if (!a(e)) {
38
+ W(z, !!e);
39
+ return;
40
+ }
41
+ let t = L.current += 1, n = z;
42
+ I(!0), e.then((e) => G(t, n, !!e), () => G(t, n, !1));
43
+ }, q = /* @__PURE__ */ l("div", {
30
44
  className: "kapi-wz__nav",
31
- children: [/* @__PURE__ */ s(e, {
32
- onClick: N === 0 ? y : () => I(N - 1),
33
- children: N === 0 ? h(x, A.cancel) : /* @__PURE__ */ c(o, { children: [/* @__PURE__ */ s(d, { ...m }), h(S, A.back)] })
34
- }), /* @__PURE__ */ s(e, {
45
+ children: [/* @__PURE__ */ c(e, {
46
+ onClick: z === 0 ? x : () => H(z - 1),
47
+ disabled: F,
48
+ children: z === 0 ? _(C, M.cancel) : /* @__PURE__ */ l(s, { children: [/* @__PURE__ */ c(p, { ...g }), _(w, M.back)] })
49
+ }), /* @__PURE__ */ c(e, {
35
50
  variant: "primary",
36
- onClick: R,
37
- children: F ? h(b, A.finish) : /* @__PURE__ */ c(o, { children: [h(C, A.next), /* @__PURE__ */ s(f, { ...m })] })
51
+ onClick: K,
52
+ disabled: F,
53
+ "aria-busy": F || void 0,
54
+ children: F ? M.validating : V ? _(S, M.finish) : /* @__PURE__ */ l(s, { children: [_(T, M.next), /* @__PURE__ */ c(m, { ...g })] })
38
55
  })]
39
- }), B = /* @__PURE__ */ s("div", {
56
+ }), J = /* @__PURE__ */ c("div", {
40
57
  className: "kapi-wz__pane",
41
- children: P.content
42
- }, N);
43
- return _ === "vertical" ? /* @__PURE__ */ c("div", {
44
- ...k,
45
- className: n("kapi-wz kapi-wz--v", O),
46
- children: [/* @__PURE__ */ s("div", {
58
+ children: B.content
59
+ }, z);
60
+ return y === "vertical" ? /* @__PURE__ */ l("div", {
61
+ ...j,
62
+ className: n("kapi-wz kapi-wz--v", A),
63
+ children: [/* @__PURE__ */ c("div", {
47
64
  className: "kapi-wz__rail",
48
- children: g.map((e, t) => /* @__PURE__ */ c("button", {
65
+ children: v.map((e, t) => /* @__PURE__ */ l("button", {
49
66
  type: "button",
50
- className: `kapi-wz__vstep${t === N ? " is-on" : ""}`,
51
- "aria-current": t === N ? "step" : void 0,
52
- onClick: () => L(t),
53
- children: [/* @__PURE__ */ s("span", {
54
- className: `kapi-step__dot${t < N ? " kapi-step__dot--done" : ""}${t === N ? " kapi-step__dot--on" : ""}`,
55
- children: t < N ? /* @__PURE__ */ s(u, {
67
+ className: `kapi-wz__vstep${t === z ? " is-on" : ""}`,
68
+ "aria-current": t === z ? "step" : void 0,
69
+ onClick: () => U(t),
70
+ children: [/* @__PURE__ */ c("span", {
71
+ className: `kapi-step__dot${t < z ? " kapi-step__dot--done" : ""}${t === z ? " kapi-step__dot--on" : ""}`,
72
+ children: t < z ? /* @__PURE__ */ c(f, {
56
73
  size: 13,
57
74
  strokeWidth: 3
58
75
  }) : t + 1
59
- }), /* @__PURE__ */ c("span", {
76
+ }), /* @__PURE__ */ l("span", {
60
77
  className: "kapi-wz__vlabel",
61
- children: [/* @__PURE__ */ s("b", { children: e.title }), e.desc && /* @__PURE__ */ s("span", { children: e.desc })]
78
+ children: [/* @__PURE__ */ c("b", { children: e.title }), e.desc && /* @__PURE__ */ c("span", { children: e.desc })]
62
79
  })]
63
80
  }, `${t}-${e.title}`))
64
- }), /* @__PURE__ */ c("div", {
81
+ }), /* @__PURE__ */ l("div", {
65
82
  className: "kapi-wz__main",
66
- children: [B, z]
83
+ children: [J, q]
67
84
  })]
68
- }) : /* @__PURE__ */ c("div", {
69
- ...k,
70
- className: n("kapi-wz", O),
85
+ }) : /* @__PURE__ */ l("div", {
86
+ ...j,
87
+ className: n("kapi-wz", A),
71
88
  children: [
72
- /* @__PURE__ */ s("div", {
89
+ /* @__PURE__ */ c("div", {
73
90
  className: "kapi-wz__steps",
74
- children: g.map((e, t) => /* @__PURE__ */ c("button", {
91
+ children: v.map((e, t) => /* @__PURE__ */ l("button", {
75
92
  type: "button",
76
- className: `kapi-wz__step${t === N ? " is-on" : ""}${t < N ? " is-done" : ""}`,
77
- "aria-current": t === N ? "step" : void 0,
78
- onClick: () => L(t),
93
+ className: `kapi-wz__step${t === z ? " is-on" : ""}${t < z ? " is-done" : ""}`,
94
+ "aria-current": t === z ? "step" : void 0,
95
+ onClick: () => U(t),
79
96
  children: [
80
- /* @__PURE__ */ s("span", {
97
+ /* @__PURE__ */ c("span", {
81
98
  className: "kapi-wz__num",
82
- children: t < N ? /* @__PURE__ */ s(u, {
99
+ children: t < z ? /* @__PURE__ */ c(f, {
83
100
  size: 16,
84
101
  strokeWidth: 3
85
102
  }) : t + 1
86
103
  }),
87
- /* @__PURE__ */ s("span", {
104
+ /* @__PURE__ */ c("span", {
88
105
  className: "kapi-wz__st",
89
106
  children: e.title
90
107
  }),
91
- e.desc && /* @__PURE__ */ s("span", {
108
+ e.desc && /* @__PURE__ */ c("span", {
92
109
  className: "kapi-wz__ss",
93
110
  children: e.desc
94
111
  })
95
112
  ]
96
113
  }, `${t}-${e.title}`))
97
114
  }),
98
- B,
99
- z
115
+ J,
116
+ q
100
117
  ]
101
118
  });
102
119
  };
103
120
  //#endregion
104
- export { p as DEFAULT_WIZARD_LABELS, g as KapiWizard };
121
+ export { h as DEFAULT_WIZARD_LABELS, v as KapiWizard };
@@ -1,11 +1,21 @@
1
1
  /** Lo mínimo que la lógica necesita de un paso. */
2
2
  export type WizardStepLike = {
3
- validate?: (() => boolean) | undefined;
3
+ validate?: (() => boolean | Promise<boolean>) | undefined;
4
4
  };
5
5
  export type WizardAdvance = "blocked" | "next" | "finish";
6
6
  /** Mantiene el índice dentro de [0, total-1]. Con 0 pasos devuelve 0. */
7
7
  export declare const clampStep: (index: number, total: number) => number;
8
8
  /** Sólo se puede saltar hacia atrás por click (nunca adelante). */
9
9
  export declare const canJumpTo: (target: number, current: number) => boolean;
10
- /** Qué pasa al apretar "Siguiente": respeta `validate()` del paso actual. */
11
- export declare const resolveAdvance: (steps: readonly WizardStepLike[], current: number) => WizardAdvance;
10
+ /**
11
+ * Un `validate` que devuelve una promesa. Se mira el `then` y no `instanceof Promise`: un
12
+ * consumidor puede devolver la promesa de otra realm o de una librería, y seguiría siendo
13
+ * esperable.
14
+ */
15
+ export declare const isThenable: (value: unknown) => value is Promise<boolean>;
16
+ /**
17
+ * Qué pasa al apretar "Siguiente", una vez que YA se sabe si el paso dio por válido. No llama a
18
+ * `validate`: quien la llama decide si esperó una promesa o no, y así esta función queda pura y
19
+ * sirve igual para el camino sincrónico y para el asíncrono.
20
+ */
21
+ export declare const resolveAdvance: (steps: readonly WizardStepLike[], current: number, valid: boolean) => WizardAdvance;
@@ -1,7 +1,4 @@
1
1
  //#region src/components/navigation/wizardLogic.ts
2
- var e = (e, t) => t <= 0 ? 0 : Math.min(Math.max(Math.trunc(e) || 0, 0), t - 1), t = (e, t) => e < t, n = (e, t) => {
3
- let n = e[t];
4
- return !n || n.validate && !n.validate() ? "blocked" : t >= e.length - 1 ? "finish" : "next";
5
- };
2
+ var e = (e, t) => t <= 0 ? 0 : Math.min(Math.max(Math.trunc(e) || 0, 0), t - 1), t = (e, t) => e < t, n = (e) => typeof e?.then == "function", r = (e, t, n) => !e[t] || !n ? "blocked" : t >= e.length - 1 ? "finish" : "next";
6
3
  //#endregion
7
- export { t as canJumpTo, e as clampStep, n as resolveAdvance };
4
+ export { t as canJumpTo, e as clampStep, n as isThenable, r as resolveAdvance };