@dloizides/ui-forms 1.42.1 → 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
@@ -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) };
@@ -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,31 @@ 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 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
+ }
3014
3054
  var SINGLE_PICKER_WIDTH = 340;
3015
3055
  var ICON_ROOM = 48;
3016
3056
  var ICON_SLOT = { position: "absolute", right: 2, top: 0, bottom: 0, display: "flex", alignItems: "center" };
@@ -3023,7 +3063,8 @@ function DatePickerInput({ fieldProps, context, onTypedError }) {
3023
3063
  const anchorRef = useRef(null);
3024
3064
  const inputRef = useRef(null);
3025
3065
  const bounds = { min, max };
3026
- 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);
3027
3068
  const { style, focusBind, hoverBind } = useThemedInput({ hasError: context.hasError, editable: !disabled });
3028
3069
  const focus = focusBind;
3029
3070
  const webA11y = webFieldA11y({ describedById: context.describedById, hasError: context.hasError, required });
@@ -3035,6 +3076,7 @@ function DatePickerInput({ fieldProps, context, onTypedError }) {
3035
3076
  close();
3036
3077
  };
3037
3078
  const onKeyDown = (event) => {
3079
+ edit.onEditKey(event);
3038
3080
  if (event.key === "ArrowDown" && event.altKey) {
3039
3081
  event.preventDefault();
3040
3082
  setOpen(true);
@@ -3069,7 +3111,9 @@ function DatePickerInput({ fieldProps, context, onTypedError }) {
3069
3111
  focus.onFocus?.(event);
3070
3112
  typed.onFocus();
3071
3113
  },
3072
- onKeyDown
3114
+ onKeyDown,
3115
+ onCut: edit.onCut,
3116
+ onPaste: edit.onPaste
3073
3117
  }
3074
3118
  ),
3075
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) }) }),
@@ -3293,8 +3337,8 @@ function WebDateRange({ value, onChange, onSubmit, fromLabel, toLabel, hint, tes
3293
3337
  const [toError, setToError] = useState(void 0);
3294
3338
  const anchorRef = useRef(null);
3295
3339
  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 });
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 });
3298
3342
  const { style, focusBind, hoverBind } = useThemedInput({ hasError: context.hasError, editable: true });
3299
3343
  const focus = focusBind;
3300
3344
  const webA11y = webFieldA11y({ describedById: context.describedById, hasError: context.hasError });
@@ -3334,7 +3378,12 @@ function WebDateRange({ value, onChange, onSubmit, fromLabel, toLabel, hint, tes
3334
3378
  focus.onFocus?.(event);
3335
3379
  typed.onFocus();
3336
3380
  },
3337
- onKeyDown
3381
+ onCut: typedEditBind(typed).onCut,
3382
+ onKeyDown: (event) => {
3383
+ typedEditBind(typed).onEditKey(event);
3384
+ onKeyDown(event);
3385
+ },
3386
+ onPaste: typedEditBind(typed).onPaste
3338
3387
  }
3339
3388
  );
3340
3389
  return /* @__PURE__ */ jsxs(View, { ref: anchorRef, style: anchorStyles.wrap, children: [