@samline/forms 2.2.2 → 2.2.3

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.2.2/dist/browser/global.global.js"></script>
45
+ <script src="https://unpkg.com/@samline/forms@2.2.3/dist/browser/global.global.js"></script>
46
46
  ```
47
47
 
48
- > Pin the version in production. Replace `2.2.2` with the version you ship.
48
+ > Pin the version in production. Replace `2.2.3` 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.2.2/dist/browser/global.global.js"></script>
58
+ <script src="https://unpkg.com/@samline/forms@2.2.3/dist/browser/global.global.js"></script>
59
59
  <script>
60
60
  const contactForm = window.Forms.newForm({ id: 'contact-form' })
61
61
 
@@ -4692,23 +4692,36 @@ var Forms = (() => {
4692
4692
  form2.appendChild(input);
4693
4693
  return input;
4694
4694
  };
4695
- var applyFormattedValue = (visible, mirror, formatted, raw) => {
4695
+ var applyFormattedValue = (visible, mirror, formatted, raw, inputType) => {
4696
4696
  const rawInput = visible.value;
4697
4697
  const caret = clamp(
4698
4698
  visible.selectionStart ?? rawInput.length,
4699
4699
  0,
4700
4700
  rawInput.length
4701
4701
  );
4702
+ if (mirror && mirror.value !== raw) mirror.value = raw;
4702
4703
  if (rawInput === formatted) {
4703
- if (mirror && mirror.value !== raw) mirror.value = raw;
4704
- return caret;
4704
+ const newCaret2 = pullCaretPastLeadingDelimiter(caret, formatted, inputType);
4705
+ if (newCaret2 !== caret) restoreCursor(visible, newCaret2);
4706
+ return newCaret2;
4705
4707
  }
4706
4708
  visible.value = formatted;
4707
- if (mirror) mirror.value = raw;
4708
- const newCaret = computeCursorPosition(caret, rawInput, formatted);
4709
+ const newCaret = pullCaretPastLeadingDelimiter(
4710
+ computeCursorPosition(caret, rawInput, formatted),
4711
+ formatted,
4712
+ inputType
4713
+ );
4709
4714
  restoreCursor(visible, newCaret);
4710
4715
  return newCaret;
4711
4716
  };
4717
+ var pullCaretPastLeadingDelimiter = (caret, value, inputType) => {
4718
+ if (!inputType || !inputType.startsWith("delete")) return caret;
4719
+ let cursor = caret;
4720
+ while (cursor > 0 && isFormatChar(value[cursor - 1])) {
4721
+ cursor -= 1;
4722
+ }
4723
+ return cursor;
4724
+ };
4712
4725
  var clamp = (value, min, max) => Math.max(min, Math.min(max, value));
4713
4726
  var computeCursorPosition = (caretPos, rawInput, newValue) => {
4714
4727
  if (newValue.length === 0) return 0;
@@ -4815,17 +4828,22 @@ var Forms = (() => {
4815
4828
  if (!field.isConnected) return;
4816
4829
  if (event.target !== field) return;
4817
4830
  const rawInput = event.target.value;
4831
+ const inputType = readInputType(event);
4818
4832
  const { formatted, raw } = formatFn(rawInput, formatType, formatOptions);
4819
4833
  if (!formatted && !raw) {
4820
4834
  field.value = "";
4821
4835
  if (mirror && mirror.value !== "") mirror.value = "";
4822
4836
  return;
4823
4837
  }
4824
- applyFormattedValue(field, mirror, formatted, raw);
4838
+ applyFormattedValue(field, mirror, formatted, raw, inputType);
4825
4839
  void mirrorFieldName;
4826
4840
  };
4827
4841
  return handler;
4828
4842
  };
4843
+ var readInputType = (event) => {
4844
+ const candidate = event;
4845
+ return typeof candidate.inputType === "string" ? candidate.inputType : void 0;
4846
+ };
4829
4847
  var applyFormat = async (state2, helpers, config) => {
4830
4848
  if (!state2.element || !state2.api) return;
4831
4849
  const formatter = await loadFormatter();