@samline/forms 2.3.3 → 2.4.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.js CHANGED
@@ -231,6 +231,11 @@ var readInputType = (event) => {
231
231
  const candidate = event;
232
232
  return typeof candidate.inputType === "string" ? candidate.inputType : void 0;
233
233
  };
234
+ var buildAutoInterpretedOptions = (formatOptions) => ({
235
+ ...formatOptions ?? {},
236
+ interpretInputAs: "auto"
237
+ });
238
+ var shouldForceAutoInterpretation = (formatOptions) => formatOptions?.interpretInputAs === void 0;
234
239
  var buildHandler = (visible, mirror, formatFn, formatType, formatOptions) => {
235
240
  const handler = (event) => {
236
241
  if (!visible.isConnected || !mirror.isConnected) return;
@@ -239,7 +244,8 @@ var buildHandler = (visible, mirror, formatFn, formatType, formatOptions) => {
239
244
  if (!isVisible && !isMirror) return;
240
245
  const rawInput = isMirror ? mirror.value : visible.value;
241
246
  const inputType = readInputType(event);
242
- const { formatted, raw } = formatFn(rawInput, formatType, formatOptions);
247
+ const sourceOptions = isMirror && shouldForceAutoInterpretation(formatOptions) ? buildAutoInterpretedOptions(formatOptions) : formatOptions;
248
+ const { formatted, raw } = formatFn(rawInput, formatType, sourceOptions);
243
249
  if (!formatted && !raw) {
244
250
  visible.value = "";
245
251
  if (mirror.value !== "") mirror.value = "";
@@ -324,7 +330,8 @@ var applyFormat = async (state2, helpers, config) => {
324
330
  entry.handler = handler;
325
331
  const initial = entry.visible.value;
326
332
  if (initial !== "") {
327
- const { formatted, raw } = formatter.format(initial, formatType, formatOptions);
333
+ const initialOptions = shouldForceAutoInterpretation(formatOptions) ? buildAutoInterpretedOptions(formatOptions) : formatOptions;
334
+ const { formatted, raw } = formatter.format(initial, formatType, initialOptions);
328
335
  applyFormattedValue(entry.visible, entry.mirror, formatted, raw);
329
336
  } else if (entry.mirrorIsOwned && entry.mirror.value !== "") {
330
337
  entry.mirror.value = "";
@@ -516,6 +523,7 @@ var createGetState = (state2, helpers) => () => {
516
523
 
517
524
  // src/core/dom.ts
518
525
  var isFormFieldElement = (value) => value instanceof HTMLInputElement || value instanceof HTMLSelectElement || value instanceof HTMLTextAreaElement;
526
+ var isArraySyntax = (field) => typeof field.name === "string" && field.name.endsWith("[]");
519
527
  var resolveFormElement = (target) => {
520
528
  if (!target) return null;
521
529
  if (target instanceof HTMLFormElement) return target;
@@ -552,6 +560,30 @@ var readFieldValue = (fields) => {
552
560
  if (fields.length === 0) return void 0;
553
561
  const first = fields[0];
554
562
  if (!first) return void 0;
563
+ if (isArraySyntax(first) && fields.length > 1) {
564
+ if (first instanceof HTMLSelectElement || first instanceof HTMLTextAreaElement) {
565
+ return fields.map((f) => f.value);
566
+ }
567
+ if (first instanceof HTMLInputElement) {
568
+ if (first.type === "radio") {
569
+ const checked = fields.find(
570
+ (f) => f instanceof HTMLInputElement && f.checked
571
+ );
572
+ return checked?.value ?? "";
573
+ }
574
+ if (first.type === "checkbox") {
575
+ return fields.filter(
576
+ (f) => f instanceof HTMLInputElement && f.checked
577
+ ).map((f) => f.value);
578
+ }
579
+ if (first.type === "file") {
580
+ return fields.flatMap(
581
+ (f) => f instanceof HTMLInputElement && f.files ? Array.from(f.files) : []
582
+ );
583
+ }
584
+ }
585
+ return fields.map((f) => f.value);
586
+ }
555
587
  if (first instanceof HTMLSelectElement || first instanceof HTMLTextAreaElement) {
556
588
  return first.value;
557
589
  }
@@ -577,7 +609,10 @@ var readFieldValue = (fields) => {
577
609
  var writeFieldValue = (fields, value) => {
578
610
  const normalizedArray = Array.isArray(value) ? value.map((item) => String(item)) : null;
579
611
  const normalizedValue = value === void 0 || value === null ? "" : String(value);
580
- for (const field of fields) {
612
+ const distributeArray = fields.length > 0 && fields[0] !== void 0 && isArraySyntax(fields[0]) && normalizedArray !== null;
613
+ for (let index = 0; index < fields.length; index++) {
614
+ const field = fields[index];
615
+ if (!field) continue;
581
616
  if (field instanceof HTMLInputElement) {
582
617
  if (field.type === "checkbox") {
583
618
  field.checked = normalizedArray ? normalizedArray.includes(field.value) : normalizedValue === field.value;
@@ -591,9 +626,17 @@ var writeFieldValue = (fields, value) => {
591
626
  if (Array.isArray(value) && value.length === 0) field.value = "";
592
627
  continue;
593
628
  }
629
+ if (distributeArray && normalizedArray) {
630
+ field.value = normalizedArray[index] ?? "";
631
+ continue;
632
+ }
594
633
  field.value = normalizedValue;
595
634
  continue;
596
635
  }
636
+ if (distributeArray && normalizedArray) {
637
+ field.value = normalizedArray[index] ?? "";
638
+ continue;
639
+ }
597
640
  field.value = normalizedValue;
598
641
  }
599
642
  };