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 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
@@ -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, eventToValue: b, valueToProps: x, children: S }) {
10
- let C = `haze-field-${r()}`, w = `${C}-error`, { value: T, onChange: E, onBlur: D, errors: O } = i({
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
- }), k = O.length > 0, A = b ?? ((e) => e);
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: C,
25
+ htmlFor: T,
24
26
  className: e(o),
25
27
  children: u
26
28
  }),
27
- v ? /* @__PURE__ */ t(v, {
28
- id: C,
29
- "aria-invalid": k || void 0,
30
- "aria-describedby": k ? w : void 0,
31
- onBlur: D,
32
- onChange: (e) => E(A(e)),
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
- ...x ? x(T) : { value: T }
35
- }) : S({
36
- id: C,
37
- errorId: w,
38
- invalid: k,
39
- errors: O,
40
- onBlur: D,
41
- value: T,
42
- onChange: E
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
- k && /* @__PURE__ */ t("span", {
45
- id: w,
55
+ j && /* @__PURE__ */ t("span", {
56
+ id: E,
46
57
  role: "alert",
47
58
  className: e(s),
48
- children: _ ? _(O[0].message, w) : O[0].message
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
- export type FormItemProps<TValues extends Record<string, any> = any, P extends FieldPath<TValues> | Name = Name> = {
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 both the `as` and children modes. */
101
+ * styling — stays FormItem's, in every control-binding mode. */
99
102
  renderError?: (error: string, id: string) => ReactNode;
100
103
  className?: string;
101
- } & FormItemAsProps & ({
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';
@@ -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';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "haze-ui",
3
- "version": "1.14.0",
3
+ "version": "1.15.0",
4
4
  "type": "module",
5
5
  "description": "A React UI component library powered by react-use-control and Linaria",
6
6
  "main": "./dist/index.js",