@inertiajs/core 2.1.1 → 2.1.3
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/index.esm.js +183 -154
- package/dist/index.esm.js.map +3 -3
- package/dist/index.js +187 -158
- package/dist/index.js.map +4 -4
- package/dist/server.esm.js +4 -2
- package/dist/server.esm.js.map +2 -2
- package/dist/server.js +4 -2
- package/dist/server.js.map +2 -2
- package/package.json +9 -5
- package/types/index.d.ts +3 -3
- package/types/navigationEvents.d.ts +12 -0
- package/types/prefetched.d.ts +2 -1
- package/types/router.d.ts +14 -13
- package/types/types.d.ts +15 -10
- package/types/url.d.ts +2 -1
- package/types/shouldIntercept.d.ts +0 -1
package/dist/index.js
CHANGED
|
@@ -34,6 +34,7 @@ __export(index_exports, {
|
|
|
34
34
|
formDataToObject: () => formDataToObject,
|
|
35
35
|
hideProgress: () => hide,
|
|
36
36
|
hrefToUrl: () => hrefToUrl,
|
|
37
|
+
isUrlMethodPair: () => isUrlMethodPair,
|
|
37
38
|
mergeDataIntoQueryString: () => mergeDataIntoQueryString,
|
|
38
39
|
objectToFormData: () => objectToFormData,
|
|
39
40
|
resetFormFields: () => resetFormFields,
|
|
@@ -41,6 +42,7 @@ __export(index_exports, {
|
|
|
41
42
|
router: () => router,
|
|
42
43
|
setupProgress: () => setupProgress,
|
|
43
44
|
shouldIntercept: () => shouldIntercept,
|
|
45
|
+
shouldNavigate: () => shouldNavigate,
|
|
44
46
|
urlWithoutHash: () => urlWithoutHash
|
|
45
47
|
});
|
|
46
48
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -431,6 +433,9 @@ var setHashIfSameUrl = (originUrl, destinationUrl) => {
|
|
|
431
433
|
var isSameUrlWithoutHash = (url1, url2) => {
|
|
432
434
|
return urlWithoutHash(url1).href === urlWithoutHash(url2).href;
|
|
433
435
|
};
|
|
436
|
+
function isUrlMethodPair(href) {
|
|
437
|
+
return href !== null && typeof href === "object" && href !== void 0 && "url" in href && "method" in href;
|
|
438
|
+
}
|
|
434
439
|
|
|
435
440
|
// src/page.ts
|
|
436
441
|
var CurrentPage = class {
|
|
@@ -652,12 +657,10 @@ var History = class {
|
|
|
652
657
|
if (!window.history.state?.page) {
|
|
653
658
|
return;
|
|
654
659
|
}
|
|
655
|
-
this.doReplaceState(
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
}
|
|
660
|
-
);
|
|
660
|
+
this.doReplaceState({
|
|
661
|
+
page: window.history.state.page,
|
|
662
|
+
scrollRegions
|
|
663
|
+
});
|
|
661
664
|
});
|
|
662
665
|
});
|
|
663
666
|
}
|
|
@@ -667,12 +670,10 @@ var History = class {
|
|
|
667
670
|
if (!window.history.state?.page) {
|
|
668
671
|
return;
|
|
669
672
|
}
|
|
670
|
-
this.doReplaceState(
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
}
|
|
675
|
-
);
|
|
673
|
+
this.doReplaceState({
|
|
674
|
+
page: window.history.state.page,
|
|
675
|
+
documentScrollPosition: scrollRegion
|
|
676
|
+
});
|
|
676
677
|
});
|
|
677
678
|
});
|
|
678
679
|
}
|
|
@@ -993,7 +994,7 @@ var Polls = class {
|
|
|
993
994
|
var polls = new Polls();
|
|
994
995
|
|
|
995
996
|
// src/prefetched.ts
|
|
996
|
-
var
|
|
997
|
+
var import_lodash_es = require("lodash-es");
|
|
997
998
|
|
|
998
999
|
// src/objectUtils.ts
|
|
999
1000
|
var objectsAreEqual = (obj1, obj2, excludeKeys) => {
|
|
@@ -1052,7 +1053,7 @@ var PrefetchedRequests = class {
|
|
|
1052
1053
|
this.removalTimers = [];
|
|
1053
1054
|
this.currentUseId = null;
|
|
1054
1055
|
}
|
|
1055
|
-
add(params, sendFunc, { cacheFor }) {
|
|
1056
|
+
add(params, sendFunc, { cacheFor, cacheTags }) {
|
|
1056
1057
|
const inFlight = this.findInFlight(params);
|
|
1057
1058
|
if (inFlight) {
|
|
1058
1059
|
return Promise.resolve();
|
|
@@ -1097,7 +1098,8 @@ var PrefetchedRequests = class {
|
|
|
1097
1098
|
response: promise,
|
|
1098
1099
|
singleUse: expires === 0,
|
|
1099
1100
|
timestamp: Date.now(),
|
|
1100
|
-
inFlight: false
|
|
1101
|
+
inFlight: false,
|
|
1102
|
+
tags: Array.isArray(cacheTags) ? cacheTags : [cacheTags]
|
|
1101
1103
|
});
|
|
1102
1104
|
this.scheduleForRemoval(params, expires);
|
|
1103
1105
|
this.removeFromInFlight(params);
|
|
@@ -1119,6 +1121,11 @@ var PrefetchedRequests = class {
|
|
|
1119
1121
|
});
|
|
1120
1122
|
this.removalTimers = [];
|
|
1121
1123
|
}
|
|
1124
|
+
removeByTags(tags) {
|
|
1125
|
+
this.cached = this.cached.filter((prefetched) => {
|
|
1126
|
+
return !prefetched.tags.some((tag) => tags.includes(tag));
|
|
1127
|
+
});
|
|
1128
|
+
}
|
|
1122
1129
|
remove(params) {
|
|
1123
1130
|
this.cached = this.cached.filter((prefetched) => {
|
|
1124
1131
|
return !this.paramsAreEqual(prefetched.params, params);
|
|
@@ -1204,7 +1211,7 @@ var PrefetchedRequests = class {
|
|
|
1204
1211
|
}) || null;
|
|
1205
1212
|
}
|
|
1206
1213
|
withoutPurposePrefetchHeader(params) {
|
|
1207
|
-
const newParams = (0,
|
|
1214
|
+
const newParams = (0, import_lodash_es.cloneDeep)(params);
|
|
1208
1215
|
if (newParams.headers["Purpose"] === "prefetch") {
|
|
1209
1216
|
delete newParams.headers["Purpose"];
|
|
1210
1217
|
}
|
|
@@ -1468,6 +1475,7 @@ var Response = class _Response {
|
|
|
1468
1475
|
fireErrorEvent(scopedErrors);
|
|
1469
1476
|
return this.requestParams.all().onError(scopedErrors);
|
|
1470
1477
|
}
|
|
1478
|
+
router.flushByCacheTags(this.requestParams.all().invalidateCacheTags || []);
|
|
1471
1479
|
fireSuccessEvent(page.get());
|
|
1472
1480
|
await this.requestParams.all().onSuccess(page.get());
|
|
1473
1481
|
history.preserveUrl = false;
|
|
@@ -1910,11 +1918,15 @@ var Router = class {
|
|
|
1910
1918
|
flushAll() {
|
|
1911
1919
|
prefetchedRequests.removeAll();
|
|
1912
1920
|
}
|
|
1921
|
+
flushByCacheTags(tags) {
|
|
1922
|
+
prefetchedRequests.removeByTags(Array.isArray(tags) ? tags : [tags]);
|
|
1923
|
+
}
|
|
1913
1924
|
getPrefetching(href, options = {}) {
|
|
1914
1925
|
return prefetchedRequests.findInFlight(this.getPrefetchParams(href, options));
|
|
1915
1926
|
}
|
|
1916
|
-
prefetch(href, options = {},
|
|
1917
|
-
|
|
1927
|
+
prefetch(href, options = {}, prefetchOptions = {}) {
|
|
1928
|
+
const method = options.method ?? (isUrlMethodPair(href) ? href.method : "get");
|
|
1929
|
+
if (method !== "get") {
|
|
1918
1930
|
throw new Error("Prefetch requests must use the GET method");
|
|
1919
1931
|
}
|
|
1920
1932
|
const visit = this.getPendingVisit(href, {
|
|
@@ -1956,7 +1968,11 @@ var Router = class {
|
|
|
1956
1968
|
(params) => {
|
|
1957
1969
|
this.asyncRequestStream.send(Request.create(params, page.get()));
|
|
1958
1970
|
},
|
|
1959
|
-
{
|
|
1971
|
+
{
|
|
1972
|
+
cacheFor: 3e4,
|
|
1973
|
+
cacheTags: [],
|
|
1974
|
+
...prefetchOptions
|
|
1975
|
+
}
|
|
1960
1976
|
);
|
|
1961
1977
|
});
|
|
1962
1978
|
}
|
|
@@ -2011,6 +2027,11 @@ var Router = class {
|
|
|
2011
2027
|
};
|
|
2012
2028
|
}
|
|
2013
2029
|
getPendingVisit(href, options, pendingVisitOptions = {}) {
|
|
2030
|
+
if (isUrlMethodPair(href)) {
|
|
2031
|
+
const urlMethodPair = href;
|
|
2032
|
+
href = urlMethodPair.url;
|
|
2033
|
+
options.method = options.method ?? urlMethodPair.method;
|
|
2034
|
+
}
|
|
2014
2035
|
const mergedOptions = {
|
|
2015
2036
|
method: "get",
|
|
2016
2037
|
data: {},
|
|
@@ -2029,6 +2050,7 @@ var Router = class {
|
|
|
2029
2050
|
reset: [],
|
|
2030
2051
|
preserveUrl: false,
|
|
2031
2052
|
prefetch: false,
|
|
2053
|
+
invalidateCacheTags: [],
|
|
2032
2054
|
...options
|
|
2033
2055
|
};
|
|
2034
2056
|
const [url, _data] = transformUrlAndData(
|
|
@@ -2087,7 +2109,7 @@ var Router = class {
|
|
|
2087
2109
|
};
|
|
2088
2110
|
|
|
2089
2111
|
// src/formObject.ts
|
|
2090
|
-
var
|
|
2112
|
+
var import_lodash_es2 = require("lodash-es");
|
|
2091
2113
|
function undotKey(key) {
|
|
2092
2114
|
if (!key.includes(".")) {
|
|
2093
2115
|
return key;
|
|
@@ -2122,150 +2144,19 @@ function formDataToObject(source) {
|
|
|
2122
2144
|
const path = parseKey(undotKey(key));
|
|
2123
2145
|
if (path[path.length - 1] === "") {
|
|
2124
2146
|
const arrayPath = path.slice(0, -1);
|
|
2125
|
-
const existing = (0,
|
|
2147
|
+
const existing = (0, import_lodash_es2.get)(form, arrayPath);
|
|
2126
2148
|
if (Array.isArray(existing)) {
|
|
2127
2149
|
existing.push(value);
|
|
2128
2150
|
} else {
|
|
2129
|
-
(0,
|
|
2151
|
+
(0, import_lodash_es2.set)(form, arrayPath, [value]);
|
|
2130
2152
|
}
|
|
2131
2153
|
continue;
|
|
2132
2154
|
}
|
|
2133
|
-
(0,
|
|
2155
|
+
(0, import_lodash_es2.set)(form, path, value);
|
|
2134
2156
|
}
|
|
2135
2157
|
return form;
|
|
2136
2158
|
}
|
|
2137
2159
|
|
|
2138
|
-
// src/resetFormFields.ts
|
|
2139
|
-
function isFormElement(element) {
|
|
2140
|
-
return element instanceof HTMLInputElement || element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement;
|
|
2141
|
-
}
|
|
2142
|
-
function resetInputElement(input, defaultValues) {
|
|
2143
|
-
const oldValue = input.value;
|
|
2144
|
-
const oldChecked = input.checked;
|
|
2145
|
-
switch (input.type.toLowerCase()) {
|
|
2146
|
-
case "checkbox":
|
|
2147
|
-
input.checked = defaultValues.includes(input.value);
|
|
2148
|
-
break;
|
|
2149
|
-
case "radio":
|
|
2150
|
-
input.checked = defaultValues[0] === input.value;
|
|
2151
|
-
break;
|
|
2152
|
-
case "file":
|
|
2153
|
-
input.value = "";
|
|
2154
|
-
break;
|
|
2155
|
-
case "button":
|
|
2156
|
-
case "submit":
|
|
2157
|
-
case "reset":
|
|
2158
|
-
case "image":
|
|
2159
|
-
break;
|
|
2160
|
-
default:
|
|
2161
|
-
input.value = defaultValues[0] !== null && defaultValues[0] !== void 0 ? String(defaultValues[0]) : "";
|
|
2162
|
-
}
|
|
2163
|
-
return input.value !== oldValue || input.checked !== oldChecked;
|
|
2164
|
-
}
|
|
2165
|
-
function resetSelectElement(select, defaultValues) {
|
|
2166
|
-
const oldValue = select.value;
|
|
2167
|
-
const oldSelectedOptions = Array.from(select.selectedOptions).map((opt) => opt.value);
|
|
2168
|
-
if (select.multiple) {
|
|
2169
|
-
const defaultStrings = defaultValues.map((value) => String(value));
|
|
2170
|
-
Array.from(select.options).forEach((option) => {
|
|
2171
|
-
option.selected = defaultStrings.includes(option.value);
|
|
2172
|
-
});
|
|
2173
|
-
} else {
|
|
2174
|
-
select.value = defaultValues[0] !== void 0 ? String(defaultValues[0]) : "";
|
|
2175
|
-
}
|
|
2176
|
-
const newSelectedOptions = Array.from(select.selectedOptions).map((opt) => opt.value);
|
|
2177
|
-
const hasChanged = select.multiple ? JSON.stringify(oldSelectedOptions.sort()) !== JSON.stringify(newSelectedOptions.sort()) : select.value !== oldValue;
|
|
2178
|
-
return hasChanged;
|
|
2179
|
-
}
|
|
2180
|
-
function resetFormElement(element, defaultValues) {
|
|
2181
|
-
if (element.disabled) {
|
|
2182
|
-
if (element instanceof HTMLInputElement) {
|
|
2183
|
-
const oldValue = element.value;
|
|
2184
|
-
const oldChecked = element.checked;
|
|
2185
|
-
switch (element.type.toLowerCase()) {
|
|
2186
|
-
case "checkbox":
|
|
2187
|
-
case "radio":
|
|
2188
|
-
element.checked = element.defaultChecked;
|
|
2189
|
-
return element.checked !== oldChecked;
|
|
2190
|
-
case "file":
|
|
2191
|
-
element.value = "";
|
|
2192
|
-
return oldValue !== "";
|
|
2193
|
-
case "button":
|
|
2194
|
-
case "submit":
|
|
2195
|
-
case "reset":
|
|
2196
|
-
case "image":
|
|
2197
|
-
return false;
|
|
2198
|
-
default:
|
|
2199
|
-
element.value = element.defaultValue;
|
|
2200
|
-
return element.value !== oldValue;
|
|
2201
|
-
}
|
|
2202
|
-
} else if (element instanceof HTMLSelectElement) {
|
|
2203
|
-
const oldSelectedOptions = Array.from(element.selectedOptions).map((opt) => opt.value);
|
|
2204
|
-
Array.from(element.options).forEach((option) => {
|
|
2205
|
-
option.selected = option.defaultSelected;
|
|
2206
|
-
});
|
|
2207
|
-
const newSelectedOptions = Array.from(element.selectedOptions).map((opt) => opt.value);
|
|
2208
|
-
return JSON.stringify(oldSelectedOptions.sort()) !== JSON.stringify(newSelectedOptions.sort());
|
|
2209
|
-
} else if (element instanceof HTMLTextAreaElement) {
|
|
2210
|
-
const oldValue = element.value;
|
|
2211
|
-
element.value = element.defaultValue;
|
|
2212
|
-
return element.value !== oldValue;
|
|
2213
|
-
}
|
|
2214
|
-
return false;
|
|
2215
|
-
}
|
|
2216
|
-
if (element instanceof HTMLInputElement) {
|
|
2217
|
-
return resetInputElement(element, defaultValues);
|
|
2218
|
-
} else if (element instanceof HTMLSelectElement) {
|
|
2219
|
-
return resetSelectElement(element, defaultValues);
|
|
2220
|
-
} else if (element instanceof HTMLTextAreaElement) {
|
|
2221
|
-
const oldValue = element.value;
|
|
2222
|
-
element.value = defaultValues[0] !== void 0 ? String(defaultValues[0]) : "";
|
|
2223
|
-
return element.value !== oldValue;
|
|
2224
|
-
}
|
|
2225
|
-
return false;
|
|
2226
|
-
}
|
|
2227
|
-
function resetFieldElements(elements, defaultValues) {
|
|
2228
|
-
let hasChanged = false;
|
|
2229
|
-
if (elements instanceof RadioNodeList || elements instanceof HTMLCollection) {
|
|
2230
|
-
Array.from(elements).forEach((node, index) => {
|
|
2231
|
-
if (node instanceof Element && isFormElement(node)) {
|
|
2232
|
-
if (node instanceof HTMLInputElement && ["checkbox", "radio"].includes(node.type.toLowerCase())) {
|
|
2233
|
-
if (resetFormElement(node, defaultValues)) {
|
|
2234
|
-
hasChanged = true;
|
|
2235
|
-
}
|
|
2236
|
-
} else {
|
|
2237
|
-
const indexedDefaultValues = defaultValues[index] !== void 0 ? [defaultValues[index]] : [defaultValues[0] ?? null].filter(Boolean);
|
|
2238
|
-
if (resetFormElement(node, indexedDefaultValues)) {
|
|
2239
|
-
hasChanged = true;
|
|
2240
|
-
}
|
|
2241
|
-
}
|
|
2242
|
-
}
|
|
2243
|
-
});
|
|
2244
|
-
} else if (isFormElement(elements)) {
|
|
2245
|
-
hasChanged = resetFormElement(elements, defaultValues);
|
|
2246
|
-
}
|
|
2247
|
-
return hasChanged;
|
|
2248
|
-
}
|
|
2249
|
-
function resetFormFields(formElement, defaults, fieldNames) {
|
|
2250
|
-
if (!fieldNames || fieldNames.length === 0) {
|
|
2251
|
-
const formData = new FormData(formElement);
|
|
2252
|
-
const formElementNames = Array.from(formElement.elements).map((el) => isFormElement(el) ? el.name : "").filter(Boolean);
|
|
2253
|
-
fieldNames = [.../* @__PURE__ */ new Set([...defaults.keys(), ...formData.keys(), ...formElementNames])];
|
|
2254
|
-
}
|
|
2255
|
-
let hasChanged = false;
|
|
2256
|
-
fieldNames.forEach((fieldName) => {
|
|
2257
|
-
const elements = formElement.elements.namedItem(fieldName);
|
|
2258
|
-
if (elements) {
|
|
2259
|
-
if (resetFieldElements(elements, defaults.getAll(fieldName))) {
|
|
2260
|
-
hasChanged = true;
|
|
2261
|
-
}
|
|
2262
|
-
}
|
|
2263
|
-
});
|
|
2264
|
-
if (hasChanged) {
|
|
2265
|
-
formElement.dispatchEvent(new Event("reset", { bubbles: true }));
|
|
2266
|
-
}
|
|
2267
|
-
}
|
|
2268
|
-
|
|
2269
2160
|
// src/head.ts
|
|
2270
2161
|
var Renderer = {
|
|
2271
2162
|
buildDOMElement(tag) {
|
|
@@ -2378,6 +2269,16 @@ function createHeadManager(isServer2, titleCallback, onUpdate) {
|
|
|
2378
2269
|
};
|
|
2379
2270
|
}
|
|
2380
2271
|
|
|
2272
|
+
// src/navigationEvents.ts
|
|
2273
|
+
function shouldIntercept(event) {
|
|
2274
|
+
const isLink = event.currentTarget.tagName.toLowerCase() === "a";
|
|
2275
|
+
return !(event.target && (event?.target).isContentEditable || event.defaultPrevented || isLink && event.altKey || isLink && event.ctrlKey || isLink && event.metaKey || isLink && event.shiftKey || isLink && "button" in event && event.button !== 0);
|
|
2276
|
+
}
|
|
2277
|
+
function shouldNavigate(event) {
|
|
2278
|
+
const isButton = event.currentTarget.tagName.toLowerCase() === "button";
|
|
2279
|
+
return event.key === "Enter" || isButton && event.key === " ";
|
|
2280
|
+
}
|
|
2281
|
+
|
|
2381
2282
|
// src/progress-component.ts
|
|
2382
2283
|
var baseComponentSelector = "nprogress";
|
|
2383
2284
|
var progress;
|
|
@@ -2712,10 +2613,138 @@ function setupProgress({
|
|
|
2712
2613
|
progress_component_default.configure({ showSpinner, includeCSS, color });
|
|
2713
2614
|
}
|
|
2714
2615
|
|
|
2715
|
-
// src/
|
|
2716
|
-
function
|
|
2717
|
-
|
|
2718
|
-
|
|
2616
|
+
// src/resetFormFields.ts
|
|
2617
|
+
function isFormElement(element) {
|
|
2618
|
+
return element instanceof HTMLInputElement || element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement;
|
|
2619
|
+
}
|
|
2620
|
+
function resetInputElement(input, defaultValues) {
|
|
2621
|
+
const oldValue = input.value;
|
|
2622
|
+
const oldChecked = input.checked;
|
|
2623
|
+
switch (input.type.toLowerCase()) {
|
|
2624
|
+
case "checkbox":
|
|
2625
|
+
input.checked = defaultValues.includes(input.value);
|
|
2626
|
+
break;
|
|
2627
|
+
case "radio":
|
|
2628
|
+
input.checked = defaultValues[0] === input.value;
|
|
2629
|
+
break;
|
|
2630
|
+
case "file":
|
|
2631
|
+
input.value = "";
|
|
2632
|
+
break;
|
|
2633
|
+
case "button":
|
|
2634
|
+
case "submit":
|
|
2635
|
+
case "reset":
|
|
2636
|
+
case "image":
|
|
2637
|
+
break;
|
|
2638
|
+
default:
|
|
2639
|
+
input.value = defaultValues[0] !== null && defaultValues[0] !== void 0 ? String(defaultValues[0]) : "";
|
|
2640
|
+
}
|
|
2641
|
+
return input.value !== oldValue || input.checked !== oldChecked;
|
|
2642
|
+
}
|
|
2643
|
+
function resetSelectElement(select, defaultValues) {
|
|
2644
|
+
const oldValue = select.value;
|
|
2645
|
+
const oldSelectedOptions = Array.from(select.selectedOptions).map((opt) => opt.value);
|
|
2646
|
+
if (select.multiple) {
|
|
2647
|
+
const defaultStrings = defaultValues.map((value) => String(value));
|
|
2648
|
+
Array.from(select.options).forEach((option) => {
|
|
2649
|
+
option.selected = defaultStrings.includes(option.value);
|
|
2650
|
+
});
|
|
2651
|
+
} else {
|
|
2652
|
+
select.value = defaultValues[0] !== void 0 ? String(defaultValues[0]) : "";
|
|
2653
|
+
}
|
|
2654
|
+
const newSelectedOptions = Array.from(select.selectedOptions).map((opt) => opt.value);
|
|
2655
|
+
const hasChanged = select.multiple ? JSON.stringify(oldSelectedOptions.sort()) !== JSON.stringify(newSelectedOptions.sort()) : select.value !== oldValue;
|
|
2656
|
+
return hasChanged;
|
|
2657
|
+
}
|
|
2658
|
+
function resetFormElement(element, defaultValues) {
|
|
2659
|
+
if (element.disabled) {
|
|
2660
|
+
if (element instanceof HTMLInputElement) {
|
|
2661
|
+
const oldValue = element.value;
|
|
2662
|
+
const oldChecked = element.checked;
|
|
2663
|
+
switch (element.type.toLowerCase()) {
|
|
2664
|
+
case "checkbox":
|
|
2665
|
+
case "radio":
|
|
2666
|
+
element.checked = element.defaultChecked;
|
|
2667
|
+
return element.checked !== oldChecked;
|
|
2668
|
+
case "file":
|
|
2669
|
+
element.value = "";
|
|
2670
|
+
return oldValue !== "";
|
|
2671
|
+
case "button":
|
|
2672
|
+
case "submit":
|
|
2673
|
+
case "reset":
|
|
2674
|
+
case "image":
|
|
2675
|
+
return false;
|
|
2676
|
+
default:
|
|
2677
|
+
element.value = element.defaultValue;
|
|
2678
|
+
return element.value !== oldValue;
|
|
2679
|
+
}
|
|
2680
|
+
} else if (element instanceof HTMLSelectElement) {
|
|
2681
|
+
const oldSelectedOptions = Array.from(element.selectedOptions).map((opt) => opt.value);
|
|
2682
|
+
Array.from(element.options).forEach((option) => {
|
|
2683
|
+
option.selected = option.defaultSelected;
|
|
2684
|
+
});
|
|
2685
|
+
const newSelectedOptions = Array.from(element.selectedOptions).map((opt) => opt.value);
|
|
2686
|
+
return JSON.stringify(oldSelectedOptions.sort()) !== JSON.stringify(newSelectedOptions.sort());
|
|
2687
|
+
} else if (element instanceof HTMLTextAreaElement) {
|
|
2688
|
+
const oldValue = element.value;
|
|
2689
|
+
element.value = element.defaultValue;
|
|
2690
|
+
return element.value !== oldValue;
|
|
2691
|
+
}
|
|
2692
|
+
return false;
|
|
2693
|
+
}
|
|
2694
|
+
if (element instanceof HTMLInputElement) {
|
|
2695
|
+
return resetInputElement(element, defaultValues);
|
|
2696
|
+
} else if (element instanceof HTMLSelectElement) {
|
|
2697
|
+
return resetSelectElement(element, defaultValues);
|
|
2698
|
+
} else if (element instanceof HTMLTextAreaElement) {
|
|
2699
|
+
const oldValue = element.value;
|
|
2700
|
+
element.value = defaultValues[0] !== void 0 ? String(defaultValues[0]) : "";
|
|
2701
|
+
return element.value !== oldValue;
|
|
2702
|
+
}
|
|
2703
|
+
return false;
|
|
2704
|
+
}
|
|
2705
|
+
function resetFieldElements(elements, defaultValues) {
|
|
2706
|
+
let hasChanged = false;
|
|
2707
|
+
if (elements instanceof RadioNodeList || elements instanceof HTMLCollection) {
|
|
2708
|
+
Array.from(elements).forEach((node, index) => {
|
|
2709
|
+
if (node instanceof Element && isFormElement(node)) {
|
|
2710
|
+
if (node instanceof HTMLInputElement && ["checkbox", "radio"].includes(node.type.toLowerCase())) {
|
|
2711
|
+
if (resetFormElement(node, defaultValues)) {
|
|
2712
|
+
hasChanged = true;
|
|
2713
|
+
}
|
|
2714
|
+
} else {
|
|
2715
|
+
const indexedDefaultValues = defaultValues[index] !== void 0 ? [defaultValues[index]] : [defaultValues[0] ?? null].filter(Boolean);
|
|
2716
|
+
if (resetFormElement(node, indexedDefaultValues)) {
|
|
2717
|
+
hasChanged = true;
|
|
2718
|
+
}
|
|
2719
|
+
}
|
|
2720
|
+
}
|
|
2721
|
+
});
|
|
2722
|
+
} else if (isFormElement(elements)) {
|
|
2723
|
+
hasChanged = resetFormElement(elements, defaultValues);
|
|
2724
|
+
}
|
|
2725
|
+
return hasChanged;
|
|
2726
|
+
}
|
|
2727
|
+
function resetFormFields(formElement, defaults, fieldNames) {
|
|
2728
|
+
if (!formElement) {
|
|
2729
|
+
return;
|
|
2730
|
+
}
|
|
2731
|
+
if (!fieldNames || fieldNames.length === 0) {
|
|
2732
|
+
const formData = new FormData(formElement);
|
|
2733
|
+
const formElementNames = Array.from(formElement.elements).map((el) => isFormElement(el) ? el.name : "").filter(Boolean);
|
|
2734
|
+
fieldNames = [.../* @__PURE__ */ new Set([...defaults.keys(), ...formData.keys(), ...formElementNames])];
|
|
2735
|
+
}
|
|
2736
|
+
let hasChanged = false;
|
|
2737
|
+
fieldNames.forEach((fieldName) => {
|
|
2738
|
+
const elements = formElement.elements.namedItem(fieldName);
|
|
2739
|
+
if (elements) {
|
|
2740
|
+
if (resetFieldElements(elements, defaults.getAll(fieldName))) {
|
|
2741
|
+
hasChanged = true;
|
|
2742
|
+
}
|
|
2743
|
+
}
|
|
2744
|
+
});
|
|
2745
|
+
if (hasChanged) {
|
|
2746
|
+
formElement.dispatchEvent(new Event("reset", { bubbles: true }));
|
|
2747
|
+
}
|
|
2719
2748
|
}
|
|
2720
2749
|
|
|
2721
2750
|
// src/index.ts
|