@ukiahinsure/a2ui-react-adapter 0.1.19 → 0.1.20

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
@@ -78,6 +78,14 @@ function parseDateDraft(raw) {
78
78
  if (year < 1 || month < 1 || month > 12 || day < 1 || day > days[month - 1]) return null;
79
79
  return `${String(year).padStart(4, "0")}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
80
80
  }
81
+ function formatDateDigits(raw) {
82
+ if (!/^(\d*|\d{2}\/\d*|\d{2}\/\d{2}\/\d*)$/.test(raw)) return raw;
83
+ const digits = raw.replace(/\D/g, "").slice(0, 8);
84
+ const typedSlash = raw.endsWith("/") && (digits.length === 2 || digits.length === 4) ? "/" : "";
85
+ if (digits.length <= 2) return digits + typedSlash;
86
+ if (digits.length <= 4) return `${digits.slice(0, 2)}/${digits.slice(2)}${typedSlash}`;
87
+ return `${digits.slice(0, 2)}/${digits.slice(2, 4)}/${digits.slice(4)}`;
88
+ }
81
89
  function displayDate(value) {
82
90
  const iso = parseDateDraft(value);
83
91
  return iso ? `${iso.slice(5, 7)}/${iso.slice(8, 10)}/${iso.slice(0, 4)}` : value;
@@ -103,7 +111,8 @@ function DateOnlyInput(props) {
103
111
  const message = draft.trim() && !parsed ? "Enter a valid date in MM/DD/YYYY format." : parsed && min && parsed < min ? `Enter a date on or after ${displayDate(min)}.` : parsed && max && parsed > max ? `Enter a date on or before ${displayDate(max)}.` : "";
104
112
  inputRef.current?.setCustomValidity(message);
105
113
  }, [draft, parsed, min, max]);
106
- const update = (raw) => {
114
+ const update = (typed) => {
115
+ const raw = formatDateDigits(typed);
107
116
  setDraft(raw);
108
117
  const value = parseDateDraft(raw) || "";
109
118
  published.current = value;
@@ -1831,7 +1840,7 @@ function A2UISurfaceHost({
1831
1840
  const lastViewedSection = useRef2(void 0);
1832
1841
  const lastValidationKey = useRef2("");
1833
1842
  const pendingFieldUpdates = useRef2(/* @__PURE__ */ new Map());
1834
- const lastCurrentGroupId = useRef2(void 0);
1843
+ const lastQuestionKey = useRef2(void 0);
1835
1844
  const onAgentUpdateRef = useRef2(onAgentUpdate);
1836
1845
  onAgentUpdateRef.current = onAgentUpdate;
1837
1846
  const resolvedSurfaceMetadata = surfaceMetadata ?? controllerSnapshot?.surface ?? null;
@@ -1902,9 +1911,11 @@ function A2UISurfaceHost({
1902
1911
  const currentGroup = resolvedSurfaceMetadata?.fieldInteractions.find(
1903
1912
  (group) => group.mode === "current"
1904
1913
  );
1905
- const previousGroupId = lastCurrentGroupId.current;
1906
- if (currentGroup !== void 0) {
1907
- lastCurrentGroupId.current = currentGroup.groupId;
1914
+ const activeStateId = resolvedSurfaceMetadata?.activeStateId;
1915
+ const questionKey = currentGroup?.groupId ?? (activeStateId == null ? void 0 : `state:${activeStateId}`);
1916
+ const previousQuestionKey = lastQuestionKey.current;
1917
+ if (questionKey !== void 0) {
1918
+ lastQuestionKey.current = questionKey;
1908
1919
  }
1909
1920
  const report = onAgentUpdateRef.current;
1910
1921
  if (report === void 0) {
@@ -1918,14 +1929,14 @@ function A2UISurfaceHost({
1918
1929
  element: agentUpdateElement(root, changedFields, structure.fields)
1919
1930
  });
1920
1931
  }
1921
- if (currentGroup !== void 0 && previousGroupId !== void 0 && currentGroup.groupId !== previousGroupId) {
1922
- const fields = currentGroup.fields.map((binding) => binding.field);
1932
+ if (questionKey !== void 0 && previousQuestionKey !== void 0 && questionKey !== previousQuestionKey) {
1933
+ const fields = currentGroup?.fields.map((binding) => binding.field) ?? [];
1923
1934
  report({
1924
1935
  reason: "current-group",
1925
1936
  surfaceId: surface.id,
1926
1937
  fields,
1927
- groupId: currentGroup.groupId,
1928
- element: agentUpdateElement(root, fields, structure.fields)
1938
+ ...currentGroup === void 0 ? {} : { groupId: currentGroup.groupId },
1939
+ element: agentUpdateElement(root, fields, structure.fields) ?? currentQuestionElement(root)
1929
1940
  });
1930
1941
  }
1931
1942
  };
@@ -2070,15 +2081,33 @@ function agentUpdateElement(root, fields, descriptors) {
2070
2081
  if (descriptor !== void 0) {
2071
2082
  labels.add(descriptor.label);
2072
2083
  }
2073
- const label = [
2074
- ...root.querySelectorAll("label, strong, legend")
2075
- ].find((candidate) => labels.has(candidate.textContent?.trim() ?? ""));
2084
+ const label = [...root.querySelectorAll("*")].find(
2085
+ (candidate) => candidate.childElementCount === 0 && labels.has(candidate.textContent?.trim() ?? "")
2086
+ );
2076
2087
  if (label?.parentElement != null && root.contains(label.parentElement)) {
2077
2088
  return label.parentElement;
2078
2089
  }
2079
2090
  }
2080
2091
  return void 0;
2081
2092
  }
2093
+ function currentQuestionElement(root) {
2094
+ const action = [
2095
+ ...root.querySelectorAll("[data-a2ui-action='true']")
2096
+ ].find(
2097
+ (candidate) => candidate.dataset.a2uiActionLabel !== "skip" && candidate.closest("[data-a2ui-loop-row]") === null
2098
+ );
2099
+ if (action === void 0) {
2100
+ return void 0;
2101
+ }
2102
+ let container = action.parentElement;
2103
+ if (container !== null && textOf(container) === [...container.querySelectorAll("button")].map(textOf).join("")) {
2104
+ container = container.parentElement;
2105
+ }
2106
+ return container !== null && container !== root && root.contains(container) ? container : action;
2107
+ }
2108
+ function textOf(element) {
2109
+ return (element.textContent ?? "").replace(/\s+/g, "");
2110
+ }
2082
2111
  function isControlDisabled(control) {
2083
2112
  return control.getAttribute("aria-disabled") === "true" || "disabled" in control && control.disabled === true;
2084
2113
  }