@torpor/ui 1.1.6 → 1.1.7
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/dist/Form/Form.torp +59 -30
- package/package.json +5 -5
package/dist/Form/Form.torp
CHANGED
|
@@ -54,33 +54,56 @@ export default function Form<Data = Record<string, any>>($props: FormProps<Data>
|
|
|
54
54
|
|
|
55
55
|
$context[FormContextName] = context;
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
let
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
// TODO: arrays with `[0]` etc
|
|
69
|
-
issues.push({
|
|
70
|
-
// @ts-ignore
|
|
71
|
-
path: issue.path.map((p) => p.key ?? p).join("."),
|
|
72
|
-
message: issue.message,
|
|
73
|
-
});
|
|
74
|
-
}
|
|
57
|
+
function applyIssues(result: StandardSchemaV1.Result<any> | undefined) {
|
|
58
|
+
let issues: ValidationIssue[] = [];
|
|
59
|
+
if (result?.issues) {
|
|
60
|
+
for (let issue of result.issues) {
|
|
61
|
+
if (issue.path) {
|
|
62
|
+
// TODO: arrays with `[0]` etc
|
|
63
|
+
issues.push({
|
|
64
|
+
// @ts-ignore
|
|
65
|
+
path: issue.path.map((p) => p.key ?? p).join("."),
|
|
66
|
+
message: issue.message,
|
|
67
|
+
});
|
|
75
68
|
}
|
|
76
69
|
}
|
|
77
|
-
$state.issues = issues;
|
|
78
70
|
}
|
|
71
|
+
$state.issues = issues;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Validates the form's data. Returns synchronously for synchronous schemas,
|
|
76
|
+
* or a promise that must be awaited for asynchronous ones.
|
|
77
|
+
*/
|
|
78
|
+
function validate(): void | Promise<void> {
|
|
79
|
+
if (!$props?.schema) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const result = $props.schema["~standard"].validate($props.data);
|
|
83
|
+
if (result instanceof Promise) {
|
|
84
|
+
return result.then(applyIssues);
|
|
85
|
+
}
|
|
86
|
+
applyIssues(result);
|
|
79
87
|
}
|
|
80
88
|
|
|
81
89
|
async function handleSubmit(e: SubmitEvent) {
|
|
82
|
-
//
|
|
83
|
-
|
|
90
|
+
// Run validation. A synchronous schema is fully resolved by this point,
|
|
91
|
+
// but an async one returns a promise -- and if we awaited it we'd only
|
|
92
|
+
// call `preventDefault` after the browser had already started its own
|
|
93
|
+
// submission (loading the action's response as a full page). So stop the
|
|
94
|
+
// native submit up front and carry on once validation resolves.
|
|
95
|
+
const validation = validate();
|
|
96
|
+
if (validation instanceof Promise) {
|
|
97
|
+
e.preventDefault();
|
|
98
|
+
await validation;
|
|
99
|
+
await finishSubmit(e, true);
|
|
100
|
+
} else {
|
|
101
|
+
await finishSubmit(e, false);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function finishSubmit(e: SubmitEvent, prePrevented: boolean) {
|
|
106
|
+
// Validation has run: proceed if there are no errors
|
|
84
107
|
let proceed = $state.valid;
|
|
85
108
|
|
|
86
109
|
// Changing inputs from now on will cause revalidation
|
|
@@ -98,7 +121,10 @@ export default function Form<Data = Record<string, any>>($props: FormProps<Data>
|
|
|
98
121
|
if (userSubmit instanceof Promise) {
|
|
99
122
|
userSubmit = await userSubmit;
|
|
100
123
|
}
|
|
101
|
-
|
|
124
|
+
// When the default was already prevented (async validation) we
|
|
125
|
+
// can't use `defaultPrevented` to see if the handler opted out, so
|
|
126
|
+
// only its return value counts
|
|
127
|
+
proceed = userSubmit !== false && (prePrevented || !e.defaultPrevented);
|
|
102
128
|
}
|
|
103
129
|
|
|
104
130
|
// If the user handled the submit, don't continue
|
|
@@ -107,23 +133,26 @@ export default function Form<Data = Record<string, any>>($props: FormProps<Data>
|
|
|
107
133
|
return false;
|
|
108
134
|
}
|
|
109
135
|
|
|
110
|
-
// If there's a @torpor/build context,
|
|
136
|
+
// If there's a @torpor/build context, it handles the submission itself
|
|
137
|
+
// (it posts the form and prevents the native submit)
|
|
111
138
|
let buildContext = $context?.["TorporBuildContext"];
|
|
112
139
|
if (buildContext?.onformsubmit) {
|
|
113
140
|
let buildSubmit = buildContext.onformsubmit(e);
|
|
114
141
|
if (buildSubmit instanceof Promise) {
|
|
115
142
|
buildSubmit = await buildSubmit;
|
|
116
143
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
// If @torpor/build handled the submit, don't continue
|
|
121
|
-
if (!proceed) {
|
|
122
|
-
e.preventDefault();
|
|
144
|
+
if (buildSubmit === false) {
|
|
145
|
+
e.preventDefault();
|
|
146
|
+
}
|
|
123
147
|
return false;
|
|
124
148
|
}
|
|
125
149
|
|
|
126
|
-
//
|
|
150
|
+
// Nothing handled the submit. When validation was synchronous the
|
|
151
|
+
// browser's own submission is still pending, so let it happen.
|
|
152
|
+
// Otherwise it was already prevented, so submit the form ourselves.
|
|
153
|
+
if (prePrevented) {
|
|
154
|
+
(e.target as HTMLFormElement).submit();
|
|
155
|
+
}
|
|
127
156
|
}
|
|
128
157
|
|
|
129
158
|
async function handleValidate() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@torpor/ui",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.7",
|
|
4
4
|
"description": "Accessible, unstyled components for use in your Torpor front-end",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"torpor": true,
|
|
@@ -181,8 +181,8 @@
|
|
|
181
181
|
"author": "",
|
|
182
182
|
"license": "ISC",
|
|
183
183
|
"dependencies": {
|
|
184
|
-
"@torpor/unplugin": "^1.2.
|
|
185
|
-
"@torpor/view": "^1.1.
|
|
184
|
+
"@torpor/unplugin": "^1.2.1",
|
|
185
|
+
"@torpor/view": "^1.1.3"
|
|
186
186
|
},
|
|
187
187
|
"devDependencies": {
|
|
188
188
|
"@testing-library/dom": "^10.4.1",
|
|
@@ -198,8 +198,8 @@
|
|
|
198
198
|
"vite": "npm:@voidzero-dev/vite-plus-core@0.3.0",
|
|
199
199
|
"vite-plus": "0.3.0",
|
|
200
200
|
"zod": "^4.4.3",
|
|
201
|
-
"@torpor/
|
|
202
|
-
"@torpor/
|
|
201
|
+
"@torpor/check": "^1.0.7",
|
|
202
|
+
"@torpor/prettier": "^1.0.7"
|
|
203
203
|
},
|
|
204
204
|
"scripts": {
|
|
205
205
|
"build": "tsgo --noEmit && vp pack",
|