@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.d.mts CHANGED
@@ -1491,8 +1491,8 @@ interface DropdownFrame {
1491
1491
  }
1492
1492
  /** Opens below when the list fits there, else on whichever side has more room. */
1493
1493
  declare function placeDropdown(anchor: AnchorRect, panelHeight: number, viewportHeight: number): DropdownPlacement;
1494
- /** The window frame for the list next to `anchor`, on the side `placeDropdown` picks. */
1495
- declare function dropdownFrame(anchor: AnchorRect, panelHeight: number, viewportHeight: number): DropdownFrame;
1494
+ /** The window frame for the list next to `anchor`, kept inside the viewport; `preferBelow` opens under the field unless only the room above fits the list. */
1495
+ declare function dropdownFrame(anchor: AnchorRect, panelHeight: number, viewportHeight: number, preferBelow?: boolean): DropdownFrame;
1496
1496
  /** Widens a narrow anchor's list to `minWidth` (default a readable width) and keeps it inside the viewport. */
1497
1497
  declare function fitDropdownWidth(frame: DropdownFrame, viewportWidth: number, minWidth?: number): DropdownFrame;
1498
1498
 
package/dist/index.d.ts CHANGED
@@ -1491,8 +1491,8 @@ interface DropdownFrame {
1491
1491
  }
1492
1492
  /** Opens below when the list fits there, else on whichever side has more room. */
1493
1493
  declare function placeDropdown(anchor: AnchorRect, panelHeight: number, viewportHeight: number): DropdownPlacement;
1494
- /** The window frame for the list next to `anchor`, on the side `placeDropdown` picks. */
1495
- declare function dropdownFrame(anchor: AnchorRect, panelHeight: number, viewportHeight: number): DropdownFrame;
1494
+ /** The window frame for the list next to `anchor`, kept inside the viewport; `preferBelow` opens under the field unless only the room above fits the list. */
1495
+ declare function dropdownFrame(anchor: AnchorRect, panelHeight: number, viewportHeight: number, preferBelow?: boolean): DropdownFrame;
1496
1496
  /** Widens a narrow anchor's list to `minWidth` (default a readable width) and keeps it inside the viewport. */
1497
1497
  declare function fitDropdownWidth(frame: DropdownFrame, viewportWidth: number, minWidth?: number): DropdownFrame;
1498
1498
 
package/dist/index.js CHANGED
@@ -2396,7 +2396,7 @@ function PickerDialog({ testID, sheet, onDismiss, children }) {
2396
2396
  event.preventDefault();
2397
2397
  (event.shiftKey ? last : first).focus();
2398
2398
  };
2399
- const shape = sheet ? { borderTopLeftRadius: SHEET_RADIUS, borderTopRightRadius: SHEET_RADIUS, maxHeight: SHEET_MAX_HEIGHT, overflowY: "auto" } : { borderRadius: RADIUS, boxShadow: `0 12px 32px ${colors.border}` };
2399
+ 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}` };
2400
2400
  const style = { boxSizing: "border-box", padding: PAD, background: colors.surface, color: colors.text, border: `1px solid ${colors.border}`, fontFamily: "inherit", ...shape };
2401
2401
  return /* @__PURE__ */ jsxRuntime.jsx("div", { ref, "data-testid": testID, "data-variant": sheet ? "sheet" : "popover", style, onKeyDown, children });
2402
2402
  }
@@ -2423,23 +2423,31 @@ function placeDropdown(anchor, panelHeight, viewportHeight) {
2423
2423
  if (roomBelow >= panelHeight) return "below" /* Below */;
2424
2424
  return anchor.y > roomBelow ? "above" /* Above */ : "below" /* Below */;
2425
2425
  }
2426
- function dropdownFrame(anchor, panelHeight, viewportHeight) {
2427
- const placement = placeDropdown(anchor, panelHeight, viewportHeight);
2426
+ function placeBelowFirst(anchor, panelHeight, viewportHeight) {
2427
+ const roomBelow = viewportHeight - (anchor.y + anchor.height);
2428
+ const fitsOnlyAbove = roomBelow < panelHeight && anchor.y - EDGE >= panelHeight;
2429
+ return fitsOnlyAbove ? "above" /* Above */ : "below" /* Below */;
2430
+ }
2431
+ function clampTop(top, panelHeight, viewportHeight) {
2432
+ return Math.max(EDGE, Math.min(top, viewportHeight - panelHeight - EDGE));
2433
+ }
2434
+ function dropdownFrame(anchor, panelHeight, viewportHeight, preferBelow = false) {
2435
+ const placement = preferBelow ? placeBelowFirst(anchor, panelHeight, viewportHeight) : placeDropdown(anchor, panelHeight, viewportHeight);
2428
2436
  const base = { placement, left: anchor.x, width: anchor.width };
2429
- if (placement === "above" /* Above */) return { ...base, bottom: viewportHeight - anchor.y };
2430
- return { ...base, top: anchor.y + anchor.height };
2437
+ if (placement === "above" /* Above */) return { ...base, bottom: viewportHeight - clampTop(anchor.y - panelHeight, panelHeight, viewportHeight) - panelHeight };
2438
+ return { ...base, top: clampTop(anchor.y + anchor.height, panelHeight, viewportHeight) };
2431
2439
  }
2432
2440
  function fitDropdownWidth(frame, viewportWidth, minWidth = MIN_PANEL_WIDTH) {
2433
2441
  const width = Math.max(frame.width, Math.min(minWidth, viewportWidth - EDGE - EDGE));
2434
2442
  const left = Math.max(EDGE, Math.min(frame.left, viewportWidth - width - EDGE));
2435
2443
  return { ...frame, width, left: frame.width >= width ? frame.left : left };
2436
2444
  }
2437
- function DropdownSurface({ progress, translateY, anchorRef, onDismiss, closeLabel, closeHint, testID, children, minWidth, scale, accessibilityLabel }) {
2445
+ function DropdownSurface({ progress, translateY, anchorRef, onDismiss, closeLabel, closeHint, testID, children, minWidth, scale, accessibilityLabel, preferBelow = false }) {
2438
2446
  const { height: viewportHeight, width: viewportWidth } = reactNative.useWindowDimensions();
2439
2447
  const anchor = useAnchorRect2(anchorRef);
2440
2448
  const [panelHeight, setPanelHeight] = React37.useState(0);
2441
2449
  const onLayout = React37.useCallback((event) => setPanelHeight(event.nativeEvent.layout.height), []);
2442
- const frame = anchor === null ? null : fitDropdownWidth(dropdownFrame(anchor, panelHeight, viewportHeight), viewportWidth, minWidth);
2450
+ const frame = anchor === null ? null : fitDropdownWidth(dropdownFrame(anchor, panelHeight, viewportHeight, preferBelow), viewportWidth, minWidth);
2443
2451
  const place = frame === null ? null : { left: frame.left, width: frame.width, top: frame.top, bottom: frame.bottom };
2444
2452
  return /* @__PURE__ */ jsxRuntime.jsxs(reactNative.Modal, { transparent: true, visible: true, animationType: "none", "aria-label": accessibilityLabel, onRequestClose: onDismiss, children: [
2445
2453
  /* @__PURE__ */ jsxRuntime.jsx(reactNative.Pressable, { accessibilityHint: closeHint, accessibilityLabel: closeLabel, accessibilityRole: "button", style: reactNative.StyleSheet.absoluteFill, testID: `${testID}-backdrop`, onPress: onDismiss }),
@@ -2453,11 +2461,12 @@ var styles15 = reactNative.StyleSheet.create({
2453
2461
  function SheetSurface({ translateY, onDismiss, closeLabel, closeHint, testID, children, progress, scale, accessibilityLabel }) {
2454
2462
  return /* @__PURE__ */ jsxRuntime.jsx(reactNative.Modal, { transparent: true, visible: true, animationType: "none", "aria-label": accessibilityLabel, onRequestClose: onDismiss, children: /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { style: styles16.backdrop, children: [
2455
2463
  /* @__PURE__ */ jsxRuntime.jsx(reactNative.Pressable, { accessibilityHint: closeHint, accessibilityLabel: closeLabel, accessibilityRole: "button", style: reactNative.StyleSheet.absoluteFill, testID: `${testID}-backdrop`, onPress: onDismiss }),
2456
- /* @__PURE__ */ jsxRuntime.jsx(reactNative.Animated.View, { style: [{ transform: scale === void 0 ? [{ translateY }] : [{ translateY }, { scale }] }, progress === void 0 ? null : { opacity: progress }], children })
2464
+ /* @__PURE__ */ jsxRuntime.jsx(reactNative.Animated.View, { testID: `${testID}-sheet`, style: [styles16.sheet, { transform: scale === void 0 ? [{ translateY }] : [{ translateY }, { scale }] }, progress === void 0 ? null : { opacity: progress }], children })
2457
2465
  ] }) });
2458
2466
  }
2459
2467
  var styles16 = reactNative.StyleSheet.create({
2460
- backdrop: { flex: 1, justifyContent: "flex-end" }
2468
+ backdrop: { flex: 1, justifyContent: "flex-end" },
2469
+ sheet: { width: "100%", alignSelf: "stretch" }
2461
2470
  });
2462
2471
  function useReducedMotion() {
2463
2472
  const [reduced, setReduced] = React37.useState(false);
@@ -2639,9 +2648,9 @@ function PickerSurface({ anchorRef, phone, minWidth, label, testID, onDismiss, c
2639
2648
  const { progress, scale, still } = useOpenProgress();
2640
2649
  const body = /* @__PURE__ */ jsxRuntime.jsx(PickerDialog, { sheet: phone, testID, onDismiss, children });
2641
2650
  if (phone) {
2642
- return /* @__PURE__ */ jsxRuntime.jsx(SheetSurface, { accessibilityLabel: label, closeHint: labels.cancelHint, closeLabel: labels.cancel, progress, scale, testID, translateY: still, onDismiss, children: body });
2651
+ return /* @__PURE__ */ jsxRuntime.jsx(SheetSurface, { accessibilityLabel: label, closeHint: labels.cancelHint, closeLabel: labels.cancel, progress, testID, translateY: still, onDismiss, children: body });
2643
2652
  }
2644
- return /* @__PURE__ */ jsxRuntime.jsx(DropdownSurface, { accessibilityLabel: label, anchorRef, closeHint: labels.cancelHint, closeLabel: labels.cancel, minWidth, progress, scale, testID, translateY: still, onDismiss, children: body });
2653
+ return /* @__PURE__ */ jsxRuntime.jsx(DropdownSurface, { accessibilityLabel: label, anchorRef, closeHint: labels.cancelHint, closeLabel: labels.cancel, minWidth, preferBelow: true, progress, scale, testID, translateY: still, onDismiss, children: body });
2645
2654
  }
2646
2655
 
2647
2656
  // src/DatePicker/utils/calendarKeys.ts
@@ -2706,7 +2715,7 @@ var REGULAR = 400;
2706
2715
  function DayCell({ iso, state, testID, label, hint, onPick, onKey }) {
2707
2716
  const colors = usePickerColors();
2708
2717
  const { inRange, isEnd, isToday, enabled, focusable } = state;
2709
- const cellStyle = { width: DAY_CELL_PX, height: DAY_CELL_PX, background: inRange && !isEnd ? colors.tint : "transparent" };
2718
+ const cellStyle = { width: DAY_CELL_PX, height: DAY_CELL_PX, flexShrink: 0, background: inRange && !isEnd ? colors.tint : "transparent" };
2710
2719
  const textColor = isEnd ? colors.onBrand : enabled ? colors.text : colors.textSecondary;
2711
2720
  const buttonStyle = {
2712
2721
  width: DAY_CELL_PX,
@@ -2829,7 +2838,7 @@ var CAPTION_FONT = 15;
2829
2838
  var WEEKDAY_FONT = 12;
2830
2839
  var BOLD2 = 700;
2831
2840
  var ROW = { display: "flex" };
2832
- var EMPTY = { width: DAY_CELL_PX, height: DAY_CELL_PX };
2841
+ var EMPTY = { width: DAY_CELL_PX, height: DAY_CELL_PX, flexShrink: 0 };
2833
2842
  var FALLBACK = { year: 1970, month: 1};
2834
2843
  function MonthGrid({ month, selection, focusedDay, bounds, testID, onPick, onKey, showCaption = false, motion }) {
2835
2844
  const { labels, locale, today, firstDay } = useDatePickerConfig();
@@ -2946,13 +2955,16 @@ function useIsPhone() {
2946
2955
  function useReturnFocus(open, target) {
2947
2956
  const wasOpen = React37.useRef(open);
2948
2957
  React37.useEffect(() => {
2949
- if (wasOpen.current && !open) target.current?.focus();
2958
+ const closed = wasOpen.current && !open;
2950
2959
  wasOpen.current = open;
2960
+ if (!closed) return void 0;
2961
+ const timer = setTimeout(() => target.current?.focus(), 0);
2962
+ return () => clearTimeout(timer);
2951
2963
  }, [open, target]);
2952
2964
  }
2953
- function useTypedDate({ value, onChange, bounds, format, onError, rangeMessage }) {
2965
+ function useTypedDate({ value, onChange, bounds, format, onError, rangeMessage, editOnType = false }) {
2954
2966
  const { labels, locale } = useDatePickerConfig();
2955
- const [focused, setFocused] = React37.useState(false);
2967
+ const [editing, setEditing] = React37.useState(false);
2956
2968
  const [text, setText] = React37.useState("");
2957
2969
  React37.useEffect(() => {
2958
2970
  setText((current) => {
@@ -2967,7 +2979,12 @@ function useTypedDate({ value, onChange, bounds, format, onError, rangeMessage }
2967
2979
  if (parsed === null) return labels.invalidDate;
2968
2980
  return isWithinBounds(parsed, bounds.min, bounds.max) ? void 0 : rangeMessage ?? labels.outOfRange;
2969
2981
  };
2982
+ const startEdit = () => {
2983
+ setEditing(true);
2984
+ setText(value === "" ? "" : formatTyped(value, locale));
2985
+ };
2970
2986
  const onText = (next) => {
2987
+ setEditing(true);
2971
2988
  setText(next);
2972
2989
  if (next.trim() === "") {
2973
2990
  onError?.(void 0);
@@ -2981,25 +2998,66 @@ function useTypedDate({ value, onChange, bounds, format, onError, rangeMessage }
2981
2998
  if (message === void 0) onChange(parsed);
2982
2999
  };
2983
3000
  const isReady = () => {
2984
- if (!focused) return true;
3001
+ if (!editing) return true;
2985
3002
  const message = verdict(text);
2986
3003
  onError?.(message);
2987
3004
  return message === void 0;
2988
3005
  };
2989
3006
  return {
2990
- display: focused ? text : value === "" ? "" : format(value, locale),
3007
+ display: editing ? text : value === "" ? "" : format(value, locale),
2991
3008
  onText,
2992
3009
  onFocus: () => {
2993
- setFocused(true);
2994
- setText(value === "" ? "" : formatTyped(value, locale));
3010
+ if (!editOnType) startEdit();
2995
3011
  },
2996
3012
  onBlur: () => {
2997
- setFocused(false);
2998
- if (verdict(text) !== void 0) onError?.(verdict(text));
3013
+ setEditing(false);
3014
+ if (editing && verdict(text) !== void 0) onError?.(verdict(text));
3015
+ },
3016
+ onEdit: (replacement) => {
3017
+ if (editing) return false;
3018
+ if (replacement !== void 0) onText(replacement);
3019
+ else startEdit();
3020
+ return replacement !== void 0;
2999
3021
  },
3000
3022
  isReady
3001
3023
  };
3002
3024
  }
3025
+
3026
+ // src/DatePicker/utils/fieldWidth.ts
3027
+ var FONT_PX = 14;
3028
+ var GLYPH_EM = 0.62;
3029
+ var PAD_INLINE = 12;
3030
+ var BORDER = 1;
3031
+ var PLACEHOLDER_CHARS = 15;
3032
+ function closedFieldMinWidth(text, iconRoom) {
3033
+ const chars = Math.max(text.length, PLACEHOLDER_CHARS);
3034
+ return Math.ceil(chars * FONT_PX * GLYPH_EM) + PAD_INLINE + iconRoom + BORDER + BORDER;
3035
+ }
3036
+
3037
+ // src/DatePicker/utils/typedEdit.ts
3038
+ var DELETE_KEYS = /* @__PURE__ */ new Set(["Backspace", "Delete"]);
3039
+ function coversAll(input) {
3040
+ const { selectionStart, selectionEnd, value } = input;
3041
+ return value !== "" && selectionStart === 0 && selectionEnd === value.length;
3042
+ }
3043
+ function isPrintable(event) {
3044
+ return event.key.length === 1 && !event.ctrlKey && !event.metaKey;
3045
+ }
3046
+ function typedEditBind(typed) {
3047
+ const start = (event, replacement) => {
3048
+ if (typed.onEdit(coversAll(event.currentTarget) ? replacement : void 0)) event.preventDefault();
3049
+ };
3050
+ return {
3051
+ onEditKey: (event) => {
3052
+ if (isPrintable(event)) start(event, event.key);
3053
+ else if (DELETE_KEYS.has(event.key)) start(event, "");
3054
+ },
3055
+ onPaste: (event) => start(event, event.clipboardData.getData("text")),
3056
+ onCut: (event) => {
3057
+ if (!coversAll(event.currentTarget)) typed.onEdit();
3058
+ }
3059
+ };
3060
+ }
3003
3061
  var SINGLE_PICKER_WIDTH = 340;
3004
3062
  var ICON_ROOM = 48;
3005
3063
  var ICON_SLOT = { position: "absolute", right: 2, top: 0, bottom: 0, display: "flex", alignItems: "center" };
@@ -3012,7 +3070,8 @@ function DatePickerInput({ fieldProps, context, onTypedError }) {
3012
3070
  const anchorRef = React37.useRef(null);
3013
3071
  const inputRef = React37.useRef(null);
3014
3072
  const bounds = { min, max };
3015
- const typed = useTypedDate({ value, onChange, bounds, format: formatDisplay, onError: onTypedError });
3073
+ const typed = useTypedDate({ value, onChange, bounds, format: formatDisplay, onError: onTypedError, editOnType: true });
3074
+ const edit = typedEditBind(typed);
3016
3075
  const { style, focusBind, hoverBind } = useThemedInput({ hasError: context.hasError, editable: !disabled });
3017
3076
  const focus = focusBind;
3018
3077
  const webA11y = webFieldA11y({ describedById: context.describedById, hasError: context.hasError, required });
@@ -3024,6 +3083,7 @@ function DatePickerInput({ fieldProps, context, onTypedError }) {
3024
3083
  close();
3025
3084
  };
3026
3085
  const onKeyDown = (event) => {
3086
+ edit.onEditKey(event);
3027
3087
  if (event.key === "ArrowDown" && event.altKey) {
3028
3088
  event.preventDefault();
3029
3089
  setOpen(true);
@@ -3033,7 +3093,7 @@ function DatePickerInput({ fieldProps, context, onTypedError }) {
3033
3093
  event.preventDefault();
3034
3094
  if (typed.isReady()) onSubmit();
3035
3095
  };
3036
- return /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { ref: anchorRef, style: anchorStyles.wrap, children: [
3096
+ return /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { ref: anchorRef, style: [anchorStyles.wrap, { minWidth: closedFieldMinWidth(typed.display, ICON_ROOM) }], testID: `${testID}-anchor`, children: [
3037
3097
  /* @__PURE__ */ jsxRuntime.jsx(
3038
3098
  "input",
3039
3099
  {
@@ -3058,7 +3118,9 @@ function DatePickerInput({ fieldProps, context, onTypedError }) {
3058
3118
  focus.onFocus?.(event);
3059
3119
  typed.onFocus();
3060
3120
  },
3061
- onKeyDown
3121
+ onKeyDown,
3122
+ onCut: edit.onCut,
3123
+ onPaste: edit.onPaste
3062
3124
  }
3063
3125
  ),
3064
3126
  /* @__PURE__ */ jsxRuntime.jsx("div", { style: ICON_SLOT, children: /* @__PURE__ */ jsxRuntime.jsx(uiButtons.IconButton, { accessibilityHint: labels.openCalendarHint, accessibilityLabel: labels.openCalendar, appearance: "ghost", disabled, renderIcon: (color) => /* @__PURE__ */ jsxRuntime.jsx(Glyph, { color, name: "calendar" /* Calendar */ }), testID: `${testID}-calendar`, onPress: () => setOpen((current) => !current) }) }),
@@ -3282,8 +3344,8 @@ function WebDateRange({ value, onChange, onSubmit, fromLabel, toLabel, hint, tes
3282
3344
  const [toError, setToError] = React37.useState(void 0);
3283
3345
  const anchorRef = React37.useRef(null);
3284
3346
  const fromRef = React37.useRef(null);
3285
- 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 });
3286
- 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 });
3347
+ 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 });
3348
+ 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 });
3287
3349
  const { style, focusBind, hoverBind } = useThemedInput({ hasError: context.hasError, editable: true });
3288
3350
  const focus = focusBind;
3289
3351
  const webA11y = webFieldA11y({ describedById: context.describedById, hasError: context.hasError });
@@ -3323,7 +3385,12 @@ function WebDateRange({ value, onChange, onSubmit, fromLabel, toLabel, hint, tes
3323
3385
  focus.onFocus?.(event);
3324
3386
  typed.onFocus();
3325
3387
  },
3326
- onKeyDown
3388
+ onCut: typedEditBind(typed).onCut,
3389
+ onKeyDown: (event) => {
3390
+ typedEditBind(typed).onEditKey(event);
3391
+ onKeyDown(event);
3392
+ },
3393
+ onPaste: typedEditBind(typed).onPaste
3327
3394
  }
3328
3395
  );
3329
3396
  return /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { ref: anchorRef, style: anchorStyles.wrap, children: [