@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.
@@ -19,8 +19,9 @@
19
19
  import { normalizePickerFieldFormat } from './picker-mask.engine';
20
20
  import { createPickerMaskInput, type PickerMaskSharedKey } from './picker-mask-input.model.svelte.js';
21
21
  import {
22
- createPickerEditor, isPickerInput, isPickerInputEvent,
23
- pickerEditorMaskEvents, type PickerEditorEvent
22
+ createPickerEditor, isPickerInput, isPickerInputEvent, pickerEditorMaskEvents,
23
+ pickerEditorServerAutofocus, pickerEditorSize, pickerInputNeedsHelp, usePickerEditorAutofocus,
24
+ type PickerEditorEvent
24
25
  } from './picker-editor.model.svelte.js';
25
26
  import type { DatePickerEditorAttributes, DatePickerPanelContext } from './picker-components.types';
26
27
  import { createPickerPanelEngine, pickerPanelClock, pickerPanelSlot } from './picker-panel.engine';
@@ -117,6 +118,9 @@
117
118
  needConfirm,
118
119
  showNow,
119
120
  allowClear,
121
+ autoFocus,
122
+ tabIndex,
123
+ autoComplete,
120
124
  inputReadOnly,
121
125
  preserveInvalidOnBlur = false,
122
126
  onChange,
@@ -235,6 +239,9 @@
235
239
  onDestroy(() => { destroyed = true; });
236
240
  const panelUpdates = createPickerPanelUpdates(() => !destroyed);
237
241
  let visible = $derived((open ?? internalOpen) && !blocked.every(Boolean));
242
+ let committedOpenRequest = untrack(captureOpenRequest);
243
+ $effect(() => { committedOpenRequest = captureOpenRequest(); });
244
+
238
245
  let lang = $derived(
239
246
  { ...datePickerEnUS.lang, ...config.locale.datePicker?.lang, ...locale?.lang }
240
247
  );
@@ -248,11 +255,9 @@
248
255
  let committedBlur = untrack(captureEditorBlur);
249
256
  $effect(() => { committedBlur = captureEditorBlur(); });
250
257
  let maskFormat = $derived(normalizePickerFieldFormat<DateType>(time ? time.format : format, '').mask);
251
- let readOnly = $derived(
252
- time && typeof formats[0] === 'function'
253
- || (inputReadOnly ?? (attributes.readonly
254
- || formats.every(item => typeof item === 'function')))
255
- );
258
+ let firstFormat = $derived(formats[0]);
259
+ let readOnly = $derived(typeof firstFormat === 'function'
260
+ || (inputReadOnly ?? (attributes.readonly || formats.length === 0)));
256
261
  let calendar = $state.raw<RangePickerValue<DateType>>(
257
262
  untrack(() => value === undefined ? [...initial] : rangePair(value))
258
263
  );
@@ -321,6 +326,9 @@
321
326
  ));
322
327
  let startElement = $derived(components?.input ? customInputs[0].element : nativeStart);
323
328
  let endElement = $derived(components?.input ? customInputs[1].element : nativeEnd);
329
+ let firstEnabledEditor = $derived(blocked.findIndex(item => !item));
330
+ usePickerEditorAutofocus(() => startElement, () => editorAutofocus(0));
331
+ usePickerEditorAutofocus(() => endElement, () => editorAutofocus(1));
324
332
  let popup = $state<HTMLDivElement>();
325
333
  let popupWrapper = $state<HTMLDivElement>();
326
334
  let panelContainer = $state<HTMLDivElement>();
@@ -404,7 +412,7 @@
404
412
  readOnly, preserveInvalidOnBlur,
405
413
  validateText: next => !!parseFieldInput(next, calendar[index]),
406
414
  acceptInput: next => inputChange(index, next, true),
407
- onHelp: () => requestOpen(true)
415
+ onHelp: completeEditorOpen
408
416
  })));
409
417
  const maskEvents = maskInputs.map(mask => pickerEditorMaskEvents(mask, () => attributes));
410
418
  $effect(() => { for (const input of maskInputs) input.sync(); });
@@ -469,13 +477,7 @@
469
477
  let suffixContent = $derived(time
470
478
  ? time.suffixIcon === undefined ? config.timePicker?.suffixIcon : time.suffixIcon
471
479
  : suffixIcon !== undefined ? suffixIcon : config.datePicker?.suffixIcon);
472
- let timeInputSize = $derived.by(() => {
473
- if (!time) {
474
- return undefined;
475
- }
476
- const first = formats[0];
477
- return Math.max(8, typeof first === 'function' ? first(now).length : first.length) + 2;
478
- });
480
+ let editorSize = $derived(pickerEditorSize(firstFormat, Boolean(time), () => adapter.getNow()));
479
481
  let previousVisible = untrack(() => visible);
480
482
  let previousCommitted = untrack(() => committed);
481
483
  let previousDateValue = untrack(() => value);
@@ -592,10 +594,13 @@
592
594
  }
593
595
  });
594
596
  $effect(() => {
597
+ if (container.processingOutsideClose) {
598
+ return;
599
+ }
595
600
  const next = visible;
596
601
  if (previousVisible && !next) {
597
602
  untrack(() => send(interaction.currentIndex ?? active, 'popupClose'));
598
- if (!time && !preserveInvalidOnBlur)
603
+ if (!destroyed && !time && !preserveInvalidOnBlur)
599
604
  texts = dateTexts(calendar);
600
605
  }
601
606
  previousVisible = next;
@@ -699,11 +704,16 @@
699
704
  shown = next;
700
705
  },
701
706
  onDismiss: reason => {
702
- if (reason === 'escape') {
703
- send(active, 'esc');
707
+ if (reason === 'outside') {
708
+ container.scheduleClose(anchor, () => requestOpen(false));
709
+ return;
710
+ }
711
+
712
+ send(active, 'esc');
713
+ if (!destroyed) {
704
714
  inputs()[active]?.focus();
715
+ requestOpen(false);
705
716
  }
706
- requestOpen(false);
707
717
  }
708
718
  }));
709
719
  });
@@ -839,6 +849,8 @@
839
849
  disabledDate: (date, info) => Boolean(disabledDate?.(date, info)),
840
850
  disabledTime: (date, part, info) => disabledTime?.(date, part, info) ?? {}
841
851
  };
852
+ let committedInputDraft = untrack(captureInputDraft);
853
+ $effect(() => { committedInputDraft = captureInputDraft(); });
842
854
 
843
855
  function invalidCalendarDate(
844
856
  date: DateType,
@@ -951,6 +963,7 @@
951
963
  return passed;
952
964
  }
953
965
  function requestOpen(next: boolean, opened?: boolean) {
966
+ container.cancelClose();
954
967
  if (next && blocked.every(Boolean) || next === (opened ?? visible)) {
955
968
  return;
956
969
  }
@@ -967,6 +980,24 @@
967
980
  publishRangeAction(() => onOpenChange?.(next));
968
981
  }
969
982
 
983
+ /** Retains the committed visibility and handler if an action removes its endpoints. */
984
+ function captureOpenRequest() {
985
+ return {
986
+ allowed: !blocked.every(Boolean), wasOpen: visible, notify: onOpenChange?.bind(callbackOwner)
987
+ };
988
+ }
989
+
990
+ /** Completes an opening requested by a click or edit, including after native removal. */
991
+ function completeEditorOpen() {
992
+ if (!destroyed) {
993
+ requestOpen(true);
994
+ return;
995
+ }
996
+
997
+ const { allowed, wasOpen, notify } = committedOpenRequest;
998
+ if (allowed && !wasOpen) notify?.(true);
999
+ }
1000
+
970
1001
  /** Records the applicable close request before an owner callback can remove the field. */
971
1002
  function captureClose(opened?: boolean) {
972
1003
  const notify = onOpenChange?.bind(callbackOwner);
@@ -1167,6 +1198,9 @@
1167
1198
  }
1168
1199
 
1169
1200
  requestOpen(true);
1201
+ if (destroyed) {
1202
+ return;
1203
+ }
1170
1204
  void tick().then(() => {
1171
1205
  const root = rootElement;
1172
1206
  if (!root?.isConnected || interaction.currentIndex !== target) {
@@ -1181,16 +1215,19 @@
1181
1215
  }
1182
1216
  });
1183
1217
  }
1184
- function focusField(index: number, suppressOpen = false) {
1218
+ function focusField(index: number) {
1185
1219
  focused = true;
1186
1220
  applyRangeAction(index, 'field-switch');
1221
+ if (destroyed) {
1222
+ return;
1223
+ }
1224
+
1187
1225
  // Receiving focus at the requested field must not request that focus again.
1188
- if (!suppressOpen || interaction.currentIndex !== index) {
1226
+ if (interaction.currentIndex !== index) {
1189
1227
  focusNextRangeField();
1190
1228
  }
1191
- if (!time && !suppressOpen) requestOpen(true);
1192
1229
  const accepted = interaction.currentIndex;
1193
- if (accepted !== null && accepted !== index) {
1230
+ if (!destroyed && accepted !== null && accepted !== index) {
1194
1231
  void tick().then(() => {
1195
1232
  if (rootElement?.isConnected && visible
1196
1233
  && inputs()[index]?.ownerDocument.activeElement === inputs()[index]) {
@@ -1203,10 +1240,8 @@
1203
1240
  index: number,
1204
1241
  event: PickerEditorEvent<FocusEvent>
1205
1242
  ) {
1206
- if (!preserveInvalidOnBlur && (time ? rawInvalid[index] : inputInvalid(index))) {
1207
- restoreEditorText(index);
1208
- if (time) clearClockInputError(index);
1209
- }
1243
+ if (isPickerFocus(event.relatedTarget)) restoreInvalidEditorText(index);
1244
+
1210
1245
  if (time) {
1211
1246
  (time.onblur ?? time.onBlur)?.(event, { range: index === 1 ? 'end' : 'start' });
1212
1247
  }
@@ -1219,6 +1254,19 @@
1219
1254
  blurWithin(event);
1220
1255
  }
1221
1256
 
1257
+ /** Restores a pending edit once that field has lost its focus. */
1258
+ function restoreInvalidEditorText(index: number) {
1259
+ if (preserveInvalidOnBlur) {
1260
+ return;
1261
+ }
1262
+
1263
+ const erased = texts[index] === '' && calendar[index] !== null;
1264
+ if (erased || (time ? rawInvalid[index] : inputInvalid(index))) {
1265
+ restoreEditorText(index);
1266
+ if (time) clearClockInputError(index);
1267
+ }
1268
+ }
1269
+
1222
1270
  function restoreEditorText(index: number, settings?: RangeTextSettings) {
1223
1271
  const copy: [string, string] = [...texts];
1224
1272
  copy[index] = formatPicker(calendar[index], settings?.locale ?? lang.locale,
@@ -1232,16 +1280,18 @@
1232
1280
  rawInvalid = errors;
1233
1281
  }
1234
1282
 
1283
+ /** A portaled panel and the two editors share the focus boundary. */
1284
+ function isPickerFocus(target: EventTarget | null) {
1285
+ return target instanceof Node && (rootElement?.contains(target) || popup?.contains(target));
1286
+ }
1287
+
1235
1288
  function blurWithin(event: FocusEvent) {
1236
- if (event.relatedTarget instanceof Node
1237
- && (rootElement?.contains(event.relatedTarget)
1238
- || popup?.contains(
1239
- event.relatedTarget
1240
- ))) {
1289
+ if (destroyed || isPickerFocus(event.relatedTarget)) {
1241
1290
  return;
1242
1291
  }
1243
1292
  const authoredControl = pickerBlurredAuthoredControl(event, popup);
1244
- queueMicrotask(() => {
1293
+ const blurredEditor = event.target;
1294
+ container.scheduleClose(rootElement, () => {
1245
1295
  // Blur on field removal can finish after the derived refs have been disposed.
1246
1296
  if (destroyed) return;
1247
1297
  const root = rootElement;
@@ -1260,8 +1310,11 @@
1260
1310
  return;
1261
1311
  }
1262
1312
  focused = false;
1313
+ const blurredIndex = inputs().findIndex(editor => editor === blurredEditor);
1314
+ if (blurredIndex >= 0) restoreInvalidEditorText(blurredIndex);
1315
+
1263
1316
  field?.onBlur();
1264
- requestOpen(false);
1317
+ if (!destroyed) requestOpen(false);
1265
1318
  });
1266
1319
  }
1267
1320
  /** Retains the last mounted mode handler if a preceding callback removes the range. */
@@ -1528,26 +1581,87 @@
1528
1581
  }
1529
1582
  };
1530
1583
  }
1584
+ /** Retains the format that belongs to the editor event before an open callback updates it. */
1585
+ function inputParser() {
1586
+ const parse = time ? parseTimeInput ?? parseAdapterTimeRangeInput : parsePicker;
1587
+ return { parse, locale: lang.locale, formats };
1588
+ }
1589
+
1531
1590
  // Typing, masking, and confirmation use the date of the same edited endpoint.
1532
- function parseFieldInput(text: string, boundary: DateType | null): DateType | null {
1533
- if (time) {
1534
- const parse = parseTimeInput ?? parseAdapterTimeRangeInput;
1535
- return parse(text, lang.locale, formats, boundary);
1591
+ function parseFieldInput(
1592
+ text: string, boundary: DateType | null, input = inputParser()
1593
+ ): DateType | null {
1594
+ const { parse, locale, formats } = input;
1595
+ return parse(text, locale, formats, boundary);
1596
+ }
1597
+
1598
+ function inputReference() {
1599
+ return picker === 'date' || picker === 'quarter' || picker === 'week' ? undefined : from();
1600
+ }
1601
+
1602
+ /** Retains the active endpoint, date rules, and publication from the last rendered range. */
1603
+ function captureInputDraft() {
1604
+ void revision;
1605
+ const clock = Boolean(time);
1606
+ return {
1607
+ values: calendar,
1608
+ endpoint: interaction.currentIndex,
1609
+ blocked,
1610
+ picker,
1611
+ compare: clock ? timePickerSameValue : dates.sameValue,
1612
+ locale: lang.locale,
1613
+ formats,
1614
+ rules: clock ? undefined : retainRangeRestrictions().calendar,
1615
+ reference: clock ? undefined : inputReference(),
1616
+ notify: onCalendarChange?.bind(callbackOwner)
1617
+ };
1618
+ }
1619
+
1620
+ /** An input that started opening can still report its draft after the owner removes it. */
1621
+ function completeRemovedInputDraft(index: number, next: DateType | null) {
1622
+ const state = committedInputDraft;
1623
+ if (!next || state.endpoint !== null && state.endpoint !== index
1624
+ || state.compare(state.values[index], next)) {
1625
+ return;
1626
+ }
1627
+ if (state.rules && (
1628
+ crossesLockedBoundary(next, index, state.picker,
1629
+ state.values[index ^ 1], state.blocked[index ^ 1], state.locale)
1630
+ || invalidCalendarDate(next, index, state.reference, state.picker,
1631
+ state.picker === 'date', state.rules)
1632
+ )) {
1633
+ return;
1536
1634
  }
1537
1635
 
1538
- return parsePicker(text, lang.locale, formats);
1636
+ const pair: RangePickerValue<DateType> = [...state.values];
1637
+ pair[index] = next;
1638
+ state.notify?.(pair, dateTexts(pair, state), { range: index === 0 ? 'start' : 'end' });
1539
1639
  }
1540
1640
 
1541
1641
  function inputChange(index: number, text: string, masked = false) {
1542
- focusField(index, masked);
1543
- if (time && !masked && text && text !== formatPicker(calendar[index], lang.locale, formats))
1544
- requestOpen(true);
1545
- const copy: [string, string] = [...texts];
1546
- copy[index] = text;
1547
- texts = copy;
1642
+ if (destroyed) {
1643
+ return;
1644
+ }
1645
+
1646
+ const input = inputParser();
1647
+ const needsPanel = !masked && !visible && text !== '' && pickerInputNeedsHelp(
1648
+ text, formatPicker(calendar[index], input.locale, input.formats)
1649
+ );
1650
+ focusField(index);
1651
+ if (needsPanel) completeEditorOpen();
1652
+ if (!destroyed) {
1653
+ const copy: [string, string] = [...texts];
1654
+ copy[index] = text;
1655
+ texts = copy;
1656
+ }
1657
+
1658
+ const current = (destroyed ? committedInputDraft.values : calendar)[index];
1659
+ const parsed = text ? parseFieldInput(text, current, input) : null;
1660
+ if (destroyed) {
1661
+ completeRemovedInputDraft(index, parsed);
1662
+ return;
1663
+ }
1548
1664
 
1549
- const current = calendar[index];
1550
- const parsed = text ? parseFieldInput(text, current) : null;
1551
1665
  const next = !time && picker === 'date' && parsed && current
1552
1666
  && timePickerSameValue(parsed, current) ? current : parsed;
1553
1667
  if (!time && (picker === 'date' || picker === 'quarter' || picker === 'week')) {
@@ -1562,11 +1676,10 @@
1562
1676
  send(index, 'input', next ?? undefined);
1563
1677
  return;
1564
1678
  }
1565
- const reference = picker === 'date' || picker === 'quarter' || picker === 'week'
1566
- ? undefined : from();
1567
- if (!text || next && !invalid(next, index, reference, picker, picker === 'date')) {
1679
+ const reference = inputReference();
1680
+ if (next && !invalid(next, index, reference, picker, picker === 'date')) {
1568
1681
  send(index, 'input', next);
1569
- if (next && picker === 'date') {
1682
+ if (!destroyed && picker === 'date') {
1570
1683
  const defaultPanel = endpointPanelDate(defaultPickerValue, index);
1571
1684
  if (!defaultPanel) syncPanel(index, 'reset', false, next);
1572
1685
  }
@@ -1622,17 +1735,31 @@
1622
1735
  }
1623
1736
  }
1624
1737
  function editorFocus(index: number, event: PickerEditorEvent<FocusEvent>) {
1738
+ container.cancelClose();
1625
1739
  maskInputs[index].focus();
1626
- focusField(index, true);
1627
- if (time) {
1628
- (time.onfocus ?? time.onFocus)?.(event, { range: index === 0 ? 'start' : 'end' });
1740
+ focusField(index);
1741
+ if (destroyed) {
1742
+ return;
1629
1743
  }
1630
- else {
1744
+
1745
+ const info: RangePickerInfo = { range: index === 0 ? 'start' : 'end' };
1746
+ publishRangeAction(() => {
1747
+ if (time) {
1748
+ (time.onfocus ?? time.onFocus)?.(event, info);
1749
+ return;
1750
+ }
1751
+
1752
+ const retainedFocus = onFocus?.bind(callbackOwner);
1631
1753
  if (isPickerInputEvent(event)) {
1632
1754
  onfocus?.(event);
1633
1755
  }
1634
- onFocus?.(event, { range: index === 0 ? 'start' : 'end' });
1635
- }
1756
+ if (destroyed) {
1757
+ retainedFocus?.(event, info);
1758
+ }
1759
+ else {
1760
+ onFocus?.(event, info);
1761
+ }
1762
+ });
1636
1763
  }
1637
1764
  function editorBlur(
1638
1765
  index: number,
@@ -1677,7 +1804,9 @@
1677
1804
  ) {
1678
1805
  const context = committedBlur;
1679
1806
  const invalid = context.clockMode ? rawInvalid[index] : renderedInvalid;
1680
- if (!destroyed && !context.preserveText && invalid) {
1807
+ const erased = texts[index] === '' && calendar[index] !== null;
1808
+ if (!destroyed && isPickerFocus(event.relatedTarget) && !context.preserveText
1809
+ && (invalid || erased)) {
1681
1810
  restoreEditorText(index, context.textSettings);
1682
1811
  if (context.clockMode) clearClockInputError(index);
1683
1812
  }
@@ -1693,6 +1822,14 @@
1693
1822
  if (!destroyed) blurWithin(event);
1694
1823
  }
1695
1824
 
1825
+ /** Only the first enabled endpoint receives the component autofocus request. */
1826
+ function editorAutofocus(index: 0 | 1) {
1827
+ if (attributes.autofocus != null) {
1828
+ return null;
1829
+ }
1830
+ return autoFocus && firstEnabledEditor === index;
1831
+ }
1832
+
1696
1833
  /** Keeps a separate mask, accessibility state, and active-endpoint indicator. */
1697
1834
  function editorAttributes(index: 0 | 1): DatePickerEditorAttributes {
1698
1835
  const nativeAttributes: object = attributes;
@@ -1704,6 +1841,8 @@
1704
1841
  const invalid = inputInvalid(index);
1705
1842
  return {
1706
1843
  ...nativeAttributes,
1844
+ ...pickerEditorServerAutofocus(editorAutofocus(index)),
1845
+ tabindex: attributes.tabindex ?? tabIndex,
1707
1846
  id: fieldId,
1708
1847
  name: name ?? field?.name,
1709
1848
  placeholder: time ? timePlaceholder?.[index]
@@ -1719,12 +1858,12 @@
1719
1858
  required: attributes.required ?? field?.required,
1720
1859
  disabled: blocked[index],
1721
1860
  readonly: readOnly,
1722
- autocomplete: 'off',
1861
+ autocomplete: attributes.autocomplete ?? autoComplete ?? 'off',
1723
1862
  value: maskInputs[index].value,
1724
1863
  defaultValue: maskInputs[index].value,
1725
1864
  class: semantic?.classes.input,
1726
1865
  style: semantic?.styles.input,
1727
- size: timeInputSize,
1866
+ size: editorSize,
1728
1867
  'data-range': time || components?.input ? range : undefined,
1729
1868
  ...maskEvents[index],
1730
1869
  oninput: event => editorInput(index, event),
@@ -2057,19 +2196,18 @@
2057
2196
  return;
2058
2197
  }
2059
2198
  const firstEnabled = blocked.findIndex(disabled => !disabled);
2199
+ const clock = time;
2200
+ const notifyClick = clock ? clock.onclick ?? clock.onClick : onClick?.bind(callbackOwner);
2060
2201
  if (firstEnabled !== -1) {
2061
2202
  const root = event.currentTarget;
2062
2203
  if (!root.contains(getPopupActiveElement(root))) {
2063
2204
  inputs()[firstEnabled]?.focus();
2064
2205
  }
2065
- requestOpen(true);
2206
+ completeEditorOpen();
2066
2207
  }
2067
2208
 
2068
- if (time) {
2069
- if (firstEnabled !== -1) (time.onclick ?? time.onClick)?.(event);
2070
- }
2071
- else {
2072
- onClick?.(event);
2209
+ if (notifyClick && (firstEnabled !== -1 || !clock)) {
2210
+ publishRangeAction(() => notifyClick(event));
2073
2211
  }
2074
2212
  }
2075
2213
  /** The separator and adornments keep the endpoint focused during the mouse press. */
@@ -2085,6 +2223,7 @@
2085
2223
  }
2086
2224
  /** In the popup, the event belongs to the current range endpoint. */
2087
2225
  function panelFocusIn(event: DatePickerFocusEvent) {
2226
+ container.cancelClose();
2088
2227
  focused = true;
2089
2228
  if (time) {
2090
2229
  requestOpen(true);
@@ -2134,7 +2273,7 @@
2134
2273
  export function focus(
2135
2274
  options?: FocusOptions & { index?: 0 | 1; cursor?: 'start' | 'end' | 'all'; }
2136
2275
  ) {
2137
- const input = inputs()[options?.index ?? blocked.findIndex(item => !item)];
2276
+ const input = inputs()[options?.index ?? firstEnabledEditor];
2138
2277
  input?.focus(options);
2139
2278
  if (options?.cursor) {
2140
2279
  input?.setSelectionRange(