@wickermoney/ui-kit 0.3.0 → 0.4.1
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/README.md +11 -1
- package/dist/Field.d.ts +2 -2
- package/dist/Field.d.ts.map +1 -1
- package/dist/Field.js +3 -2
- package/dist/Field.js.map +1 -1
- package/dist/FieldCommon.d.ts +11 -0
- package/dist/FieldCommon.d.ts.map +1 -1
- package/dist/FieldCommon.js +13 -1
- package/dist/FieldCommon.js.map +1 -1
- package/dist/FormError.d.ts +15 -0
- package/dist/FormError.d.ts.map +1 -0
- package/dist/FormError.js +15 -0
- package/dist/FormError.js.map +1 -0
- package/dist/FormErrors.d.ts +23 -0
- package/dist/FormErrors.d.ts.map +1 -0
- package/dist/FormErrors.js +12 -0
- package/dist/FormErrors.js.map +1 -0
- package/dist/SelectField.d.ts +2 -2
- package/dist/SelectField.d.ts.map +1 -1
- package/dist/SelectField.js +3 -2
- package/dist/SelectField.js.map +1 -1
- package/dist/ValidationIssue.d.ts +15 -0
- package/dist/ValidationIssue.d.ts.map +1 -0
- package/dist/ValidationIssue.js +2 -0
- package/dist/ValidationIssue.js.map +1 -0
- package/dist/components.css +10 -0
- package/dist/formErrorsFrom.d.ts +25 -0
- package/dist/formErrorsFrom.d.ts.map +1 -0
- package/dist/formErrorsFrom.js +41 -0
- package/dist/formErrorsFrom.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/useFormErrors.d.ts +29 -0
- package/dist/useFormErrors.d.ts.map +1 -0
- package/dist/useFormErrors.js +47 -0
- package/dist/useFormErrors.js.map +1 -0
- package/dist/validationIssuesOf.d.ts +14 -0
- package/dist/validationIssuesOf.d.ts.map +1 -0
- package/dist/validationIssuesOf.js +26 -0
- package/dist/validationIssuesOf.js.map +1 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -48,7 +48,8 @@ A federated remote's emitted stylesheet is never requested.
|
|
|
48
48
|
|---|---|
|
|
49
49
|
| `Surface` | Card/panel with an optional title and header action |
|
|
50
50
|
| `Button` | `variant`: `default`, `primary` or `danger` |
|
|
51
|
-
| `Field`, `SelectField` | Labelled form inputs |
|
|
51
|
+
| `Field`, `SelectField` | Labelled form inputs; optional `hint` and `error` (wires `aria-invalid` and `aria-describedby`) |
|
|
52
|
+
| `FormError` | A form-level error, shown beside the form's submit button |
|
|
52
53
|
| `Table` | Typed table: `columns`, `rows`, `rowKey`; `numeric` columns right-align |
|
|
53
54
|
| `Stat` | Label + value; `tone="auto"` colours by sign |
|
|
54
55
|
| `CategoryOptions`, `orderByParent` | Category `<option>` list grouped under parents |
|
|
@@ -56,6 +57,15 @@ A federated remote's emitted stylesheet is never requested.
|
|
|
56
57
|
| `EmptyState` | Title, hint and an optional action for empty views |
|
|
57
58
|
| `Spinner` | Loading indicator |
|
|
58
59
|
|
|
60
|
+
### Form errors
|
|
61
|
+
|
|
62
|
+
`useFormErrors()` holds a form's errors and, after `show()`, moves focus to the
|
|
63
|
+
first invalid field (or the `FormError`). `formErrorsFrom(error, fields,
|
|
64
|
+
fallback)` maps an API error's `issues: { path, message }[]` to your field
|
|
65
|
+
names and returns anything left over as one form-level message;
|
|
66
|
+
`validationIssuesOf(error)` reads the issues on their own. `NO_FORM_ERRORS`
|
|
67
|
+
and `hasFormErrors` cover the empty case.
|
|
68
|
+
|
|
59
69
|
Every component's props type is exported alongside it (`SurfaceProps`,
|
|
60
70
|
`TableProps<T>`, ...).
|
|
61
71
|
|
package/dist/Field.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type InputHTMLAttributes } from 'react';
|
|
2
|
-
import type
|
|
2
|
+
import { type FieldCommon } from './FieldCommon.js';
|
|
3
3
|
/** Props for {@link Field}: the shared field props plus every native `<input>` attribute. */
|
|
4
4
|
export type FieldProps = FieldCommon & InputHTMLAttributes<HTMLInputElement>;
|
|
5
5
|
/** A labelled text input. The label is always associated, never a bare placeholder. */
|
|
6
|
-
export declare function Field({ label, error, ...rest }: FieldProps): import("react").JSX.Element;
|
|
6
|
+
export declare function Field({ label, error, hint, ...rest }: FieldProps): import("react").JSX.Element;
|
|
7
7
|
//# sourceMappingURL=Field.d.ts.map
|
package/dist/Field.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Field.d.ts","sourceRoot":"","sources":["../src/Field.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,mBAAmB,EAAE,MAAM,OAAO,CAAA;AACvD,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"Field.d.ts","sourceRoot":"","sources":["../src/Field.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,mBAAmB,EAAE,MAAM,OAAO,CAAA;AACvD,OAAO,EAAe,KAAK,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEhE,6FAA6F;AAC7F,MAAM,MAAM,UAAU,GAAG,WAAW,GAAG,mBAAmB,CAAC,gBAAgB,CAAC,CAAA;AAE5E,uFAAuF;AACvF,wBAAgB,KAAK,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,EAAE,UAAU,+BAkBhE"}
|
package/dist/Field.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { useId } from 'react';
|
|
3
|
+
import { describedBy } from './FieldCommon.js';
|
|
3
4
|
/** A labelled text input. The label is always associated, never a bare placeholder. */
|
|
4
|
-
export function Field({ label, error, ...rest }) {
|
|
5
|
+
export function Field({ label, error, hint, ...rest }) {
|
|
5
6
|
const id = useId();
|
|
6
|
-
return (_jsxs("div", { className: "wm-field", children: [_jsx("label", { className: "wm-field__label", htmlFor: id, children: label }), _jsx("input", { id: id, className: "wm-field__control", "aria-invalid": error !== undefined, "aria-describedby": error !== undefined ? `${id}-
|
|
7
|
+
return (_jsxs("div", { className: "wm-field", children: [_jsx("label", { className: "wm-field__label", htmlFor: id, children: label }), _jsx("input", { id: id, className: "wm-field__control", "aria-invalid": error !== undefined, "aria-describedby": describedBy(id, hint, error), ...rest }), hint !== undefined ? _jsx("span", { className: "wm-field__hint", id: `${id}-hint`, children: hint }) : null, error !== undefined ? (_jsx("span", { className: "wm-field__error", id: `${id}-error`, children: error })) : null] }));
|
|
7
8
|
}
|
|
8
9
|
//# sourceMappingURL=Field.js.map
|
package/dist/Field.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Field.js","sourceRoot":"","sources":["../src/Field.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,KAAK,EAA4B,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"Field.js","sourceRoot":"","sources":["../src/Field.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,KAAK,EAA4B,MAAM,OAAO,CAAA;AACvD,OAAO,EAAE,WAAW,EAAoB,MAAM,kBAAkB,CAAA;AAKhE,uFAAuF;AACvF,MAAM,UAAU,KAAK,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,IAAI,EAAc;IAC/D,MAAM,EAAE,GAAG,KAAK,EAAE,CAAA;IAClB,OAAO,CACL,eAAK,SAAS,EAAC,UAAU,aACvB,gBAAO,SAAS,EAAC,iBAAiB,EAAC,OAAO,EAAE,EAAE,YAAG,KAAK,GAAS,EAC/D,gBACE,EAAE,EAAE,EAAE,EACN,SAAS,EAAC,mBAAmB,kBACf,KAAK,KAAK,SAAS,sBACf,WAAW,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,KAC1C,IAAI,GACR,EACD,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,eAAM,SAAS,EAAC,gBAAgB,EAAC,EAAE,EAAE,GAAG,EAAE,OAAO,YAAG,IAAI,GAAQ,CAAC,CAAC,CAAC,IAAI,EAC5F,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,CACrB,eAAM,SAAS,EAAC,iBAAiB,EAAC,EAAE,EAAE,GAAG,EAAE,QAAQ,YAAG,KAAK,GAAQ,CACpE,CAAC,CAAC,CAAC,IAAI,IACJ,CACP,CAAA;AACH,CAAC"}
|
package/dist/FieldCommon.d.ts
CHANGED
|
@@ -4,5 +4,16 @@ export interface FieldCommon {
|
|
|
4
4
|
readonly label: string;
|
|
5
5
|
/** Validation message shown under the control. Also marks the control invalid. */
|
|
6
6
|
readonly error?: string;
|
|
7
|
+
/** A short note under the control, such as what an empty value means. Read with the control. */
|
|
8
|
+
readonly hint?: string;
|
|
7
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* The ids a control is described by: its hint, then its error.
|
|
12
|
+
*
|
|
13
|
+
* @param id - The control's id.
|
|
14
|
+
* @param hint - The hint, if there is one.
|
|
15
|
+
* @param error - The error, if there is one.
|
|
16
|
+
* @returns The `aria-describedby` value, or `undefined` when there is nothing.
|
|
17
|
+
*/
|
|
18
|
+
export declare function describedBy(id: string, hint: string | undefined, error: string | undefined): string | undefined;
|
|
8
19
|
//# sourceMappingURL=FieldCommon.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FieldCommon.d.ts","sourceRoot":"","sources":["../src/FieldCommon.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,MAAM,WAAW,WAAW;IAC1B,yDAAyD;IACzD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,kFAAkF;IAClF,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"FieldCommon.d.ts","sourceRoot":"","sources":["../src/FieldCommon.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,MAAM,WAAW,WAAW;IAC1B,yDAAyD;IACzD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,kFAAkF;IAClF,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;IACvB,gGAAgG;IAChG,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CACvB;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,EAAE,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAI/G"}
|
package/dist/FieldCommon.js
CHANGED
|
@@ -1,2 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The ids a control is described by: its hint, then its error.
|
|
3
|
+
*
|
|
4
|
+
* @param id - The control's id.
|
|
5
|
+
* @param hint - The hint, if there is one.
|
|
6
|
+
* @param error - The error, if there is one.
|
|
7
|
+
* @returns The `aria-describedby` value, or `undefined` when there is nothing.
|
|
8
|
+
*/
|
|
9
|
+
export function describedBy(id, hint, error) {
|
|
10
|
+
const ids = [hint !== undefined ? `${id}-hint` : null, error !== undefined ? `${id}-error` : null]
|
|
11
|
+
.filter((x) => x !== null);
|
|
12
|
+
return ids.length > 0 ? ids.join(' ') : undefined;
|
|
13
|
+
}
|
|
2
14
|
//# sourceMappingURL=FieldCommon.js.map
|
package/dist/FieldCommon.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FieldCommon.js","sourceRoot":"","sources":["../src/FieldCommon.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"FieldCommon.js","sourceRoot":"","sources":["../src/FieldCommon.ts"],"names":[],"mappings":"AAUA;;;;;;;GAOG;AACH,MAAM,UAAU,WAAW,CAAC,EAAU,EAAE,IAAwB,EAAE,KAAyB;IACzF,MAAM,GAAG,GAAG,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC;SAC/F,MAAM,CAAC,CAAC,CAAC,EAAe,EAAE,CAAC,CAAC,KAAK,IAAI,CAAC,CAAA;IACzC,OAAO,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;AACnD,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Props for {@link FormError}. */
|
|
2
|
+
export interface FormErrorProps {
|
|
3
|
+
/** The message, or `null` to render nothing. */
|
|
4
|
+
readonly message: string | null;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* A form's own error, for problems that are not about one field.
|
|
8
|
+
*
|
|
9
|
+
* Place it beside the submit button, not at the top of the page: that is
|
|
10
|
+
* where the person is looking when the form refuses. {@link useFormErrors}
|
|
11
|
+
* moves focus here when there is no field to focus instead, which also
|
|
12
|
+
* scrolls it into view.
|
|
13
|
+
*/
|
|
14
|
+
export declare function FormError({ message }: FormErrorProps): import("react").JSX.Element | null;
|
|
15
|
+
//# sourceMappingURL=FormError.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormError.d.ts","sourceRoot":"","sources":["../src/FormError.tsx"],"names":[],"mappings":"AAAA,mCAAmC;AACnC,MAAM,WAAW,cAAc;IAC7B,gDAAgD;IAChD,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;CAChC;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,EAAE,OAAO,EAAE,EAAE,cAAc,sCAGpD"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* A form's own error, for problems that are not about one field.
|
|
4
|
+
*
|
|
5
|
+
* Place it beside the submit button, not at the top of the page: that is
|
|
6
|
+
* where the person is looking when the form refuses. {@link useFormErrors}
|
|
7
|
+
* moves focus here when there is no field to focus instead, which also
|
|
8
|
+
* scrolls it into view.
|
|
9
|
+
*/
|
|
10
|
+
export function FormError({ message }) {
|
|
11
|
+
if (message === null)
|
|
12
|
+
return null;
|
|
13
|
+
return _jsx("p", { className: "wm-form-error", role: "alert", tabIndex: -1, children: message });
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=FormError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormError.js","sourceRoot":"","sources":["../src/FormError.tsx"],"names":[],"mappings":";AAMA;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CAAC,EAAE,OAAO,EAAkB;IACnD,IAAI,OAAO,KAAK,IAAI;QAAE,OAAO,IAAI,CAAA;IACjC,OAAO,YAAG,SAAS,EAAC,eAAe,EAAC,IAAI,EAAC,OAAO,EAAC,QAAQ,EAAE,CAAC,CAAC,YAAG,OAAO,GAAK,CAAA;AAC9E,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Everything wrong with a form, split by where it is shown.
|
|
3
|
+
*
|
|
4
|
+
* `fields` is keyed by the form's own field names and goes to each control's
|
|
5
|
+
* `error` prop. `form` is whatever is left, shown next to the submit action
|
|
6
|
+
* with {@link FormError}.
|
|
7
|
+
*/
|
|
8
|
+
export interface FormErrors {
|
|
9
|
+
/** Message per field name. A field with no entry is fine. */
|
|
10
|
+
readonly fields: Readonly<Record<string, string>>;
|
|
11
|
+
/** A problem that is not about one field, or `null`. */
|
|
12
|
+
readonly form: string | null;
|
|
13
|
+
}
|
|
14
|
+
/** A form with nothing wrong with it. */
|
|
15
|
+
export declare const NO_FORM_ERRORS: FormErrors;
|
|
16
|
+
/**
|
|
17
|
+
* Whether a form has anything to show.
|
|
18
|
+
*
|
|
19
|
+
* @param errors - The form's errors.
|
|
20
|
+
* @returns `true` when any field or the form itself has a message.
|
|
21
|
+
*/
|
|
22
|
+
export declare function hasFormErrors(errors: FormErrors): boolean;
|
|
23
|
+
//# sourceMappingURL=FormErrors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormErrors.d.ts","sourceRoot":"","sources":["../src/FormErrors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,WAAW,UAAU;IACzB,6DAA6D;IAC7D,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;IACjD,wDAAwD;IACxD,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;CAC7B;AAED,yCAAyC;AACzC,eAAO,MAAM,cAAc,EAAE,UAAuC,CAAA;AAEpE;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAEzD"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** A form with nothing wrong with it. */
|
|
2
|
+
export const NO_FORM_ERRORS = { fields: {}, form: null };
|
|
3
|
+
/**
|
|
4
|
+
* Whether a form has anything to show.
|
|
5
|
+
*
|
|
6
|
+
* @param errors - The form's errors.
|
|
7
|
+
* @returns `true` when any field or the form itself has a message.
|
|
8
|
+
*/
|
|
9
|
+
export function hasFormErrors(errors) {
|
|
10
|
+
return errors.form !== null || Object.keys(errors.fields).length > 0;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=FormErrors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormErrors.js","sourceRoot":"","sources":["../src/FormErrors.ts"],"names":[],"mappings":"AAcA,yCAAyC;AACzC,MAAM,CAAC,MAAM,cAAc,GAAe,EAAE,MAAM,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAA;AAEpE;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,MAAkB;IAC9C,OAAO,MAAM,CAAC,IAAI,KAAK,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,GAAG,CAAC,CAAA;AACtE,CAAC"}
|
package/dist/SelectField.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { type ReactNode, type SelectHTMLAttributes } from 'react';
|
|
2
|
-
import type
|
|
2
|
+
import { type FieldCommon } from './FieldCommon.js';
|
|
3
3
|
/** Props for {@link SelectField}: the shared field props plus every native `<select>` attribute. */
|
|
4
4
|
export type SelectFieldProps = FieldCommon & SelectHTMLAttributes<HTMLSelectElement> & {
|
|
5
5
|
/** The `<option>` and `<optgroup>` elements to choose between. */
|
|
6
6
|
readonly children: ReactNode;
|
|
7
7
|
};
|
|
8
8
|
/** A labelled `<select>`. The label is always associated with the control. */
|
|
9
|
-
export declare function SelectField({ label, error, children, ...rest }: SelectFieldProps): import("react").JSX.Element;
|
|
9
|
+
export declare function SelectField({ label, error, hint, children, ...rest }: SelectFieldProps): import("react").JSX.Element;
|
|
10
10
|
//# sourceMappingURL=SelectField.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SelectField.d.ts","sourceRoot":"","sources":["../src/SelectField.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,SAAS,EAAE,KAAK,oBAAoB,EAAE,MAAM,OAAO,CAAA;AACxE,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"SelectField.d.ts","sourceRoot":"","sources":["../src/SelectField.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,SAAS,EAAE,KAAK,oBAAoB,EAAE,MAAM,OAAO,CAAA;AACxE,OAAO,EAAe,KAAK,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEhE,oGAAoG;AACpG,MAAM,MAAM,gBAAgB,GAAG,WAAW,GACxC,oBAAoB,CAAC,iBAAiB,CAAC,GAAG;IACxC,kEAAkE;IAClE,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAA;CAC7B,CAAA;AAEH,8EAA8E;AAC9E,wBAAgB,WAAW,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,IAAI,EAAE,EAAE,gBAAgB,+BAkBtF"}
|
package/dist/SelectField.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { useId } from 'react';
|
|
3
|
+
import { describedBy } from './FieldCommon.js';
|
|
3
4
|
/** A labelled `<select>`. The label is always associated with the control. */
|
|
4
|
-
export function SelectField({ label, error, children, ...rest }) {
|
|
5
|
+
export function SelectField({ label, error, hint, children, ...rest }) {
|
|
5
6
|
const id = useId();
|
|
6
|
-
return (_jsxs("div", { className: "wm-field", children: [_jsx("label", { className: "wm-field__label", htmlFor: id, children: label }), _jsx("select", { id: id, className: "wm-field__control", "aria-invalid": error !== undefined, ...rest, children: children }), error !== undefined ? _jsx("span", { className: "wm-field__error", children: error }) : null] }));
|
|
7
|
+
return (_jsxs("div", { className: "wm-field", children: [_jsx("label", { className: "wm-field__label", htmlFor: id, children: label }), _jsx("select", { id: id, className: "wm-field__control", "aria-invalid": error !== undefined, "aria-describedby": describedBy(id, hint, error), ...rest, children: children }), hint !== undefined ? _jsx("span", { className: "wm-field__hint", id: `${id}-hint`, children: hint }) : null, error !== undefined ? _jsx("span", { className: "wm-field__error", id: `${id}-error`, children: error }) : null] }));
|
|
7
8
|
}
|
|
8
9
|
//# sourceMappingURL=SelectField.js.map
|
package/dist/SelectField.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SelectField.js","sourceRoot":"","sources":["../src/SelectField.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,KAAK,EAA6C,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"SelectField.js","sourceRoot":"","sources":["../src/SelectField.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,KAAK,EAA6C,MAAM,OAAO,CAAA;AACxE,OAAO,EAAE,WAAW,EAAoB,MAAM,kBAAkB,CAAA;AAShE,8EAA8E;AAC9E,MAAM,UAAU,WAAW,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,IAAI,EAAoB;IACrF,MAAM,EAAE,GAAG,KAAK,EAAE,CAAA;IAClB,OAAO,CACL,eAAK,SAAS,EAAC,UAAU,aACvB,gBAAO,SAAS,EAAC,iBAAiB,EAAC,OAAO,EAAE,EAAE,YAAG,KAAK,GAAS,EAC/D,iBACE,EAAE,EAAE,EAAE,EACN,SAAS,EAAC,mBAAmB,kBACf,KAAK,KAAK,SAAS,sBACf,WAAW,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,KAC1C,IAAI,YAEP,QAAQ,GACF,EACR,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,eAAM,SAAS,EAAC,gBAAgB,EAAC,EAAE,EAAE,GAAG,EAAE,OAAO,YAAG,IAAI,GAAQ,CAAC,CAAC,CAAC,IAAI,EAC5F,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,eAAM,SAAS,EAAC,iBAAiB,EAAC,EAAE,EAAE,GAAG,EAAE,QAAQ,YAAG,KAAK,GAAQ,CAAC,CAAC,CAAC,IAAI,IAC7F,CACP,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One problem the server found with a request, tied to the field it came from.
|
|
3
|
+
*
|
|
4
|
+
* The shape of each entry in a `validation_failed` response's `issues`.
|
|
5
|
+
* `path` spells the field as the request body does (`['conditions', 0,
|
|
6
|
+
* 'amountMin']`); an empty path means the request as a whole. `message` is a
|
|
7
|
+
* sentence about the field that does not repeat its name.
|
|
8
|
+
*/
|
|
9
|
+
export interface ValidationIssue {
|
|
10
|
+
/** Where in the request body the problem is; empty for the whole request. */
|
|
11
|
+
readonly path: readonly (string | number)[];
|
|
12
|
+
/** What is wrong, as a sentence. */
|
|
13
|
+
readonly message: string;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=ValidationIssue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValidationIssue.d.ts","sourceRoot":"","sources":["../src/ValidationIssue.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,WAAW,eAAe;IAC9B,6EAA6E;IAC7E,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAA;IAC3C,oCAAoC;IACpC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CACzB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValidationIssue.js","sourceRoot":"","sources":["../src/ValidationIssue.ts"],"names":[],"mappings":""}
|
package/dist/components.css
CHANGED
|
@@ -37,6 +37,16 @@
|
|
|
37
37
|
}
|
|
38
38
|
.wm-field__control:focus-visible { outline: 2px solid var(--wm-accent); outline-offset: 1px; }
|
|
39
39
|
.wm-field__error { font-size: 12px; color: var(--wm-negative); }
|
|
40
|
+
.wm-field__hint { font-size: 12px; color: var(--wm-text-muted); }
|
|
41
|
+
.wm-field__control[aria-invalid="true"] { border-color: var(--wm-negative); }
|
|
42
|
+
/* A form's own error, beside its submit action. Focusable so the form can move
|
|
43
|
+
focus (and the viewport) to it; the outline is the focus ring, not a border. */
|
|
44
|
+
.wm-form-error {
|
|
45
|
+
margin: var(--wm-gap-sm) 0; font-size: 13px; color: var(--wm-negative);
|
|
46
|
+
border-left: 3px solid var(--wm-negative); padding: 2px 0 2px 8px;
|
|
47
|
+
}
|
|
48
|
+
.wm-form-error:focus { outline: none; }
|
|
49
|
+
.wm-form-error:focus-visible { outline: 2px solid var(--wm-accent); outline-offset: 2px; }
|
|
40
50
|
|
|
41
51
|
.wm-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
|
|
42
52
|
.wm-table th, .wm-table td {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { FormErrors } from './FormErrors.js';
|
|
2
|
+
/**
|
|
3
|
+
* Which form field a server path belongs to: a list of field names that are
|
|
4
|
+
* the dotted path itself (`'name'`, `'conditions.0.amountMin'`), or a
|
|
5
|
+
* function from the dotted path to a field name, `undefined` for none.
|
|
6
|
+
*/
|
|
7
|
+
export type FieldMatcher = readonly string[] | ((path: string) => string | undefined);
|
|
8
|
+
/**
|
|
9
|
+
* Turns a failed request into messages for a form: each server issue on the
|
|
10
|
+
* field it names, and the rest as one form-level message.
|
|
11
|
+
*
|
|
12
|
+
* A failure with no issues (a conflict, a network error, an older server)
|
|
13
|
+
* becomes a form-level message: the error's own message when it has one,
|
|
14
|
+
* otherwise `fallback`. Only the first message per field is kept, since one
|
|
15
|
+
* thing to fix at a time is easier to act on.
|
|
16
|
+
*
|
|
17
|
+
* @param error - Whatever the request rejected with.
|
|
18
|
+
* @param fields - Which server paths the form has a field for.
|
|
19
|
+
* @param fallback - What to say when the error has no message of its own.
|
|
20
|
+
* @returns The errors to show.
|
|
21
|
+
* @example
|
|
22
|
+
* formErrorsFrom(e, ['name', 'amount'], 'Could not save that.')
|
|
23
|
+
*/
|
|
24
|
+
export declare function formErrorsFrom(error: unknown, fields: FieldMatcher, fallback: string): FormErrors;
|
|
25
|
+
//# sourceMappingURL=formErrorsFrom.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formErrorsFrom.d.ts","sourceRoot":"","sources":["../src/formErrorsFrom.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAGjD;;;;GAIG;AACH,MAAM,MAAM,YAAY,GAAG,SAAS,MAAM,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC,CAAA;AAErF;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,GAAG,UAAU,CAoBjG"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { validationIssuesOf } from './validationIssuesOf.js';
|
|
2
|
+
/**
|
|
3
|
+
* Turns a failed request into messages for a form: each server issue on the
|
|
4
|
+
* field it names, and the rest as one form-level message.
|
|
5
|
+
*
|
|
6
|
+
* A failure with no issues (a conflict, a network error, an older server)
|
|
7
|
+
* becomes a form-level message: the error's own message when it has one,
|
|
8
|
+
* otherwise `fallback`. Only the first message per field is kept, since one
|
|
9
|
+
* thing to fix at a time is easier to act on.
|
|
10
|
+
*
|
|
11
|
+
* @param error - Whatever the request rejected with.
|
|
12
|
+
* @param fields - Which server paths the form has a field for.
|
|
13
|
+
* @param fallback - What to say when the error has no message of its own.
|
|
14
|
+
* @returns The errors to show.
|
|
15
|
+
* @example
|
|
16
|
+
* formErrorsFrom(e, ['name', 'amount'], 'Could not save that.')
|
|
17
|
+
*/
|
|
18
|
+
export function formErrorsFrom(error, fields, fallback) {
|
|
19
|
+
const issues = validationIssuesOf(error);
|
|
20
|
+
if (issues.length === 0) {
|
|
21
|
+
const own = error instanceof Error && error.message !== '' ? error.message : fallback;
|
|
22
|
+
return { fields: {}, form: own };
|
|
23
|
+
}
|
|
24
|
+
const fieldFor = typeof fields === 'function'
|
|
25
|
+
? fields
|
|
26
|
+
: (path) => (fields.includes(path) ? path : undefined);
|
|
27
|
+
const byField = {};
|
|
28
|
+
const rest = [];
|
|
29
|
+
for (const issue of issues) {
|
|
30
|
+
const field = issue.path.length > 0 ? fieldFor(issue.path.join('.')) : undefined;
|
|
31
|
+
if (field === undefined) {
|
|
32
|
+
if (!rest.includes(issue.message))
|
|
33
|
+
rest.push(issue.message);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
byField[field] ??= issue.message;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return { fields: byField, form: rest.length > 0 ? rest.join(' ') : null };
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=formErrorsFrom.js.map
|
|
@@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IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QAC7D,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,KAAK,CAAC,KAAK,KAAK,CAAC,OAAO,CAAA;QAClC,CAAC;IACH,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAA;AAC3E,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,16 @@ export type { ButtonProps } from './Button.js';
|
|
|
7
7
|
export { Field } from './Field.js';
|
|
8
8
|
export type { FieldProps } from './Field.js';
|
|
9
9
|
export type { FieldCommon } from './FieldCommon.js';
|
|
10
|
+
export { FormError } from './FormError.js';
|
|
11
|
+
export type { FormErrorProps } from './FormError.js';
|
|
12
|
+
export { NO_FORM_ERRORS, hasFormErrors } from './FormErrors.js';
|
|
13
|
+
export type { FormErrors } from './FormErrors.js';
|
|
14
|
+
export { formErrorsFrom } from './formErrorsFrom.js';
|
|
15
|
+
export type { FieldMatcher } from './formErrorsFrom.js';
|
|
16
|
+
export { useFormErrors } from './useFormErrors.js';
|
|
17
|
+
export type { FormErrorState } from './useFormErrors.js';
|
|
18
|
+
export { validationIssuesOf } from './validationIssuesOf.js';
|
|
19
|
+
export type { ValidationIssue } from './ValidationIssue.js';
|
|
10
20
|
export { SelectField } from './SelectField.js';
|
|
11
21
|
export type { SelectFieldProps } from './SelectField.js';
|
|
12
22
|
export { Table } from './Table.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAA;AACrB,OAAO,kBAAkB,CAAA;AAEzB,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA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ACtD,YAAY,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAA;AAChE,YAAY,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAC5C,YAAY,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,11 @@ import './components.css';
|
|
|
3
3
|
export { Surface } from './Surface.js';
|
|
4
4
|
export { Button } from './Button.js';
|
|
5
5
|
export { Field } from './Field.js';
|
|
6
|
+
export { FormError } from './FormError.js';
|
|
7
|
+
export { NO_FORM_ERRORS, hasFormErrors } from './FormErrors.js';
|
|
8
|
+
export { formErrorsFrom } from './formErrorsFrom.js';
|
|
9
|
+
export { useFormErrors } from './useFormErrors.js';
|
|
10
|
+
export { validationIssuesOf } from './validationIssuesOf.js';
|
|
6
11
|
export { SelectField } from './SelectField.js';
|
|
7
12
|
export { Table } from './Table.js';
|
|
8
13
|
export { Stat } from './Stat.js';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAA;AACrB,OAAO,kBAAkB,CAAA;AAEzB,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAEtC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEpC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAGlC,OAAO,EAAE,WAAW,EAAE,MAA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EAAE,MAAM,iBAAiB,CAAA;AAE5C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type RefObject } from 'react';
|
|
2
|
+
import { type FormErrors } from './FormErrors.js';
|
|
3
|
+
/** What {@link useFormErrors} returns. */
|
|
4
|
+
export interface FormErrorState<T extends HTMLElement> {
|
|
5
|
+
/** What to show now. */
|
|
6
|
+
readonly errors: FormErrors;
|
|
7
|
+
/** Attach to the form (or the element around its controls) so focus can move inside it. */
|
|
8
|
+
readonly ref: RefObject<T | null>;
|
|
9
|
+
/** Shows a new set of errors, then focuses the first invalid control or the form's error. */
|
|
10
|
+
readonly show: (next: FormErrors) => void;
|
|
11
|
+
/** Clears every message. */
|
|
12
|
+
readonly clear: () => void;
|
|
13
|
+
/** Clears one field's message, for when its value changes. */
|
|
14
|
+
readonly clearField: (field: string) => void;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A form's field and form-level errors, and the focus move that makes sure
|
|
18
|
+
* they are seen.
|
|
19
|
+
*
|
|
20
|
+
* After {@link FormErrorState.show} with anything to show, focus goes to the
|
|
21
|
+
* first control marked `aria-invalid` inside `ref`, or to the form's
|
|
22
|
+
* {@link FormError} when no field is invalid. Moving focus also scrolls the
|
|
23
|
+
* element into view, so an error never sits off-screen with nothing
|
|
24
|
+
* pointing at it.
|
|
25
|
+
*
|
|
26
|
+
* @returns The errors and the actions on them.
|
|
27
|
+
*/
|
|
28
|
+
export declare function useFormErrors<T extends HTMLElement = HTMLFormElement>(): FormErrorState<T>;
|
|
29
|
+
//# sourceMappingURL=useFormErrors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormErrors.d.ts","sourceRoot":"","sources":["../src/useFormErrors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqD,KAAK,SAAS,EAAE,MAAM,OAAO,CAAA;AACzF,OAAO,EAAiC,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEhF,0CAA0C;AAC1C,MAAM,WAAW,cAAc,CAAC,CAAC,SAAS,WAAW;IACnD,wBAAwB;IACxB,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAA;IAC3B,2FAA2F;IAC3F,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC,GAAG,IAAI,CAAC,CAAA;IACjC,6FAA6F;IAC7F,QAAQ,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,IAAI,CAAA;IACzC,4BAA4B;IAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,IAAI,CAAA;IAC1B,8DAA8D;IAC9D,QAAQ,CAAC,UAAU,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;CAC7C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,WAAW,GAAG,eAAe,KAAK,cAAc,CAAC,CAAC,CAAC,CA8B1F"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { NO_FORM_ERRORS, hasFormErrors } from './FormErrors.js';
|
|
3
|
+
/**
|
|
4
|
+
* A form's field and form-level errors, and the focus move that makes sure
|
|
5
|
+
* they are seen.
|
|
6
|
+
*
|
|
7
|
+
* After {@link FormErrorState.show} with anything to show, focus goes to the
|
|
8
|
+
* first control marked `aria-invalid` inside `ref`, or to the form's
|
|
9
|
+
* {@link FormError} when no field is invalid. Moving focus also scrolls the
|
|
10
|
+
* element into view, so an error never sits off-screen with nothing
|
|
11
|
+
* pointing at it.
|
|
12
|
+
*
|
|
13
|
+
* @returns The errors and the actions on them.
|
|
14
|
+
*/
|
|
15
|
+
export function useFormErrors() {
|
|
16
|
+
const [errors, setErrors] = useState(NO_FORM_ERRORS);
|
|
17
|
+
const [attempt, setAttempt] = useState(0);
|
|
18
|
+
const ref = useRef(null);
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (attempt === 0 || ref.current === null)
|
|
21
|
+
return;
|
|
22
|
+
const target = ref.current.querySelector('[aria-invalid="true"]')
|
|
23
|
+
?? ref.current.querySelector('.wm-form-error');
|
|
24
|
+
if (target === null)
|
|
25
|
+
return;
|
|
26
|
+
target.focus();
|
|
27
|
+
// Not every environment implements it (jsdom does not); focus is the part that matters.
|
|
28
|
+
target.scrollIntoView?.({ block: 'nearest' });
|
|
29
|
+
}, [attempt]);
|
|
30
|
+
const show = useCallback((next) => {
|
|
31
|
+
setErrors(next);
|
|
32
|
+
if (hasFormErrors(next))
|
|
33
|
+
setAttempt((a) => a + 1);
|
|
34
|
+
}, []);
|
|
35
|
+
const clear = useCallback(() => setErrors(NO_FORM_ERRORS), []);
|
|
36
|
+
const clearField = useCallback((field) => {
|
|
37
|
+
setErrors((current) => {
|
|
38
|
+
if (!(field in current.fields))
|
|
39
|
+
return current;
|
|
40
|
+
const fields = { ...current.fields };
|
|
41
|
+
delete fields[field];
|
|
42
|
+
return { ...current, fields };
|
|
43
|
+
});
|
|
44
|
+
}, []);
|
|
45
|
+
return useMemo(() => ({ errors, ref, show, clear, clearField }), [errors, show, clear, clearField]);
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=useFormErrors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormErrors.js","sourceRoot":"","sources":["../src/useFormErrors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAkB,MAAM,OAAO,CAAA;AACzF,OAAO,EAAE,cAAc,EAAE,aAAa,EAAmB,MAAM,iBAAiB,CAAA;AAgBhF;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,aAAa;IAC3B,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,QAAQ,CAAa,cAAc,CAAC,CAAA;IAChE,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ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AM,EAAE,CAAA;QAC/B,CAAC,CAAC,CAAA;IACJ,CAAC,EAAE,EAAE,CAAC,CAAA;IAEN,OAAO,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC,CAAA;AACrG,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ValidationIssue } from './ValidationIssue.js';
|
|
2
|
+
/**
|
|
3
|
+
* The field-level issues a failed request carries, if any.
|
|
4
|
+
*
|
|
5
|
+
* Duck-typed on an `issues` property so it works with the host's own error
|
|
6
|
+
* class and with whatever a plugin's scoped client rejects with, neither of
|
|
7
|
+
* which a plugin can import. Anything not shaped like `{ path, message }[]`
|
|
8
|
+
* is ignored rather than trusted.
|
|
9
|
+
*
|
|
10
|
+
* @param error - Whatever a request rejected with.
|
|
11
|
+
* @returns The issues, or an empty list when there are none.
|
|
12
|
+
*/
|
|
13
|
+
export declare function validationIssuesOf(error: unknown): readonly ValidationIssue[];
|
|
14
|
+
//# sourceMappingURL=validationIssuesOf.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validationIssuesOf.d.ts","sourceRoot":"","sources":["../src/validationIssuesOf.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAE3D;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,SAAS,eAAe,EAAE,CAU7E"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The field-level issues a failed request carries, if any.
|
|
3
|
+
*
|
|
4
|
+
* Duck-typed on an `issues` property so it works with the host's own error
|
|
5
|
+
* class and with whatever a plugin's scoped client rejects with, neither of
|
|
6
|
+
* which a plugin can import. Anything not shaped like `{ path, message }[]`
|
|
7
|
+
* is ignored rather than trusted.
|
|
8
|
+
*
|
|
9
|
+
* @param error - Whatever a request rejected with.
|
|
10
|
+
* @returns The issues, or an empty list when there are none.
|
|
11
|
+
*/
|
|
12
|
+
export function validationIssuesOf(error) {
|
|
13
|
+
if (typeof error !== 'object' || error === null || !('issues' in error))
|
|
14
|
+
return [];
|
|
15
|
+
const { issues } = error;
|
|
16
|
+
if (!Array.isArray(issues))
|
|
17
|
+
return [];
|
|
18
|
+
return issues.filter((i) => {
|
|
19
|
+
if (typeof i !== 'object' || i === null)
|
|
20
|
+
return false;
|
|
21
|
+
const { path, message } = i;
|
|
22
|
+
return typeof message === 'string' && Array.isArray(path)
|
|
23
|
+
&& path.every((p) => typeof p === 'string' || typeof p === 'number');
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=validationIssuesOf.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validationIssuesOf.js","sourceRoot":"","sources":["../src/validationIssuesOf.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAc;IAC/C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,CAAC,QAAQ,IAAI,KAAK,CAAC;QAAE,OAAO,EAAE,CAAA;IAClF,MAAM,EAAE,MAAM,EAAE,GAAG,KAA4B,CAAA;IAC/C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;QAAE,OAAO,EAAE,CAAA;IACrC,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAwB,EAAE;QAC/C,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI;YAAE,OAAO,KAAK,CAAA;QACrD,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,CAA0C,CAAA;QACpE,OAAO,OAAO,OAAO,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;eACpD,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAA;IACxE,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wickermoney/ui-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -36,11 +36,11 @@
|
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@testing-library/react": "^16.3.3",
|
|
38
38
|
"@types/react": "^19.3.0",
|
|
39
|
-
"jsdom": "
|
|
39
|
+
"jsdom": "30.1.1",
|
|
40
40
|
"react": "^19.3.0",
|
|
41
41
|
"react-dom": "^19.3.0",
|
|
42
42
|
"typescript": "^6.0.3",
|
|
43
|
-
"vitest": "^5.0.
|
|
43
|
+
"vitest": "^5.0.3"
|
|
44
44
|
},
|
|
45
45
|
"publishConfig": {
|
|
46
46
|
"access": "public"
|