@samline/forms 2.3.2 → 2.3.4

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 CHANGED
@@ -42,10 +42,10 @@ Requires Node 20+ when bundling. Runtime target is ES2020.
42
42
  Use the browser build when you do not have a bundler and need to run the package directly in HTML, Shopify, WordPress, or any traditional template.
43
43
 
44
44
  ```html
45
- <script src="https://unpkg.com/@samline/forms@2.3.2/dist/browser/global.global.js"></script>
45
+ <script src="https://unpkg.com/@samline/forms@2.3.4/dist/browser/global.global.js"></script>
46
46
  ```
47
47
 
48
- > Pin the version in production. Replace `2.3.2` with the version you ship.
48
+ > Pin the version in production. Replace `2.3.4` with the version you ship.
49
49
 
50
50
  The browser bundle exposes a single global: `window.Forms`.
51
51
 
@@ -55,7 +55,7 @@ The browser bundle exposes a single global: `window.Forms`.
55
55
  <button type="submit">Send</button>
56
56
  </form>
57
57
 
58
- <script src="https://unpkg.com/@samline/forms@2.3.2/dist/browser/global.global.js"></script>
58
+ <script src="https://unpkg.com/@samline/forms@2.3.4/dist/browser/global.global.js"></script>
59
59
  <script>
60
60
  const contactForm = window.Forms.newForm({ id: 'contact-form' })
61
61
 
@@ -5131,6 +5131,7 @@ var Forms = (() => {
5131
5131
 
5132
5132
  // src/core/dom.ts
5133
5133
  var isFormFieldElement = (value) => value instanceof HTMLInputElement || value instanceof HTMLSelectElement || value instanceof HTMLTextAreaElement;
5134
+ var isArraySyntax = (field) => typeof field.name === "string" && field.name.endsWith("[]");
5134
5135
  var resolveFormElement = (target) => {
5135
5136
  if (!target) return null;
5136
5137
  if (target instanceof HTMLFormElement) return target;
@@ -5167,6 +5168,30 @@ var Forms = (() => {
5167
5168
  if (fields.length === 0) return void 0;
5168
5169
  const first = fields[0];
5169
5170
  if (!first) return void 0;
5171
+ if (isArraySyntax(first) && fields.length > 1) {
5172
+ if (first instanceof HTMLSelectElement || first instanceof HTMLTextAreaElement) {
5173
+ return fields.map((f2) => f2.value);
5174
+ }
5175
+ if (first instanceof HTMLInputElement) {
5176
+ if (first.type === "radio") {
5177
+ const checked = fields.find(
5178
+ (f2) => f2 instanceof HTMLInputElement && f2.checked
5179
+ );
5180
+ return checked?.value ?? "";
5181
+ }
5182
+ if (first.type === "checkbox") {
5183
+ return fields.filter(
5184
+ (f2) => f2 instanceof HTMLInputElement && f2.checked
5185
+ ).map((f2) => f2.value);
5186
+ }
5187
+ if (first.type === "file") {
5188
+ return fields.flatMap(
5189
+ (f2) => f2 instanceof HTMLInputElement && f2.files ? Array.from(f2.files) : []
5190
+ );
5191
+ }
5192
+ }
5193
+ return fields.map((f2) => f2.value);
5194
+ }
5170
5195
  if (first instanceof HTMLSelectElement || first instanceof HTMLTextAreaElement) {
5171
5196
  return first.value;
5172
5197
  }
@@ -5192,7 +5217,10 @@ var Forms = (() => {
5192
5217
  var writeFieldValue = (fields, value) => {
5193
5218
  const normalizedArray = Array.isArray(value) ? value.map((item) => String(item)) : null;
5194
5219
  const normalizedValue = value === void 0 || value === null ? "" : String(value);
5195
- for (const field of fields) {
5220
+ const distributeArray = fields.length > 0 && fields[0] !== void 0 && isArraySyntax(fields[0]) && normalizedArray !== null;
5221
+ for (let index = 0; index < fields.length; index++) {
5222
+ const field = fields[index];
5223
+ if (!field) continue;
5196
5224
  if (field instanceof HTMLInputElement) {
5197
5225
  if (field.type === "checkbox") {
5198
5226
  field.checked = normalizedArray ? normalizedArray.includes(field.value) : normalizedValue === field.value;
@@ -5206,9 +5234,17 @@ var Forms = (() => {
5206
5234
  if (Array.isArray(value) && value.length === 0) field.value = "";
5207
5235
  continue;
5208
5236
  }
5237
+ if (distributeArray && normalizedArray) {
5238
+ field.value = normalizedArray[index] ?? "";
5239
+ continue;
5240
+ }
5209
5241
  field.value = normalizedValue;
5210
5242
  continue;
5211
5243
  }
5244
+ if (distributeArray && normalizedArray) {
5245
+ field.value = normalizedArray[index] ?? "";
5246
+ continue;
5247
+ }
5212
5248
  field.value = normalizedValue;
5213
5249
  }
5214
5250
  };
@@ -5497,11 +5533,13 @@ var Forms = (() => {
5497
5533
  const getMergedErrors = () => mergeErrors(state2.validationErrors, state2.manualErrors);
5498
5534
  const syncVisualState = (names) => {
5499
5535
  if (!state2.element) return;
5500
- const targetNames = names ?? getTrackedFieldNames();
5536
+ const sourceNames = names ?? getTrackedFieldNames();
5501
5537
  const errors = getMergedErrors();
5502
- for (const name of targetNames) {
5503
- const fields = getFieldsByName(name);
5504
- const hasError = Boolean(errors[name]?.length);
5538
+ for (const name of sourceNames) {
5539
+ const displayName = resolveDisplayNameForName(state2, name);
5540
+ const canonical = resolveCanonicalForName(state2, name) ?? name;
5541
+ const fields = getFieldsByName(displayName);
5542
+ const hasError = Boolean(errors[canonical]?.length);
5505
5543
  for (const field of fields) {
5506
5544
  applyBooleanAttribute(field, attributes.filled, isFieldFilled(field));
5507
5545
  applyBooleanAttribute(field, attributes.error, hasError);