@canonical/svelte-ds-global-form 0.48.0 → 0.50.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 +213 -8
- package/dist/_work_in_progress/subcomponent/ComboboxInput/MultipleCombobox.svelte +1 -1
- package/dist/_work_in_progress/subcomponent/ComboboxInput/SingleCombobox.svelte +1 -1
- package/dist/common/useConditionalDisplay.svelte.js +1 -1
- package/dist/component/CheckboxField/CheckboxField.svelte +21 -3
- package/dist/component/CheckboxField/CheckboxField.svelte.d.ts +2 -2
- package/dist/component/CheckboxField/types.d.ts +13 -1
- package/dist/component/ChoicesField/Choices.svelte +5 -3
- package/dist/component/RichChoicesField/RichChoices.svelte +5 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/pattern/Field/Field.svelte +20 -4
- package/dist/pattern/Field/types.d.ts +1 -1
- package/dist/pattern/Form/Form.svelte +1 -2
- package/dist/pattern/Form/Form.svelte.d.ts +1 -1
- package/dist/pattern/Form/types.d.ts +1 -1
- package/dist/subcomponent/CheckboxInput/CheckboxInput.svelte +18 -2
- package/dist/subcomponent/CheckboxInput/CheckboxInput.svelte.d.ts +4 -2
- package/dist/subcomponent/CheckboxInput/types.d.ts +7 -0
- package/dist/subcomponent/RadioInput/RadioInput.svelte +28 -4
- package/dist/subcomponent/RadioInput/RadioInput.svelte.d.ts +4 -2
- package/dist/subcomponent/RadioInput/types.d.ts +7 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/middleware/types.d.ts +1 -1
- package/dist/utils/middleware/validateRemotely.js +1 -1
- package/dist/utils/owner/bindField.d.ts +5 -3
- package/dist/utils/owner/bindField.js +3 -1
- package/dist/utils/owner/bindFieldArray.d.ts +24 -0
- package/dist/utils/owner/bindFieldArray.js +81 -0
- package/dist/utils/owner/bindTanStackForm.d.ts +18 -0
- package/dist/utils/owner/bindTanStackForm.js +65 -0
- package/dist/utils/owner/createBinding.js +6 -0
- package/dist/utils/owner/createFormState.svelte.d.ts +20 -2
- package/dist/utils/owner/createFormState.svelte.js +188 -32
- package/dist/utils/owner/createSchemaValidator.d.ts +20 -0
- package/dist/utils/owner/createSchemaValidator.js +39 -0
- package/dist/utils/owner/createValidatedBinding.d.ts +2 -2
- package/dist/utils/owner/createValidatedBinding.js +9 -4
- package/dist/utils/owner/getPath.d.ts +6 -0
- package/dist/utils/owner/getPath.js +15 -0
- package/dist/utils/owner/index.d.ts +8 -0
- package/dist/utils/owner/index.js +8 -0
- package/dist/utils/owner/normalizePath.d.ts +6 -0
- package/dist/utils/owner/normalizePath.js +9 -0
- package/dist/utils/owner/readOwnerValue.d.ts +7 -0
- package/dist/utils/owner/readOwnerValue.js +11 -0
- package/dist/utils/owner/setPath.d.ts +12 -0
- package/dist/utils/owner/setPath.js +36 -0
- package/dist/utils/owner/splitPath.d.ts +7 -0
- package/dist/utils/owner/splitPath.js +9 -0
- package/dist/utils/owner/types.d.ts +164 -14
- package/dist/utils/remoteForm/bindRemoteField.d.ts +24 -0
- package/dist/utils/remoteForm/bindRemoteField.js +69 -0
- package/dist/utils/remoteForm/bindRemoteForm.d.ts +20 -0
- package/dist/utils/remoteForm/bindRemoteForm.js +31 -0
- package/dist/utils/remoteForm/constants.d.ts +42 -0
- package/dist/utils/remoteForm/constants.js +42 -0
- package/dist/utils/remoteForm/index.d.ts +3 -0
- package/dist/utils/remoteForm/index.js +2 -0
- package/dist/utils/remoteForm/types.d.ts +90 -0
- package/dist/utils/remoteForm/types.js +1 -0
- package/package.json +23 -11
- /package/dist/{component/ChoicesField → utils}/isChoiceChecked.d.ts +0 -0
- /package/dist/{component/ChoicesField → utils}/isChoiceChecked.js +0 -0
- /package/dist/{component/ChoicesField → utils}/toggleChoice.d.ts +0 -0
- /package/dist/{component/ChoicesField → utils}/toggleChoice.js +0 -0
|
@@ -10,10 +10,16 @@
|
|
|
10
10
|
class: className,
|
|
11
11
|
style,
|
|
12
12
|
checked = $bindable(),
|
|
13
|
+
group = $bindable(),
|
|
13
14
|
onchange,
|
|
14
15
|
...rest
|
|
15
16
|
}: RadioInputProps = $props();
|
|
16
17
|
|
|
18
|
+
// The value the radio posts, and sets `group` to when selected.
|
|
19
|
+
const choice = $derived(String(rest.value ?? "on"));
|
|
20
|
+
// With a `group`, the radio is selected while the group holds its value.
|
|
21
|
+
const isSelected = $derived(group === undefined ? checked : group === choice);
|
|
22
|
+
|
|
17
23
|
// Keeps a selection made before hydration: a pre-effect runs once, in the
|
|
18
24
|
// browser, before the `checked` property below is written to the
|
|
19
25
|
// server-rendered input.
|
|
@@ -24,14 +30,29 @@
|
|
|
24
30
|
inputElement instanceof HTMLInputElement &&
|
|
25
31
|
inputElement.checked !== inputElement.defaultChecked
|
|
26
32
|
)
|
|
27
|
-
|
|
33
|
+
select(inputElement.checked);
|
|
28
34
|
});
|
|
29
35
|
});
|
|
30
36
|
|
|
37
|
+
/** Records the radio's selection, and sets the group to it when selected. */
|
|
38
|
+
function select(isChecked: boolean): void {
|
|
39
|
+
checked = isChecked;
|
|
40
|
+
if (group !== undefined && isChecked) group = choice;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// A radio is unchecked silently when a sibling is chosen. A group changed
|
|
44
|
+
// away and back before the page updates leaves `isSelected` as it was, so
|
|
45
|
+
// the `checked` attribute is not written again: every write to the group
|
|
46
|
+
// writes the property.
|
|
47
|
+
const syncChecked = (inputElement: HTMLInputElement) => {
|
|
48
|
+
void group;
|
|
49
|
+
inputElement.checked = untrack(() => isSelected === true);
|
|
50
|
+
};
|
|
51
|
+
|
|
31
52
|
// A radio is checked by its own change event, but unchecked silently when a
|
|
32
53
|
// sibling is chosen, so `checked` reports selection rather than tracking it.
|
|
33
54
|
const handleChange = (event: Event & { currentTarget: HTMLInputElement }) => {
|
|
34
|
-
|
|
55
|
+
select(event.currentTarget.checked);
|
|
35
56
|
onchange?.(event);
|
|
36
57
|
};
|
|
37
58
|
</script>
|
|
@@ -40,15 +61,18 @@
|
|
|
40
61
|
{id}
|
|
41
62
|
{style}
|
|
42
63
|
class={[componentCssClassName, className]}
|
|
43
|
-
{
|
|
64
|
+
checked={isSelected}
|
|
44
65
|
onchange={handleChange}
|
|
66
|
+
{@attach syncChecked}
|
|
45
67
|
{...rest}
|
|
46
68
|
type="radio"
|
|
47
69
|
/>
|
|
48
70
|
|
|
49
71
|
<!-- @component
|
|
50
72
|
Presentational radio: the atomic single-select indicator, with no label of its
|
|
51
|
-
own. Bind `checked` to learn when it is selected
|
|
73
|
+
own. Bind `checked` to learn when it is selected, or bind `group` to a string
|
|
74
|
+
shared by several radios, which holds the `value` of the selected one, as
|
|
75
|
+
Svelte's `bind:group` does for inputs in one component.
|
|
52
76
|
|
|
53
77
|
`import { RadioInput } from "@canonical/svelte-ds-global-form";`
|
|
54
78
|
|
|
@@ -2,12 +2,14 @@ import type { RadioInputProps } from "./types.js";
|
|
|
2
2
|
import "./styles.css";
|
|
3
3
|
/**
|
|
4
4
|
* Presentational radio: the atomic single-select indicator, with no label of its
|
|
5
|
-
* own. Bind `checked` to learn when it is selected
|
|
5
|
+
* own. Bind `checked` to learn when it is selected, or bind `group` to a string
|
|
6
|
+
* shared by several radios, which holds the `value` of the selected one, as
|
|
7
|
+
* Svelte's `bind:group` does for inputs in one component.
|
|
6
8
|
*
|
|
7
9
|
* `import { RadioInput } from "@canonical/svelte-ds-global-form";`
|
|
8
10
|
*
|
|
9
11
|
* @implements ds:global.subcomponent.radio_input
|
|
10
12
|
*/
|
|
11
|
-
declare const RadioInput: import("svelte").Component<RadioInputProps, {}, "checked">;
|
|
13
|
+
declare const RadioInput: import("svelte").Component<RadioInputProps, {}, "checked" | "group">;
|
|
12
14
|
type RadioInput = ReturnType<typeof RadioInput>;
|
|
13
15
|
export default RadioInput;
|
|
@@ -3,4 +3,11 @@ import type { HTMLInputAttributes } from "svelte/elements";
|
|
|
3
3
|
export type RadioInputProps = Omit<HTMLInputAttributes, "type"> & {
|
|
4
4
|
/** Whether the radio is selected. Bindable. */
|
|
5
5
|
checked?: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* The selected value of a radio group: the radio is selected while it
|
|
8
|
+
* equals `value`, and selecting it sets it to `value`. Start it as a
|
|
9
|
+
* string, empty when nothing is selected; `checked` is ignored while it is
|
|
10
|
+
* set. Bindable.
|
|
11
|
+
*/
|
|
12
|
+
group?: string;
|
|
6
13
|
};
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
export { default as isChoiceChecked } from "./isChoiceChecked.js";
|
|
1
2
|
export * from "./middleware/index.js";
|
|
2
3
|
export * from "./owner/index.js";
|
|
3
4
|
export * from "./phone/index.js";
|
|
4
5
|
export { default as readEditedChoices } from "./readEditedChoices.js";
|
|
5
6
|
export { default as readEditedSelection } from "./readEditedSelection.js";
|
|
7
|
+
export * from "./remoteForm/index.js";
|
|
8
|
+
export { default as toggleChoice } from "./toggleChoice.js";
|
package/dist/utils/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
export { default as isChoiceChecked } from "./isChoiceChecked.js";
|
|
1
2
|
export * from "./middleware/index.js";
|
|
2
3
|
export * from "./owner/index.js";
|
|
3
4
|
export * from "./phone/index.js";
|
|
4
5
|
export { default as readEditedChoices } from "./readEditedChoices.js";
|
|
5
6
|
export { default as readEditedSelection } from "./readEditedSelection.js";
|
|
7
|
+
export * from "./remoteForm/index.js";
|
|
8
|
+
export { default as toggleChoice } from "./toggleChoice.js";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Option } from "../../subcomponent/types.js";
|
|
2
|
-
import type { FieldValidator } from "../owner/
|
|
2
|
+
import type { FieldValidator } from "../owner/index.js";
|
|
3
3
|
/** A control whose value `validateRemotely` checks. */
|
|
4
4
|
export type ValidatedControl = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
|
|
5
5
|
/** When `validateRemotely` checks a value, and how long it waits for an answer. */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { VALIDITY_EVENT } from "../../common/constants.js";
|
|
2
|
-
import runLatest from "../owner/
|
|
2
|
+
import { runLatest } from "../owner/index.js";
|
|
3
3
|
/** The message shown when the endpoint rejects a value without a readable one. */
|
|
4
4
|
const DEFAULT_ERROR_MESSAGE = "Validation failed";
|
|
5
5
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FieldBinding, FormOwner, FormValues } from "./types.js";
|
|
1
|
+
import type { FieldBinding, FormOwner, FormValues, Path } from "./types.js";
|
|
2
2
|
/**
|
|
3
3
|
* Returns a field's error and blur handler from an owner, to
|
|
4
4
|
* spread onto a `Field` or field component outside a `Form` with that owner.
|
|
@@ -10,7 +10,9 @@ import type { FieldBinding, FormOwner, FormValues } from "./types.js";
|
|
|
10
10
|
* {...bindField(owner, "email")} />
|
|
11
11
|
* ```
|
|
12
12
|
*
|
|
13
|
-
*
|
|
13
|
+
* A nested name reads and writes its path, as `address.city`; read the value
|
|
14
|
+
* with `owner.getValue` when the owner has it. The properties are getters, so
|
|
15
|
+
* the spread stays reactive.
|
|
14
16
|
* @note Pure.
|
|
15
17
|
*/
|
|
16
|
-
export default function bindField<Values extends FormValues>(owner: FormOwner<Values>, name:
|
|
18
|
+
export default function bindField<Values extends FormValues>(owner: FormOwner<Values>, name: Path<Values>): FieldBinding;
|
|
@@ -9,7 +9,9 @@
|
|
|
9
9
|
* {...bindField(owner, "email")} />
|
|
10
10
|
* ```
|
|
11
11
|
*
|
|
12
|
-
*
|
|
12
|
+
* A nested name reads and writes its path, as `address.city`; read the value
|
|
13
|
+
* with `owner.getValue` when the owner has it. The properties are getters, so
|
|
14
|
+
* the spread stays reactive.
|
|
13
15
|
* @note Pure.
|
|
14
16
|
*/
|
|
15
17
|
export default function bindField(owner, name) {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { FieldArray, FieldArrayItem, FormOwner, FormValues, Path } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Binds a list of repeated fields to the array at `name` in an owner, as
|
|
4
|
+
* react-hook-form's `useFieldArray`. `items` gives each item a key that stays
|
|
5
|
+
* with it across `move` and `remove`, for a keyed `{#each}`; the fields inside
|
|
6
|
+
* are named by index:
|
|
7
|
+
*
|
|
8
|
+
* ```svelte
|
|
9
|
+
* {#each contacts.items as item, index (item.key)}
|
|
10
|
+
* <Field inputType="email" name={`contacts.${index}.email`} label="Email" />
|
|
11
|
+
* {/each}
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* Every change writes the whole array through `owner.setValue`, so the
|
|
15
|
+
* owner's validation mode applies to it. Before `remove`, `move` and
|
|
16
|
+
* `replace` write, the owner's `reindex`, if any, moves the errors and
|
|
17
|
+
* touched fields kept under the array with their items, as `useFieldArray`
|
|
18
|
+
* does: `remove` drops the removed item's, `move` carries them along,
|
|
19
|
+
* `replace` keeps those within the new length and drops the rest, and
|
|
20
|
+
* `append` adds none. A change made elsewhere, such as a `reset`, keeps the
|
|
21
|
+
* keys of the items still in place and adds keys for new ones.
|
|
22
|
+
* @note Pure: the returned binding holds only its items' keys.
|
|
23
|
+
*/
|
|
24
|
+
export default function bindFieldArray<Values extends FormValues, Name extends Path<Values>>(owner: FormOwner<Values>, name: Name): FieldArray<FieldArrayItem<Values, Name>>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import readOwnerValue from "./readOwnerValue.js";
|
|
2
|
+
/**
|
|
3
|
+
* Binds a list of repeated fields to the array at `name` in an owner, as
|
|
4
|
+
* react-hook-form's `useFieldArray`. `items` gives each item a key that stays
|
|
5
|
+
* with it across `move` and `remove`, for a keyed `{#each}`; the fields inside
|
|
6
|
+
* are named by index:
|
|
7
|
+
*
|
|
8
|
+
* ```svelte
|
|
9
|
+
* {#each contacts.items as item, index (item.key)}
|
|
10
|
+
* <Field inputType="email" name={`contacts.${index}.email`} label="Email" />
|
|
11
|
+
* {/each}
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* Every change writes the whole array through `owner.setValue`, so the
|
|
15
|
+
* owner's validation mode applies to it. Before `remove`, `move` and
|
|
16
|
+
* `replace` write, the owner's `reindex`, if any, moves the errors and
|
|
17
|
+
* touched fields kept under the array with their items, as `useFieldArray`
|
|
18
|
+
* does: `remove` drops the removed item's, `move` carries them along,
|
|
19
|
+
* `replace` keeps those within the new length and drops the rest, and
|
|
20
|
+
* `append` adds none. A change made elsewhere, such as a `reset`, keeps the
|
|
21
|
+
* keys of the items still in place and adds keys for new ones.
|
|
22
|
+
* @note Pure: the returned binding holds only its items' keys.
|
|
23
|
+
*/
|
|
24
|
+
export default function bindFieldArray(owner, name) {
|
|
25
|
+
const keys = [];
|
|
26
|
+
let nextKey = 0;
|
|
27
|
+
/** Reads the array, and matches the keys to its length. */
|
|
28
|
+
function readItems() {
|
|
29
|
+
const current = readOwnerValue(owner, name);
|
|
30
|
+
const items = Array.isArray(current) ? current : [];
|
|
31
|
+
keys.length = Math.min(keys.length, items.length);
|
|
32
|
+
while (keys.length < items.length)
|
|
33
|
+
keys.push(String(nextKey++));
|
|
34
|
+
return items;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Tells the owner where each item went: `order` holds the old index of
|
|
38
|
+
* each item, in the new order.
|
|
39
|
+
*/
|
|
40
|
+
function reindex(order) {
|
|
41
|
+
owner.reindex?.(name, (index) => {
|
|
42
|
+
const to = order.indexOf(index);
|
|
43
|
+
return to === -1 ? undefined : to;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/** Writes the array through the owner. */
|
|
47
|
+
function write(items) {
|
|
48
|
+
owner.setValue(name, items);
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
get items() {
|
|
52
|
+
return readItems().map((value, index) => ({
|
|
53
|
+
key: keys.at(index),
|
|
54
|
+
value,
|
|
55
|
+
}));
|
|
56
|
+
},
|
|
57
|
+
append(value) {
|
|
58
|
+
write([...readItems(), value]);
|
|
59
|
+
},
|
|
60
|
+
remove(index) {
|
|
61
|
+
const items = readItems();
|
|
62
|
+
keys.splice(index, 1);
|
|
63
|
+
reindex(items.map((_, at) => at).filter((at) => at !== index));
|
|
64
|
+
write(items.filter((_, at) => at !== index));
|
|
65
|
+
},
|
|
66
|
+
move(from, to) {
|
|
67
|
+
const items = [...readItems()];
|
|
68
|
+
const order = items.map((_, at) => at);
|
|
69
|
+
items.splice(to, 0, ...items.splice(from, 1));
|
|
70
|
+
order.splice(to, 0, ...order.splice(from, 1));
|
|
71
|
+
keys.splice(to, 0, ...keys.splice(from, 1));
|
|
72
|
+
reindex(order);
|
|
73
|
+
write(items);
|
|
74
|
+
},
|
|
75
|
+
replace(values) {
|
|
76
|
+
keys.length = 0;
|
|
77
|
+
reindex(values.map((_, at) => at));
|
|
78
|
+
write([...values]);
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { FormValues, ResettableOwner, TanStackFormApi } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Makes a TanStack Form instance a form owner, for a `Form` or `bindField`:
|
|
4
|
+
*
|
|
5
|
+
* ```svelte
|
|
6
|
+
* <Form owner={bindTanStackForm(form)}>
|
|
7
|
+
* ```
|
|
8
|
+
*
|
|
9
|
+
* Values, including nested ones, are read with TanStack's own path read and
|
|
10
|
+
* written with `setFieldValue`. A field shows its first error: the `message`
|
|
11
|
+
* of a schema issue, or a validator's string. Leaving a field marks it
|
|
12
|
+
* blurred and runs the `onBlur` validators. A submit runs `handleSubmit`,
|
|
13
|
+
* which validates and calls the form's `onSubmit`, instead of posting, and
|
|
14
|
+
* `reset` resets the form. TanStack keys an index in brackets
|
|
15
|
+
* (`contacts[0].email`); either path form may name a field.
|
|
16
|
+
* @note Pure: subscribes to the form only while an effect reads the owner.
|
|
17
|
+
*/
|
|
18
|
+
export default function bindTanStackForm<Values extends FormValues>(form: TanStackFormApi<Values>): ResettableOwner<Values>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import createBinding from "./createBinding.js";
|
|
2
|
+
import splitPath from "./splitPath.js";
|
|
3
|
+
/**
|
|
4
|
+
* Writes a field name as TanStack Form keys its fields and errors: an index
|
|
5
|
+
* in brackets, as `contacts[0].email`, whichever path form it is given.
|
|
6
|
+
*/
|
|
7
|
+
const formatTanStackName = (name) => splitPath(name)
|
|
8
|
+
.map((segment, index) => /^\d+$/.test(segment)
|
|
9
|
+
? `[${segment}]`
|
|
10
|
+
: index === 0
|
|
11
|
+
? segment
|
|
12
|
+
: `.${segment}`)
|
|
13
|
+
.join("");
|
|
14
|
+
/** Returns the message of a TanStack error: a schema issue or a string. */
|
|
15
|
+
const readMessage = (error) => typeof error === "string"
|
|
16
|
+
? error
|
|
17
|
+
: error?.message;
|
|
18
|
+
/**
|
|
19
|
+
* Makes a TanStack Form instance a form owner, for a `Form` or `bindField`:
|
|
20
|
+
*
|
|
21
|
+
* ```svelte
|
|
22
|
+
* <Form owner={bindTanStackForm(form)}>
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* Values, including nested ones, are read with TanStack's own path read and
|
|
26
|
+
* written with `setFieldValue`. A field shows its first error: the `message`
|
|
27
|
+
* of a schema issue, or a validator's string. Leaving a field marks it
|
|
28
|
+
* blurred and runs the `onBlur` validators. A submit runs `handleSubmit`,
|
|
29
|
+
* which validates and calls the form's `onSubmit`, instead of posting, and
|
|
30
|
+
* `reset` resets the form. TanStack keys an index in brackets
|
|
31
|
+
* (`contacts[0].email`); either path form may name a field.
|
|
32
|
+
* @note Pure: subscribes to the form only while an effect reads the owner.
|
|
33
|
+
*/
|
|
34
|
+
export default function bindTanStackForm(form) {
|
|
35
|
+
const owner = createBinding({
|
|
36
|
+
getValues: () => form.state.values,
|
|
37
|
+
getValue: (name) => form.getFieldValue(formatTanStackName(name)),
|
|
38
|
+
setValue: (name, value) => form.setFieldValue(formatTanStackName(name), (() => value)),
|
|
39
|
+
subscribe: (notify) => form.store.subscribe(notify).unsubscribe,
|
|
40
|
+
getError: (name) => readMessage(form.getFieldMeta(formatTanStackName(name))?.errors?.at(0)),
|
|
41
|
+
blur: (name) => {
|
|
42
|
+
const field = formatTanStackName(name);
|
|
43
|
+
form.setFieldMeta(field, ((meta) => ({
|
|
44
|
+
...meta,
|
|
45
|
+
isBlurred: true,
|
|
46
|
+
})));
|
|
47
|
+
void form.validateField(field, "blur");
|
|
48
|
+
},
|
|
49
|
+
submit: (event) => {
|
|
50
|
+
event.preventDefault();
|
|
51
|
+
void form.handleSubmit();
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
return {
|
|
55
|
+
get values() {
|
|
56
|
+
return owner.values;
|
|
57
|
+
},
|
|
58
|
+
getValue: owner.getValue,
|
|
59
|
+
setValue: owner.setValue,
|
|
60
|
+
getError: owner.getError,
|
|
61
|
+
blur: owner.blur,
|
|
62
|
+
submit: owner.submit,
|
|
63
|
+
reset: () => form.reset(),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -12,6 +12,12 @@ export default function createBinding(source) {
|
|
|
12
12
|
track();
|
|
13
13
|
return source.getValues();
|
|
14
14
|
},
|
|
15
|
+
getValue: source.getValue
|
|
16
|
+
? (name) => {
|
|
17
|
+
track();
|
|
18
|
+
return source.getValue?.(name);
|
|
19
|
+
}
|
|
20
|
+
: undefined,
|
|
15
21
|
setValue: (name, value) => source.setValue(name, value),
|
|
16
22
|
getError: (name) => {
|
|
17
23
|
track();
|
|
@@ -2,16 +2,34 @@ import type { FormState, FormStateOptions, FormValues } from "./types.js";
|
|
|
2
2
|
/**
|
|
3
3
|
* Builds a minimal form owner on `$state`: values, errors and touched fields,
|
|
4
4
|
* validated by one `validate` function at the time `mode` says, as
|
|
5
|
-
* react-hook-form's `useForm`.
|
|
5
|
+
* react-hook-form's `useForm`. Names reach into nested values, as
|
|
6
|
+
* `address.city` or `contacts.0.email` (SvelteKit's `contacts[0].email` too):
|
|
7
|
+
* a write creates the objects along the path, or an array where the next
|
|
8
|
+
* segment is an index, and errors and touched fields are keyed by the path
|
|
9
|
+
* joined with `.`, as `createSchemaValidator` keys its messages. Pass it to a `Form`:
|
|
6
10
|
*
|
|
7
11
|
* ```svelte
|
|
8
12
|
* <Form owner={createFormState({ defaultValues, validate, onSubmit })}>
|
|
9
13
|
* ```
|
|
10
14
|
*
|
|
15
|
+
* Seed `errors` with messages to show from the start, such as a SvelteKit
|
|
16
|
+
* action's `form.errors` after a post without JavaScript.
|
|
17
|
+
*
|
|
11
18
|
* On submit, every field is validated. An invalid form does not post and its
|
|
12
19
|
* first invalid control is focused. A valid form calls `onSubmit` instead of
|
|
13
20
|
* posting, or posts natively when `onSubmit` is unset. After the first submit,
|
|
14
21
|
* a field is validated again on its every change.
|
|
22
|
+
*
|
|
23
|
+
* `validate` may return a promise, as react-hook-form's asynchronous resolvers
|
|
24
|
+
* do. Only the latest validation counts, and `reset` cancels it. A submit that
|
|
25
|
+
* meets one is stopped, since a submit event can only be stopped at once; when
|
|
26
|
+
* the validation settles, a valid form calls `onSubmit`, or is submitted again
|
|
27
|
+
* with the same submitter so it posts (and SvelteKit's `enhance` sees it), and
|
|
28
|
+
* an invalid one focuses its first invalid control. A validation that throws
|
|
29
|
+
* keeps the messages as they are and drops the waiting submit.
|
|
30
|
+
*
|
|
31
|
+
* A field array (`bindFieldArray`) moves the errors and touched fields under
|
|
32
|
+
* its array with the items, through `reindex`, without validating.
|
|
15
33
|
* @note Pure: the returned owner holds its own state.
|
|
16
34
|
*/
|
|
17
|
-
export default function createFormState<Values extends FormValues>({ defaultValues, validate, mode, onSubmit, }?: FormStateOptions<Values>): FormState<Values>;
|
|
35
|
+
export default function createFormState<Values extends FormValues>({ defaultValues, errors: initialErrors, validate, mode, onSubmit, }?: FormStateOptions<Values>): FormState<Values>;
|