@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.cjs CHANGED
@@ -272,6 +272,11 @@ var readInputType = (event) => {
272
272
  const candidate = event;
273
273
  return typeof candidate.inputType === "string" ? candidate.inputType : void 0;
274
274
  };
275
+ var buildAutoInterpretedOptions = (formatOptions) => ({
276
+ ...formatOptions ?? {},
277
+ interpretInputAs: "auto"
278
+ });
279
+ var shouldForceAutoInterpretation = (formatOptions) => formatOptions?.interpretInputAs === void 0;
275
280
  var buildHandler = (visible, mirror, formatFn, formatType, formatOptions) => {
276
281
  const handler = (event) => {
277
282
  if (!visible.isConnected || !mirror.isConnected) return;
@@ -280,7 +285,8 @@ var buildHandler = (visible, mirror, formatFn, formatType, formatOptions) => {
280
285
  if (!isVisible && !isMirror) return;
281
286
  const rawInput = isMirror ? mirror.value : visible.value;
282
287
  const inputType = readInputType(event);
283
- const { formatted, raw } = formatFn(rawInput, formatType, formatOptions);
288
+ const sourceOptions = isMirror && shouldForceAutoInterpretation(formatOptions) ? buildAutoInterpretedOptions(formatOptions) : formatOptions;
289
+ const { formatted, raw } = formatFn(rawInput, formatType, sourceOptions);
284
290
  if (!formatted && !raw) {
285
291
  visible.value = "";
286
292
  if (mirror.value !== "") mirror.value = "";
@@ -365,7 +371,8 @@ var applyFormat = async (state2, helpers, config) => {
365
371
  entry.handler = handler;
366
372
  const initial = entry.visible.value;
367
373
  if (initial !== "") {
368
- const { formatted, raw } = formatter.format(initial, formatType, formatOptions);
374
+ const initialOptions = shouldForceAutoInterpretation(formatOptions) ? buildAutoInterpretedOptions(formatOptions) : formatOptions;
375
+ const { formatted, raw } = formatter.format(initial, formatType, initialOptions);
369
376
  applyFormattedValue(entry.visible, entry.mirror, formatted, raw);
370
377
  } else if (entry.mirrorIsOwned && entry.mirror.value !== "") {
371
378
  entry.mirror.value = "";
@@ -557,6 +564,7 @@ var createGetState = (state2, helpers) => () => {
557
564
 
558
565
  // src/core/dom.ts
559
566
  var isFormFieldElement = (value) => value instanceof HTMLInputElement || value instanceof HTMLSelectElement || value instanceof HTMLTextAreaElement;
567
+ var isArraySyntax = (field) => typeof field.name === "string" && field.name.endsWith("[]");
560
568
  var resolveFormElement = (target) => {
561
569
  if (!target) return null;
562
570
  if (target instanceof HTMLFormElement) return target;
@@ -593,6 +601,30 @@ var readFieldValue = (fields) => {
593
601
  if (fields.length === 0) return void 0;
594
602
  const first = fields[0];
595
603
  if (!first) return void 0;
604
+ if (isArraySyntax(first) && fields.length > 1) {
605
+ if (first instanceof HTMLSelectElement || first instanceof HTMLTextAreaElement) {
606
+ return fields.map((f) => f.value);
607
+ }
608
+ if (first instanceof HTMLInputElement) {
609
+ if (first.type === "radio") {
610
+ const checked = fields.find(
611
+ (f) => f instanceof HTMLInputElement && f.checked
612
+ );
613
+ return checked?.value ?? "";
614
+ }
615
+ if (first.type === "checkbox") {
616
+ return fields.filter(
617
+ (f) => f instanceof HTMLInputElement && f.checked
618
+ ).map((f) => f.value);
619
+ }
620
+ if (first.type === "file") {
621
+ return fields.flatMap(
622
+ (f) => f instanceof HTMLInputElement && f.files ? Array.from(f.files) : []
623
+ );
624
+ }
625
+ }
626
+ return fields.map((f) => f.value);
627
+ }
596
628
  if (first instanceof HTMLSelectElement || first instanceof HTMLTextAreaElement) {
597
629
  return first.value;
598
630
  }
@@ -618,7 +650,10 @@ var readFieldValue = (fields) => {
618
650
  var writeFieldValue = (fields, value) => {
619
651
  const normalizedArray = Array.isArray(value) ? value.map((item) => String(item)) : null;
620
652
  const normalizedValue = value === void 0 || value === null ? "" : String(value);
621
- for (const field of fields) {
653
+ const distributeArray = fields.length > 0 && fields[0] !== void 0 && isArraySyntax(fields[0]) && normalizedArray !== null;
654
+ for (let index = 0; index < fields.length; index++) {
655
+ const field = fields[index];
656
+ if (!field) continue;
622
657
  if (field instanceof HTMLInputElement) {
623
658
  if (field.type === "checkbox") {
624
659
  field.checked = normalizedArray ? normalizedArray.includes(field.value) : normalizedValue === field.value;
@@ -632,9 +667,17 @@ var writeFieldValue = (fields, value) => {
632
667
  if (Array.isArray(value) && value.length === 0) field.value = "";
633
668
  continue;
634
669
  }
670
+ if (distributeArray && normalizedArray) {
671
+ field.value = normalizedArray[index] ?? "";
672
+ continue;
673
+ }
635
674
  field.value = normalizedValue;
636
675
  continue;
637
676
  }
677
+ if (distributeArray && normalizedArray) {
678
+ field.value = normalizedArray[index] ?? "";
679
+ continue;
680
+ }
638
681
  field.value = normalizedValue;
639
682
  }
640
683
  };