@sentry/scraps 0.4.0 → 0.6.0
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/README.md +74 -8
- package/dist/alert/alert.js +0 -3
- package/dist/badge/badge.js +1 -2
- package/dist/badge/tag.js +4 -4
- package/dist/button/button.js +1 -0
- package/dist/button/index.d.ts +0 -1
- package/dist/button/index.js +0 -1
- package/dist/button/linkButton.js +1 -0
- package/dist/button/styles.js +157 -152
- package/dist/clipboard/copyToClipboardButton.d.ts +15 -0
- package/dist/clipboard/copyToClipboardButton.js +32 -0
- package/dist/clipboard/index.d.ts +1 -0
- package/dist/clipboard/index.js +1 -0
- package/dist/code/index.d.ts +1 -0
- package/dist/code/index.js +1 -0
- package/dist/codeBlock/codeBlock.d.ts +68 -0
- package/dist/codeBlock/codeBlock.js +243 -0
- package/dist/codeBlock/index.d.ts +2 -0
- package/dist/codeBlock/index.js +2 -0
- package/dist/codeBlock/prism.d.ts +11 -0
- package/dist/codeBlock/prism.js +101 -0
- package/dist/codeBlock/styles.d.ts +2 -0
- package/dist/codeBlock/styles.js +133 -0
- package/dist/compactSelect/compactSelect.d.ts +21 -0
- package/dist/compactSelect/compactSelect.js +150 -0
- package/dist/compactSelect/composite.d.ts +54 -0
- package/dist/compactSelect/composite.js +101 -0
- package/dist/compactSelect/control.d.ts +112 -0
- package/dist/compactSelect/control.js +355 -0
- package/dist/compactSelect/gridList/index.d.ts +46 -0
- package/dist/compactSelect/gridList/index.js +102 -0
- package/dist/compactSelect/gridList/option.d.ts +15 -0
- package/dist/compactSelect/gridList/option.js +111 -0
- package/dist/compactSelect/gridList/section.d.ts +15 -0
- package/dist/compactSelect/gridList/section.js +58 -0
- package/dist/compactSelect/highlightText.d.ts +6 -0
- package/dist/compactSelect/highlightText.js +33 -0
- package/dist/compactSelect/index.d.ts +9 -0
- package/dist/compactSelect/index.js +8 -0
- package/dist/compactSelect/list.d.ts +94 -0
- package/dist/compactSelect/list.js +167 -0
- package/dist/compactSelect/listBox/index.d.ts +84 -0
- package/dist/compactSelect/listBox/index.js +175 -0
- package/dist/compactSelect/listBox/option.d.ts +19 -0
- package/dist/compactSelect/listBox/option.js +101 -0
- package/dist/compactSelect/listBox/section.d.ts +21 -0
- package/dist/compactSelect/listBox/section.js +58 -0
- package/dist/compactSelect/menuComponents.d.ts +89 -0
- package/dist/compactSelect/menuComponents.js +117 -0
- package/dist/compactSelect/selectFilterContext.d.ts +2 -0
- package/dist/compactSelect/selectFilterContext.js +3 -0
- package/dist/compactSelect/styles.d.ts +39 -0
- package/dist/compactSelect/styles.js +155 -0
- package/dist/compactSelect/types.d.ts +1 -0
- package/dist/compactSelect/types.js +0 -0
- package/dist/compactSelect/useVirtualizedItems.d.ts +52 -0
- package/dist/compactSelect/useVirtualizedItems.js +74 -0
- package/dist/compactSelect/utils.d.ts +80 -0
- package/dist/compactSelect/utils.js +264 -0
- package/dist/confirm/index.d.ts +153 -0
- package/dist/confirm/index.js +132 -0
- package/dist/dateTime/config.d.ts +23 -0
- package/dist/dateTime/config.js +50 -0
- package/dist/dateTime/context.d.ts +18 -0
- package/dist/dateTime/context.js +46 -0
- package/dist/dateTime/dateTime.d.ts +6 -0
- package/dist/dateTime/dateTime.js +30 -0
- package/dist/dateTime/format.d.ts +24 -0
- package/dist/dateTime/format.js +45 -0
- package/dist/dateTime/index.d.ts +6 -0
- package/dist/dateTime/index.js +6 -0
- package/dist/dateTime/intlFormatter.d.ts +9 -0
- package/dist/dateTime/intlFormatter.js +16 -0
- package/dist/dateTime/parseDate.d.ts +3 -0
- package/dist/dateTime/parseDate.js +12 -0
- package/dist/descriptionList/descriptionList.js +4 -3
- package/dist/disclosure/disclosure.js +2 -2
- package/dist/dragHandle/dragHandle.d.ts +3 -1
- package/dist/dragHandle/dragHandle.js +8 -5
- package/dist/drawer/components.d.ts +46 -0
- package/dist/drawer/components.js +265 -0
- package/dist/drawer/globalDrawer.d.ts +106 -0
- package/dist/drawer/globalDrawer.js +173 -0
- package/dist/drawer/index.d.ts +3 -0
- package/dist/drawer/index.js +2 -0
- package/dist/drawer/useDrawerResizing.d.ts +19 -0
- package/dist/drawer/useDrawerResizing.js +111 -0
- package/dist/drawer/useDrawerWidth.d.ts +1 -0
- package/dist/drawer/useDrawerWidth.js +42 -0
- package/dist/dropdownMenu/content.d.ts +1 -0
- package/dist/dropdownMenu/content.js +1 -0
- package/dist/dropdownMenu/context.d.ts +39 -0
- package/dist/dropdownMenu/context.js +3 -0
- package/dist/dropdownMenu/dropdownMenu.d.ts +70 -0
- package/dist/dropdownMenu/dropdownMenu.js +109 -0
- package/dist/dropdownMenu/index.d.ts +2 -0
- package/dist/dropdownMenu/index.js +1 -0
- package/dist/dropdownMenu/item.d.ts +140 -0
- package/dist/dropdownMenu/item.js +142 -0
- package/dist/dropdownMenu/list.d.ts +85 -0
- package/dist/dropdownMenu/list.js +429 -0
- package/dist/dropdownMenu/search.d.ts +16 -0
- package/dist/dropdownMenu/search.js +16 -0
- package/dist/dropdownMenu/section.d.ts +21 -0
- package/dist/dropdownMenu/section.js +40 -0
- package/dist/dropdownMenu/separator.d.ts +7 -0
- package/dist/dropdownMenu/separator.js +15 -0
- package/dist/dropdownMenu/useDropdownMenuKeyboard.d.ts +24 -0
- package/dist/dropdownMenu/useDropdownMenuKeyboard.js +31 -0
- package/dist/dropdownMenu/useDropdownMenuRowLayout.d.ts +29 -0
- package/dist/dropdownMenu/useDropdownMenuRowLayout.js +64 -0
- package/dist/dropdownMenu/useDropdownRootMenuState.d.ts +13 -0
- package/dist/dropdownMenu/useDropdownRootMenuState.js +30 -0
- package/dist/dropdownMenu/useDropdownSubmenuState.d.ts +19 -0
- package/dist/dropdownMenu/useDropdownSubmenuState.js +25 -0
- package/dist/dropdownMenu/utils.d.ts +21 -0
- package/dist/dropdownMenu/utils.js +55 -0
- package/dist/duration/formatElapsedDuration.d.ts +4 -0
- package/dist/duration/formatElapsedDuration.js +10 -0
- package/dist/duration/getDuration.d.ts +68 -0
- package/dist/duration/getDuration.js +80 -0
- package/dist/duration/index.d.ts +2 -0
- package/dist/duration/index.js +2 -0
- package/dist/form/FormSearch.d.ts +19 -0
- package/dist/form/FormSearch.js +4 -0
- package/dist/form/autoSaveForm.d.ts +110 -0
- package/dist/form/autoSaveForm.js +100 -0
- package/dist/{formPresentation → form}/displayField.js +2 -2
- package/dist/form/field/baseField.d.ts +23 -0
- package/dist/form/field/baseField.js +116 -0
- package/dist/form/field/checkboxField.d.ts +11 -0
- package/dist/form/field/checkboxField.js +42 -0
- package/dist/form/field/checkboxGroupField.d.ts +16 -0
- package/dist/form/field/checkboxGroupField.js +80 -0
- package/dist/form/field/compactSelectField.d.ts +19 -0
- package/dist/form/field/compactSelectField.js +77 -0
- package/dist/{formPresentation → form/field}/fieldIds.js +1 -1
- package/dist/form/field/inputField.d.ts +19 -0
- package/dist/form/field/inputField.js +31 -0
- package/dist/{formPresentation → form/field}/meta.js +6 -6
- package/dist/form/field/numberField.d.ts +5 -0
- package/dist/form/field/numberField.js +11 -0
- package/dist/form/field/passwordField.d.ts +3 -0
- package/dist/form/field/passwordField.js +27 -0
- package/dist/form/field/radioGroupField.d.ts +16 -0
- package/dist/form/field/radioGroupField.js +92 -0
- package/dist/form/field/rangeField.d.ts +8 -0
- package/dist/form/field/rangeField.js +34 -0
- package/dist/form/field/selectAsyncField.d.ts +31 -0
- package/dist/form/field/selectAsyncField.js +22 -0
- package/dist/form/field/selectField.d.ts +9 -0
- package/dist/form/field/selectField.js +72 -0
- package/dist/form/field/switchField.d.ts +7 -0
- package/dist/form/field/switchField.js +37 -0
- package/dist/form/field/textAreaField.d.ts +7 -0
- package/dist/form/field/textAreaField.js +25 -0
- package/dist/{formPresentation → form}/formContext.js +1 -1
- package/dist/form/formErrorContext/index.d.ts +9 -0
- package/dist/form/formErrorContext/index.js +7 -0
- package/dist/form/index.d.ts +9 -0
- package/dist/form/index.js +8 -0
- package/dist/{formPresentation → form/layout}/base.d.ts +1 -1
- package/dist/{formPresentation → form/layout}/base.js +1 -1
- package/dist/form/layout/fieldGroup.d.ts +6 -0
- package/dist/form/layout/fieldGroup.js +60 -0
- package/dist/{formPresentation/fieldLayout.d.ts → form/layout/index.d.ts} +1 -1
- package/dist/{formPresentation/fieldLayout.js → form/layout/index.js} +3 -3
- package/dist/{formPresentation → form/layout}/useStackedLayout.js +1 -1
- package/dist/form/scrapsForm.d.ts +220 -0
- package/dist/form/scrapsForm.js +126 -0
- package/dist/fzf.d.ts +9 -0
- package/dist/fzf.js +155 -0
- package/dist/hotkey/hotkey.d.ts +9 -8
- package/dist/hotkey/hotkey.js +13 -6
- package/dist/hotkey/index.d.ts +2 -1
- package/dist/hotkey/index.js +2 -1
- package/dist/hotkey/keyMappings.d.ts +1 -15
- package/dist/hotkey/keyMappings.js +29 -174
- package/dist/hotkey/useHotkeys.d.ts +3 -47
- package/dist/hotkey/useHotkeys.js +8 -42
- package/dist/image/image.js +2 -1
- package/dist/info/infoTip.d.ts +1 -1
- package/dist/info/infoTip.js +4 -3
- package/dist/input/index.d.ts +0 -3
- package/dist/input/index.js +0 -2
- package/dist/input/input.d.ts +2 -0
- package/dist/input/input.js +2 -0
- package/dist/input/passwordManagerIgnoreProps.d.ts +11 -0
- package/dist/input/passwordManagerIgnoreProps.js +8 -0
- package/dist/layout/container.d.ts +8 -2
- package/dist/layout/container.js +2 -2
- package/dist/layout/index.d.ts +2 -2
- package/dist/layout/index.js +1 -1
- package/dist/layout/styles.d.ts +2 -1
- package/dist/layout/styles.js +11 -5
- package/dist/legacyDatetime.d.ts +21 -0
- package/dist/legacyDatetime.js +13 -0
- package/dist/link/index.d.ts +1 -1
- package/dist/link/index.js +1 -1
- package/dist/link/link.js +4 -3
- package/dist/link/linkBehaviorContext.d.ts +11 -0
- package/dist/link/linkBehaviorContext.js +48 -11
- package/dist/menu/index.d.ts +1 -1
- package/dist/menu/index.js +1 -1
- package/dist/menu/menu.d.ts +14 -8
- package/dist/menu/menu.js +163 -60
- package/dist/modal/components.d.ts +19 -0
- package/dist/modal/components.js +99 -0
- package/dist/modal/errorBoundary.d.ts +2 -0
- package/dist/modal/errorBoundary.js +35 -0
- package/dist/modal/index.d.ts +33 -0
- package/dist/modal/index.js +260 -0
- package/dist/modal/portal.d.ts +1 -0
- package/dist/modal/portal.js +10 -0
- package/dist/modal/state.d.ts +17 -0
- package/dist/modal/state.js +41 -0
- package/dist/modal/types.d.ts +75 -0
- package/dist/modal/types.js +0 -0
- package/dist/numberInput/index.d.ts +3 -0
- package/dist/numberInput/index.js +2 -0
- package/dist/{input → numberInput}/numberDragInput.d.ts +1 -1
- package/dist/{input → numberInput}/numberDragInput.js +1 -1
- package/dist/{input → numberInput}/numberInput.d.ts +1 -1
- package/dist/{input → numberInput}/numberInput.js +1 -1
- package/dist/overlayContext.d.ts +29 -0
- package/dist/overlayContext.js +7 -0
- package/dist/pagination/index.d.ts +1 -0
- package/dist/pagination/index.js +1 -0
- package/dist/pagination/pagination.d.ts +24 -0
- package/dist/pagination/pagination.js +63 -0
- package/dist/pictureInPicture/index.d.ts +2 -0
- package/dist/pictureInPicture/index.js +2 -0
- package/dist/pictureInPicture/pictureInPicture.d.ts +47 -0
- package/dist/pictureInPicture/pictureInPicture.js +100 -0
- package/dist/pictureInPicture/pictureInPicturePortal.d.ts +16 -0
- package/dist/pictureInPicture/pictureInPicturePortal.js +46 -0
- package/dist/pictureInPicture/portalContext.d.ts +2 -0
- package/dist/pictureInPicture/portalContext.js +3 -0
- package/dist/relativeTime/getRelativeDate.d.ts +14 -0
- package/dist/relativeTime/getRelativeDate.js +107 -0
- package/dist/relativeTime/index.d.ts +3 -0
- package/dist/relativeTime/index.js +3 -0
- package/dist/relativeTime/relativeTime.d.ts +55 -0
- package/dist/relativeTime/relativeTime.js +102 -0
- package/dist/relativeTime/useRelativeTimeFormatter.d.ts +2 -0
- package/dist/relativeTime/useRelativeTimeFormatter.js +10 -0
- package/dist/revealOnHover/revealOnHover.js +1 -1
- package/dist/segmentedControl/segmentedControl.js +1 -1
- package/dist/select/filter.d.ts +4 -0
- package/dist/select/filter.js +12 -0
- package/dist/select/index.d.ts +3 -0
- package/dist/select/index.js +2 -0
- package/dist/select/option.d.ts +16 -0
- package/dist/select/option.js +81 -0
- package/dist/select/reactSelectWrapper.d.ts +8 -0
- package/dist/select/reactSelectWrapper.js +25 -0
- package/dist/select/select.d.ts +79 -0
- package/dist/select/select.js +454 -0
- package/dist/select/styles.d.ts +9 -0
- package/dist/select/styles.js +27 -0
- package/dist/select/types.d.ts +60 -0
- package/dist/select/types.js +0 -0
- package/dist/select/virtualizedMenuList.d.ts +24 -0
- package/dist/select/virtualizedMenuList.js +123 -0
- package/dist/slideOverPanel.d.ts +20 -0
- package/dist/slideOverPanel.js +141 -0
- package/dist/splitPanel/splitPanel.d.ts +3 -1
- package/dist/splitPanel/splitPanel.js +2 -1
- package/dist/table/table.d.ts +1 -1
- package/dist/table/table.js +39 -20
- package/dist/table/useColumnResize.d.ts +6 -5
- package/dist/table/useColumnResize.js +5 -4
- package/dist/tabs/context.d.ts +9 -0
- package/dist/tabs/context.js +9 -0
- package/dist/tabs/index.d.ts +2 -0
- package/dist/tabs/index.js +1 -0
- package/dist/tabs/item.d.ts +24 -0
- package/dist/tabs/item.js +4 -0
- package/dist/tabs/tab.d.ts +22 -0
- package/dist/tabs/tab.js +208 -0
- package/dist/tabs/tabList.d.ts +18 -0
- package/dist/tabs/tabList.js +258 -0
- package/dist/tabs/tabPanels.d.ts +13 -0
- package/dist/tabs/tabPanels.js +62 -0
- package/dist/tabs/tabs.d.ts +53 -0
- package/dist/tabs/tabs.js +59 -0
- package/dist/tabs/utils.d.ts +3 -0
- package/dist/tabs/utils.js +6 -0
- package/dist/text/heading.d.ts +1 -1
- package/dist/text/heading.js +1 -1
- package/dist/text/prose.d.ts +2 -2
- package/dist/text/styles.js +1 -1
- package/dist/text/text.d.ts +1 -1
- package/dist/text/text.js +1 -1
- package/dist/theme/dark.js +9 -9
- package/dist/theme/light.js +9 -9
- package/dist/theme/theme.d.ts +34 -34
- package/dist/theme/theme.js +9 -0
- package/dist/theme/types.d.ts +20 -2
- package/dist/tooltip/tooltip.js +2 -5
- package/dist/translation/index.d.ts +1 -0
- package/dist/translation/index.js +1 -0
- package/dist/translation/translate.d.ts +9 -0
- package/dist/translation/translate.js +5 -0
- package/dist/translation/translationContext.d.ts +2 -1
- package/dist/translation/translationContext.js +2 -1
- package/dist/useMedia.d.ts +15 -0
- package/dist/useMedia.js +24 -0
- package/dist/useOnClickOutside.d.ts +2 -0
- package/dist/useOnClickOutside.js +22 -0
- package/dist/useOverlay.d.ts +1 -0
- package/dist/useOverlay.js +1 -0
- package/dist/useVirtualCollection.d.ts +154 -0
- package/dist/useVirtualCollection.js +143 -0
- package/dist/useVirtualRows.d.ts +26 -0
- package/dist/useVirtualRows.js +26 -0
- package/dist/virtualCollectionLayout.d.ts +51 -0
- package/dist/virtualCollectionLayout.js +49 -0
- package/fonts/OFL.txt +94 -0
- package/fonts/roboto-mono-variable-cyrillic-ext.woff2 +0 -0
- package/fonts/roboto-mono-variable-cyrillic.woff2 +0 -0
- package/fonts/roboto-mono-variable-greek.woff2 +0 -0
- package/fonts/roboto-mono-variable-latin-ext.woff2 +0 -0
- package/fonts/roboto-mono-variable-vietnamese.woff2 +0 -0
- package/fonts/roboto-mono-variable.woff2 +0 -0
- package/fonts/rubik-variable-cyrillic-ext.woff2 +0 -0
- package/fonts/rubik-variable-cyrillic.woff2 +0 -0
- package/fonts/rubik-variable-latin-ext.woff2 +0 -0
- package/fonts/rubik-variable.woff2 +0 -0
- package/fonts.css +211 -0
- package/package.json +140 -23
- package/dist/datetime.d.ts +0 -39
- package/dist/datetime.js +0 -20
- package/dist/formPresentation/index.d.ts +0 -8
- package/dist/formPresentation/index.js +0 -8
- package/dist/interactionStateLayer/index.d.ts +0 -1
- package/dist/interactionStateLayer/index.js +0 -1
- package/dist/interactionStateLayer/interactionStateLayer.d.ts +0 -23
- package/dist/interactionStateLayer/interactionStateLayer.js +0 -67
- package/dist/unreachable.d.ts +0 -1
- package/dist/unreachable.js +0 -4
- /package/dist/{formPresentation → form}/autoSaveContext.d.ts +0 -0
- /package/dist/{formPresentation → form}/autoSaveContext.js +0 -0
- /package/dist/{formPresentation → form}/displayField.d.ts +0 -0
- /package/dist/{formPresentation → form/field}/fieldIds.d.ts +0 -0
- /package/dist/{formPresentation → form/field}/groupContext.d.ts +0 -0
- /package/dist/{formPresentation → form/field}/groupContext.js +0 -0
- /package/dist/{formPresentation → form/field}/meta.d.ts +0 -0
- /package/dist/{formPresentation → form}/formContext.d.ts +0 -0
- /package/dist/{formPresentation → form}/icons.d.ts +0 -0
- /package/dist/{formPresentation → form}/icons.js +0 -0
- /package/dist/{formPresentation → form/layout}/useStackedLayout.d.ts +0 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { useCallback, useLayoutEffect, useMemo } from "react";
|
|
2
|
+
import { useTheme } from "@emotion/react";
|
|
3
|
+
import { SECTION_ITEM_SPACE } from "../menu/index.js";
|
|
4
|
+
import { estimateRowHeight, useVirtualCollection } from "../useVirtualCollection.js";
|
|
5
|
+
import { SEPARATOR_HEIGHT } from "./separator.js";
|
|
6
|
+
function useDropdownMenuRowLayout({ collection, focusedKey, isWidthLocked, menuRef, onLockWidth, size, virtualized, widestKey }) {
|
|
7
|
+
const theme = useTheme();
|
|
8
|
+
const sectionRowSpace = parseFloat(theme.space[SECTION_ITEM_SPACE]);
|
|
9
|
+
const itemsAfterSections = useMemo(()=>new Set(collection.filter((_, index)=>collection[index - 1]?.type === 'section').map((node)=>node.key)), [
|
|
10
|
+
collection
|
|
11
|
+
]);
|
|
12
|
+
const sectionsByKey = useMemo(()=>new Map(collection.filter((node)=>'section' === node.type).map((node)=>[
|
|
13
|
+
node.key,
|
|
14
|
+
node
|
|
15
|
+
])), [
|
|
16
|
+
collection
|
|
17
|
+
]);
|
|
18
|
+
const estimateSize = useCallback((node)=>{
|
|
19
|
+
if ('section' === node.type) {
|
|
20
|
+
const separator = node.key === collection[0]?.key ? 0 : SEPARATOR_HEIGHT;
|
|
21
|
+
const heading = node.rendered ? estimateRowHeight(node, void 0, size, sectionRowSpace) : 0;
|
|
22
|
+
return separator + heading;
|
|
23
|
+
}
|
|
24
|
+
const section = null === node.parentKey || void 0 === node.parentKey ? void 0 : sectionsByKey.get(node.parentKey);
|
|
25
|
+
const height = estimateRowHeight(node, section, size, sectionRowSpace);
|
|
26
|
+
return itemsAfterSections.has(node.key) ? height + SEPARATOR_HEIGHT : height;
|
|
27
|
+
}, [
|
|
28
|
+
collection,
|
|
29
|
+
itemsAfterSections,
|
|
30
|
+
sectionsByKey,
|
|
31
|
+
size,
|
|
32
|
+
sectionRowSpace
|
|
33
|
+
]);
|
|
34
|
+
const virtual = useVirtualCollection({
|
|
35
|
+
collection,
|
|
36
|
+
enabled: virtualized,
|
|
37
|
+
estimateSize,
|
|
38
|
+
scrollRef: menuRef,
|
|
39
|
+
focusedKey,
|
|
40
|
+
sectionRowSpace,
|
|
41
|
+
size,
|
|
42
|
+
widestKey
|
|
43
|
+
});
|
|
44
|
+
const isWidestRowMounted = virtual.rows.some((row)=>row.node.key === widestKey);
|
|
45
|
+
useLayoutEffect(()=>{
|
|
46
|
+
const menu = menuRef.current;
|
|
47
|
+
if (!virtualized || !isWidestRowMounted || isWidthLocked || !menu) return;
|
|
48
|
+
const { width } = menu.style;
|
|
49
|
+
menu.style.width = 'max-content';
|
|
50
|
+
onLockWidth(Math.ceil(menu.getBoundingClientRect().width));
|
|
51
|
+
menu.style.width = width;
|
|
52
|
+
}, [
|
|
53
|
+
virtualized,
|
|
54
|
+
isWidestRowMounted,
|
|
55
|
+
isWidthLocked,
|
|
56
|
+
menuRef,
|
|
57
|
+
onLockWidth
|
|
58
|
+
]);
|
|
59
|
+
return {
|
|
60
|
+
itemsAfterSections,
|
|
61
|
+
virtual
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export { useDropdownMenuRowLayout };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RootMenuTriggerState } from '@react-stately/menu';
|
|
2
|
+
import type { OverlayTriggerState } from '@react-stately/overlays';
|
|
3
|
+
/**
|
|
4
|
+
* State for the root menu, matching react-stately's `useMenuTriggerState`.
|
|
5
|
+
*
|
|
6
|
+
* `useOverlay` keeps owning the open state and positioning, so this wraps its
|
|
7
|
+
* overlay state instead of creating another one, and adds the stack of open
|
|
8
|
+
* submenus that `useSubmenuTrigger` reads.
|
|
9
|
+
*
|
|
10
|
+
* A root menu rendered inside another menu starts its own tree, but closing
|
|
11
|
+
* it, for example by selecting one of its items, also closes the outer menu.
|
|
12
|
+
*/
|
|
13
|
+
export declare function useDropdownRootMenuState(overlayState: OverlayTriggerState, parentMenuState?: RootMenuTriggerState): RootMenuTriggerState;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
function useDropdownRootMenuState(overlayState, parentMenuState) {
|
|
3
|
+
const [expandedKeysStack, setExpandedKeysStack] = useState([]);
|
|
4
|
+
const [wasOpen, setWasOpen] = useState(overlayState.isOpen);
|
|
5
|
+
if (wasOpen !== overlayState.isOpen) {
|
|
6
|
+
setWasOpen(overlayState.isOpen);
|
|
7
|
+
if (!overlayState.isOpen) setExpandedKeysStack([]);
|
|
8
|
+
}
|
|
9
|
+
const { isOpen, setOpen, open, close, toggle } = overlayState;
|
|
10
|
+
const closeParentMenu = parentMenuState?.close;
|
|
11
|
+
return {
|
|
12
|
+
isOpen,
|
|
13
|
+
setOpen,
|
|
14
|
+
focusStrategy: 'first',
|
|
15
|
+
open: ()=>open(),
|
|
16
|
+
toggle: ()=>toggle(),
|
|
17
|
+
close: ()=>{
|
|
18
|
+
setExpandedKeysStack([]);
|
|
19
|
+
close();
|
|
20
|
+
closeParentMenu?.();
|
|
21
|
+
},
|
|
22
|
+
expandedKeysStack,
|
|
23
|
+
openSubmenu: (triggerKey, level)=>setExpandedKeysStack((stack)=>level > stack.length ? stack : [
|
|
24
|
+
...stack.slice(0, level),
|
|
25
|
+
triggerKey
|
|
26
|
+
]),
|
|
27
|
+
closeSubmenu: (triggerKey, level)=>setExpandedKeysStack((stack)=>stack[level] === triggerKey ? stack.slice(0, level) : stack)
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export { useDropdownRootMenuState };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { RootMenuTriggerState, SubmenuTriggerState } from '@react-stately/menu';
|
|
2
|
+
import type { Key } from '@react-types/shared';
|
|
3
|
+
interface UseDropdownSubmenuStateOptions {
|
|
4
|
+
/** How deep the submenu sits in the menu tree, starting at 0. */
|
|
5
|
+
level: number;
|
|
6
|
+
rootMenuState: RootMenuTriggerState;
|
|
7
|
+
/** Key of the item that opens the submenu. */
|
|
8
|
+
triggerKey: Key;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* State for a submenu trigger, matching react-stately's `useSubmenuTriggerState`.
|
|
12
|
+
*
|
|
13
|
+
* react-stately takes the submenu level from the number of open submenus when
|
|
14
|
+
* the trigger mounts. A virtualized menu mounts triggers as they scroll into
|
|
15
|
+
* view, possibly while a sibling submenu is open, which would place the
|
|
16
|
+
* trigger one level too deep. The level comes from the menu tree instead.
|
|
17
|
+
*/
|
|
18
|
+
export declare function useDropdownSubmenuState({ level, rootMenuState, triggerKey, }: UseDropdownSubmenuStateOptions): SubmenuTriggerState;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
function useDropdownSubmenuState({ level, rootMenuState, triggerKey }) {
|
|
3
|
+
const { expandedKeysStack, openSubmenu, closeSubmenu, close: closeAll } = rootMenuState;
|
|
4
|
+
const isOpen = expandedKeysStack[level] === triggerKey;
|
|
5
|
+
const [focusStrategy, setFocusStrategy] = useState(null);
|
|
6
|
+
const open = (strategy)=>{
|
|
7
|
+
setFocusStrategy(strategy ?? null);
|
|
8
|
+
openSubmenu(triggerKey, level);
|
|
9
|
+
};
|
|
10
|
+
const close = ()=>{
|
|
11
|
+
setFocusStrategy(null);
|
|
12
|
+
closeSubmenu(triggerKey, level);
|
|
13
|
+
};
|
|
14
|
+
return {
|
|
15
|
+
focusStrategy,
|
|
16
|
+
isOpen,
|
|
17
|
+
open,
|
|
18
|
+
close,
|
|
19
|
+
closeAll,
|
|
20
|
+
submenuLevel: level,
|
|
21
|
+
setOpen: ()=>{},
|
|
22
|
+
toggle: (strategy)=>isOpen ? close() : open(strategy)
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export { useDropdownSubmenuState };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { useResolveLinkHref } from '../link/linkBehaviorContext.js';
|
|
2
|
+
import type { MenuItemProps } from './item.js';
|
|
3
|
+
/**
|
|
4
|
+
* Recursively removes hidden items, including those nested in submenus
|
|
5
|
+
* Apply href to items that have a to or externalHref prop
|
|
6
|
+
*/
|
|
7
|
+
export declare function removeHiddenItemsAndSetHref(source: MenuItemProps[], resolveHref: ReturnType<typeof useResolveLinkHref>): MenuItemProps[];
|
|
8
|
+
/** Items with children render as a section, unless they open a submenu. */
|
|
9
|
+
export declare function isMenuSection(item: MenuItemProps): boolean;
|
|
10
|
+
export declare function filterMenuItems(items: MenuItemProps[], search: string): MenuItemProps[];
|
|
11
|
+
/**
|
|
12
|
+
* Number of items a menu lists, counting the items inside sections but not
|
|
13
|
+
* the sections themselves. Matches the count `DropdownMenuList` virtualizes by.
|
|
14
|
+
*/
|
|
15
|
+
export declare function countMenuItems(items: MenuItemProps[]): number;
|
|
16
|
+
/** The key of the menu item an event fired on, if it fired on one. */
|
|
17
|
+
export declare function getMenuItemKey(target: EventTarget | null): string | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Recursively finds and returns disabled items
|
|
20
|
+
*/
|
|
21
|
+
export declare function getDisabledKeys(source: MenuItemProps[]): Array<MenuItemProps['key']>;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { getSearchScore } from "../fzf.js";
|
|
2
|
+
function removeHiddenItemsAndSetHref(source, resolveHref) {
|
|
3
|
+
return source.filter((item)=>!item.hidden).map((item)=>{
|
|
4
|
+
const href = item.to ? resolveHref(item.to) : item.externalHref;
|
|
5
|
+
return {
|
|
6
|
+
...item,
|
|
7
|
+
...void 0 === href ? {} : {
|
|
8
|
+
href
|
|
9
|
+
},
|
|
10
|
+
...item.children ? {
|
|
11
|
+
children: removeHiddenItemsAndSetHref(item.children, resolveHref)
|
|
12
|
+
} : {}
|
|
13
|
+
};
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
function isMenuSection(item) {
|
|
17
|
+
return Boolean(item.children && item.children.length > 0 && !item.submenu);
|
|
18
|
+
}
|
|
19
|
+
function filterMenuItems(items, search) {
|
|
20
|
+
const query = search.trim();
|
|
21
|
+
if (!query) return items;
|
|
22
|
+
return items.flatMap((item)=>{
|
|
23
|
+
if (item.children && !item.submenu) {
|
|
24
|
+
const children = filterMenuItems(item.children, query);
|
|
25
|
+
return children.length > 0 ? [
|
|
26
|
+
{
|
|
27
|
+
...item,
|
|
28
|
+
children
|
|
29
|
+
}
|
|
30
|
+
] : [];
|
|
31
|
+
}
|
|
32
|
+
const text = item.textValue ?? ('string' == typeof item.label ? item.label : '');
|
|
33
|
+
return getSearchScore(text, query) > 0 ? [
|
|
34
|
+
item
|
|
35
|
+
] : [];
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
function countMenuItems(items) {
|
|
39
|
+
return items.reduce((count, item)=>count + (isMenuSection(item) ? item.children?.length ?? 0 : 1), 0);
|
|
40
|
+
}
|
|
41
|
+
function getMenuItemKey(target) {
|
|
42
|
+
if (!(target instanceof HTMLElement) || !target.getAttribute('role')?.startsWith('menuitem')) return;
|
|
43
|
+
return target.dataset.key;
|
|
44
|
+
}
|
|
45
|
+
function getDisabledKeys(source) {
|
|
46
|
+
return source.reduce((acc, cur)=>{
|
|
47
|
+
if (cur.disabled) {
|
|
48
|
+
acc.push(cur.key);
|
|
49
|
+
return acc;
|
|
50
|
+
}
|
|
51
|
+
if (cur.children) return acc.concat(getDisabledKeys(cur.children));
|
|
52
|
+
return acc;
|
|
53
|
+
}, []);
|
|
54
|
+
}
|
|
55
|
+
export { countMenuItems, filterMenuItems, getDisabledKeys, getMenuItemKey, isMenuSection, removeHiddenItemsAndSetHref };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
function formatElapsedDuration(ms) {
|
|
2
|
+
const tenths = Math.floor(ms / 100);
|
|
3
|
+
if (tenths < 600) return `${(tenths / 10).toFixed(1)}s`;
|
|
4
|
+
const totalSeconds = Math.floor(ms / 1000);
|
|
5
|
+
const hours = Math.floor(totalSeconds / 3600);
|
|
6
|
+
const minutes = Math.floor(totalSeconds % 3600 / 60);
|
|
7
|
+
const seconds = totalSeconds % 60;
|
|
8
|
+
return hours > 0 ? `${hours}h ${minutes}m ${seconds}s` : `${minutes}m ${seconds}s`;
|
|
9
|
+
}
|
|
10
|
+
export { formatElapsedDuration };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Translation } from '../translation/translate.js';
|
|
2
|
+
export interface DurationOptions {
|
|
3
|
+
absolute?: boolean;
|
|
4
|
+
/** Smallest display unit. Defaults to milliseconds. */
|
|
5
|
+
minimumUnit?: DurationUnit;
|
|
6
|
+
precision?: number;
|
|
7
|
+
style?: 'long' | 'short' | 'narrow';
|
|
8
|
+
}
|
|
9
|
+
declare const units: readonly [{
|
|
10
|
+
readonly unit: "year";
|
|
11
|
+
readonly size: 31536000000;
|
|
12
|
+
readonly long: (count: number, { tn }: Translation) => string;
|
|
13
|
+
readonly short: ({ t }: Translation) => string;
|
|
14
|
+
readonly narrow: ({ t }: Translation) => string;
|
|
15
|
+
}, {
|
|
16
|
+
readonly unit: "month";
|
|
17
|
+
readonly size: 2629800000;
|
|
18
|
+
readonly long: (count: number, { tn }: Translation) => string;
|
|
19
|
+
readonly short: ({ t }: Translation) => string;
|
|
20
|
+
readonly narrow: ({ pgettext }: Translation) => string;
|
|
21
|
+
}, {
|
|
22
|
+
readonly unit: "week";
|
|
23
|
+
readonly size: 604800000;
|
|
24
|
+
readonly long: (count: number, { tn }: Translation) => string;
|
|
25
|
+
readonly short: ({ t }: Translation) => string;
|
|
26
|
+
readonly narrow: ({ t }: Translation) => string;
|
|
27
|
+
}, {
|
|
28
|
+
readonly unit: "day";
|
|
29
|
+
readonly size: 86400000;
|
|
30
|
+
readonly long: (count: number, { tn }: Translation) => string;
|
|
31
|
+
readonly short: ({ t }: Translation) => string;
|
|
32
|
+
readonly narrow: ({ t }: Translation) => string;
|
|
33
|
+
}, {
|
|
34
|
+
readonly unit: "hour";
|
|
35
|
+
readonly size: 3600000;
|
|
36
|
+
readonly long: (count: number, { tn }: Translation) => string;
|
|
37
|
+
readonly short: ({ t }: Translation) => string;
|
|
38
|
+
readonly narrow: ({ t }: Translation) => string;
|
|
39
|
+
}, {
|
|
40
|
+
readonly unit: "minute";
|
|
41
|
+
readonly size: 60000;
|
|
42
|
+
readonly long: (count: number, { tn }: Translation) => string;
|
|
43
|
+
readonly short: ({ t }: Translation) => string;
|
|
44
|
+
readonly narrow: ({ pgettext }: Translation) => string;
|
|
45
|
+
}, {
|
|
46
|
+
readonly unit: "second";
|
|
47
|
+
readonly size: 1000;
|
|
48
|
+
readonly long: (count: number, { tn }: Translation) => string;
|
|
49
|
+
readonly short: ({ t }: Translation) => string;
|
|
50
|
+
readonly narrow: ({ t }: Translation) => string;
|
|
51
|
+
}, {
|
|
52
|
+
readonly unit: "millisecond";
|
|
53
|
+
readonly size: 1;
|
|
54
|
+
readonly long: (count: number, { tn }: Translation) => string;
|
|
55
|
+
readonly short: ({ t }: Translation) => string;
|
|
56
|
+
readonly narrow: ({ t }: Translation) => string;
|
|
57
|
+
}, {
|
|
58
|
+
readonly unit: "microsecond";
|
|
59
|
+
readonly size: 0.001;
|
|
60
|
+
readonly long: (count: number, { tn }: Translation) => string;
|
|
61
|
+
readonly short: () => string;
|
|
62
|
+
readonly narrow: () => string;
|
|
63
|
+
}];
|
|
64
|
+
export type DurationUnit = (typeof units)[number]['unit'];
|
|
65
|
+
/** Format seconds using the shared application translations. */
|
|
66
|
+
export declare function getDuration(seconds: number, options?: DurationOptions): string;
|
|
67
|
+
export declare function getDurationWithMessages(seconds: number, { precision, style, absolute, minimumUnit, }: DurationOptions, messages: Translation): string;
|
|
68
|
+
export {};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { getSnapshot } from "../dateTime/config.js";
|
|
2
|
+
const units = [
|
|
3
|
+
{
|
|
4
|
+
unit: 'year',
|
|
5
|
+
size: 31536000000,
|
|
6
|
+
long: (count, { tn })=>tn('year', 'years', count),
|
|
7
|
+
short: ({ t })=>t('yr'),
|
|
8
|
+
narrow: ({ t })=>t('y')
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
unit: 'month',
|
|
12
|
+
size: 2629800000,
|
|
13
|
+
long: (count, { tn })=>tn('month', 'months', count),
|
|
14
|
+
short: ({ t })=>t('mo'),
|
|
15
|
+
narrow: ({ pgettext })=>pgettext('abbreviated months', 'm')
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
unit: 'week',
|
|
19
|
+
size: 604800000,
|
|
20
|
+
long: (count, { tn })=>tn('week', 'weeks', count),
|
|
21
|
+
short: ({ t })=>t('wk'),
|
|
22
|
+
narrow: ({ t })=>t('w')
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
unit: 'day',
|
|
26
|
+
size: 86400000,
|
|
27
|
+
long: (count, { tn })=>tn('day', 'days', count),
|
|
28
|
+
short: ({ t })=>t('d'),
|
|
29
|
+
narrow: ({ t })=>t('d')
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
unit: 'hour',
|
|
33
|
+
size: 3600000,
|
|
34
|
+
long: (count, { tn })=>tn('hour', 'hours', count),
|
|
35
|
+
short: ({ t })=>t('hr'),
|
|
36
|
+
narrow: ({ t })=>t('h')
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
unit: 'minute',
|
|
40
|
+
size: 60000,
|
|
41
|
+
long: (count, { tn })=>tn('minute', 'minutes', count),
|
|
42
|
+
short: ({ t })=>t('min'),
|
|
43
|
+
narrow: ({ pgettext })=>pgettext('abbreviated minutes', 'm')
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
unit: 'second',
|
|
47
|
+
size: 1000,
|
|
48
|
+
long: (count, { tn })=>tn('second', 'seconds', count),
|
|
49
|
+
short: ({ t })=>t('s'),
|
|
50
|
+
narrow: ({ t })=>t('s')
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
unit: 'millisecond',
|
|
54
|
+
size: 1,
|
|
55
|
+
long: (count, { tn })=>tn('millisecond', 'milliseconds', count),
|
|
56
|
+
short: ({ t })=>t('ms'),
|
|
57
|
+
narrow: ({ t })=>t('ms')
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
unit: 'microsecond',
|
|
61
|
+
size: 0.001,
|
|
62
|
+
long: (count, { tn })=>tn('microsecond', 'microseconds', count),
|
|
63
|
+
short: ()=>'μs',
|
|
64
|
+
narrow: ()=>'μs'
|
|
65
|
+
}
|
|
66
|
+
];
|
|
67
|
+
function getDuration(seconds, options = {}) {
|
|
68
|
+
return getDurationWithMessages(seconds, options, getSnapshot().messages);
|
|
69
|
+
}
|
|
70
|
+
function getDurationWithMessages(seconds, { precision = 0, style = 'long', absolute = false, minimumUnit = 'millisecond' }, messages) {
|
|
71
|
+
const milliseconds = 1000 * seconds;
|
|
72
|
+
const magnitude = Math.abs(milliseconds);
|
|
73
|
+
const unit = units.find(({ size, unit })=>magnitude >= size || minimumUnit === unit) ?? units[8];
|
|
74
|
+
const value = (absolute ? magnitude : milliseconds) / unit.size;
|
|
75
|
+
const label = value.toFixed(precision);
|
|
76
|
+
if ('long' !== style) return `${label}${unit[style](messages)}`;
|
|
77
|
+
const count = precision <= 0 ? Math.round(value) : value;
|
|
78
|
+
return `${label} ${unit.long(count, messages)}`;
|
|
79
|
+
}
|
|
80
|
+
export { getDuration, getDurationWithMessages };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
interface FormSearchProps {
|
|
3
|
+
children: ReactNode;
|
|
4
|
+
/**
|
|
5
|
+
* Route pattern for settings search (e.g., '/settings/account/details/').
|
|
6
|
+
* This prop is used by the static extraction script to associate
|
|
7
|
+
* form fields with their navigation route. It has no runtime effect.
|
|
8
|
+
*/
|
|
9
|
+
route: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Wrapper component that marks form fields as searchable in settings search.
|
|
13
|
+
* The `route` prop is extracted at build time by scripts/extractFormFields.ts
|
|
14
|
+
* to generate the form field registry used for search.
|
|
15
|
+
*
|
|
16
|
+
* This component has no runtime behavior - it simply renders its children.
|
|
17
|
+
*/
|
|
18
|
+
export declare function FormSearch({ children }: FormSearchProps): ReactNode;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type * as React from 'react';
|
|
2
|
+
import type { DeepKeys, DeepValue, FieldApi } from '@tanstack/react-form';
|
|
3
|
+
import { type UseMutationOptions } from '@tanstack/react-query';
|
|
4
|
+
import { type z } from 'zod';
|
|
5
|
+
import { type BoundFieldComponents } from './scrapsForm.js';
|
|
6
|
+
/**
|
|
7
|
+
* Configuration for confirmation dialogs before applying changes.
|
|
8
|
+
* Used for dangerous operations like security settings.
|
|
9
|
+
* Always focuses the Cancel button for safety.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // Simple string - always show this message
|
|
13
|
+
* confirm="Are you sure you want to save?"
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Function - return message based on new value, or undefined to skip
|
|
17
|
+
* confirm={(value) => value ? "Enable this feature?" : "Disable this feature?"}
|
|
18
|
+
*/
|
|
19
|
+
type ConfirmConfig<TValue = unknown> = React.ReactNode | ((value: TValue) => React.ReactNode | undefined);
|
|
20
|
+
type SchemaInput<TSchema extends z.ZodObject> = z.input<TSchema>;
|
|
21
|
+
type SchemaOutput<TSchema extends z.ZodObject> = z.output<TSchema>;
|
|
22
|
+
/** Form data type coming from the schema input */
|
|
23
|
+
type SchemaFieldName<TSchema extends z.ZodObject> = Extract<DeepKeys<SchemaInput<TSchema>>, string>;
|
|
24
|
+
/** FieldApi’s TData must be DeepValue<TParentData, TName> */
|
|
25
|
+
type SchemaFieldInputValue<TSchema extends z.ZodObject, TFieldName extends SchemaFieldName<TSchema>> = DeepValue<SchemaInput<TSchema>, TFieldName>;
|
|
26
|
+
type AutoSaveFormRenderArg<TSchema extends z.ZodObject, TFieldName extends SchemaFieldName<TSchema>> = FieldApi<SchemaInput<TSchema>, TFieldName, SchemaFieldInputValue<TSchema, TFieldName>, undefined, // TOnMount
|
|
27
|
+
undefined, // TOnChange
|
|
28
|
+
undefined, // TOnChangeAsync
|
|
29
|
+
undefined, // TOnBlur
|
|
30
|
+
undefined, // TOnBlurAsync
|
|
31
|
+
undefined, // TOnSubmit
|
|
32
|
+
undefined, // TOnSubmitAsync
|
|
33
|
+
undefined, // TOnDynamic
|
|
34
|
+
undefined, // TOnDynamicAsync
|
|
35
|
+
undefined, // TFormOnMount
|
|
36
|
+
undefined, // TFormOnChange
|
|
37
|
+
undefined, // TFormOnChangeAsync
|
|
38
|
+
undefined, // TFormOnBlur
|
|
39
|
+
undefined, // TFormOnBlurAsync
|
|
40
|
+
undefined, // TFormOnSubmit
|
|
41
|
+
undefined, // TFormOnSubmitAsync
|
|
42
|
+
undefined, // TFormOnDynamic
|
|
43
|
+
undefined, // TFormOnDynamicAsync
|
|
44
|
+
undefined, // TFormOnServer
|
|
45
|
+
unknown> & {
|
|
46
|
+
[K in keyof BoundFieldComponents]: BoundFieldComponents[K];
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* A component that provides field props and mutation state via render prop.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* <AutoSaveForm
|
|
53
|
+
* name="lastName"
|
|
54
|
+
* schema={baseUserSchema}
|
|
55
|
+
* initialValue={user.data?.lastName ?? ''}
|
|
56
|
+
* mutationOptions={{
|
|
57
|
+
* mutationFn: async (data) => api.patch('/user', data),
|
|
58
|
+
* }}
|
|
59
|
+
* >
|
|
60
|
+
* {field => (
|
|
61
|
+
* <InputField
|
|
62
|
+
* label="Last Name:"
|
|
63
|
+
* required
|
|
64
|
+
* value={field.state.value}
|
|
65
|
+
* onChange={field.handleChange}
|
|
66
|
+
* />
|
|
67
|
+
* )}
|
|
68
|
+
* </AutoSaveForm>
|
|
69
|
+
*/
|
|
70
|
+
interface AutoSaveFormProps<TData, TContext, TSchema extends z.ZodObject, TFieldName extends Extract<keyof SchemaInput<TSchema>, string>> {
|
|
71
|
+
/**
|
|
72
|
+
* Render prop that receives field props and additional props
|
|
73
|
+
*/
|
|
74
|
+
children: (field: AutoSaveFormRenderArg<TSchema, TFieldName>) => React.ReactElement | null;
|
|
75
|
+
/**
|
|
76
|
+
* Initial value - must match the schema's type for this field
|
|
77
|
+
*/
|
|
78
|
+
initialValue: SchemaInput<TSchema>[TFieldName];
|
|
79
|
+
/**
|
|
80
|
+
* TanStack Query mutation options - mutationFn receives single-field data
|
|
81
|
+
*/
|
|
82
|
+
mutationOptions: UseMutationOptions<TData, Error, NoInfer<Record<TFieldName, SchemaOutput<TSchema>[TFieldName]>>, TContext>;
|
|
83
|
+
/**
|
|
84
|
+
* Field name - must be a valid string key in the schema
|
|
85
|
+
*/
|
|
86
|
+
name: TFieldName;
|
|
87
|
+
/**
|
|
88
|
+
* Zod schema for validation
|
|
89
|
+
*/
|
|
90
|
+
schema: TSchema;
|
|
91
|
+
/**
|
|
92
|
+
* Optional confirmation dialog before saving.
|
|
93
|
+
* Shows a modal and requires user confirmation before applying changes.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* // Simple string - always show this message
|
|
97
|
+
* confirm="Are you sure you want to save this change?"
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* // Function - return message based on new value, or undefined to skip
|
|
101
|
+
* confirm={(value) => value ? "Enable this feature?" : "Disable this feature?"}
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* // Function with conditional confirmation
|
|
105
|
+
* confirm={(value) => value === 'dangerous' ? "This is irreversible!" : undefined}
|
|
106
|
+
*/
|
|
107
|
+
confirm?: ConfirmConfig<SchemaInput<TSchema>[TFieldName]>;
|
|
108
|
+
}
|
|
109
|
+
export declare function AutoSaveForm<TData, TContext, TSchema extends z.ZodObject<z.ZodRawShape>, TFieldName extends Extract<keyof SchemaInput<TSchema>, string>>(props: AutoSaveFormProps<TData, TContext, TSchema, TFieldName>): import("@emotion/react/jsx-runtime").JSX.Element;
|
|
110
|
+
export {};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { jsx } from "@emotion/react/jsx-runtime";
|
|
2
|
+
import { useId, useRef } from "react";
|
|
3
|
+
import { useMutation } from "@tanstack/react-query";
|
|
4
|
+
import { openConfirmModal } from "../confirm/index.js";
|
|
5
|
+
import { AutoSaveContextProvider } from "./autoSaveContext.js";
|
|
6
|
+
import { useFormErrorMapper } from "./formErrorContext/index.js";
|
|
7
|
+
import { setFieldErrors, useScrapsForm } from "./scrapsForm.js";
|
|
8
|
+
import { useTranslation } from "../translation/useTranslation.js";
|
|
9
|
+
function AutoSaveForm(props) {
|
|
10
|
+
const { name, schema, initialValue, mutationOptions, confirm, children } = props;
|
|
11
|
+
const { t } = useTranslation();
|
|
12
|
+
const mapFormError = useFormErrorMapper();
|
|
13
|
+
const id = useId();
|
|
14
|
+
const mutation = useMutation(mutationOptions);
|
|
15
|
+
const pendingConfirmRef = useRef(false);
|
|
16
|
+
const resetOnErrorRef = useRef(false);
|
|
17
|
+
const form = useScrapsForm({
|
|
18
|
+
formId: `${name}-${id}-(auto-save)`,
|
|
19
|
+
defaultValues: {
|
|
20
|
+
[name]: initialValue
|
|
21
|
+
},
|
|
22
|
+
validators: {
|
|
23
|
+
onChange: schema.pick({
|
|
24
|
+
[name]: true
|
|
25
|
+
})
|
|
26
|
+
},
|
|
27
|
+
listeners: {
|
|
28
|
+
onBlur: ({ formApi, fieldApi })=>{
|
|
29
|
+
if (!fieldApi.state.meta.isDefaultValue) formApi.handleSubmit();
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
onSubmit: ({ value, formApi })=>{
|
|
33
|
+
if ('pending' === mutation.status || pendingConfirmRef.current) return Promise.resolve();
|
|
34
|
+
const onError = (error)=>{
|
|
35
|
+
if (resetOnErrorRef.current) formApi.reset();
|
|
36
|
+
const fallbackMessage = t('Failed to save');
|
|
37
|
+
const mappedError = mapFormError(error, formApi.state.values, fallbackMessage);
|
|
38
|
+
if (mappedError && 'fieldErrors' in mappedError && setFieldErrors(formApi, mappedError.fieldErrors)) return;
|
|
39
|
+
setFieldErrors(formApi, {
|
|
40
|
+
[name]: {
|
|
41
|
+
message: mappedError && 'message' in mappedError ? mappedError.message : fallbackMessage
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
const onSuccess = ()=>{
|
|
46
|
+
formApi.reset();
|
|
47
|
+
};
|
|
48
|
+
const parsedValue = schema.pick({
|
|
49
|
+
[name]: true
|
|
50
|
+
}).safeParse(value);
|
|
51
|
+
if (!parsedValue.success) return Promise.resolve();
|
|
52
|
+
const submittedValue = parsedValue.data;
|
|
53
|
+
const fieldValue = value[name];
|
|
54
|
+
const confirmMessage = 'function' == typeof confirm ? confirm(fieldValue) : confirm;
|
|
55
|
+
if (confirmMessage) {
|
|
56
|
+
pendingConfirmRef.current = true;
|
|
57
|
+
return new Promise((resolve)=>{
|
|
58
|
+
openConfirmModal({
|
|
59
|
+
message: confirmMessage,
|
|
60
|
+
isDangerous: true,
|
|
61
|
+
onConfirm: ()=>{
|
|
62
|
+
pendingConfirmRef.current = false;
|
|
63
|
+
mutation.mutateAsync(submittedValue, {
|
|
64
|
+
onError,
|
|
65
|
+
onSuccess
|
|
66
|
+
}).then(()=>{
|
|
67
|
+
resolve();
|
|
68
|
+
}, resolve);
|
|
69
|
+
},
|
|
70
|
+
onClose: ()=>{
|
|
71
|
+
if (pendingConfirmRef.current) {
|
|
72
|
+
form.reset();
|
|
73
|
+
resolve();
|
|
74
|
+
}
|
|
75
|
+
pendingConfirmRef.current = false;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return mutation.mutateAsync(submittedValue, {
|
|
81
|
+
onError,
|
|
82
|
+
onSuccess
|
|
83
|
+
}).catch(()=>{});
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
return /*#__PURE__*/ jsx(form.AppForm, {
|
|
87
|
+
form: form,
|
|
88
|
+
children: /*#__PURE__*/ jsx(AutoSaveContextProvider, {
|
|
89
|
+
value: {
|
|
90
|
+
status: mutation.status,
|
|
91
|
+
resetOnErrorRef
|
|
92
|
+
},
|
|
93
|
+
children: /*#__PURE__*/ jsx(form.AppField, {
|
|
94
|
+
name: name,
|
|
95
|
+
children: (field)=>children(field)
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
export { AutoSaveForm };
|
|
@@ -3,8 +3,8 @@ import { DescriptionList } from "../descriptionList/index.js";
|
|
|
3
3
|
import { InfoText } from "../info/index.js";
|
|
4
4
|
import { Container, Stack } from "../layout/index.js";
|
|
5
5
|
import { Text } from "../text/index.js";
|
|
6
|
-
import { BaseLayout, LayoutLabel } from "./base.js";
|
|
7
|
-
import { useStackedLayout } from "./useStackedLayout.js";
|
|
6
|
+
import { BaseLayout, LayoutLabel } from "./layout/base.js";
|
|
7
|
+
import { useStackedLayout } from "./layout/useStackedLayout.js";
|
|
8
8
|
function RowDisplay(props) {
|
|
9
9
|
const stacked = useStackedLayout();
|
|
10
10
|
return /*#__PURE__*/ jsx(DisplayLayout, {
|