haze-ui 1.14.0 → 1.15.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/README.md +53 -0
- package/dist/form/FormItem.js +33 -22
- package/dist/types/form/FormItem.d.ts +70 -4
- package/dist/types/form/index.d.ts +1 -1
- package/dist/types/index.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -154,6 +154,59 @@ render-prop are mutually exclusive.
|
|
|
154
154
|
- With a typed form, `validate`'s value argument is the field's actual
|
|
155
155
|
type (`PathValueOf<TValues, P>`), not `any`.
|
|
156
156
|
|
|
157
|
+
#### `input`: declarative binding for haze-ui cores (typed prop forwarding)
|
|
158
|
+
|
|
159
|
+
The ergonomic form for the controlled cores — pass the component and the
|
|
160
|
+
rest of the JSX goes straight to it, type-checked against its own props:
|
|
161
|
+
|
|
162
|
+
```jsx
|
|
163
|
+
<FormItem
|
|
164
|
+
form={form}
|
|
165
|
+
name="email"
|
|
166
|
+
label="Email"
|
|
167
|
+
input={InputCore}
|
|
168
|
+
placeholder="you@x.dev"
|
|
169
|
+
mode="onBlur"
|
|
170
|
+
validate={(v) => (v.includes('@') ? undefined : 'must be an email')}
|
|
171
|
+
/>
|
|
172
|
+
|
|
173
|
+
// JSX children forward too — a SelectCore's options:
|
|
174
|
+
<FormItem form={form} name="role" label="Role" input={SelectCore}>
|
|
175
|
+
<option value="admin">Admin</option>
|
|
176
|
+
<option value="viewer">Viewer</option>
|
|
177
|
+
</FormItem>
|
|
178
|
+
|
|
179
|
+
// checkbox-style controls keep the valueToProps adapter:
|
|
180
|
+
<FormItem
|
|
181
|
+
form={form}
|
|
182
|
+
name="subscribed"
|
|
183
|
+
label="Subscribe"
|
|
184
|
+
input={CheckboxCore}
|
|
185
|
+
valueToProps={(checked) => ({ checked })}
|
|
186
|
+
/>
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
`input` wires the same id/aria/`onBlur`/`onChange`/value contract as `as`
|
|
190
|
+
— every haze core (`InputCore`, `TextareaCore`, `SelectCore`,
|
|
191
|
+
`TagInputCore`, `CheckboxCore`, `SwitchCore`, …) speaks the plain
|
|
192
|
+
`{value, onChange}` pair, so the default adapters need nothing
|
|
193
|
+
(`TagInputCore`'s `onChange` already emits the next `string[]`; a
|
|
194
|
+
checkbox-style core pairs with `valueToProps`). The differences from
|
|
195
|
+
`as`:
|
|
196
|
+
|
|
197
|
+
- Forwarded props are **type-checked against the core's own props** —
|
|
198
|
+
`input={InputCore} size="xl"` is a compile error, while `asProps` is an
|
|
199
|
+
untyped bag.
|
|
200
|
+
- JSX **children** forward to the core (a `SelectCore`'s `<option>`s);
|
|
201
|
+
the render-prop children and `input` are mutually exclusive (a
|
|
202
|
+
render-prop next to `input` throws — it's a migration leftover).
|
|
203
|
+
- The wiring (`id`, `aria-invalid`, `aria-describedby`, `onBlur`,
|
|
204
|
+
`onChange`, `value`/`checked`) and FormItem's own prop names are
|
|
205
|
+
**reserved**: they are excluded from the forwarded type and always win
|
|
206
|
+
at runtime. A control prop that collides with one (e.g. CheckboxCore's
|
|
207
|
+
own `label`) is unreachable through `input` — use the render-prop or
|
|
208
|
+
`as`/`asProps` for it.
|
|
209
|
+
|
|
157
210
|
#### `mode`: per-field validation timing (react-f0rm ≥ 0.6)
|
|
158
211
|
|
|
159
212
|
Pass `mode` to validate one field on its own schedule instead of the
|
package/dist/form/FormItem.js
CHANGED
|
@@ -6,8 +6,8 @@ import { useId as r } from "react";
|
|
|
6
6
|
import { useField as i } from "react-f0rm";
|
|
7
7
|
//#region src/lib/form/FormItem.tsx
|
|
8
8
|
var a = "haze-FormItem__item", o = "haze-FormItem__labelText", s = "haze-FormItem__errorText";
|
|
9
|
-
function c({ form: c, name: l, label: u, validate: d, mode: f, validateDebounce: p, delayError: m, rules: h, className: g, renderError: _, as: v, asProps: y,
|
|
10
|
-
let
|
|
9
|
+
function c({ form: c, name: l, label: u, validate: d, mode: f, validateDebounce: p, delayError: m, rules: h, className: g, renderError: _, as: v, asProps: y, input: b, eventToValue: x, valueToProps: S, children: C, ...w }) {
|
|
10
|
+
let T = `haze-field-${r()}`, E = `${T}-error`, { value: D, onChange: O, onBlur: k, errors: A } = i({
|
|
11
11
|
form: c,
|
|
12
12
|
name: l,
|
|
13
13
|
validate: d,
|
|
@@ -15,37 +15,48 @@ function c({ form: c, name: l, label: u, validate: d, mode: f, validateDebounce:
|
|
|
15
15
|
validateDebounce: p,
|
|
16
16
|
delayError: m,
|
|
17
17
|
rules: h
|
|
18
|
-
}),
|
|
18
|
+
}), j = A.length > 0, M = x ?? ((e) => e);
|
|
19
|
+
if (b && typeof C == "function") throw Error("FormItem: `input` and the render-prop `children` are mutually exclusive — the input component is wired declaratively; remove the render-prop.");
|
|
20
|
+
let N = b;
|
|
19
21
|
return /* @__PURE__ */ n("div", {
|
|
20
22
|
className: e([a, g]),
|
|
21
23
|
children: [
|
|
22
24
|
u !== void 0 && /* @__PURE__ */ t("label", {
|
|
23
|
-
htmlFor:
|
|
25
|
+
htmlFor: T,
|
|
24
26
|
className: e(o),
|
|
25
27
|
children: u
|
|
26
28
|
}),
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"aria-
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
N ? /* @__PURE__ */ t(N, {
|
|
30
|
+
...w,
|
|
31
|
+
id: T,
|
|
32
|
+
"aria-invalid": j || void 0,
|
|
33
|
+
"aria-describedby": j ? E : void 0,
|
|
34
|
+
onBlur: k,
|
|
35
|
+
onChange: (e) => O(M(e)),
|
|
36
|
+
...S ? S(D) : { value: D },
|
|
37
|
+
children: C
|
|
38
|
+
}) : v ? /* @__PURE__ */ t(v, {
|
|
39
|
+
id: T,
|
|
40
|
+
"aria-invalid": j || void 0,
|
|
41
|
+
"aria-describedby": j ? E : void 0,
|
|
42
|
+
onBlur: k,
|
|
43
|
+
onChange: (e) => O(M(e)),
|
|
33
44
|
...y,
|
|
34
|
-
...
|
|
35
|
-
}) :
|
|
36
|
-
id:
|
|
37
|
-
errorId:
|
|
38
|
-
invalid:
|
|
39
|
-
errors:
|
|
40
|
-
onBlur:
|
|
41
|
-
value:
|
|
42
|
-
onChange:
|
|
45
|
+
...S ? S(D) : { value: D }
|
|
46
|
+
}) : C({
|
|
47
|
+
id: T,
|
|
48
|
+
errorId: E,
|
|
49
|
+
invalid: j,
|
|
50
|
+
errors: A,
|
|
51
|
+
onBlur: k,
|
|
52
|
+
value: D,
|
|
53
|
+
onChange: O
|
|
43
54
|
}),
|
|
44
|
-
|
|
45
|
-
id:
|
|
55
|
+
j && /* @__PURE__ */ t("span", {
|
|
56
|
+
id: E,
|
|
46
57
|
role: "alert",
|
|
47
58
|
className: e(s),
|
|
48
|
-
children: _ ? _(
|
|
59
|
+
children: _ ? _(A[0].message, E) : A[0].message
|
|
49
60
|
})
|
|
50
61
|
]
|
|
51
62
|
});
|
|
@@ -62,7 +62,10 @@ export type FormItemAsProps = {
|
|
|
62
62
|
* passing `{value}`. */
|
|
63
63
|
valueToProps?: (value: any) => Record<string, any>;
|
|
64
64
|
};
|
|
65
|
-
|
|
65
|
+
/** FormItem's own, non-polymorphic props — everything the item itself
|
|
66
|
+
* consumes regardless of how the control is bound (render-prop, `as`
|
|
67
|
+
* or `input`). */
|
|
68
|
+
export type FormItemOwnProps<TValues extends Record<string, any> = any, P extends FieldPath<TValues> | Name = Name> = {
|
|
66
69
|
form: FormInstance<TValues>;
|
|
67
70
|
name: P;
|
|
68
71
|
label?: ReactNode;
|
|
@@ -95,14 +98,56 @@ export type FormItemProps<TValues extends Record<string, any> = any, P extends F
|
|
|
95
98
|
/** Custom error renderer: when provided and the field has errors, the
|
|
96
99
|
* built-in error span renders `renderError(errors[0].message, errorId)`
|
|
97
100
|
* instead of the bare message. The span itself — id, `role='alert'`,
|
|
98
|
-
* styling — stays FormItem's, in
|
|
101
|
+
* styling — stays FormItem's, in every control-binding mode. */
|
|
99
102
|
renderError?: (error: string, id: string) => ReactNode;
|
|
100
103
|
className?: string;
|
|
101
|
-
}
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* Props FormItem wires itself onto an `input`/`as` control — the bridge's
|
|
107
|
+
* own contract. Used to keep them out of `input`'s forwarded rest props
|
|
108
|
+
* (type level) and to document that they always win (runtime level):
|
|
109
|
+
* passing one anyway is a compile error, never a silent override.
|
|
110
|
+
*/
|
|
111
|
+
type FormItemWiredProps = {
|
|
112
|
+
id?: unknown;
|
|
113
|
+
onBlur?: unknown;
|
|
114
|
+
onChange?: unknown;
|
|
115
|
+
/** the value channel: `value` directly, or the prop `valueToProps`
|
|
116
|
+
* derives (e.g. `checked` for CheckboxCore) — either way FormItem's */
|
|
117
|
+
value?: unknown;
|
|
118
|
+
checked?: unknown;
|
|
119
|
+
'aria-invalid'?: unknown;
|
|
120
|
+
'aria-describedby'?: unknown;
|
|
121
|
+
};
|
|
122
|
+
export type FormItemProps<TValues extends Record<string, any> = any, P extends FieldPath<TValues> | Name = Name, TInputProps extends Record<string, any> = Record<never, never>> = FormItemOwnProps<TValues, P> & FormItemAsProps & {
|
|
123
|
+
/**
|
|
124
|
+
* Declarative binding for haze-ui cores: pass the component
|
|
125
|
+
* (`InputCore`, `TextareaCore`, `TagInputCore`, `SelectCore`,
|
|
126
|
+
* `CheckboxCore`, …) and FormItem wires `id`, `aria-invalid`,
|
|
127
|
+
* `aria-describedby`, `onBlur`, `onChange` and the value channel
|
|
128
|
+
* itself. Every other prop — and JSX children (a `SelectCore`'s
|
|
129
|
+
* `<option>`s) — is forwarded to the component, fully type-checked
|
|
130
|
+
* against its own props. Cores' `onChange` emits the next plain value
|
|
131
|
+
* (identity `eventToValue`); checkbox-style controls pair with
|
|
132
|
+
* `valueToProps={(checked) => ({checked})}`. Props FormItem owns or
|
|
133
|
+
* wires (label, className, id, aria-*, onBlur, onChange, value,
|
|
134
|
+
* checked, …) are reserved and cannot be forwarded — use the
|
|
135
|
+
* render-prop or `as`/`asProps` for a colliding control prop.
|
|
136
|
+
*/
|
|
137
|
+
input?: ComponentType<TInputProps>;
|
|
138
|
+
} & Omit<TInputProps, keyof FormItemOwnProps<TValues, P> | keyof FormItemAsProps | keyof FormItemWiredProps | 'input'> & ({
|
|
102
139
|
as: ComponentType<any>;
|
|
140
|
+
input?: never;
|
|
103
141
|
children?: never;
|
|
104
142
|
} | {
|
|
105
143
|
as?: undefined;
|
|
144
|
+
input: ComponentType<TInputProps>;
|
|
145
|
+
/** JSX children forward to the control (SelectCore's options);
|
|
146
|
+
* the render-prop form is mutually exclusive with `input`. */
|
|
147
|
+
children?: 'children' extends keyof TInputProps ? TInputProps['children'] : undefined;
|
|
148
|
+
} | {
|
|
149
|
+
as?: undefined;
|
|
150
|
+
input?: undefined;
|
|
106
151
|
children: (binding: FormItemBinding<TValues, P>) => ReactNode;
|
|
107
152
|
});
|
|
108
153
|
/**
|
|
@@ -141,8 +186,29 @@ export type FormItemProps<TValues extends Record<string, any> = any, P extends F
|
|
|
141
186
|
* value lands as `{value}` or, with `valueToProps`, whatever props the
|
|
142
187
|
* control wants (e.g. `{checked}` for CheckboxCore).
|
|
143
188
|
*
|
|
189
|
+
* The ergonomic form for haze-ui cores is `input`: the rest of the JSX
|
|
190
|
+
* props — and JSX children, e.g. a `SelectCore`'s `<option>`s — are
|
|
191
|
+
* forwarded to the component, type-checked against its own props
|
|
192
|
+
* (`input` and the render-prop children are mutually exclusive):
|
|
193
|
+
*
|
|
194
|
+
* ```tsx
|
|
195
|
+
* <FormItem
|
|
196
|
+
* form={form}
|
|
197
|
+
* name='email'
|
|
198
|
+
* input={InputCore}
|
|
199
|
+
* placeholder='Email'
|
|
200
|
+
* mode='onBlur'
|
|
201
|
+
* />
|
|
202
|
+
* ```
|
|
203
|
+
*
|
|
204
|
+
* The same wiring as `as` applies (id, aria, onBlur, onChange, value);
|
|
205
|
+
* wired and FormItem-owned prop names are reserved — a control prop that
|
|
206
|
+
* collides (CheckboxCore's `label`) needs the render-prop or
|
|
207
|
+
* `as`/`asProps` channel.
|
|
208
|
+
*
|
|
144
209
|
* When the field has errors, the first error's message is rendered into a
|
|
145
210
|
* `<span id={errorId} role='alert'>` next to the control; with no errors
|
|
146
211
|
* no extra element is rendered.
|
|
147
212
|
*/
|
|
148
|
-
export default function FormItem<TValues extends Record<string, any> = any, P extends FieldPath<TValues> | Name = Name>({ form, name, label, validate, mode, validateDebounce, delayError, rules, className, renderError, as: As, asProps, eventToValue, valueToProps, children }: FormItemProps<TValues, P>): import("react").JSX.Element;
|
|
213
|
+
export default function FormItem<TValues extends Record<string, any> = any, P extends FieldPath<TValues> | Name = Name, TInputProps extends Record<string, any> = Record<never, never>>({ form, name, label, validate, mode, validateDebounce, delayError, rules, className, renderError, as: As, asProps, input: Input, eventToValue, valueToProps, children, ...inputProps }: FormItemProps<TValues, P, TInputProps>): import("react").JSX.Element;
|
|
214
|
+
export {};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { default as FormItem } from './FormItem';
|
|
2
|
-
export type { FieldValidator, FormItemAsProps, FormItemBinding, FormItemProps } from './FormItem';
|
|
2
|
+
export type { FieldValidator, FormItemAsProps, FormItemBinding, FormItemOwnProps, FormItemProps } from './FormItem';
|
|
3
3
|
export type { FormInstance, PathValueOf } from 'react-f0rm';
|
package/dist/types/index.d.ts
CHANGED
|
@@ -182,6 +182,6 @@ export type { DiffViewerProps, DiffLine } from './components/DiffViewer';
|
|
|
182
182
|
export { LogViewer } from './components/LogViewer';
|
|
183
183
|
export type { LogViewerProps, LogEntry, LogLevel } from './components/LogViewer';
|
|
184
184
|
export { FormItem } from './form';
|
|
185
|
-
export type { FieldValidator, FormItemAsProps, FormItemBinding, FormItemProps, FormInstance, PathValueOf, } from './form';
|
|
185
|
+
export type { FieldValidator, FormItemAsProps, FormItemBinding, FormItemOwnProps, FormItemProps, FormInstance, PathValueOf, } from './form';
|
|
186
186
|
export { useControl } from 'react-use-control';
|
|
187
187
|
export type { Control, ControlOrValue } from 'react-use-control';
|