@dloizides/ui-forms 1.42.1 → 1.42.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/dist/index.mjs CHANGED
@@ -2416,11 +2416,16 @@ 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 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
+ }
2419
2424
  function clampTop(top, panelHeight, viewportHeight) {
2420
2425
  return Math.max(EDGE, Math.min(top, viewportHeight - panelHeight - EDGE));
2421
2426
  }
2422
2427
  function dropdownFrame(anchor, panelHeight, viewportHeight, preferBelow = false) {
2423
- const placement = preferBelow ? "below" /* Below */ : placeDropdown(anchor, panelHeight, viewportHeight);
2428
+ const placement = preferBelow ? placeBelowFirst(anchor, panelHeight, viewportHeight) : placeDropdown(anchor, panelHeight, viewportHeight);
2424
2429
  const base = { placement, left: anchor.x, width: anchor.width };
2425
2430
  if (placement === "above" /* Above */) return { ...base, bottom: viewportHeight - clampTop(anchor.y - panelHeight, panelHeight, viewportHeight) - panelHeight };
2426
2431
  return { ...base, top: clampTop(anchor.y + anchor.height, panelHeight, viewportHeight) };
@@ -2828,17 +2833,17 @@ var BOLD2 = 700;
2828
2833
  var ROW = { display: "flex" };
2829
2834
  var EMPTY = { width: DAY_CELL_PX, height: DAY_CELL_PX, flexShrink: 0 };
2830
2835
  var FALLBACK = { year: 1970, month: 1};
2831
- function MonthGrid({ month, selection, focusedDay, bounds, testID, onPick, onKey, showCaption = false, motion }) {
2836
+ function MonthGrid({ month, selection: selection2, focusedDay, bounds, testID, onPick, onKey, showCaption = false, motion }) {
2832
2837
  const { labels, locale, today, firstDay } = useDatePickerConfig();
2833
2838
  const colors = usePickerColors();
2834
2839
  const parts = parseIso(month) ?? FALLBACK;
2835
2840
  const weeks = useMemo(() => buildMonthGrid(parts.year, parts.month, firstDay), [parts.year, parts.month, firstDay]);
2836
2841
  const weekdays = useMemo(() => weekdayNames(locale, firstDay), [locale, firstDay]);
2837
2842
  const title = formatMonthYear(month, locale);
2838
- const end = selection.to === "" ? selection.from : selection.to;
2843
+ const end = selection2.to === "" ? selection2.from : selection2.to;
2839
2844
  const stateOf = (iso) => ({
2840
- inRange: selection.from !== "" && iso >= selection.from && iso <= end,
2841
- isEnd: iso === selection.from || iso === selection.to,
2845
+ inRange: selection2.from !== "" && iso >= selection2.from && iso <= end,
2846
+ isEnd: iso === selection2.from || iso === selection2.to,
2842
2847
  isToday: iso === today,
2843
2848
  enabled: isWithinBounds(iso, bounds.min, bounds.max),
2844
2849
  focusable: iso === focusedDay
@@ -2950,9 +2955,9 @@ function useReturnFocus(open, target) {
2950
2955
  return () => clearTimeout(timer);
2951
2956
  }, [open, target]);
2952
2957
  }
2953
- function useTypedDate({ value, onChange, bounds, format, onError, rangeMessage }) {
2958
+ function useTypedDate({ value, onChange, bounds, format, onError, rangeMessage, editOnType = false }) {
2954
2959
  const { labels, locale } = useDatePickerConfig();
2955
- const [focused, setFocused] = useState(false);
2960
+ const [editing, setEditing] = useState(false);
2956
2961
  const [text, setText] = useState("");
2957
2962
  useEffect(() => {
2958
2963
  setText((current) => {
@@ -2967,7 +2972,12 @@ function useTypedDate({ value, onChange, bounds, format, onError, rangeMessage }
2967
2972
  if (parsed === null) return labels.invalidDate;
2968
2973
  return isWithinBounds(parsed, bounds.min, bounds.max) ? void 0 : rangeMessage ?? labels.outOfRange;
2969
2974
  };
2975
+ const startEdit = () => {
2976
+ setEditing(true);
2977
+ setText(value === "" ? "" : formatTyped(value, locale));
2978
+ };
2970
2979
  const onText = (next) => {
2980
+ setEditing(true);
2971
2981
  setText(next);
2972
2982
  if (next.trim() === "") {
2973
2983
  onError?.(void 0);
@@ -2981,21 +2991,26 @@ function useTypedDate({ value, onChange, bounds, format, onError, rangeMessage }
2981
2991
  if (message === void 0) onChange(parsed);
2982
2992
  };
2983
2993
  const isReady = () => {
2984
- if (!focused) return true;
2994
+ if (!editing) return true;
2985
2995
  const message = verdict(text);
2986
2996
  onError?.(message);
2987
2997
  return message === void 0;
2988
2998
  };
2989
2999
  return {
2990
- display: focused ? text : value === "" ? "" : format(value, locale),
3000
+ display: editing ? text : value === "" ? "" : format(value, locale),
2991
3001
  onText,
2992
3002
  onFocus: () => {
2993
- setFocused(true);
2994
- setText(value === "" ? "" : formatTyped(value, locale));
3003
+ if (!editOnType) startEdit();
2995
3004
  },
2996
3005
  onBlur: () => {
2997
- setFocused(false);
2998
- 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;
2999
3014
  },
3000
3015
  isReady
3001
3016
  };
@@ -3011,6 +3026,51 @@ function closedFieldMinWidth(text, iconRoom) {
3011
3026
  const chars = Math.max(text.length, PLACEHOLDER_CHARS);
3012
3027
  return Math.ceil(chars * FONT_PX * GLYPH_EM) + PAD_INLINE + iconRoom + BORDER + BORDER;
3013
3028
  }
3029
+
3030
+ // src/DatePicker/utils/typedEdit.ts
3031
+ var DELETE_KEYS = /* @__PURE__ */ new Set(["Backspace", "Delete"]);
3032
+ function isPrintable(event) {
3033
+ return event.key.length === 1 && !event.ctrlKey && !event.metaKey;
3034
+ }
3035
+ function selection(input) {
3036
+ const end = input.selectionEnd ?? input.value.length;
3037
+ return [input.selectionStart ?? end, end];
3038
+ }
3039
+ function editedText(input, insert) {
3040
+ const [start, end] = selection(input);
3041
+ return { text: input.value.slice(0, start) + insert + input.value.slice(end), caret: start + insert.length };
3042
+ }
3043
+ function placeCaret(input, caret) {
3044
+ requestAnimationFrame(() => input.setSelectionRange(caret, caret));
3045
+ }
3046
+ function typedEditBind(typed) {
3047
+ const start = (event, insert) => {
3048
+ const input = event.currentTarget;
3049
+ const [from, to] = selection(input);
3050
+ if (from === to) {
3051
+ typed.onEdit();
3052
+ return;
3053
+ }
3054
+ const edit = editedText(input, insert);
3055
+ if (!typed.onEdit(edit.text)) return;
3056
+ event.preventDefault();
3057
+ placeCaret(input, edit.caret);
3058
+ };
3059
+ return {
3060
+ onEditKey: (event) => {
3061
+ if (isPrintable(event)) start(event, event.key);
3062
+ else if (DELETE_KEYS.has(event.key)) start(event, "");
3063
+ },
3064
+ onPaste: (event) => start(event, event.clipboardData.getData("text")),
3065
+ onCut: (event) => {
3066
+ const input = event.currentTarget;
3067
+ const [from, to] = selection(input);
3068
+ if (from === to) return;
3069
+ event.clipboardData.setData("text/plain", input.value.slice(from, to));
3070
+ start(event, "");
3071
+ }
3072
+ };
3073
+ }
3014
3074
  var SINGLE_PICKER_WIDTH = 340;
3015
3075
  var ICON_ROOM = 48;
3016
3076
  var ICON_SLOT = { position: "absolute", right: 2, top: 0, bottom: 0, display: "flex", alignItems: "center" };
@@ -3023,7 +3083,8 @@ function DatePickerInput({ fieldProps, context, onTypedError }) {
3023
3083
  const anchorRef = useRef(null);
3024
3084
  const inputRef = useRef(null);
3025
3085
  const bounds = { min, max };
3026
- const typed = useTypedDate({ value, onChange, bounds, format: formatDisplay, onError: onTypedError });
3086
+ const typed = useTypedDate({ value, onChange, bounds, format: formatDisplay, onError: onTypedError, editOnType: true });
3087
+ const edit = typedEditBind(typed);
3027
3088
  const { style, focusBind, hoverBind } = useThemedInput({ hasError: context.hasError, editable: !disabled });
3028
3089
  const focus = focusBind;
3029
3090
  const webA11y = webFieldA11y({ describedById: context.describedById, hasError: context.hasError, required });
@@ -3035,6 +3096,7 @@ function DatePickerInput({ fieldProps, context, onTypedError }) {
3035
3096
  close();
3036
3097
  };
3037
3098
  const onKeyDown = (event) => {
3099
+ edit.onEditKey(event);
3038
3100
  if (event.key === "ArrowDown" && event.altKey) {
3039
3101
  event.preventDefault();
3040
3102
  setOpen(true);
@@ -3069,7 +3131,9 @@ function DatePickerInput({ fieldProps, context, onTypedError }) {
3069
3131
  focus.onFocus?.(event);
3070
3132
  typed.onFocus();
3071
3133
  },
3072
- onKeyDown
3134
+ onKeyDown,
3135
+ onCut: edit.onCut,
3136
+ onPaste: edit.onPaste
3073
3137
  }
3074
3138
  ),
3075
3139
  /* @__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) }) }),
@@ -3293,8 +3357,8 @@ function WebDateRange({ value, onChange, onSubmit, fromLabel, toLabel, hint, tes
3293
3357
  const [toError, setToError] = useState(void 0);
3294
3358
  const anchorRef = useRef(null);
3295
3359
  const fromRef = useRef(null);
3296
- 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 });
3297
- 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 });
3360
+ 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 });
3361
+ 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 });
3298
3362
  const { style, focusBind, hoverBind } = useThemedInput({ hasError: context.hasError, editable: true });
3299
3363
  const focus = focusBind;
3300
3364
  const webA11y = webFieldA11y({ describedById: context.describedById, hasError: context.hasError });
@@ -3312,31 +3376,39 @@ function WebDateRange({ value, onChange, onSubmit, fromLabel, toLabel, hint, tes
3312
3376
  const ready = [from.isReady(), to.isReady()].every(Boolean);
3313
3377
  if (ready) onSubmit();
3314
3378
  };
3315
- const segment = (side, typed) => /* @__PURE__ */ jsx(
3316
- "input",
3317
- {
3318
- ...webA11y,
3319
- ref: side === "from" /* From */ ? fromRef : void 0,
3320
- "aria-label": side === "from" /* From */ ? fromLabel : toLabel,
3321
- autoComplete: "off",
3322
- "data-testid": `${testID}-${side}`,
3323
- id: side === "from" /* From */ ? context.controlId : void 0,
3324
- style: SEGMENT,
3325
- title: hint,
3326
- type: "text",
3327
- value: typed.display,
3328
- onBlur: (event) => {
3329
- focus.onBlur?.(event);
3330
- typed.onBlur();
3331
- },
3332
- onChange: (event) => typed.onText(event.target.value),
3333
- onFocus: (event) => {
3334
- focus.onFocus?.(event);
3335
- typed.onFocus();
3336
- },
3337
- onKeyDown
3338
- }
3339
- );
3379
+ const segment = (side, typed) => {
3380
+ const edit = typedEditBind(typed);
3381
+ return /* @__PURE__ */ jsx(
3382
+ "input",
3383
+ {
3384
+ ...webA11y,
3385
+ ref: side === "from" /* From */ ? fromRef : void 0,
3386
+ "aria-label": side === "from" /* From */ ? fromLabel : toLabel,
3387
+ autoComplete: "off",
3388
+ "data-testid": `${testID}-${side}`,
3389
+ id: side === "from" /* From */ ? context.controlId : void 0,
3390
+ style: SEGMENT,
3391
+ title: hint,
3392
+ type: "text",
3393
+ value: typed.display,
3394
+ onBlur: (event) => {
3395
+ focus.onBlur?.(event);
3396
+ typed.onBlur();
3397
+ },
3398
+ onChange: (event) => typed.onText(event.target.value),
3399
+ onFocus: (event) => {
3400
+ focus.onFocus?.(event);
3401
+ typed.onFocus();
3402
+ },
3403
+ onCut: edit.onCut,
3404
+ onKeyDown: (event) => {
3405
+ edit.onEditKey(event);
3406
+ onKeyDown(event);
3407
+ },
3408
+ onPaste: edit.onPaste
3409
+ }
3410
+ );
3411
+ };
3340
3412
  return /* @__PURE__ */ jsxs(View, { ref: anchorRef, style: anchorStyles.wrap, children: [
3341
3413
  /* @__PURE__ */ jsxs("div", { ...hoverBind, style: { ...buildNativeInputStyle(style), display: "flex", alignItems: "center", gap: GAP5, paddingInlineEnd: ICON_ROOM }, children: [
3342
3414
  segment("from" /* From */, from),
@@ -4916,15 +4988,15 @@ function rawCaretAfterChange(previous, previousCaret, next) {
4916
4988
 
4917
4989
  // src/IbanInput/hooks/useIbanCaret.ts
4918
4990
  function useIbanCaret(display) {
4919
- const [selection, setSelection] = useState({ start: display.length, end: display.length });
4991
+ const [selection2, setSelection] = useState({ start: display.length, end: display.length });
4920
4992
  const onSelectionChange = useCallback((event) => {
4921
4993
  setSelection(event.nativeEvent.selection);
4922
4994
  }, []);
4923
4995
  const placeAfterEdit = useCallback((text) => {
4924
- const caret = caretAfterEdit(text, rawCaretAfterChange(display, selection.end, text));
4996
+ const caret = caretAfterEdit(text, rawCaretAfterChange(display, selection2.end, text));
4925
4997
  setSelection({ start: caret, end: caret });
4926
- }, [display, selection.end]);
4927
- const clamped = { start: Math.min(selection.start, display.length), end: Math.min(selection.end, display.length) };
4998
+ }, [display, selection2.end]);
4999
+ const clamped = { start: Math.min(selection2.start, display.length), end: Math.min(selection2.end, display.length) };
4928
5000
  return { selection: clamped, onSelectionChange, placeAfterEdit };
4929
5001
  }
4930
5002
  var FONT_SIZE = 14;
@@ -5031,7 +5103,7 @@ function IbanInput({ value, onChange, onVerdict, accessibilityLabel, accessibili
5031
5103
  const { verdict, onEdit, onLeave } = useBlurVerdict(validate);
5032
5104
  const message = value === "" ? null : rejectionMessage(verdict?.rejection ?? null, "None" /* None */, rejectionLabels);
5033
5105
  const counter = ibanCounter(draft);
5034
- const { selection, onSelectionChange, placeAfterEdit } = useIbanCaret(draft.display);
5106
+ const { selection: selection2, onSelectionChange, placeAfterEdit } = useIbanCaret(draft.display);
5035
5107
  const onChangeText = useCallback((text) => {
5036
5108
  onEdit();
5037
5109
  onVerdict?.(null);
@@ -5060,7 +5132,7 @@ function IbanInput({ value, onChange, onVerdict, accessibilityLabel, accessibili
5060
5132
  autoCorrect: false,
5061
5133
  placeholder: draft.display === "" ? placeholder : void 0,
5062
5134
  placeholderTextColor: colors.textSecondary,
5063
- selection,
5135
+ selection: selection2,
5064
5136
  spellCheck: false,
5065
5137
  style: [ghostStyles.mono, ghostStyles.input, { color: colors.text }],
5066
5138
  testID,