@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/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.3/dist/browser/global.global.js"></script>
45
+ <script src="https://unpkg.com/@samline/forms@2.4.0/dist/browser/global.global.js"></script>
46
46
  ```
47
47
 
48
- > Pin the version in production. Replace `2.3.3` with the version you ship.
48
+ > Pin the version in production. Replace `2.4.0` 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.3/dist/browser/global.global.js"></script>
58
+ <script src="https://unpkg.com/@samline/forms@2.4.0/dist/browser/global.global.js"></script>
59
59
  <script>
60
60
  const contactForm = window.Forms.newForm({ id: 'contact-form' })
61
61
 
@@ -4846,6 +4846,11 @@ var Forms = (() => {
4846
4846
  const candidate = event;
4847
4847
  return typeof candidate.inputType === "string" ? candidate.inputType : void 0;
4848
4848
  };
4849
+ var buildAutoInterpretedOptions = (formatOptions) => ({
4850
+ ...formatOptions ?? {},
4851
+ interpretInputAs: "auto"
4852
+ });
4853
+ var shouldForceAutoInterpretation = (formatOptions) => formatOptions?.interpretInputAs === void 0;
4849
4854
  var buildHandler = (visible, mirror, formatFn, formatType, formatOptions) => {
4850
4855
  const handler = (event) => {
4851
4856
  if (!visible.isConnected || !mirror.isConnected) return;
@@ -4854,7 +4859,8 @@ var Forms = (() => {
4854
4859
  if (!isVisible && !isMirror) return;
4855
4860
  const rawInput = isMirror ? mirror.value : visible.value;
4856
4861
  const inputType = readInputType(event);
4857
- const { formatted, raw } = formatFn(rawInput, formatType, formatOptions);
4862
+ const sourceOptions = isMirror && shouldForceAutoInterpretation(formatOptions) ? buildAutoInterpretedOptions(formatOptions) : formatOptions;
4863
+ const { formatted, raw } = formatFn(rawInput, formatType, sourceOptions);
4858
4864
  if (!formatted && !raw) {
4859
4865
  visible.value = "";
4860
4866
  if (mirror.value !== "") mirror.value = "";
@@ -4939,7 +4945,8 @@ var Forms = (() => {
4939
4945
  entry.handler = handler;
4940
4946
  const initial = entry.visible.value;
4941
4947
  if (initial !== "") {
4942
- const { formatted, raw } = formatter.format(initial, formatType, formatOptions);
4948
+ const initialOptions = shouldForceAutoInterpretation(formatOptions) ? buildAutoInterpretedOptions(formatOptions) : formatOptions;
4949
+ const { formatted, raw } = formatter.format(initial, formatType, initialOptions);
4943
4950
  applyFormattedValue(entry.visible, entry.mirror, formatted, raw);
4944
4951
  } else if (entry.mirrorIsOwned && entry.mirror.value !== "") {
4945
4952
  entry.mirror.value = "";
@@ -5131,6 +5138,7 @@ var Forms = (() => {
5131
5138
 
5132
5139
  // src/core/dom.ts
5133
5140
  var isFormFieldElement = (value) => value instanceof HTMLInputElement || value instanceof HTMLSelectElement || value instanceof HTMLTextAreaElement;
5141
+ var isArraySyntax = (field) => typeof field.name === "string" && field.name.endsWith("[]");
5134
5142
  var resolveFormElement = (target) => {
5135
5143
  if (!target) return null;
5136
5144
  if (target instanceof HTMLFormElement) return target;
@@ -5167,6 +5175,30 @@ var Forms = (() => {
5167
5175
  if (fields.length === 0) return void 0;
5168
5176
  const first = fields[0];
5169
5177
  if (!first) return void 0;
5178
+ if (isArraySyntax(first) && fields.length > 1) {
5179
+ if (first instanceof HTMLSelectElement || first instanceof HTMLTextAreaElement) {
5180
+ return fields.map((f2) => f2.value);
5181
+ }
5182
+ if (first instanceof HTMLInputElement) {
5183
+ if (first.type === "radio") {
5184
+ const checked = fields.find(
5185
+ (f2) => f2 instanceof HTMLInputElement && f2.checked
5186
+ );
5187
+ return checked?.value ?? "";
5188
+ }
5189
+ if (first.type === "checkbox") {
5190
+ return fields.filter(
5191
+ (f2) => f2 instanceof HTMLInputElement && f2.checked
5192
+ ).map((f2) => f2.value);
5193
+ }
5194
+ if (first.type === "file") {
5195
+ return fields.flatMap(
5196
+ (f2) => f2 instanceof HTMLInputElement && f2.files ? Array.from(f2.files) : []
5197
+ );
5198
+ }
5199
+ }
5200
+ return fields.map((f2) => f2.value);
5201
+ }
5170
5202
  if (first instanceof HTMLSelectElement || first instanceof HTMLTextAreaElement) {
5171
5203
  return first.value;
5172
5204
  }
@@ -5192,7 +5224,10 @@ var Forms = (() => {
5192
5224
  var writeFieldValue = (fields, value) => {
5193
5225
  const normalizedArray = Array.isArray(value) ? value.map((item) => String(item)) : null;
5194
5226
  const normalizedValue = value === void 0 || value === null ? "" : String(value);
5195
- for (const field of fields) {
5227
+ const distributeArray = fields.length > 0 && fields[0] !== void 0 && isArraySyntax(fields[0]) && normalizedArray !== null;
5228
+ for (let index = 0; index < fields.length; index++) {
5229
+ const field = fields[index];
5230
+ if (!field) continue;
5196
5231
  if (field instanceof HTMLInputElement) {
5197
5232
  if (field.type === "checkbox") {
5198
5233
  field.checked = normalizedArray ? normalizedArray.includes(field.value) : normalizedValue === field.value;
@@ -5206,9 +5241,17 @@ var Forms = (() => {
5206
5241
  if (Array.isArray(value) && value.length === 0) field.value = "";
5207
5242
  continue;
5208
5243
  }
5244
+ if (distributeArray && normalizedArray) {
5245
+ field.value = normalizedArray[index] ?? "";
5246
+ continue;
5247
+ }
5209
5248
  field.value = normalizedValue;
5210
5249
  continue;
5211
5250
  }
5251
+ if (distributeArray && normalizedArray) {
5252
+ field.value = normalizedArray[index] ?? "";
5253
+ continue;
5254
+ }
5212
5255
  field.value = normalizedValue;
5213
5256
  }
5214
5257
  };