react-f0rm 0.4.1 → 0.5.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/dist/index.umd.js CHANGED
@@ -166,6 +166,31 @@
166
166
  function isPromise(value) {
167
167
  return value && typeof value.then === "function";
168
168
  }
169
+ function isEqual(a, b) {
170
+ if (Object.is(a, b)) return true;
171
+ if (a instanceof Date && b instanceof Date)
172
+ return a.getTime() === b.getTime();
173
+ if (!a || !b || typeof a !== "object" || typeof b !== "object") return false;
174
+ const isArray = Array.isArray(a);
175
+ if (isArray !== Array.isArray(b)) return false;
176
+ if (isArray) {
177
+ if (a.length !== b.length) return false;
178
+ for (let i = 0; i < a.length; i++) {
179
+ if (!isEqual(a[i], b[i])) return false;
180
+ }
181
+ return true;
182
+ }
183
+ const proto = Object.getPrototypeOf(a);
184
+ if (proto !== Object.prototype && proto !== null) return false;
185
+ if (Object.getPrototypeOf(b) !== proto) return false;
186
+ const keysA = Object.keys(a);
187
+ const keysB = Object.keys(b);
188
+ if (keysA.length !== keysB.length) return false;
189
+ for (const key of keysA) {
190
+ if (!isEqual(a[key], b[key])) return false;
191
+ }
192
+ return true;
193
+ }
169
194
  function waitUntil(emitter, event, isResolve, isReject) {
170
195
  return new Promise((resolve, reject) => {
171
196
  if (isReject()) return void reject();
@@ -323,6 +348,19 @@
323
348
  for (const { key } of paths) errors.delete(key);
324
349
  for (const path of paths) emit(emitter, "errors", path);
325
350
  }
351
+ function setServerErrors(form, errors, options) {
352
+ if (!options?.keepExisting) clearErrors(form);
353
+ for (const [name, error] of Object.entries(errors)) {
354
+ setError(
355
+ form,
356
+ name,
357
+ (Array.isArray(error) ? error : [error]).map((message) => ({
358
+ type: "server",
359
+ message
360
+ }))
361
+ );
362
+ }
363
+ }
326
364
  function setTouched(form, name) {
327
365
  setTouchedByPath(form, create$1(name));
328
366
  }
@@ -423,7 +461,9 @@
423
461
  }
424
462
  }
425
463
  function setInitialValues(form, initialValues) {
426
- if (form.initialValues === initialValues) return;
464
+ if (form.initialValues === initialValues || isEqual(form.initialValues, initialValues)) {
465
+ return;
466
+ }
427
467
  form.initialValues = initialValues;
428
468
  form.parsedValues = void 0;
429
469
  form.values.clear();
@@ -1093,31 +1133,6 @@
1093
1133
  function useSubmitCount(form) {
1094
1134
  return useWatch(form.emitter, "submitCount", () => form.submitCount);
1095
1135
  }
1096
- function isEqual(a, b) {
1097
- if (Object.is(a, b)) return true;
1098
- if (a instanceof Date && b instanceof Date)
1099
- return a.getTime() === b.getTime();
1100
- if (!a || !b || typeof a !== "object" || typeof b !== "object") return false;
1101
- const isArray = Array.isArray(a);
1102
- if (isArray !== Array.isArray(b)) return false;
1103
- if (isArray) {
1104
- if (a.length !== b.length) return false;
1105
- for (let i = 0; i < a.length; i++) {
1106
- if (!isEqual(a[i], b[i])) return false;
1107
- }
1108
- return true;
1109
- }
1110
- const proto = Object.getPrototypeOf(a);
1111
- if (proto !== Object.prototype && proto !== null) return false;
1112
- if (Object.getPrototypeOf(b) !== proto) return false;
1113
- const keysA = Object.keys(a);
1114
- const keysB = Object.keys(b);
1115
- if (keysA.length !== keysB.length) return false;
1116
- for (const key of keysA) {
1117
- if (!isEqual(a[key], b[key])) return false;
1118
- }
1119
- return true;
1120
- }
1121
1136
 
1122
1137
  function usePath(name) {
1123
1138
  const path = React.useMemo(() => create$1(normalizePath(name)), [name]);
@@ -1500,6 +1515,9 @@
1500
1515
  const id = key.replace(/["'[\],\s]+/g, "-").replace(/^-+|-+$/g, "");
1501
1516
  return id || "field";
1502
1517
  }
1518
+ function fieldErrorId(name) {
1519
+ return errorIdFromKey(create$1(name).key);
1520
+ }
1503
1521
  const Field = React__namespace.forwardRef(
1504
1522
  ({
1505
1523
  validate,
@@ -1588,16 +1606,17 @@
1588
1606
  );
1589
1607
  const toValue = eventToValue ?? ((e) => e.target.value);
1590
1608
  const errorId = errorIdFromKey(fieldKey);
1609
+ const describedBy = error ? [props["aria-describedby"], errorId].filter(Boolean).join(" ") : props["aria-describedby"];
1591
1610
  return /* @__PURE__ */ React__namespace.createElement(React__namespace.Fragment, null, /* @__PURE__ */ React__namespace.createElement(
1592
1611
  Component,
1593
1612
  {
1594
- "aria-invalid": error ? true : void 0,
1595
- "aria-describedby": error && renderError ? errorId : void 0,
1596
1613
  ...props,
1597
1614
  name: fieldKey,
1598
1615
  onBlur,
1599
1616
  ...asProps,
1600
1617
  ...valueToProps ? valueToProps(value) : { value },
1618
+ "aria-invalid": error ? true : props["aria-invalid"],
1619
+ "aria-describedby": describedBy,
1601
1620
  disabled: isDisabled,
1602
1621
  onChange: (e) => onChange(toValue(e)),
1603
1622
  ref: mergedRef
@@ -1639,12 +1658,13 @@
1639
1658
  return /* @__PURE__ */ React__namespace.createElement(
1640
1659
  "input",
1641
1660
  {
1642
- "aria-invalid": error ? true : void 0,
1643
1661
  ...props,
1644
1662
  name: fieldKey,
1645
1663
  onBlur,
1646
1664
  type: "checkbox",
1647
1665
  checked: !!value,
1666
+ "aria-invalid": error ? true : props["aria-invalid"],
1667
+ "aria-describedby": error ? [props["aria-describedby"], errorIdFromKey(fieldKey)].filter(Boolean).join(" ") : props["aria-describedby"],
1648
1668
  disabled: isDisabled,
1649
1669
  onChange: (e) => onChange(e.target.checked),
1650
1670
  ref
@@ -1692,12 +1712,13 @@
1692
1712
  return /* @__PURE__ */ React__namespace.createElement(
1693
1713
  "select",
1694
1714
  {
1695
- "aria-invalid": error ? true : void 0,
1696
1715
  ...props,
1697
1716
  name: fieldKey,
1698
1717
  onBlur,
1699
1718
  multiple,
1700
1719
  value: toSelectValue(multiple, value),
1720
+ "aria-invalid": error ? true : props["aria-invalid"],
1721
+ "aria-describedby": error ? [props["aria-describedby"], errorIdFromKey(fieldKey)].filter(Boolean).join(" ") : props["aria-describedby"],
1701
1722
  disabled: isDisabled,
1702
1723
  onChange: (e) => onChange(
1703
1724
  multiple ? Array.from(e.target.selectedOptions, (option) => option.value) : e.target.value
@@ -1722,6 +1743,7 @@
1722
1743
  exports.createForm = create;
1723
1744
  exports.createFormContext = createFormContext;
1724
1745
  exports.ensureValidate = ensureValidate;
1746
+ exports.fieldErrorId = fieldErrorId;
1725
1747
  exports.getDirtyFields = getDirtyFields;
1726
1748
  exports.getError = getError;
1727
1749
  exports.getErrorByPath = getErrorByPath;
@@ -1751,6 +1773,7 @@
1751
1773
  exports.setFocus = setFocus;
1752
1774
  exports.setInitialValues = setInitialValues;
1753
1775
  exports.setIsSubmitting = setIsSubmitting;
1776
+ exports.setServerErrors = setServerErrors;
1754
1777
  exports.setSubmitSuccessful = setSubmitSuccessful;
1755
1778
  exports.setTouched = setTouched;
1756
1779
  exports.setTouchedByPath = setTouchedByPath;