@dynamic-field-kit/angular 1.5.1 → 1.7.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/CHANGELOG.md +224 -0
- package/README.md +146 -23
- package/dist/fesm2022/dynamic-field-kit-angular.mjs +665 -116
- package/dist/types/dynamic-field-kit-angular.d.ts +404 -0
- package/package.json +25 -26
- package/dist/components/BaseInput.d.ts +0 -34
- package/dist/components/DynamicFormDevTools.d.ts +0 -16
- package/dist/components/DynamicInput.d.ts +0 -36
- package/dist/components/FieldInput.d.ts +0 -28
- package/dist/components/MultiFieldInput.d.ts +0 -60
- package/dist/fieldRegistryToken.d.ts +0 -3
- package/dist/index.d.ts +0 -5
- package/dist/layout/defaultLayouts.d.ts +0 -31
- package/dist/layout/index.d.ts +0 -2
- package/dist/layout/layoutRegistry.d.ts +0 -12
- package/dist/lib/dynamic-field-kit.module.d.ts +0 -11
- package/dist/lib/dynamic-form.store.d.ts +0 -23
- package/dist/public-api.d.ts +0 -14
- package/dist/types/layout.d.ts +0 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,229 @@
|
|
|
1
1
|
# @dynamic-field-kit/angular
|
|
2
2
|
|
|
3
|
+
## 1.7.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Dirty-baseline rebasing, accessible validation errors, form-level message catalogs, and async field options across React, Vue, and Angular.
|
|
8
|
+
- 9b06e3f: Validation messages can be set once per form via `useDynamicForm({ messages })`,
|
|
9
|
+
or process-wide via `setDefaultMessages`, instead of passing a string to every
|
|
10
|
+
validator on every field. Built-in validators now resolve their message when
|
|
11
|
+
they run rather than when the field description is built, which is what made a
|
|
12
|
+
catalog impossible before. A message passed directly to a validator still wins,
|
|
13
|
+
and the English defaults are unchanged when no catalog is supplied.
|
|
14
|
+
|
|
15
|
+
`ValidationContext` - already `validate`'s fourth argument - gains an optional
|
|
16
|
+
`t` resolver, so a hand-written validator can translate its own messages too.
|
|
17
|
+
|
|
18
|
+
Adds `validators.matches(otherFieldName)` for confirm-password and
|
|
19
|
+
confirm-email fields, which every consumer was hand-writing.
|
|
20
|
+
|
|
21
|
+
No locale bundles ship: the mechanism is here, the translations are yours.
|
|
22
|
+
|
|
23
|
+
- a7358f9: Fix per-field `dirty`, which was measured against a baseline captured at mount
|
|
24
|
+
and never re-based - wrong after `reset(newValues)` on all three adapters, and
|
|
25
|
+
wrong on React and Vue for values that arrive after mount, where every field
|
|
26
|
+
reported dirty forever.
|
|
27
|
+
|
|
28
|
+
Adds `baselineValues` and `getDirtyValues()` to the form store on all three
|
|
29
|
+
adapters, and an `initialProperties` prop to `MultiFieldInput` for re-basing
|
|
30
|
+
without a store. Comparison moves from `!==` to `Object.is`, so a `NaN` numeric
|
|
31
|
+
field no longer reads as permanently dirty.
|
|
32
|
+
|
|
33
|
+
React's `useDynamicForm` no longer validates the same data twice per change.
|
|
34
|
+
|
|
35
|
+
- 53ed45a: `options` can now return a promise, covering both dependent selects
|
|
36
|
+
(`optionsDeps`) and search-remote pickers (`onOptionsQuery`). Renderers receive
|
|
37
|
+
`optionsStatus` and `optionsError` alongside `options`.
|
|
38
|
+
|
|
39
|
+
`debounceMs` was declared on `FieldDescription`, published in the `.d.ts` and
|
|
40
|
+
read by no implementation anywhere - setting it did nothing. It now debounces
|
|
41
|
+
these loads.
|
|
42
|
+
|
|
43
|
+
Debounce, abort of a superseded request, and discarding a response that lands
|
|
44
|
+
out of order all live in core's `createOptionsLoader`, so the three adapters
|
|
45
|
+
share one implementation. Synchronous and static options are untouched and never
|
|
46
|
+
enter a loading state.
|
|
47
|
+
|
|
48
|
+
- e35e876: `ariaDescribedBy` is now `${id}-error` when a field has an error instead of
|
|
49
|
+
being hard-coded `undefined`, and `makeErrorId` is exported so a custom renderer
|
|
50
|
+
can put the matching id on its message element. Without this,
|
|
51
|
+
`focusFirstInvalidField` had nothing to find for anyone following the official
|
|
52
|
+
renderer recipe.
|
|
53
|
+
|
|
54
|
+
Default renderers now render the validation message they were already being
|
|
55
|
+
handed - the one visible change in this release. Custom renderers are untouched,
|
|
56
|
+
so nobody gets two copies of their own message.
|
|
57
|
+
|
|
58
|
+
Development builds now warn when `FieldDescription.props` carries a key the
|
|
59
|
+
renderer prop contract owns, which 1.6.0 made possible to lose silently.
|
|
60
|
+
|
|
61
|
+
## 1.6.0
|
|
62
|
+
|
|
63
|
+
### Minor Changes
|
|
64
|
+
|
|
65
|
+
- Cancellable, status-aware async validation; touched state that reaches inside repeatable groups; Angular 21 and TypeScript 5.9 support; and peer ranges corrected to the versions that actually work, proven at both ends in CI.
|
|
66
|
+
- 67e4eec: Give every adapter one renderer-prop contract, unique field ids, and a touched
|
|
67
|
+
state the form store can actually drive.
|
|
68
|
+
|
|
69
|
+
Five things went wrong at once for anyone building a real form on 1.5.1, and
|
|
70
|
+
four of them share a cause: `FieldRendererProps` was a type nobody enforced.
|
|
71
|
+
Each adapter hand-wrote the object it handed the registered renderer, and the
|
|
72
|
+
three lists drifted. React dropped `placeholder`, `min`, `max`, `step`,
|
|
73
|
+
`accept` and `multiple`. Vue dropped `required`, `id`, `dirty` and the aria
|
|
74
|
+
flags. Angular dropped `touched`, `dirty` and `id` — so an Angular renderer had
|
|
75
|
+
no way to know whether a field had been touched, and "only show the error once
|
|
76
|
+
the user leaves the field" had to be rebuilt by hand. Setting
|
|
77
|
+
`placeholder` on a `FieldDescription` therefore did nothing at all on React and
|
|
78
|
+
Vue: no error, no warning, the value simply vanished. Core now owns the list as
|
|
79
|
+
`FIELD_RENDERER_PROP_KEYS` and builds the bag once in
|
|
80
|
+
`buildFieldRendererProps`, which all three adapters call, and
|
|
81
|
+
`scripts/check-renderer-prop-parity.js` fails the build if an adapter stops
|
|
82
|
+
forwarding one. The single deliberate deviation is Vue's `class` in place of
|
|
83
|
+
`className`: forwarding `className` lets it fall through to a renderer's root
|
|
84
|
+
element, where Vue assigns `el.className` and wipes the class the renderer set
|
|
85
|
+
on itself.
|
|
86
|
+
|
|
87
|
+
Field ids were `dfk-field-${name}`, derived from the field name alone. Two
|
|
88
|
+
forms holding a field of the same name — a create form beside an edit form, the
|
|
89
|
+
most ordinary layout there is — emitted the same DOM id twice, which is invalid
|
|
90
|
+
HTML and leaves every `label[for]` pointing at two inputs. Ids are now
|
|
91
|
+
namespaced per `MultiFieldInput` instance (React `useId`, so it is SSR-safe;
|
|
92
|
+
Vue's instance uid; a counter on Angular). Set `idPrefix` to pin them —
|
|
93
|
+
`idPrefix="dfk-field"` reproduces the old ids exactly — or give a single field
|
|
94
|
+
its own id with the new `FieldDescription.id`.
|
|
95
|
+
|
|
96
|
+
Touched state had two independent trackers that never met: the one in
|
|
97
|
+
`useDynamicForm`, and a private one inside `MultiFieldInput` that only blur
|
|
98
|
+
could set and that was the one renderers actually saw. So
|
|
99
|
+
`setFieldTouched` in an `onInvalid` handler changed nothing visible, submitting
|
|
100
|
+
a form nobody had focused showed no errors at all (the button looked broken),
|
|
101
|
+
and `reset()` could not clear the touched state a previous submit had left
|
|
102
|
+
behind. `MultiFieldInput` now accepts `touched` as a controlled prop —
|
|
103
|
+
`useDynamicForm` becomes the single source of truth for it, exactly as
|
|
104
|
+
`properties`/`onChange` already were for data — plus `onTouchedChange`, and a
|
|
105
|
+
`form` shorthand (React and Vue) that wires data, change, blur and touched in
|
|
106
|
+
one prop. `handleSubmit` marks every field touched before validating, and the
|
|
107
|
+
new `touchAll()`/`resetTouched()` sit alongside it. Omit `touched` and the old
|
|
108
|
+
internal tracker still runs, so nothing breaks; for that mode a ref
|
|
109
|
+
(`resetTouched()` on React and Vue, a public method on Angular) can clear it
|
|
110
|
+
without remounting the component.
|
|
111
|
+
|
|
112
|
+
The only behaviour change to watch for is the generated ids. Anything pinned to
|
|
113
|
+
a literal `dfk-field-*` id in CSS or a test needs either `idPrefix="dfk-field"`
|
|
114
|
+
or a per-field `id`. Angular's `MultiFieldInput` also loses four undocumented
|
|
115
|
+
template helpers — `getResolvedOptions`, `getDisabled`, `getReadOnly` and
|
|
116
|
+
`getError` — which its own template no longer calls now that `FieldInput`
|
|
117
|
+
resolves all of it through core. Keeping a second copy of that logic beside the
|
|
118
|
+
shared one is how the adapters drifted apart to begin with; the equivalents are
|
|
119
|
+
`resolveOptions`, `resolveDisabled`, `resolveReadOnly` and `validateField`,
|
|
120
|
+
already re-exported from this package.
|
|
121
|
+
|
|
122
|
+
Form validity now reflects current data immediately instead of merely checking
|
|
123
|
+
the lazily populated `errors` map. The error map remains lazy for display, and
|
|
124
|
+
passing a form binding (or the new controlled `errors` input) makes that same
|
|
125
|
+
map the renderer's source of truth, removing the previous timing mismatch.
|
|
126
|
+
|
|
127
|
+
Promise-based validators are no longer silently accepted on submit.
|
|
128
|
+
`validateFields` reports unresolved field names in `pending`; every framework
|
|
129
|
+
form helper uses one async-capable validation pass before dispatching submit
|
|
130
|
+
callbacks. React, Vue and Angular also expose
|
|
131
|
+
`validateAsync()` for explicit pre-submit checks. Live `isValid` remains a
|
|
132
|
+
synchronous answer because a property/computed/signal cannot await.
|
|
133
|
+
|
|
134
|
+
The new UI-kit recipes show complete touched/error wiring for Ant Design,
|
|
135
|
+
Vuetify and Angular Material.
|
|
136
|
+
|
|
137
|
+
- 5e0b08f: Make async validation answerable: a status you can act on, runs that cancel
|
|
138
|
+
cleanly, and touched state that reaches inside repeatable groups.
|
|
139
|
+
|
|
140
|
+
`ValidationResult` gains `complete` and `status` (`'valid' | 'invalid' |
|
|
141
|
+
'pending'`). Combining `valid` with `pending` was the only way to tell "nothing
|
|
142
|
+
is wrong" from "nothing is wrong _yet_", and everyone got it wrong the same
|
|
143
|
+
way — a `valid: true` with async rules still in flight reads as a green light.
|
|
144
|
+
`status` is the single answer; `complete` says whether every applicable
|
|
145
|
+
validator finished. Both are always present on a result the library returns, so
|
|
146
|
+
reading them needs no fallback; code that constructs a `ValidationResult` by
|
|
147
|
+
hand (a mock, a wrapper typed to return one) has to supply them.
|
|
148
|
+
|
|
149
|
+
`FieldDescription.validationMode: 'async'` declares a validator that returns a
|
|
150
|
+
Promise without the `async` keyword, which detection cannot see. Declaring it
|
|
151
|
+
keeps the synchronous pass from invoking the validator at all — and, unlike
|
|
152
|
+
detection, it is an explicit opt-in, so the dev warning about a field the live
|
|
153
|
+
pass cannot check stays quiet for it.
|
|
154
|
+
|
|
155
|
+
`validateFieldsAsync` now takes a `ValidationContext` and forwards its
|
|
156
|
+
`AbortSignal` to every validator, runs independent validators in parallel
|
|
157
|
+
instead of awaiting them one after another, skips validators once the signal is
|
|
158
|
+
aborted, and reports an aborted run as `complete: false` / `status: 'pending'`.
|
|
159
|
+
A validator that honours the signal the conventional way — rejecting with an
|
|
160
|
+
`AbortError` — no longer rejects the caller's `handleSubmit`; an error that is
|
|
161
|
+
not an abort still propagates.
|
|
162
|
+
|
|
163
|
+
Each adapter's form helper exposes `isValidating`, `isValidationComplete` and
|
|
164
|
+
`validationStatus`, and applies latest-run-wins: typing cancels an in-flight
|
|
165
|
+
live validation so a stale result cannot overwrite a newer one. A submit is not
|
|
166
|
+
collateral damage of that — it validates the snapshot the user submitted under
|
|
167
|
+
a controller of its own, so editing a field mid-flight no longer leaves the
|
|
168
|
+
form with the submit silently dropped, no `onValid`/`onInvalid`, and a button
|
|
169
|
+
that just re-enables.
|
|
170
|
+
|
|
171
|
+
`touchAll()` now expands to the concrete leaf paths that exist in the data
|
|
172
|
+
(`contacts[0].email`, not `contacts`) via the new `collectFieldPaths`, skipping
|
|
173
|
+
fields validation itself skips — hidden by `appearCondition`, or disabled.
|
|
174
|
+
Repeatable group items receive `touched` and report blur with their full path,
|
|
175
|
+
so a UI kit that only shows an error once a field is touched now works inside a
|
|
176
|
+
group. An item with no touched keys still receives a map rather than
|
|
177
|
+
`undefined`, which previously flipped the nested input into tracking touched by
|
|
178
|
+
itself and left it stale after the owner cleared the map. The new
|
|
179
|
+
`indexGroupPathMap` is what indexes those maps by item, exported so a custom
|
|
180
|
+
renderer can do the same without filtering the whole map per item.
|
|
181
|
+
|
|
182
|
+
React's `isValid` is now seeded from the initial data instead of from an
|
|
183
|
+
effect. An effect never runs on the server, so a server-rendered form shipped
|
|
184
|
+
`isValid: true` for an empty required field and never corrected it — a submit
|
|
185
|
+
button rendered enabled and stayed that way.
|
|
186
|
+
|
|
187
|
+
`@dynamic-field-kit/angular`'s `types` entry pointed at `dist/index.d.ts`,
|
|
188
|
+
which is not where its type declarations are emitted any more; it and the
|
|
189
|
+
`exports` block now point at the file that actually ships, so TypeScript
|
|
190
|
+
consumers resolve the package's types again.
|
|
191
|
+
|
|
192
|
+
- eec9386: Correct the peer ranges to the ones that actually work, and prove both ends of
|
|
193
|
+
each in CI.
|
|
194
|
+
|
|
195
|
+
`@dynamic-field-kit/angular` declared `@angular/core` and `@angular/common` as
|
|
196
|
+
`>=14 <22`, but the form store is built on `signal` and `computed`, which
|
|
197
|
+
Angular introduced in **16**. On 14 or 15 npm accepted the install and the
|
|
198
|
+
package then failed on import - the manifest promised something it could not
|
|
199
|
+
do. The range is now `>=16 <22`, so the same install is refused up front.
|
|
200
|
+
|
|
201
|
+
`@dynamic-field-kit/vue` moves from `vue ^3.0.0` to `^3.2.0`.
|
|
202
|
+
`useDynamicForm` now aborts an in-flight validation when the owning effect
|
|
203
|
+
scope is disposed, using `getCurrentScope` / `onScopeDispose` - both Vue 3.2.
|
|
204
|
+
Without this an unmounted form held its request open until the response came
|
|
205
|
+
back. If you are on Vue 3.0 or 3.1, stay on 1.5.x; nothing else in the package
|
|
206
|
+
ever required 3.2, but nothing tested below it either.
|
|
207
|
+
|
|
208
|
+
Both ranges are now verified rather than asserted:
|
|
209
|
+
`scripts/verify-vue-peer-range.js` server-renders the packed tarballs under Vue
|
|
210
|
+
3.2 and the newest 3.x, and `scripts/verify-angular-peer-range.js` installs
|
|
211
|
+
them against Angular 16 and 21 and checks the package imports, its components
|
|
212
|
+
evaluate and it shares one registry with core. Both run in the CI verify job,
|
|
213
|
+
next to the React one that has existed since 1.5.0. A render is out of reach
|
|
214
|
+
for Angular - the published fesm2022 needs the CLI's linker to instantiate a
|
|
215
|
+
component - but import-and-wire is the level that breaks across majors, which
|
|
216
|
+
is exactly how a floor of 14 survived years of `signal()`.
|
|
217
|
+
|
|
218
|
+
The three adapters now re-export `collectFieldPaths`, `indexGroupPathMap` and
|
|
219
|
+
the `ValidationContext` type from core, so typing a validator's `context`
|
|
220
|
+
argument no longer means importing `@dynamic-field-kit/core` alongside the
|
|
221
|
+
adapter.
|
|
222
|
+
|
|
223
|
+
`@angular/platform-browser-dynamic`, which Angular 21 deprecates, is gone from
|
|
224
|
+
the package's devDependencies and from the demo app, which never used it - the
|
|
225
|
+
test setup now initialises through `@angular/platform-browser/testing`.
|
|
226
|
+
|
|
3
227
|
## 1.5.1
|
|
4
228
|
|
|
5
229
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -37,9 +37,11 @@ npm install @dynamic-field-kit/core@^1.5.0 @dynamic-field-kit/angular@^1.5.0
|
|
|
37
37
|
- `ColumnLayout` / `RowLayout` / `GridLayout` (the standalone layout components, registered for you)
|
|
38
38
|
- `BaseInputComponent` — the abstract base your custom renderers extend
|
|
39
39
|
- `FieldInputProps` — the inputs `BaseInputComponent` declares; the Angular
|
|
40
|
-
mirror of core's `FieldRendererProps`
|
|
40
|
+
mirror of core's `FieldRendererProps`, and complete as of 1.6 — `touched`,
|
|
41
|
+
`dirty`, `id` and the aria flags used to be missing here, which left an
|
|
42
|
+
Angular renderer no way to tell whether a field had been touched
|
|
41
43
|
- `DynamicFormOptions` — what `createDynamicFormStore` takes: `fields`,
|
|
42
|
-
`initialValues`, `validateOnBlur`, `validateOnChange`
|
|
44
|
+
`initialValues`, `validateOnBlur`, `validateOnChange`, `messages`
|
|
43
45
|
- `LayoutConfig` / `ColumnLayoutConfig` / `RowLayoutConfig` /
|
|
44
46
|
`GridLayoutConfig` — the layout config types, re-exported from core
|
|
45
47
|
- `BaseLayoutConfig` / `ResponsiveLayoutConfig` — this adapter's historical
|
|
@@ -51,20 +53,51 @@ both packages:
|
|
|
51
53
|
|
|
52
54
|
- `validateField` / `validateFieldAsync` — one field, returns `string[]`
|
|
53
55
|
- `validateFields` / `validateFieldsAsync` — a whole schema, returns `ValidationResult`
|
|
56
|
+
- `collectFieldPaths` — the leaf paths a schema actually has in the data (`contacts[0].email`)
|
|
57
|
+
- `indexGroupPathMap` — index an error or touched map by repeatable-group item
|
|
54
58
|
- `resolveDisabled` / `resolveReadOnly` / `resolveOptions` — resolve a field's dynamic conditions and options
|
|
55
59
|
- `validators` — the built-in validator helpers (`required`, `email`, `minLength`, `compose`, …)
|
|
56
60
|
- `FieldDescription` / `FieldTypeKey` / `FieldRendererProps` — the schema and
|
|
57
61
|
renderer contracts every adapter shares
|
|
58
|
-
- `ValidationResult`
|
|
59
|
-
|
|
60
|
-
`
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
- `ValidationResult` / `ValidationContext` — the context carries `signal` and the
|
|
63
|
+
optional `t` message resolver
|
|
64
|
+
- `buildFieldRendererProps` / `makeFieldId` / `makeErrorId` /
|
|
65
|
+
`FIELD_RENDERER_PROP_KEYS` — the renderer prop contract. `makeErrorId(id)` is
|
|
66
|
+
what a custom renderer puts on its message element so `aria-describedby`
|
|
67
|
+
resolves
|
|
68
|
+
- `createOptionsLoader` / `isAsyncOptions` — the async options engine
|
|
69
|
+
- `createMessageResolver` / `setDefaultMessages` / `MessageCatalog` — validation
|
|
70
|
+
message catalog
|
|
71
|
+
|
|
72
|
+
`createDynamicFormStore` keeps live validation synchronous - a validator declared or
|
|
73
|
+
detected as async is never invoked on that path. Its `handleSubmit` runs one
|
|
74
|
+
async-capable pass, and `validateAsync()` is there when you need that answer
|
|
75
|
+
before submit. Runs are latest-wins: typing aborts the live run in flight, so a
|
|
76
|
+
stale result cannot overwrite a newer one, and a submit validates the snapshot
|
|
77
|
+
it was given under a controller of its own, so editing mid-submit no longer
|
|
78
|
+
cancels it. Declare a Promise-returning validator with
|
|
79
|
+
`validationMode: 'async'` and read `context.signal` (the fourth argument) to
|
|
80
|
+
cancel the request itself. See the
|
|
64
81
|
[core README](https://github.com/vannt-dev/dynamic-field-kit/tree/develop/packages/core#sync-vs-async-validation)
|
|
65
82
|
for the full rules.
|
|
66
83
|
|
|
67
|
-
|
|
84
|
+
For a complete UI integration, see the
|
|
85
|
+
[Angular Material recipe](../../docs/ui-kit-recipes.md#angular--angular-material).
|
|
86
|
+
|
|
87
|
+
## Supported Angular versions
|
|
88
|
+
|
|
89
|
+
The package declares `@angular/core` and `@angular/common` as `>=16 <22`,
|
|
90
|
+
and `scripts/verify-angular-peer-range.js` proves both ends in CI by installing
|
|
91
|
+
the packed tarballs against Angular 16 and 21 outside the workspace.
|
|
92
|
+
|
|
93
|
+
The floor is 16 because the form store is built on `signal` and `computed`,
|
|
94
|
+
which Angular introduced in 16. It read `>=14` until 1.6.0: npm accepted the
|
|
95
|
+
install on 14 and 15 and the package then failed on import.
|
|
96
|
+
|
|
97
|
+
The suite, the build and the demo app all run Angular 21, which is the version
|
|
98
|
+
the setup below is written for.
|
|
99
|
+
|
|
100
|
+
## Basic setup
|
|
68
101
|
|
|
69
102
|
1. Import the component and register fields before bootstrap.
|
|
70
103
|
|
|
@@ -141,8 +174,11 @@ import {
|
|
|
141
174
|
<dfk-multi-field-input
|
|
142
175
|
[fieldDescriptions]="fields"
|
|
143
176
|
[properties]="store.data()"
|
|
177
|
+
[touched]="store.touched()"
|
|
178
|
+
[errors]="store.errors()"
|
|
144
179
|
(onChange)="store.handleChange($event)"
|
|
145
180
|
(onBlurField)="store.handleBlur($event)"
|
|
181
|
+
[initialProperties]="store.baselineValues()"
|
|
146
182
|
></dfk-multi-field-input>
|
|
147
183
|
<button [disabled]="store.isSubmitting()">Save</button>
|
|
148
184
|
</form>
|
|
@@ -154,6 +190,7 @@ export class MyForm {
|
|
|
154
190
|
fields,
|
|
155
191
|
initialValues: { country: 'VN' },
|
|
156
192
|
validateOnBlur: true, // default
|
|
193
|
+
messages: { required: 'Bắt buộc' }, // optional; see Validation & conditions
|
|
157
194
|
});
|
|
158
195
|
|
|
159
196
|
// handleSubmit returns a handler, exactly like React and Vue.
|
|
@@ -161,25 +198,58 @@ export class MyForm {
|
|
|
161
198
|
}
|
|
162
199
|
```
|
|
163
200
|
|
|
164
|
-
| Member | Description
|
|
165
|
-
| ----------------------------------- |
|
|
166
|
-
| `data()` | Current form data, with `computeValue` fields applied
|
|
167
|
-
| `errors()` | `Record<string, string[]>`, keyed like `validateFields`
|
|
168
|
-
| `isValid()` / `isDirty()` |
|
|
169
|
-
| `
|
|
170
|
-
| `
|
|
171
|
-
| `
|
|
172
|
-
| `
|
|
173
|
-
| `
|
|
174
|
-
| `
|
|
175
|
-
| `
|
|
176
|
-
| `
|
|
177
|
-
| `
|
|
201
|
+
| Member | Description |
|
|
202
|
+
| ----------------------------------- | -------------------------------------------------------------------------------------------------------------- |
|
|
203
|
+
| `data()` | Current form data, with `computeValue` fields applied |
|
|
204
|
+
| `errors()` | `Record<string, string[]>`, keyed like `validateFields` |
|
|
205
|
+
| `isValid()` / `isDirty()` | Current synchronous validity / any value has changed |
|
|
206
|
+
| `baselineValues()` | Signal holding the values `dirty` is measured against - `initialValues` until `reset(newValues)` replaces them |
|
|
207
|
+
| `getDirtyValues()` | Only the entries differing from `baselineValues`, for PATCH-style submits |
|
|
208
|
+
| `isValidating()` | An async validation pass is in flight |
|
|
209
|
+
| `isValidationComplete()` | Every applicable validator finished and none is in flight |
|
|
210
|
+
| `validationStatus()` | `'valid' | 'invalid' | 'pending'`— prefer it over`isValid`alone:`valid` cannot tell "nothing is wrong" from "nothing is wrong yet" |
|
|
211
|
+
| `isSubmitting()` / `isSubmitted()` | In-flight submit / at least one submit attempted |
|
|
212
|
+
| `touched()` | Fields that have been blurred |
|
|
213
|
+
| `handleChange(data)` | Replace the whole form data — bind to `(onChange)` |
|
|
214
|
+
| `setFieldValue(name, value)` | Change one field |
|
|
215
|
+
| `handleBlur(name)` | Mark touched, and validate when `validateOnBlur` |
|
|
216
|
+
| `setFieldTouched(name, value?)` | Set touched explicitly |
|
|
217
|
+
| `touchAll()` | Mark every field touched — `handleSubmit` already calls it |
|
|
218
|
+
| `resetTouched()` | Clear touched only, leaving data/errors/dirty alone |
|
|
219
|
+
| `validate()` | Validate now, returns a boolean |
|
|
220
|
+
| `validateAsync()` | Validate now, awaiting Promise-based rules |
|
|
221
|
+
| `reset(values?)` | Back to `initialValues` (or the values given), clearing errors/touched/submission |
|
|
222
|
+
| `handleSubmit(onValid, onInvalid?)` | Returns an async handler; calls `preventDefault`, validates, then dispatches |
|
|
178
223
|
|
|
179
224
|
`MultiFieldInput` emits `(onBlurField)` with the field's name, driven by a
|
|
180
225
|
`focusout` listener — so it works with any renderer, without the renderer
|
|
181
226
|
needing a blur output of its own.
|
|
182
227
|
|
|
228
|
+
Binding `[touched]="store.touched()"` and `[errors]="store.errors()"` makes the
|
|
229
|
+
store the single source of truth for renderer metadata. Touched state is what
|
|
230
|
+
makes an invalid submit visible: `handleSubmit` marks every
|
|
231
|
+
field touched before validating, so a renderer that gates its error on the
|
|
232
|
+
`touched` input shows it even for fields the user never focused. `reset()`
|
|
233
|
+
clears touched the same way. Leave `[touched]` unbound and `MultiFieldInput`
|
|
234
|
+
falls back to tracking it internally from blur alone; in that mode call its
|
|
235
|
+
public `resetTouched()` / `setFieldTouched(name, value?)` (via a `@ViewChild`)
|
|
236
|
+
to clear it, and listen to `(touchedChange)` for the next map.
|
|
237
|
+
|
|
238
|
+
## Field ids
|
|
239
|
+
|
|
240
|
+
Each field renders with ``id={`${idPrefix}-${name}`}``, where `idPrefix`
|
|
241
|
+
defaults to a value unique to the `dfk-multi-field-input` instance. Two forms
|
|
242
|
+
containing a field of the same name therefore no longer emit the same DOM id
|
|
243
|
+
twice, and the id now reaches renderers as the `id` input.
|
|
244
|
+
|
|
245
|
+
```html
|
|
246
|
+
<!-- pinned ids — reproduces the pre-1.6 `dfk-field-title` -->
|
|
247
|
+
<dfk-multi-field-input [fieldDescriptions]="fields" idPrefix="dfk-field">
|
|
248
|
+
</dfk-multi-field-input>
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
For one field, set `id` on its `FieldDescription`; it wins over the prefix.
|
|
252
|
+
|
|
183
253
|
## Default renderers
|
|
184
254
|
|
|
185
255
|
`text` · `number` · `password` · `email` · `textarea` · `checkbox` · `select` ·
|
|
@@ -189,6 +259,13 @@ Any type you have not registered falls back to one of these. `file` emits a
|
|
|
189
259
|
`File` (or `File[]` when `multiple` is set), `range` and `number` emit numbers,
|
|
190
260
|
`checkbox` / `switch` emit booleans; everything else emits strings.
|
|
191
261
|
|
|
262
|
+
Since 1.7.0 a default renderer also renders its validation message, as
|
|
263
|
+
`<div id="{fieldId}-error" class="dfk-field-error" role="alert">`, which is what
|
|
264
|
+
`aria-describedby` points at. Before that they were handed `error` and dropped
|
|
265
|
+
it, so the form showed nothing. A registered custom renderer is unaffected — the
|
|
266
|
+
node is emitted only where a default was used, so you never get two copies. Hide
|
|
267
|
+
it with `.dfk-field-error { display: none }` if you want the old silence.
|
|
268
|
+
|
|
192
269
|
## DevTools
|
|
193
270
|
|
|
194
271
|
```html
|
|
@@ -281,6 +358,52 @@ your renderer component receives `error`, `disabled`, and `readOnly` inputs, and
|
|
|
281
358
|
|
|
282
359
|
For submit-time whole-form validation, call `validateFields(fields, data)`.
|
|
283
360
|
|
|
361
|
+
### Validation messages
|
|
362
|
+
|
|
363
|
+
Set the built-in validators' messages once per form instead of on every field:
|
|
364
|
+
|
|
365
|
+
```ts
|
|
366
|
+
const store = createDynamicFormStore({
|
|
367
|
+
fields,
|
|
368
|
+
messages: { required: 'Bắt buộc', minLength: 'Tối thiểu {min} ký tự' },
|
|
369
|
+
});
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
A message passed straight to a validator still wins, and any key omitted falls
|
|
373
|
+
back to the English default. `setDefaultMessages(catalog)` sets a process-wide
|
|
374
|
+
one for code calling `validateFields` directly. Full key list in the
|
|
375
|
+
[core README](../core/README.md#validation-messages). **No locale bundles
|
|
376
|
+
ship** — the mechanism is here, the translations are yours.
|
|
377
|
+
|
|
378
|
+
Forward `ariaInvalid`, `ariaRequired` and `ariaDescribedBy` from your renderer
|
|
379
|
+
too, and put `makeErrorId(id)` on whatever element shows the message.
|
|
380
|
+
`focusFirstInvalidField` selects `[aria-invalid="true"]`, so a renderer that
|
|
381
|
+
drops those props makes that helper silently do nothing. See
|
|
382
|
+
[the recipes](../../docs/ui-kit-recipes.md#forward-the-aria-props).
|
|
383
|
+
|
|
384
|
+
### Async options
|
|
385
|
+
|
|
386
|
+
`options` may return a promise. The renderer receives `optionsStatus`
|
|
387
|
+
(`'idle' | 'loading' | 'ready' | 'error'`), `optionsError` and
|
|
388
|
+
`onOptionsQuery`:
|
|
389
|
+
|
|
390
|
+
```ts
|
|
391
|
+
{
|
|
392
|
+
name: 'assignee',
|
|
393
|
+
type: 'userPicker',
|
|
394
|
+
options: async (data, _rootData, ctx) =>
|
|
395
|
+
fetch(`/api/users?q=${ctx?.query ?? ''}`, { signal: ctx?.signal })
|
|
396
|
+
.then((r) => r.json()),
|
|
397
|
+
optionsDeps: (data) => [data.team], // reload when this changes; default []
|
|
398
|
+
debounceMs: 300, // collapses rapid reloads into one fetch
|
|
399
|
+
}
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
Superseded requests are aborted and out-of-order responses discarded, so the
|
|
403
|
+
list always reflects the newest request. Static and synchronous options are
|
|
404
|
+
untouched and never enter a loading state. See the
|
|
405
|
+
[core README](../core/README.md#async-options).
|
|
406
|
+
|
|
284
407
|
## Repeatable field groups
|
|
285
408
|
|
|
286
409
|
A field with `fields` renders as a repeatable group: `data[name]` becomes an array of items, each shaped by the nested `fields`, with "Add"/"Remove" controls rendered automatically.
|