@dloizides/ui-forms 1.42.0 → 1.42.2

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.mjs CHANGED
@@ -2389,7 +2389,7 @@ function PickerDialog({ testID, sheet, onDismiss, children }) {
2389
2389
  event.preventDefault();
2390
2390
  (event.shiftKey ? last : first).focus();
2391
2391
  };
2392
- const shape = sheet ? { borderTopLeftRadius: SHEET_RADIUS, borderTopRightRadius: SHEET_RADIUS, maxHeight: SHEET_MAX_HEIGHT, overflowY: "auto" } : { borderRadius: RADIUS, boxShadow: `0 12px 32px ${colors.border}` };
2392
+ const shape = sheet ? { borderTopLeftRadius: SHEET_RADIUS, borderTopRightRadius: SHEET_RADIUS, width: "100%", maxHeight: SHEET_MAX_HEIGHT, overflowY: "auto" } : { borderRadius: RADIUS, boxShadow: `0 12px 32px ${colors.border}` };
2393
2393
  const style = { boxSizing: "border-box", padding: PAD, background: colors.surface, color: colors.text, border: `1px solid ${colors.border}`, fontFamily: "inherit", ...shape };
2394
2394
  return /* @__PURE__ */ jsx("div", { ref, "data-testid": testID, "data-variant": sheet ? "sheet" : "popover", style, onKeyDown, children });
2395
2395
  }
@@ -2416,23 +2416,31 @@ function placeDropdown(anchor, panelHeight, viewportHeight) {
2416
2416
  if (roomBelow >= panelHeight) return "below" /* Below */;
2417
2417
  return anchor.y > roomBelow ? "above" /* Above */ : "below" /* Below */;
2418
2418
  }
2419
- function dropdownFrame(anchor, panelHeight, viewportHeight) {
2420
- const placement = placeDropdown(anchor, panelHeight, viewportHeight);
2419
+ function placeBelowFirst(anchor, panelHeight, viewportHeight) {
2420
+ const roomBelow = viewportHeight - (anchor.y + anchor.height);
2421
+ const fitsOnlyAbove = roomBelow < panelHeight && anchor.y - EDGE >= panelHeight;
2422
+ return fitsOnlyAbove ? "above" /* Above */ : "below" /* Below */;
2423
+ }
2424
+ function clampTop(top, panelHeight, viewportHeight) {
2425
+ return Math.max(EDGE, Math.min(top, viewportHeight - panelHeight - EDGE));
2426
+ }
2427
+ function dropdownFrame(anchor, panelHeight, viewportHeight, preferBelow = false) {
2428
+ const placement = preferBelow ? placeBelowFirst(anchor, panelHeight, viewportHeight) : placeDropdown(anchor, panelHeight, viewportHeight);
2421
2429
  const base = { placement, left: anchor.x, width: anchor.width };
2422
- if (placement === "above" /* Above */) return { ...base, bottom: viewportHeight - anchor.y };
2423
- return { ...base, top: anchor.y + anchor.height };
2430
+ if (placement === "above" /* Above */) return { ...base, bottom: viewportHeight - clampTop(anchor.y - panelHeight, panelHeight, viewportHeight) - panelHeight };
2431
+ return { ...base, top: clampTop(anchor.y + anchor.height, panelHeight, viewportHeight) };
2424
2432
  }
2425
2433
  function fitDropdownWidth(frame, viewportWidth, minWidth = MIN_PANEL_WIDTH) {
2426
2434
  const width = Math.max(frame.width, Math.min(minWidth, viewportWidth - EDGE - EDGE));
2427
2435
  const left = Math.max(EDGE, Math.min(frame.left, viewportWidth - width - EDGE));
2428
2436
  return { ...frame, width, left: frame.width >= width ? frame.left : left };
2429
2437
  }
2430
- function DropdownSurface({ progress, translateY, anchorRef, onDismiss, closeLabel, closeHint, testID, children, minWidth, scale, accessibilityLabel }) {
2438
+ function DropdownSurface({ progress, translateY, anchorRef, onDismiss, closeLabel, closeHint, testID, children, minWidth, scale, accessibilityLabel, preferBelow = false }) {
2431
2439
  const { height: viewportHeight, width: viewportWidth } = useWindowDimensions();
2432
2440
  const anchor = useAnchorRect2(anchorRef);
2433
2441
  const [panelHeight, setPanelHeight] = useState(0);
2434
2442
  const onLayout = useCallback((event) => setPanelHeight(event.nativeEvent.layout.height), []);
2435
- const frame = anchor === null ? null : fitDropdownWidth(dropdownFrame(anchor, panelHeight, viewportHeight), viewportWidth, minWidth);
2443
+ const frame = anchor === null ? null : fitDropdownWidth(dropdownFrame(anchor, panelHeight, viewportHeight, preferBelow), viewportWidth, minWidth);
2436
2444
  const place = frame === null ? null : { left: frame.left, width: frame.width, top: frame.top, bottom: frame.bottom };
2437
2445
  return /* @__PURE__ */ jsxs(Modal, { transparent: true, visible: true, animationType: "none", "aria-label": accessibilityLabel, onRequestClose: onDismiss, children: [
2438
2446
  /* @__PURE__ */ jsx(Pressable, { accessibilityHint: closeHint, accessibilityLabel: closeLabel, accessibilityRole: "button", style: StyleSheet.absoluteFill, testID: `${testID}-backdrop`, onPress: onDismiss }),
@@ -2446,11 +2454,12 @@ var styles15 = StyleSheet.create({
2446
2454
  function SheetSurface({ translateY, onDismiss, closeLabel, closeHint, testID, children, progress, scale, accessibilityLabel }) {
2447
2455
  return /* @__PURE__ */ jsx(Modal, { transparent: true, visible: true, animationType: "none", "aria-label": accessibilityLabel, onRequestClose: onDismiss, children: /* @__PURE__ */ jsxs(View, { style: styles16.backdrop, children: [
2448
2456
  /* @__PURE__ */ jsx(Pressable, { accessibilityHint: closeHint, accessibilityLabel: closeLabel, accessibilityRole: "button", style: StyleSheet.absoluteFill, testID: `${testID}-backdrop`, onPress: onDismiss }),
2449
- /* @__PURE__ */ jsx(Animated.View, { style: [{ transform: scale === void 0 ? [{ translateY }] : [{ translateY }, { scale }] }, progress === void 0 ? null : { opacity: progress }], children })
2457
+ /* @__PURE__ */ jsx(Animated.View, { testID: `${testID}-sheet`, style: [styles16.sheet, { transform: scale === void 0 ? [{ translateY }] : [{ translateY }, { scale }] }, progress === void 0 ? null : { opacity: progress }], children })
2450
2458
  ] }) });
2451
2459
  }
2452
2460
  var styles16 = StyleSheet.create({
2453
- backdrop: { flex: 1, justifyContent: "flex-end" }
2461
+ backdrop: { flex: 1, justifyContent: "flex-end" },
2462
+ sheet: { width: "100%", alignSelf: "stretch" }
2454
2463
  });
2455
2464
  function useReducedMotion() {
2456
2465
  const [reduced, setReduced] = useState(false);
@@ -2632,9 +2641,9 @@ function PickerSurface({ anchorRef, phone, minWidth, label, testID, onDismiss, c
2632
2641
  const { progress, scale, still } = useOpenProgress();
2633
2642
  const body = /* @__PURE__ */ jsx(PickerDialog, { sheet: phone, testID, onDismiss, children });
2634
2643
  if (phone) {
2635
- return /* @__PURE__ */ jsx(SheetSurface, { accessibilityLabel: label, closeHint: labels.cancelHint, closeLabel: labels.cancel, progress, scale, testID, translateY: still, onDismiss, children: body });
2644
+ return /* @__PURE__ */ jsx(SheetSurface, { accessibilityLabel: label, closeHint: labels.cancelHint, closeLabel: labels.cancel, progress, testID, translateY: still, onDismiss, children: body });
2636
2645
  }
2637
- return /* @__PURE__ */ jsx(DropdownSurface, { accessibilityLabel: label, anchorRef, closeHint: labels.cancelHint, closeLabel: labels.cancel, minWidth, progress, scale, testID, translateY: still, onDismiss, children: body });
2646
+ return /* @__PURE__ */ jsx(DropdownSurface, { accessibilityLabel: label, anchorRef, closeHint: labels.cancelHint, closeLabel: labels.cancel, minWidth, preferBelow: true, progress, scale, testID, translateY: still, onDismiss, children: body });
2638
2647
  }
2639
2648
 
2640
2649
  // src/DatePicker/utils/calendarKeys.ts
@@ -2699,7 +2708,7 @@ var REGULAR = 400;
2699
2708
  function DayCell({ iso, state, testID, label, hint, onPick, onKey }) {
2700
2709
  const colors = usePickerColors();
2701
2710
  const { inRange, isEnd, isToday, enabled, focusable } = state;
2702
- const cellStyle = { width: DAY_CELL_PX, height: DAY_CELL_PX, background: inRange && !isEnd ? colors.tint : "transparent" };
2711
+ const cellStyle = { width: DAY_CELL_PX, height: DAY_CELL_PX, flexShrink: 0, background: inRange && !isEnd ? colors.tint : "transparent" };
2703
2712
  const textColor = isEnd ? colors.onBrand : enabled ? colors.text : colors.textSecondary;
2704
2713
  const buttonStyle = {
2705
2714
  width: DAY_CELL_PX,
@@ -2822,7 +2831,7 @@ var CAPTION_FONT = 15;
2822
2831
  var WEEKDAY_FONT = 12;
2823
2832
  var BOLD2 = 700;
2824
2833
  var ROW = { display: "flex" };
2825
- var EMPTY = { width: DAY_CELL_PX, height: DAY_CELL_PX };
2834
+ var EMPTY = { width: DAY_CELL_PX, height: DAY_CELL_PX, flexShrink: 0 };
2826
2835
  var FALLBACK = { year: 1970, month: 1};
2827
2836
  function MonthGrid({ month, selection, focusedDay, bounds, testID, onPick, onKey, showCaption = false, motion }) {
2828
2837
  const { labels, locale, today, firstDay } = useDatePickerConfig();
@@ -2939,13 +2948,16 @@ function useIsPhone() {
2939
2948
  function useReturnFocus(open, target) {
2940
2949
  const wasOpen = useRef(open);
2941
2950
  useEffect(() => {
2942
- if (wasOpen.current && !open) target.current?.focus();
2951
+ const closed = wasOpen.current && !open;
2943
2952
  wasOpen.current = open;
2953
+ if (!closed) return void 0;
2954
+ const timer = setTimeout(() => target.current?.focus(), 0);
2955
+ return () => clearTimeout(timer);
2944
2956
  }, [open, target]);
2945
2957
  }
2946
- function useTypedDate({ value, onChange, bounds, format, onError, rangeMessage }) {
2958
+ function useTypedDate({ value, onChange, bounds, format, onError, rangeMessage, editOnType = false }) {
2947
2959
  const { labels, locale } = useDatePickerConfig();
2948
- const [focused, setFocused] = useState(false);
2960
+ const [editing, setEditing] = useState(false);
2949
2961
  const [text, setText] = useState("");
2950
2962
  useEffect(() => {
2951
2963
  setText((current) => {
@@ -2960,7 +2972,12 @@ function useTypedDate({ value, onChange, bounds, format, onError, rangeMessage }
2960
2972
  if (parsed === null) return labels.invalidDate;
2961
2973
  return isWithinBounds(parsed, bounds.min, bounds.max) ? void 0 : rangeMessage ?? labels.outOfRange;
2962
2974
  };
2975
+ const startEdit = () => {
2976
+ setEditing(true);
2977
+ setText(value === "" ? "" : formatTyped(value, locale));
2978
+ };
2963
2979
  const onText = (next) => {
2980
+ setEditing(true);
2964
2981
  setText(next);
2965
2982
  if (next.trim() === "") {
2966
2983
  onError?.(void 0);
@@ -2974,25 +2991,66 @@ function useTypedDate({ value, onChange, bounds, format, onError, rangeMessage }
2974
2991
  if (message === void 0) onChange(parsed);
2975
2992
  };
2976
2993
  const isReady = () => {
2977
- if (!focused) return true;
2994
+ if (!editing) return true;
2978
2995
  const message = verdict(text);
2979
2996
  onError?.(message);
2980
2997
  return message === void 0;
2981
2998
  };
2982
2999
  return {
2983
- display: focused ? text : value === "" ? "" : format(value, locale),
3000
+ display: editing ? text : value === "" ? "" : format(value, locale),
2984
3001
  onText,
2985
3002
  onFocus: () => {
2986
- setFocused(true);
2987
- setText(value === "" ? "" : formatTyped(value, locale));
3003
+ if (!editOnType) startEdit();
2988
3004
  },
2989
3005
  onBlur: () => {
2990
- setFocused(false);
2991
- if (verdict(text) !== void 0) onError?.(verdict(text));
3006
+ setEditing(false);
3007
+ if (editing && verdict(text) !== void 0) onError?.(verdict(text));
3008
+ },
3009
+ onEdit: (replacement) => {
3010
+ if (editing) return false;
3011
+ if (replacement !== void 0) onText(replacement);
3012
+ else startEdit();
3013
+ return replacement !== void 0;
2992
3014
  },
2993
3015
  isReady
2994
3016
  };
2995
3017
  }
3018
+
3019
+ // src/DatePicker/utils/fieldWidth.ts
3020
+ var FONT_PX = 14;
3021
+ var GLYPH_EM = 0.62;
3022
+ var PAD_INLINE = 12;
3023
+ var BORDER = 1;
3024
+ var PLACEHOLDER_CHARS = 15;
3025
+ function closedFieldMinWidth(text, iconRoom) {
3026
+ const chars = Math.max(text.length, PLACEHOLDER_CHARS);
3027
+ return Math.ceil(chars * FONT_PX * GLYPH_EM) + PAD_INLINE + iconRoom + BORDER + BORDER;
3028
+ }
3029
+
3030
+ // src/DatePicker/utils/typedEdit.ts
3031
+ var DELETE_KEYS = /* @__PURE__ */ new Set(["Backspace", "Delete"]);
3032
+ function coversAll(input) {
3033
+ const { selectionStart, selectionEnd, value } = input;
3034
+ return value !== "" && selectionStart === 0 && selectionEnd === value.length;
3035
+ }
3036
+ function isPrintable(event) {
3037
+ return event.key.length === 1 && !event.ctrlKey && !event.metaKey;
3038
+ }
3039
+ function typedEditBind(typed) {
3040
+ const start = (event, replacement) => {
3041
+ if (typed.onEdit(coversAll(event.currentTarget) ? replacement : void 0)) event.preventDefault();
3042
+ };
3043
+ return {
3044
+ onEditKey: (event) => {
3045
+ if (isPrintable(event)) start(event, event.key);
3046
+ else if (DELETE_KEYS.has(event.key)) start(event, "");
3047
+ },
3048
+ onPaste: (event) => start(event, event.clipboardData.getData("text")),
3049
+ onCut: (event) => {
3050
+ if (!coversAll(event.currentTarget)) typed.onEdit();
3051
+ }
3052
+ };
3053
+ }
2996
3054
  var SINGLE_PICKER_WIDTH = 340;
2997
3055
  var ICON_ROOM = 48;
2998
3056
  var ICON_SLOT = { position: "absolute", right: 2, top: 0, bottom: 0, display: "flex", alignItems: "center" };
@@ -3005,7 +3063,8 @@ function DatePickerInput({ fieldProps, context, onTypedError }) {
3005
3063
  const anchorRef = useRef(null);
3006
3064
  const inputRef = useRef(null);
3007
3065
  const bounds = { min, max };
3008
- const typed = useTypedDate({ value, onChange, bounds, format: formatDisplay, onError: onTypedError });
3066
+ const typed = useTypedDate({ value, onChange, bounds, format: formatDisplay, onError: onTypedError, editOnType: true });
3067
+ const edit = typedEditBind(typed);
3009
3068
  const { style, focusBind, hoverBind } = useThemedInput({ hasError: context.hasError, editable: !disabled });
3010
3069
  const focus = focusBind;
3011
3070
  const webA11y = webFieldA11y({ describedById: context.describedById, hasError: context.hasError, required });
@@ -3017,6 +3076,7 @@ function DatePickerInput({ fieldProps, context, onTypedError }) {
3017
3076
  close();
3018
3077
  };
3019
3078
  const onKeyDown = (event) => {
3079
+ edit.onEditKey(event);
3020
3080
  if (event.key === "ArrowDown" && event.altKey) {
3021
3081
  event.preventDefault();
3022
3082
  setOpen(true);
@@ -3026,7 +3086,7 @@ function DatePickerInput({ fieldProps, context, onTypedError }) {
3026
3086
  event.preventDefault();
3027
3087
  if (typed.isReady()) onSubmit();
3028
3088
  };
3029
- return /* @__PURE__ */ jsxs(View, { ref: anchorRef, style: anchorStyles.wrap, children: [
3089
+ return /* @__PURE__ */ jsxs(View, { ref: anchorRef, style: [anchorStyles.wrap, { minWidth: closedFieldMinWidth(typed.display, ICON_ROOM) }], testID: `${testID}-anchor`, children: [
3030
3090
  /* @__PURE__ */ jsx(
3031
3091
  "input",
3032
3092
  {
@@ -3051,7 +3111,9 @@ function DatePickerInput({ fieldProps, context, onTypedError }) {
3051
3111
  focus.onFocus?.(event);
3052
3112
  typed.onFocus();
3053
3113
  },
3054
- onKeyDown
3114
+ onKeyDown,
3115
+ onCut: edit.onCut,
3116
+ onPaste: edit.onPaste
3055
3117
  }
3056
3118
  ),
3057
3119
  /* @__PURE__ */ jsx("div", { style: ICON_SLOT, children: /* @__PURE__ */ jsx(IconButton, { accessibilityHint: labels.openCalendarHint, accessibilityLabel: labels.openCalendar, appearance: "ghost", disabled, renderIcon: (color) => /* @__PURE__ */ jsx(Glyph, { color, name: "calendar" /* Calendar */ }), testID: `${testID}-calendar`, onPress: () => setOpen((current) => !current) }) }),
@@ -3275,8 +3337,8 @@ function WebDateRange({ value, onChange, onSubmit, fromLabel, toLabel, hint, tes
3275
3337
  const [toError, setToError] = useState(void 0);
3276
3338
  const anchorRef = useRef(null);
3277
3339
  const fromRef = useRef(null);
3278
- const from = useTypedDate({ value: value.from, onChange: (next) => onChange({ from: next, to: value.to }), bounds: { max: orNone(value.to) }, format: formatShort, onError: setFromError, rangeMessage: labels.rangeOrder });
3279
- const to = useTypedDate({ value: value.to, onChange: (next) => onChange({ from: value.from, to: next }), bounds: { min: orNone(value.from) }, format: formatShort, onError: setToError, rangeMessage: labels.rangeOrder });
3340
+ const from = useTypedDate({ value: value.from, onChange: (next) => onChange({ from: next, to: value.to }), bounds: { max: orNone(value.to) }, format: formatShort, onError: setFromError, rangeMessage: labels.rangeOrder, editOnType: true });
3341
+ const to = useTypedDate({ value: value.to, onChange: (next) => onChange({ from: value.from, to: next }), bounds: { min: orNone(value.from) }, format: formatShort, onError: setToError, rangeMessage: labels.rangeOrder, editOnType: true });
3280
3342
  const { style, focusBind, hoverBind } = useThemedInput({ hasError: context.hasError, editable: true });
3281
3343
  const focus = focusBind;
3282
3344
  const webA11y = webFieldA11y({ describedById: context.describedById, hasError: context.hasError });
@@ -3316,7 +3378,12 @@ function WebDateRange({ value, onChange, onSubmit, fromLabel, toLabel, hint, tes
3316
3378
  focus.onFocus?.(event);
3317
3379
  typed.onFocus();
3318
3380
  },
3319
- onKeyDown
3381
+ onCut: typedEditBind(typed).onCut,
3382
+ onKeyDown: (event) => {
3383
+ typedEditBind(typed).onEditKey(event);
3384
+ onKeyDown(event);
3385
+ },
3386
+ onPaste: typedEditBind(typed).onPaste
3320
3387
  }
3321
3388
  );
3322
3389
  return /* @__PURE__ */ jsxs(View, { ref: anchorRef, style: anchorStyles.wrap, children: [