@woodger/antd 0.1.3 → 0.1.4

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.
@@ -1,5 +1,5 @@
1
1
  // Connects the picker portal with the support DOM and transfers the keyboard focus when the panel is changed.
2
- import { tick, untrack } from 'svelte';
2
+ import { onDestroy, tick, untrack } from 'svelte';
3
3
  import { createPopupContainer } from '../popup/popup-container.model.svelte.js';
4
4
  import { getPopupActiveElement } from '../popup/popup.dom';
5
5
  import { createPopupCssMotion } from '../popup/popup-css-motion.model.svelte';
@@ -104,6 +104,9 @@ export function preservePickerPanelFocus(read) {
104
104
  export function createPickerPopup(getOptions, motionOptions) {
105
105
  let target = $state();
106
106
  let alignment;
107
+ let closeFrame;
108
+ let processingOutsideClose = $state(false);
109
+ onDestroy(cancelClose);
107
110
  const motion = createPopupCssMotion({
108
111
  get visible() { return motionOptions.open; },
109
112
  get hidden() { return !motionOptions.shown; },
@@ -117,6 +120,35 @@ export function createPickerPopup(getOptions, motionOptions) {
117
120
  || undefined);
118
121
  const style = createOverlayStyle(() => [popupStyle]);
119
122
  let styleNode;
123
+ /** A confirmed action or a return of focus supersedes a pending outside close. */
124
+ function cancelClose() {
125
+ if (closeFrame) {
126
+ closeFrame.view.cancelAnimationFrame(closeFrame.id);
127
+ closeFrame = undefined;
128
+ }
129
+ }
130
+ /** Coalesces outside events using the connected anchor while the owner can be unmounting. */
131
+ function scheduleClose(root, close) {
132
+ const view = root?.ownerDocument.defaultView;
133
+ if (!root?.isConnected || !view) {
134
+ return;
135
+ }
136
+ cancelClose();
137
+ const id = view.requestAnimationFrame(() => {
138
+ closeFrame = undefined;
139
+ if (!root.isConnected)
140
+ return;
141
+ // An owner can flush its updates or remove the picker inside the close callback.
142
+ processingOutsideClose = true;
143
+ try {
144
+ close();
145
+ }
146
+ finally {
147
+ processingOutsideClose = false;
148
+ }
149
+ });
150
+ closeFrame = { view, id };
151
+ }
120
152
  /** The standard slide names share the projected plane with the popup alignment. */
121
153
  function getSlide() {
122
154
  const { name, rootPrefix } = motionOptions;
@@ -185,9 +217,10 @@ export function createPickerPopup(getOptions, motionOptions) {
185
217
  };
186
218
  }
187
219
  return {
188
- portal, connect, attachStyle, attachMotion: motion.attach,
220
+ portal, connect, attachStyle, attachMotion: motion.attach, scheduleClose, cancelClose,
189
221
  get style() { return popupStyle; },
190
222
  get target() { return target; },
223
+ get processingOutsideClose() { return processingOutsideClose; },
191
224
  get frame() { return frame; },
192
225
  get present() { return present; }
193
226
  };
@@ -91,7 +91,8 @@ export interface TimePickerProps<Mode extends DatePickerMode = 'time', DateType
91
91
  cellRender?: Snippet<[number | 'am' | 'pm', TimePickerCellRenderInfo<DateType>]>;
92
92
  /** Receives snippet the standard management panel, saving an external popup lifecycle. */
93
93
  panelRender?: Snippet<[Snippet]>;
94
- renderExtraFooter?: [Mode] extends ['time'] ? Snippet : Snippet<[Mode]>;
94
+ /** The snippet receives the current panel mode. It can omit the parameter. */
95
+ renderExtraFooter?: Snippet<[Mode]>;
95
96
  presets?: readonly TimePickerPreset<DateType>[];
96
97
  onclick?: HTMLAttributes<HTMLDivElement>['onclick'];
97
98
  /** Compatible alias; onclick has priority. */
@@ -235,6 +235,7 @@
235
235
  let focused = $state(false);
236
236
  let committedOpenChange = untrack(() => onOpenChange);
237
237
  let closeRequestRevision = 0;
238
+ let clearingSelection = false;
238
239
  let committedBlur = untrack(() => onblur);
239
240
  let committedPublicBlur = untrack(() => onBlur);
240
241
  $effect(() => {
@@ -963,7 +964,7 @@
963
964
  const state = committedBlurState;
964
965
  const disabled = state.componentInput ? componentInputBlurDisabled : state.disabled;
965
966
  if (state.tagMode) {
966
- submitTag(state.search);
967
+ if (!clearingSelection) submitTag(state.search);
967
968
  }
968
969
  else if (state.multiple) internalSearch = '';
969
970
  if (state.expanded && !disabled && !(event.relatedTarget instanceof Node && list?.contains(event.relatedTarget)))
@@ -1248,7 +1249,7 @@
1248
1249
  internalSearch = '';
1249
1250
  }
1250
1251
  }
1251
- if (!next && state.expanded !== next) state.clearSearch?.('');
1252
+ if (!next && state.expanded !== next && !clearingSelection) state.clearSearch?.('');
1252
1253
  return;
1253
1254
  }
1254
1255
  if (next && resolvedDisabled) {
@@ -1256,16 +1257,21 @@
1256
1257
  }
1257
1258
 
1258
1259
  const notify = (previousOpen ?? expanded) !== next;
1259
- const clearContextSearch = notify && !next && !multiple && Boolean(search)
1260
+ const clearContextSearch = notify && !next && !clearingSelection && !multiple && Boolean(search)
1260
1261
  && (controlledSearch === undefined || showSearch == null && typeof config.select?.showSearch === 'object')
1261
1262
  ? searchCallback : undefined;
1262
1263
  const clearInternalSearch = !next && !tagMode && (!multiple || autoClearSearch);
1264
+ const notifyOpen = notify ? onOpenChange : undefined;
1263
1265
  internalOpen = next;
1264
1266
  open = next;
1265
- // DOM removal fires blur before cleanup, when owner props are already disposed.
1266
- if (notify) (fromBlur ? committedOpenChange : onOpenChange)?.(next);
1267
+ notifyOpen?.(next);
1268
+ if (componentDestroyed) {
1269
+ clearContextSearch?.('');
1270
+ return;
1271
+ }
1272
+
1267
1273
  // An accepted binding or callback retains the active item chosen by the current keyboard event.
1268
- if (!fromBlur && providedOpen === next) previousOwnerOpen = next;
1274
+ if (providedOpen === next) previousOwnerOpen = next;
1269
1275
 
1270
1276
  if (clearInternalSearch) {
1271
1277
  if (inputSlot) {
@@ -1406,37 +1412,73 @@
1406
1412
  if (resolvedDisabled) {
1407
1413
  return;
1408
1414
  }
1409
- const previousSearch = search;
1410
- const blockedSearch = countReached;
1411
- const preserveSearch = !multiple && controlledSearch !== undefined && Boolean(selected);
1412
- onClear?.();
1415
+
1416
+ const wasMultiple = multiple;
1417
+ const hadSelection = wasMultiple ? selectedValues.length > 0 : selectedRaw !== undefined;
1418
+ const preserveSearch = !wasMultiple && controlledSearch !== undefined && Boolean(selected);
1419
+ const clearQuery = !countReached && !preserveSearch;
1420
+ // Clear, value change and search use the snapshot from the start of the action.
1421
+ const cleared: [] | undefined = wasMultiple ? [] : undefined;
1422
+ const notifyValue = onValueChange as ((value: typeof cleared) => void) | undefined;
1423
+ const notifyChange = onChange as
1424
+ ((value: typeof cleared, options: typeof cleared) => void) | undefined;
1425
+ const notifySearch = clearQuery && search ? searchCallback : undefined;
1426
+ const previousCloseRevision = closeRequestRevision;
1427
+
1428
+ // An owner can remove the focused editor. Blur can close it without submitting its draft.
1429
+ const previousClear = clearingSelection;
1430
+ clearingSelection = true;
1431
+ try {
1432
+ onClear?.();
1433
+ focusAfterClear();
1434
+
1435
+ if (!wasMultiple || hadSelection) {
1436
+ if (!componentDestroyed) {
1437
+ useDefault = false;
1438
+ rememberSelection([]);
1439
+ }
1440
+ // The binding still receives the result of this action after a synchronous removal.
1441
+ value = cleared as typeof value;
1442
+ }
1443
+ if (hadSelection) {
1444
+ notifyValue?.(cleared);
1445
+ notifyChange?.(cleared, wasMultiple ? [] : undefined);
1446
+ }
1447
+
1448
+ if (clearQuery) {
1449
+ if (!componentDestroyed) internalSearch = '';
1450
+ notifySearch?.('');
1451
+ }
1452
+ if (!wasMultiple) finishSingleClear(previousCloseRevision);
1453
+ }
1454
+ finally {
1455
+ clearingSelection = previousClear;
1456
+ }
1457
+ }
1458
+
1459
+ function focusAfterClear() {
1460
+ if (componentDestroyed) {
1461
+ return;
1462
+ }
1413
1463
  if (rootSlot || inputSlot) {
1414
1464
  focus();
1415
1465
  }
1416
1466
  else {
1417
1467
  control?.focus({ preventScroll: true });
1418
1468
  }
1419
- if (multiple) {
1420
- if (selectedValues.length) changeMultiple([], 'display');
1421
- }
1422
- else {
1423
- const changed = selectedRaw !== undefined;
1424
- useDefault = false;
1425
- rememberSelection([]);
1426
- value = undefined;
1427
- if (changed) {
1428
- (onValueChange as ((value: undefined) => void) | undefined)?.(undefined);
1429
- (onChange as ((value: undefined, option: undefined) => void) | undefined)?.(
1430
- undefined,
1431
- undefined
1432
- );
1433
- }
1469
+ }
1470
+
1471
+ function finishSingleClear(previousCloseRevision: number) {
1472
+ if (!componentDestroyed) {
1473
+ setOpen(false);
1474
+ return;
1434
1475
  }
1435
- if (!blockedSearch && !preserveSearch) {
1436
- internalSearch = '';
1437
- if (previousSearch) searchCallback?.('');
1476
+
1477
+ // The native editor may have published its close from blur during removal.
1478
+ if (closeRequestRevision === previousCloseRevision) {
1479
+ open = false;
1480
+ if (committedBlurState.expanded) committedOpenChange?.(false);
1438
1481
  }
1439
- if (!multiple) setOpen(false);
1440
1482
  }
1441
1483
 
1442
1484
  const clearPresses = new WeakSet<MouseEvent>();
@@ -1747,14 +1789,13 @@
1747
1789
  if (inputSlot && rawTextarea && !expanded && ['ArrowDown', 'ArrowUp', 'ArrowLeft', 'ArrowRight'].includes(event.key)) {
1748
1790
  return;
1749
1791
  }
1750
- // Source valid-open keys invalidate a weak close even when already open.
1751
- // Preserve its legacy which filter and modifier exclusions.
1752
- const keyCode = event.which;
1753
- if (inputSlot && componentCloseChannel && expanded && keyCode
1754
- && !event.ctrlKey && !event.altKey && !event.metaKey
1755
- && ![27, 16, 8, 9, 224, 18, 91, 92, 17, 186, 187, 20, 93, 38, 37, 39].includes(keyCode)
1756
- && !(keyCode >= 112 && keyCode <= 123)) {
1757
- cancelComponentClose();
1792
+ if (opensFromInputKey(event)) {
1793
+ // A valid key can cancel a pending close and opens an editable field before input arrives.
1794
+ if (inputSlot && componentCloseChannel && expanded) cancelComponentClose();
1795
+ if (!expanded && canSearch && event.key !== 'Enter' && event.key !== 'ArrowDown') {
1796
+ setOpen(true);
1797
+ return;
1798
+ }
1758
1799
  }
1759
1800
  if (expanded && event.cancelBubble) {
1760
1801
  return;
@@ -1762,6 +1803,13 @@
1762
1803
  handleKeyboardNavigation(event);
1763
1804
  }
1764
1805
 
1806
+ function opensFromInputKey(event: KeyboardEvent) {
1807
+ const keyCode = event.which;
1808
+ return Boolean(keyCode) && !event.ctrlKey && !event.altKey && !event.metaKey
1809
+ && ![27, 16, 8, 9, 224, 18, 91, 92, 17, 186, 187, 20, 93, 38, 37, 39].includes(keyCode)
1810
+ && !(keyCode >= 112 && keyCode <= 123);
1811
+ }
1812
+
1765
1813
  function handleKeyboardNavigation(event: KeyboardEvent) {
1766
1814
  if (expanded && customPopup && !menuElement && (['ArrowDown', 'ArrowUp', 'Home', 'End',
1767
1815
  'Enter', 'Escape', 'Tab'].includes(event.key) || event.key === ' ' && !canSearch)) {
@@ -1851,9 +1899,21 @@
1851
1899
  event: InputEvent & { currentTarget: EventTarget & HTMLInputElement; }
1852
1900
  ) {
1853
1901
  onbeforeinput?.(event);
1902
+ if (event.defaultPrevented || componentDestroyed) {
1903
+ return;
1904
+ }
1854
1905
 
1855
- if (multiple && !canSearch)
1856
- event.preventDefault();
1906
+ if (multiple) {
1907
+ if (!canSearch) event.preventDefault();
1908
+ return;
1909
+ }
1910
+
1911
+ // The closed native editor displays the selection. An insertion starts a separate query.
1912
+ if (!expanded && canSearch && event.inputType?.startsWith('insert')) {
1913
+ const input = event.currentTarget;
1914
+ const label = selectedText;
1915
+ if (label && input.value === label) input.setSelectionRange(0, label.length);
1916
+ }
1857
1917
  }
1858
1918
 
1859
1919
  function restoreControlledSearch(input: HTMLInputElement) {
@@ -1905,14 +1965,22 @@
1905
1965
  return;
1906
1966
  }
1907
1967
  }
1908
- if (compositionEnd && next === search) {
1968
+ const previousSearch = search;
1969
+ if (compositionEnd && next === previousSearch) {
1909
1970
  return;
1910
1971
  }
1911
1972
 
1912
1973
  internalSearch = next;
1913
- searchCallback?.(next);
1974
+ if (next !== previousSearch) searchCallback?.(next);
1975
+
1976
+ if (componentDestroyed) {
1977
+ // The source can finish the open request after onSearch removes the field.
1978
+ if (next && !committedBlurState.expanded) committedOpenChange?.(true);
1979
+ return;
1980
+ }
1981
+
1914
1982
  restoreControlledSearch(input);
1915
- setOpen(true);
1983
+ if (next) setOpen(true);
1916
1984
  }
1917
1985
 
1918
1986
  function handleInput(event: Event & { currentTarget: EventTarget & HTMLInputElement; }) {
@@ -2011,7 +2079,7 @@
2011
2079
 
2012
2080
  if (!(next instanceof Node && list?.contains(next))) {
2013
2081
  if (state.tagMode) {
2014
- submitTag(state.search);
2082
+ if (!clearingSelection) submitTag(state.search);
2015
2083
  }
2016
2084
  else if (state.multiple) internalSearch = '';
2017
2085
  setOpen(false, true);
@@ -2030,7 +2098,7 @@
2030
2098
  focused = false;
2031
2099
  const state = committedBlurState;
2032
2100
  if (state.tagMode) {
2033
- submitTag(state.search);
2101
+ if (!clearingSelection) submitTag(state.search);
2034
2102
  }
2035
2103
  else if (state.multiple) internalSearch = '';
2036
2104
  setOpen(false, true);