viewkitjs 0.32.0 → 0.33.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/CHANGELOG.md +69 -0
- package/fields/dist/index.d.ts +1 -1
- package/fields/dist/index.js +1 -1
- package/fields/dist/query/evaluate.d.ts +7 -0
- package/fields/dist/query/exact-number.d.ts +8 -2
- package/fields/dist/query/exact-number.js +14 -1
- package/fields/dist/query/sort.js +13 -10
- package/fields/dist/registry.js +5 -1
- package/fields/dist/types.d.ts +4 -1
- package/fields/dist/zod.d.ts +5 -0
- package/fields/dist/zod.js +10 -1
- package/fields/docs/guides/contact-values.md +12 -0
- package/fields/docs/guides/field-types.md +1 -1
- package/fields/docs/guides/queries.md +3 -1
- package/fields/docs/reference/fields.md +1 -0
- package/package.json +1 -1
- package/postgres/dist/check.js +4 -1
- package/postgres/dist/definitions/formula-forms.js +3 -1
- package/postgres/dist/definitions/money.js +2 -1
- package/postgres/dist/page.js +6 -3
- package/postgres/dist/rules.d.ts +15 -1
- package/postgres/dist/rules.js +16 -0
- package/postgres/dist/sort.js +7 -2
- package/postgres/docs/guides/definitions.md +1 -1
- package/postgres/docs/guides/field-map.md +1 -1
- package/postgres/docs/reference/definitions.md +1 -1
- package/postgres/docs/reference/postgres.md +1 -1
- package/views/dist/atoms/ToolbarButton.d.ts +1 -1
- package/views/dist/atoms/ToolbarButton.js +4 -3
- package/views/dist/atoms/frame-parts.d.ts +6 -0
- package/views/dist/atoms/frame-parts.js +15 -0
- package/views/dist/atoms/types.d.ts +16 -7
- package/views/dist/cells/cell-title.d.ts +3 -1
- package/views/dist/cells/cell-title.js +2 -2
- package/views/dist/cells/cell-values.js +2 -2
- package/views/dist/contract.d.ts +31 -6
- package/views/dist/contract.js +123 -0
- package/views/dist/controls/button/map-button.js +2 -0
- package/views/dist/controls/checkbox/checkbox.d.ts +3 -2
- package/views/dist/controls/checkbox/checkbox.js +2 -2
- package/views/dist/controls/compound-editor/compound-editor.d.ts +1 -1
- package/views/dist/controls/compound-editor/name-parts.js +4 -4
- package/views/dist/controls/compound-editor/place-parts.js +19 -19
- package/views/dist/controls/compound-editor/range-parts.js +14 -14
- package/views/dist/controls/compound-editor/row.d.ts +3 -1
- package/views/dist/controls/compound-editor/row.js +2 -2
- package/views/dist/controls/controls.d.ts +2 -0
- package/views/dist/controls/controls.js +2 -0
- package/views/dist/controls/customize/customize.js +37 -35
- package/views/dist/controls/customize/map-customize-body.d.ts +2 -0
- package/views/dist/controls/customize/map-customize-body.js +4 -0
- package/views/dist/controls/date-editor/date-days.js +2 -2
- package/views/dist/controls/date-editor/date-editor.js +18 -18
- package/views/dist/controls/date-editor/date-parts.js +6 -6
- package/views/dist/controls/date-editor/map-calendar.js +1 -1
- package/views/dist/controls/date-editor.css +2 -2
- package/views/dist/controls/field/field.d.ts +3 -0
- package/views/dist/controls/field/field.js +6 -6
- package/views/dist/controls/filter-editor/block.d.ts +2 -2
- package/views/dist/controls/filter-editor/block.js +5 -5
- package/views/dist/controls/filter-editor/filter-editor.js +31 -33
- package/views/dist/controls/filter-editor/how.d.ts +3 -1
- package/views/dist/controls/filter-editor/map-filter-body.d.ts +1 -0
- package/views/dist/controls/filter-editor/map-filter-body.js +7 -3
- package/views/dist/controls/filter-editor/map-rules.js +1 -1
- package/views/dist/controls/filter-editor/parts.js +2 -1
- package/views/dist/controls/group-editor/group-editor.js +15 -13
- package/views/dist/controls/index.d.ts +2 -0
- package/views/dist/controls/index.js +1 -0
- package/views/dist/controls/input/input.js +2 -2
- package/views/dist/controls/input/map-input.js +2 -0
- package/views/dist/controls/internal/editor-parts.js +1 -1
- package/views/dist/controls/internal/map-panel-controls.d.ts +8 -0
- package/views/dist/controls/internal/map-panel-controls.js +20 -0
- package/views/dist/controls/internal/panel-controls.d.ts +50 -0
- package/views/dist/controls/internal/panel-controls.js +197 -0
- package/views/dist/controls/internal/part-slot.d.ts +12 -0
- package/views/dist/controls/internal/part-slot.js +5 -0
- package/views/dist/controls/links-editor/links-editor.d.ts +1 -1
- package/views/dist/controls/links-editor/links-editor.js +27 -27
- package/views/dist/controls/menu/map-menu.d.ts +2 -2
- package/views/dist/controls/menu/map-menu.js +15 -12
- package/views/dist/controls/menu/menu.d.ts +2 -0
- package/views/dist/controls/menu/menu.js +3 -3
- package/views/dist/controls/menu-list/menu-list.d.ts +14 -4
- package/views/dist/controls/menu-list/menu-list.js +23 -22
- package/views/dist/controls/notice/notice.d.ts +3 -1
- package/views/dist/controls/notice/notice.js +2 -2
- package/views/dist/controls/number-editor/number-editor.d.ts +1 -1
- package/views/dist/controls/number-editor/number-editor.js +13 -13
- package/views/dist/controls/number-field/number-field.js +5 -3
- package/views/dist/controls/pane/pane.d.ts +4 -2
- package/views/dist/controls/pane/pane.js +4 -4
- package/views/dist/controls/picker/picker.d.ts +2 -0
- package/views/dist/controls/picker/picker.js +37 -36
- package/views/dist/controls/picker/rows.d.ts +4 -0
- package/views/dist/controls/popover/map-popover.d.ts +1 -1
- package/views/dist/controls/popover/map-popover.js +4 -2
- package/views/dist/controls/quick-filters/quick-filters.js +4 -4
- package/views/dist/controls/radio/radio.d.ts +3 -0
- package/views/dist/controls/radio/radio.js +4 -3
- package/views/dist/controls/select/map-select.d.ts +3 -0
- package/views/dist/controls/select/map-select.js +8 -0
- package/views/dist/controls/select/select.d.ts +3 -0
- package/views/dist/controls/select/select.js +3 -3
- package/views/dist/controls/sheet/bottom-sheet.d.ts +5 -1
- package/views/dist/controls/sheet/bottom-sheet.js +2 -2
- package/views/dist/controls/sheet/map-sheet.js +1 -1
- package/views/dist/controls/sort-editor/map-sort-body.d.ts +1 -0
- package/views/dist/controls/sort-editor/map-sort-body.js +7 -3
- package/views/dist/controls/sort-editor/sort-editor.js +24 -26
- package/views/dist/controls/sort-editor.css +5 -0
- package/views/dist/controls/text-editor/text-editor.d.ts +1 -1
- package/views/dist/controls/text-editor/text-editor.js +8 -8
- package/views/dist/controls/toolbar-button/atom-toolbar-button.d.ts +3 -3
- package/views/dist/controls/toolbar-button/atom-toolbar-button.js +7 -6
- package/views/dist/controls/values-editor/values-editor.d.ts +1 -1
- package/views/dist/controls/values-editor/values-editor.js +22 -22
- package/views/dist/controls/views-list/map-view-menu.js +1 -1
- package/views/dist/controls/views-list/view-menu.js +6 -6
- package/views/dist/controls/views-list/views-list.js +4 -4
- package/views/dist/editors/EditorPanel.js +1 -1
- package/views/dist/field-row/FieldRow.js +13 -0
- package/views/dist/fields/money.d.ts +5 -2
- package/views/dist/fields/money.js +14 -2
- package/views/dist/grid/chrome/GridTable.js +4 -1
- package/views/dist/grid/chrome/cell-clipboard.d.ts +4 -2
- package/views/dist/grid/chrome/cell-clipboard.js +2 -2
- package/views/dist/grid/chrome/cells.js +3 -1
- package/views/dist/grid/chrome/grid-row-menu-items.js +1 -1
- package/views/dist/grid/chrome/useGridKeyboard.d.ts +4 -1
- package/views/dist/grid/chrome/useGridKeyboard.js +29 -6
- package/views/dist/grid/chrome/useGridTableInteractionModel.js +9 -2
- package/views/dist/grid/core/clipboard-model.d.ts +3 -1
- package/views/dist/grid/core/clipboard-model.js +9 -8
- package/views/dist/grid/core/editing-controller.d.ts +3 -0
- package/views/dist/grid/core/editing-controller.js +6 -0
- package/views/dist/grid/core/edits-sink.d.ts +19 -0
- package/views/dist/grid/core/edits-sink.js +2 -0
- package/views/dist/grid/core/spreadsheet-operations.js +1 -1
- package/views/dist/grid/core/useGridEditing.d.ts +5 -0
- package/views/dist/grid/core/useGridEditing.js +6 -0
- package/views/dist/list/answer.d.ts +40 -0
- package/views/dist/list/answer.js +43 -0
- package/views/dist/list/bulk-bar.d.ts +7 -0
- package/views/dist/list/bulk-bar.js +10 -0
- package/views/dist/list/columns.d.ts +24 -0
- package/views/dist/list/columns.js +27 -0
- package/views/dist/list/edits.d.ts +19 -0
- package/views/dist/list/edits.js +56 -0
- package/views/dist/list/grid.d.ts +11 -0
- package/views/dist/list/grid.js +76 -0
- package/views/dist/list/index.d.ts +14 -1
- package/views/dist/list/index.js +4 -1
- package/views/dist/list/link.d.ts +8 -0
- package/views/dist/list/link.js +78 -0
- package/views/dist/list/list.d.ts +70 -0
- package/views/dist/list/list.js +14 -0
- package/views/dist/list/parts.js +10 -3
- package/views/dist/list/root-base.d.ts +46 -0
- package/views/dist/list/root-base.js +137 -0
- package/views/dist/list/root.d.ts +35 -34
- package/views/dist/list/root.js +153 -113
- package/views/dist/list/source-answers.d.ts +39 -0
- package/views/dist/list/source-answers.js +225 -0
- package/views/dist/list/store.d.ts +37 -5
- package/views/dist/list/store.js +31 -0
- package/views/dist/list/toolbar.d.ts +3 -2
- package/views/dist/list/toolbar.js +14 -2
- package/views/dist/list/views-axis.js +4 -2
- package/views/dist/list/views.d.ts +7 -2
- package/views/dist/list/views.js +16 -5
- package/views/dist/model/column-capabilities.d.ts +13 -0
- package/views/dist/model/column-capabilities.js +23 -2
- package/views/dist/model/column-schema.d.ts +11 -1
- package/views/dist/model/view-rules.js +1 -0
- package/views/dist/panels/model.d.ts +2 -0
- package/views/dist/panels/react/toolbar/ReadyToolbar.d.ts +8 -2
- package/views/dist/panels/react/toolbar/ReadyToolbar.js +2 -2
- package/views/dist/panels/react/toolbar/Toolbar.d.ts +8 -1
- package/views/dist/panels/react/toolbar/Toolbar.js +12 -3
- package/views/dist/panels/react/toolbar/ToolbarAdvancedFilters.js +1 -1
- package/views/dist/panels/react/toolbar/ToolbarAsideFilter.js +2 -2
- package/views/dist/panels/react/toolbar/ToolbarBarFilter.js +4 -4
- package/views/dist/panels/react/toolbar/ToolbarButton.js +4 -1
- package/views/dist/panels/react/toolbar/ToolbarFilter.js +1 -1
- package/views/dist/panels/react/toolbar/ToolbarFilterAdd.js +1 -1
- package/views/dist/panels/react/toolbar/ToolbarFilterSheet.js +1 -1
- package/views/dist/panels/react/toolbar/ToolbarFilters.js +2 -2
- package/views/dist/panels/react/toolbar/ToolbarNew.js +1 -1
- package/views/dist/panels/react/toolbar/ToolbarOptions.js +1 -1
- package/views/dist/panels/react/toolbar/ToolbarQuickFilters.js +1 -1
- package/views/dist/panels/react/toolbar/ToolbarSearch.js +1 -1
- package/views/dist/panels/react/toolbar/ToolbarSort.js +2 -2
- package/views/dist/panels/react/toolbar/ToolbarSortPill.js +6 -4
- package/views/dist/panels/react/toolbar/context.d.ts +10 -1
- package/views/dist/panels/react/toolbar/panel-part.d.ts +4 -1
- package/views/dist/panels/react/toolbar/panel-part.js +5 -3
- package/views/dist/panels/settings/filter-groups.js +6 -1
- package/views/dist/provider/ViewsProvider.d.ts +6 -2
- package/views/dist/provider/ViewsProvider.js +18 -1
- package/views/dist/provider/components.d.ts +59 -0
- package/views/dist/provider/components.js +1 -1
- package/views/dist/provider/context.d.ts +4 -1
- package/views/dist/provider/own-control.d.ts +4 -0
- package/views/dist/provider/own-control.js +7 -0
- package/views/dist/provider/select-items.d.ts +3 -0
- package/views/dist/provider/select-items.js +6 -0
- package/views/dist/react.d.ts +4 -2
- package/views/dist/react.js +2 -0
- package/views/dist/styles.css +86 -78
- package/views/dist/styles.unlayered.css +86 -78
- package/views/docs/contract.md +252 -5
- package/views/docs/controls/compound-editor.md +4 -0
- package/views/docs/controls/customize.md +6 -0
- package/views/docs/controls/date-editor.md +4 -0
- package/views/docs/controls/field.md +4 -0
- package/views/docs/controls/filter-editor.md +8 -0
- package/views/docs/controls/group-editor.md +6 -0
- package/views/docs/controls/input.md +4 -0
- package/views/docs/controls/links-editor.md +4 -0
- package/views/docs/controls/menu-list.md +8 -0
- package/views/docs/controls/menu.md +4 -0
- package/views/docs/controls/notice.md +4 -0
- package/views/docs/controls/number-editor.md +4 -0
- package/views/docs/controls/pane.md +4 -0
- package/views/docs/controls/picker.md +8 -0
- package/views/docs/controls/quick-filters.md +6 -0
- package/views/docs/controls/radio.md +4 -0
- package/views/docs/controls/select.md +8 -0
- package/views/docs/controls/sheet.md +4 -0
- package/views/docs/controls/sort-editor.md +8 -0
- package/views/docs/controls/text-editor.md +4 -0
- package/views/docs/controls/values-editor.md +4 -0
- package/views/docs/controls/views-list.md +6 -0
- package/views/docs/guides/the-grid.md +2 -2
- package/views/docs/guides/the-provider.md +19 -2
- package/views/docs/reference/controls.md +7 -5
- package/views/docs/reference/field-row.md +1 -1
- package/views/docs/reference/grid.md +1 -1
- package/views/docs/reference/list.md +141 -11
- package/views/docs/reference/panels-react.md +2 -2
- package/views/docs/reference/react.md +15 -9
- package/views/docs/reference/views.md +8 -4
- package/views/docs/reshaping.md +6 -1
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext, createElement, useCallback, useContext, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore, } from "react";
|
|
3
|
+
import { LIST_SETTINGS_DEFAULTS, readListLink } from "viewkitjs/fields/query";
|
|
4
|
+
import { ListRadioContext, ToolbarRadioContext, useToolbarRadioState } from "../panels/react/toolbar/context.js";
|
|
5
|
+
import { ListRegistryContext } from "./registry.js";
|
|
6
|
+
import { ViewsContext } from "../provider/context.js";
|
|
7
|
+
import { browserViewsStore } from "./browser-views-store.js";
|
|
8
|
+
import { openViewKey, useKeptView, useListLink } from "./link.js";
|
|
9
|
+
import { createListStore } from "./store.js";
|
|
10
|
+
import { createListViews } from "./views.js";
|
|
11
|
+
const ListStoreContext = createContext(null);
|
|
12
|
+
export function ListRootBase({ id, children, share, ...props }) {
|
|
13
|
+
const mode = useStoreMode(id, props, share);
|
|
14
|
+
const extra = { sortFilter: props.sortFilter, bar: props.bar };
|
|
15
|
+
const sync = mode ? mode.views.sync(extra) : props;
|
|
16
|
+
const [store] = useState(() => createListStore(id, sync));
|
|
17
|
+
useListLink(store, !!mode && props.link === true, mode?.defaults ?? LIST_SETTINGS_DEFAULTS);
|
|
18
|
+
useKeptView(store, id, !!mode);
|
|
19
|
+
store.sync(sync);
|
|
20
|
+
useLayoutEffect(() => store.flush());
|
|
21
|
+
useStoreViews(mode, store, extra);
|
|
22
|
+
const registry = useContext(ListRegistryContext);
|
|
23
|
+
useLayoutEffect(() => registry?.addStore(store.get().id, store), [registry, store]);
|
|
24
|
+
const radio = useToolbarRadioState();
|
|
25
|
+
return createElement(ListStoreContext.Provider, { value: store }, createElement(ListRadioContext.Provider, { value: radio }, createElement(ToolbarRadioContext.Provider, { value: radio }, children)));
|
|
26
|
+
}
|
|
27
|
+
export function useListStore() {
|
|
28
|
+
const store = useContext(ListStoreContext);
|
|
29
|
+
if (!store)
|
|
30
|
+
throw new Error("useList reads a list's state, and needs a ListRoot around it");
|
|
31
|
+
return store;
|
|
32
|
+
}
|
|
33
|
+
export function useList(...args) {
|
|
34
|
+
const byId = typeof args[0] === "string" ? args[0] : null;
|
|
35
|
+
const [select, equal = Object.is] = (byId === null ? args : args.slice(1));
|
|
36
|
+
const inside = useContext(ListStoreContext);
|
|
37
|
+
const registry = useContext(ListRegistryContext);
|
|
38
|
+
if (byId === null && !inside)
|
|
39
|
+
throw new Error("useList reads a list's state, and needs a ListRoot around it");
|
|
40
|
+
if (byId !== null && !registry)
|
|
41
|
+
throw new Error("useList(id, select) reads a list by its id, and needs a ViewsProvider around it");
|
|
42
|
+
const storeOf = () => (byId === null ? (inside ?? undefined) : registry?.store(byId));
|
|
43
|
+
const subscribe = useCallback((listener) => {
|
|
44
|
+
if (byId === null || !registry)
|
|
45
|
+
return inside ? inside.subscribe(listener) : () => { };
|
|
46
|
+
let off = registry.store(byId)?.subscribe(listener) ?? (() => { });
|
|
47
|
+
const offRegistry = registry.subscribe(() => {
|
|
48
|
+
off();
|
|
49
|
+
off = registry.store(byId)?.subscribe(listener) ?? (() => { });
|
|
50
|
+
listener();
|
|
51
|
+
});
|
|
52
|
+
return () => {
|
|
53
|
+
offRegistry();
|
|
54
|
+
off();
|
|
55
|
+
};
|
|
56
|
+
}, [byId, registry, inside]);
|
|
57
|
+
const memo = useRef(null);
|
|
58
|
+
const read = () => {
|
|
59
|
+
const state = storeOf()?.get();
|
|
60
|
+
if (!state) {
|
|
61
|
+
memo.current = null;
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
const last = memo.current;
|
|
65
|
+
if (last && last.state === state && last.select === select)
|
|
66
|
+
return last.value;
|
|
67
|
+
const value = select(state);
|
|
68
|
+
if (last && equal(last.value, value)) {
|
|
69
|
+
memo.current = { state, select, value: last.value };
|
|
70
|
+
return last.value;
|
|
71
|
+
}
|
|
72
|
+
memo.current = { state, select, value };
|
|
73
|
+
return value;
|
|
74
|
+
};
|
|
75
|
+
return useSyncExternalStore(subscribe, read, read);
|
|
76
|
+
}
|
|
77
|
+
export function useListPanel(key) {
|
|
78
|
+
const store = useListStore();
|
|
79
|
+
const open = useList((s) => s.panels.open === key);
|
|
80
|
+
const page = useList((s) => s.panels.pages[key] ?? null);
|
|
81
|
+
const onOpenChange = useCallback((next) => (next ? store.get().openPanel(key) : store.get().closePanel(key)), [store, key]);
|
|
82
|
+
const onPageChange = useCallback((next) => store.get().setPage(key, next), [store, key]);
|
|
83
|
+
return useMemo(() => ({ open, onOpenChange, page, onPageChange }), [open, onOpenChange, page, onPageChange]);
|
|
84
|
+
}
|
|
85
|
+
function useStoreMode(id, props, share) {
|
|
86
|
+
const prefs = useContext(ViewsContext)?.ports.viewPrefs;
|
|
87
|
+
const [mode] = useState(() => {
|
|
88
|
+
if (props.settings !== undefined)
|
|
89
|
+
return null;
|
|
90
|
+
const own = props;
|
|
91
|
+
const given = own.views;
|
|
92
|
+
const options = given && "list" in given ? { store: given } : (given ?? {});
|
|
93
|
+
const store = options.store ?? browserViewsStore(options.seed ? { seed: { [id]: options.seed } } : undefined);
|
|
94
|
+
let on = { change: () => { }, error: () => { } };
|
|
95
|
+
const defaults = own.defaults ?? LIST_SETTINGS_DEFAULTS;
|
|
96
|
+
const link = own.link === true ? (typeof window === "undefined" ? undefined : readListLink(window.location.search)) : own.link || undefined;
|
|
97
|
+
const kept = prefs?.get(openViewKey(id));
|
|
98
|
+
const views = createListViews({
|
|
99
|
+
id,
|
|
100
|
+
store,
|
|
101
|
+
defaults,
|
|
102
|
+
...(link && { link }),
|
|
103
|
+
...(kept !== undefined && { kept }),
|
|
104
|
+
...(share === false && { share: false }),
|
|
105
|
+
...(options.records !== undefined && { records: options.records }),
|
|
106
|
+
onChange: () => on.change(),
|
|
107
|
+
onError: (message) => on.error(message),
|
|
108
|
+
});
|
|
109
|
+
return { views, wire: (next) => void (on = next), defaults };
|
|
110
|
+
});
|
|
111
|
+
return mode;
|
|
112
|
+
}
|
|
113
|
+
function useStoreViews(mode, store, extra) {
|
|
114
|
+
const toast = useContext(ViewsContext)?.ports.toast.error;
|
|
115
|
+
useLayoutEffect(() => {
|
|
116
|
+
if (!mode)
|
|
117
|
+
return;
|
|
118
|
+
mode.wire({
|
|
119
|
+
change: () => {
|
|
120
|
+
store.sync(mode.views.sync(extra));
|
|
121
|
+
store.patch({ views: mode.views.state() });
|
|
122
|
+
store.flush();
|
|
123
|
+
},
|
|
124
|
+
error: (message) => toast?.(message),
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
const [, settle] = useState(0);
|
|
128
|
+
useLayoutEffect(() => {
|
|
129
|
+
if (!mode)
|
|
130
|
+
return;
|
|
131
|
+
store.patch({ views: mode.views.state() });
|
|
132
|
+
const stop = mode.views.start();
|
|
133
|
+
if (mode.views.state().ready)
|
|
134
|
+
settle((n) => n + 1);
|
|
135
|
+
return stop;
|
|
136
|
+
}, [mode, store]);
|
|
137
|
+
}
|
|
@@ -1,40 +1,41 @@
|
|
|
1
1
|
import { type ReactNode } from "react";
|
|
2
|
-
import
|
|
3
|
-
import type {
|
|
4
|
-
import
|
|
5
|
-
import type {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
2
|
+
import { type ListRow } from "viewkitjs/fields/query";
|
|
3
|
+
import type { ListSave } from "../grid/core/commit-model.js";
|
|
4
|
+
import type { QuickFiltersConfig } from "../panels/quick/model.js";
|
|
5
|
+
import type { ListSource } from "./source.js";
|
|
6
|
+
import { type ListColumnsOverride, type ListFieldsRegistry } from "./columns.js";
|
|
7
|
+
import { type ListRootModes } from "./root-base.js";
|
|
8
|
+
export { useList, useListPanel, useListStore } from "./root-base.js";
|
|
9
|
+
export type { ListViewsOptions } from "./root-base.js";
|
|
10
|
+
/** What the root holds of a list's data, in either mode. */
|
|
11
|
+
export type ListData<Row extends ListRow = ListRow> = {
|
|
12
|
+
/** The list's fields (`defineFields`): its columns and, in store mode, its default settings. */
|
|
13
|
+
fields?: ListFieldsRegistry;
|
|
14
|
+
/** The rows, held by the app; the root filters, sorts and groups them. Needs `fields`. */
|
|
15
|
+
rows?: readonly Row[];
|
|
16
|
+
/** Where the rows come from instead: one list request in, its answer out (`fetchSource`, or a server action). Whether
|
|
17
|
+
* there is one is read as the list mounts; each request goes to the one handed last. One whose narrowing changes
|
|
18
|
+
* (another company's deals) wants a `key` on the list, so it starts again. Needs `fields`. */
|
|
19
|
+
source?: ListSource<Row>;
|
|
20
|
+
/** A source's page: how many rows each request asks for. Default 50, held between 1 and 1000. */
|
|
21
|
+
pageSize?: number;
|
|
22
|
+
/** The quick filters the toolbar draws: their hold narrows the rows (in memory and from a source). */
|
|
23
|
+
quickFilters?: QuickFiltersConfig;
|
|
24
|
+
/** Columns changed by their field's key. */
|
|
25
|
+
columns?: ListColumnsOverride<Row>;
|
|
26
|
+
/** One save per record (`ListSave`, viewkitjs/views/grid). None: a read-only list. */
|
|
27
|
+
save?: ListSave<Row>;
|
|
28
|
+
/** `edit: false`: every cell read-only, as with no `save`. `share: false`: no "Save for everyone" (store mode). */
|
|
29
|
+
can?: {
|
|
30
|
+
edit?: boolean;
|
|
31
|
+
share?: boolean;
|
|
32
|
+
};
|
|
13
33
|
};
|
|
14
34
|
/** The list root's props: its id, and either its settings and layer as the app holds them (memory mode: what
|
|
15
35
|
* `useListSettingsState` returns, with the shared view), or no settings, and the root keeps them through a store. A
|
|
16
|
-
* root keeps the mode it mounted in. Either way, the toolbar's `sortFilter` and `bar
|
|
17
|
-
export type ListRootProps = {
|
|
36
|
+
* root keeps the mode it mounted in. Either way, the toolbar's `sortFilter` and `bar`, and the list's data. */
|
|
37
|
+
export type ListRootProps<Row extends ListRow = ListRow> = {
|
|
18
38
|
id: string;
|
|
19
39
|
children?: ReactNode;
|
|
20
|
-
} &
|
|
21
|
-
|
|
22
|
-
/** Where the list's saved views are kept: a store, or options; default `browserViewsStore()`. */
|
|
23
|
-
views?: ViewsStore | ListViewsOptions;
|
|
24
|
-
/** What a list with no saved view shows, and a new view's start. Default `LIST_SETTINGS_DEFAULTS`. */
|
|
25
|
-
defaults?: ListSettings;
|
|
26
|
-
/** The address's reading (`readListLink`) as the list opened: its view and change win over the draft. */
|
|
27
|
-
link?: ListLinkReading;
|
|
28
|
-
sortFilter?: ListSync["sortFilter"];
|
|
29
|
-
bar?: ListSync["bar"];
|
|
30
|
-
});
|
|
31
|
-
export declare function ListRoot({ id, children, ...props }: ListRootProps): ReactNode;
|
|
32
|
-
/** The store of the list root around this part (not on the door). */
|
|
33
|
-
export declare function useListStore(): ListStore;
|
|
34
|
-
/** What this part selects of its list's state: it re-renders only when that changes (by `equal`, `Object.is` by
|
|
35
|
-
* default). Select an action as it stands (`useList((s) => s.openPanel)`): actions never change. Handed an `id` first,
|
|
36
|
-
* it reads the root of that id from anywhere under the `ViewsProvider` (VK-740): `undefined` while none stands. */
|
|
37
|
-
export declare function useList<T>(select: (state: ListState) => T, equal?: (a: T, b: T) => boolean): T;
|
|
38
|
-
export declare function useList<T>(id: string, select: (state: ListState) => T, equal?: (a: T, b: T) => boolean): T | undefined;
|
|
39
|
-
/** One of the root's panels as a part's open state and page, by its key (not on the door). */
|
|
40
|
-
export declare function useListPanel(key: string): ToolbarPanelState;
|
|
40
|
+
} & ListData<Row> & ListRootModes;
|
|
41
|
+
export declare function ListRoot<Row extends ListRow = ListRow>({ fields, rows, source, pageSize, quickFilters, columns, save, can, children, ...base }: ListRootProps<Row>): ReactNode;
|
package/views/dist/list/root.js
CHANGED
|
@@ -1,128 +1,168 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { ListRegistryContext } from "./registry.js";
|
|
2
|
+
import { createElement, useContext, useLayoutEffect, useMemo, useReducer, useRef, useState } from "react";
|
|
3
|
+
import { presentedDef } from "viewkitjs/fields";
|
|
4
|
+
import { bucketRuleFor, } from "viewkitjs/fields/query";
|
|
6
5
|
import { ViewsContext } from "../provider/context.js";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const [
|
|
16
|
-
|
|
17
|
-
useLayoutEffect(() => store.flush());
|
|
18
|
-
useStoreViews(mode, store, extra);
|
|
19
|
-
const registry = useContext(ListRegistryContext);
|
|
20
|
-
useLayoutEffect(() => registry?.addStore(store.get().id, store), [registry, store]);
|
|
21
|
-
const radio = useToolbarRadioState();
|
|
22
|
-
return createElement(ListStoreContext.Provider, { value: store }, createElement(ListRadioContext.Provider, { value: radio }, createElement(ToolbarRadioContext.Provider, { value: radio }, children)));
|
|
6
|
+
import { deriveGridSelectValues } from "../grid/core/column-values.js";
|
|
7
|
+
import { createListAnswers, requestSettingsOf } from "./source-answers.js";
|
|
8
|
+
import { answerInMemory } from "./answer.js";
|
|
9
|
+
import { columnsOf } from "./columns.js";
|
|
10
|
+
import { ListCommitsContext, ListEditsSinkContext, useListCommits, useListEditsSink } from "./edits.js";
|
|
11
|
+
import { ListRootBase, useList, useListStore } from "./root-base.js";
|
|
12
|
+
export { useList, useListPanel, useListStore } from "./root-base.js";
|
|
13
|
+
export function ListRoot({ fields, rows, source, pageSize, quickFilters, columns, save, can, children, ...base }) {
|
|
14
|
+
const [defaults] = useState(() => base.settings === undefined && base.defaults === undefined && fields ? columnsOf(fields, columns, false).defaults : undefined);
|
|
15
|
+
return createElement(ListRootBase, { ...base, ...(defaults && { defaults }), ...(can?.share === false && { share: false }) }, createElement((RootData), { fields, rows, source, pageSize, quickFilters, columns, save, can }, children));
|
|
23
16
|
}
|
|
24
|
-
|
|
25
|
-
const store =
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
17
|
+
function RootData({ fields, rows, source, pageSize, quickFilters, columns: overrides, save, can, children, }) {
|
|
18
|
+
const store = useListStore();
|
|
19
|
+
const settings = useList((s) => s.view.settings);
|
|
20
|
+
const conventions = useContext(ViewsContext)?.conventions;
|
|
21
|
+
const writable = !!save && can?.edit !== false;
|
|
22
|
+
const hold = quickFilters?.hold;
|
|
23
|
+
const sourced = useSourceAnswers(store, source, pageSize, settings, hold, fields, conventions);
|
|
24
|
+
const { bindings, ...data } = useListData(fields, sourced ? undefined : rows, overrides, settings, conventions, writable, hold);
|
|
25
|
+
const parts = useSourceParts(store, sourced?.answers ?? null, quickFilters, fields, settings, sourced?.state.total ?? null);
|
|
26
|
+
const holdKey = JSON.stringify(hold?.length ? hold : null);
|
|
27
|
+
const heldKey = useRef(holdKey);
|
|
28
|
+
useLayoutEffect(() => {
|
|
29
|
+
if (heldKey.current === holdKey)
|
|
30
|
+
return;
|
|
31
|
+
heldKey.current = holdKey;
|
|
32
|
+
store.get().select([]);
|
|
33
|
+
}, [store, holdKey]);
|
|
34
|
+
const fromSource = useMemo(() => (sourced && data.columns ? withParts(sourced.state, data.columns, parts) : null), [sourced, data.columns, parts]);
|
|
35
|
+
store.syncData(fromSource ? { ...data, answer: fromSource } : data);
|
|
36
|
+
useLayoutEffect(() => store.flush());
|
|
37
|
+
const commits = useListCommits(save, writable, bindings);
|
|
38
|
+
const sink = useListEditsSink(store);
|
|
39
|
+
return createElement(ListCommitsContext.Provider, { value: commits }, createElement(ListEditsSinkContext.Provider, { value: sink }, children));
|
|
29
40
|
}
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
throw new Error("useList(id, select) reads a list by its id, and needs a ViewsProvider around it");
|
|
39
|
-
const storeOf = () => (byId === null ? (inside ?? undefined) : registry?.store(byId));
|
|
40
|
-
const subscribe = useCallback((listener) => {
|
|
41
|
-
if (byId === null || !registry)
|
|
42
|
-
return inside ? inside.subscribe(listener) : () => { };
|
|
43
|
-
let off = registry.store(byId)?.subscribe(listener) ?? (() => { });
|
|
44
|
-
const offRegistry = registry.subscribe(() => {
|
|
45
|
-
off();
|
|
46
|
-
off = registry.store(byId)?.subscribe(listener) ?? (() => { });
|
|
47
|
-
listener();
|
|
48
|
-
});
|
|
49
|
-
return () => {
|
|
50
|
-
offRegistry();
|
|
51
|
-
off();
|
|
52
|
-
};
|
|
53
|
-
}, [byId, registry, inside]);
|
|
54
|
-
const memo = useRef(null);
|
|
55
|
-
const read = () => {
|
|
56
|
-
const state = storeOf()?.get();
|
|
57
|
-
if (!state) {
|
|
58
|
-
memo.current = null;
|
|
59
|
-
return undefined;
|
|
60
|
-
}
|
|
61
|
-
const last = memo.current;
|
|
62
|
-
if (last && last.state === state && last.select === select)
|
|
63
|
-
return last.value;
|
|
64
|
-
const value = select(state);
|
|
65
|
-
if (last && equal(last.value, value)) {
|
|
66
|
-
memo.current = { state, select, value: last.value };
|
|
67
|
-
return last.value;
|
|
68
|
-
}
|
|
69
|
-
memo.current = { state, select, value };
|
|
70
|
-
return value;
|
|
71
|
-
};
|
|
72
|
-
return useSyncExternalStore(subscribe, read, read);
|
|
41
|
+
function useListData(fields, rows, overrides, settings, conventions, writable, hold) {
|
|
42
|
+
const of = useMemo(() => (fields ? columnsOf(fields, overrides, writable) : null), [fields, overrides, writable]);
|
|
43
|
+
const answer = useMemo(() => (fields && rows && of ? answerInMemory(rows, settings, fields, of.columns, conventions, of.bindings, hold) : null), [fields, rows, of, settings, conventions, hold]);
|
|
44
|
+
return useMemo(() => ({
|
|
45
|
+
columns: (of?.columns ?? null),
|
|
46
|
+
answer: answer,
|
|
47
|
+
bindings: of?.bindings ?? null,
|
|
48
|
+
}), [of, answer]);
|
|
73
49
|
}
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
50
|
+
function useSourceAnswers(store, source, pageSize, settings, hold, fields, conventions) {
|
|
51
|
+
const toast = useContext(ViewsContext)?.ports.toast.error;
|
|
52
|
+
const latest = useRef({ source, toast, fields, conventions });
|
|
53
|
+
useLayoutEffect(() => {
|
|
54
|
+
latest.current = { source, toast, fields, conventions };
|
|
55
|
+
});
|
|
56
|
+
const [, changed] = useReducer((n) => n + 1, 0);
|
|
57
|
+
const [answers] = useState(() => (source ? answersOver(latest, pageSize, changed) : null));
|
|
58
|
+
useLayoutEffect(() => {
|
|
59
|
+
answers?.ask(settings, hold);
|
|
60
|
+
}, [answers, settings, hold]);
|
|
61
|
+
useLayoutEffect(() => {
|
|
62
|
+
if (!answers)
|
|
63
|
+
return;
|
|
64
|
+
store.wire({ loadMore: answers.loadMore, loadGroup: answers.loadGroup });
|
|
65
|
+
return () => store.wire({ loadMore: undefined, loadGroup: undefined });
|
|
66
|
+
}, [answers, store]);
|
|
67
|
+
const state = answers?.state();
|
|
68
|
+
return useMemo(() => (answers && state ? { state, answers } : null), [answers, state]);
|
|
81
69
|
}
|
|
82
|
-
function
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const store = options.store ?? browserViewsStore(options.seed ? { seed: { [id]: options.seed } } : undefined);
|
|
90
|
-
let on = { change: () => { }, error: () => { } };
|
|
91
|
-
const views = createListViews({
|
|
92
|
-
id,
|
|
93
|
-
store,
|
|
94
|
-
defaults: own.defaults ?? LIST_SETTINGS_DEFAULTS,
|
|
95
|
-
...(own.link && { link: own.link }),
|
|
96
|
-
...(options.records !== undefined && { records: options.records }),
|
|
97
|
-
onChange: () => on.change(),
|
|
98
|
-
onError: (message) => on.error(message),
|
|
99
|
-
});
|
|
100
|
-
return { views, wire: (next) => void (on = next) };
|
|
70
|
+
function answersOver(latest, pageSize, changed) {
|
|
71
|
+
return createListAnswers({
|
|
72
|
+
source: (request) => latest.current.source(request),
|
|
73
|
+
...(pageSize !== undefined && { pageSize }),
|
|
74
|
+
onChange: changed,
|
|
75
|
+
onError: (message) => latest.current.toast?.(message),
|
|
76
|
+
nameGroup: (group, key) => groupName(latest.current.fields, latest.current.conventions, group, key),
|
|
101
77
|
});
|
|
102
|
-
return mode;
|
|
103
78
|
}
|
|
104
|
-
function
|
|
105
|
-
const
|
|
79
|
+
function groupName(fields, conventions, group, key) {
|
|
80
|
+
const field = fields?.[group.fieldKey];
|
|
81
|
+
if (!field)
|
|
82
|
+
return null;
|
|
83
|
+
return bucketRuleFor(field, group.by)?.labelOf(presentedDef(field), key, group.granularity, conventions) ?? null;
|
|
84
|
+
}
|
|
85
|
+
const FILTER_PANELS = new Set(["filter", "filter-add", "options"]);
|
|
86
|
+
const NO_VALUES = {};
|
|
87
|
+
function useSourceParts(store, answers, quickFilters, fields, settings, total) {
|
|
88
|
+
const views = useList((s) => s.views);
|
|
89
|
+
const open = useList((s) => s.panels.open);
|
|
90
|
+
const show = settings.showCounts !== false;
|
|
91
|
+
const [viewCounts, setViewCounts] = useState(null);
|
|
92
|
+
const [holdCounts, setHoldCounts] = useState(null);
|
|
93
|
+
const [values, setValues] = useState(NO_VALUES);
|
|
94
|
+
const totalRef = useRef(total);
|
|
95
|
+
useLayoutEffect(() => {
|
|
96
|
+
totalRef.current = total;
|
|
97
|
+
});
|
|
106
98
|
useLayoutEffect(() => {
|
|
107
|
-
if (!
|
|
99
|
+
if (!answers)
|
|
108
100
|
return;
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
store.
|
|
112
|
-
|
|
113
|
-
store.
|
|
101
|
+
store.wire({
|
|
102
|
+
selectAll: () => {
|
|
103
|
+
const before = store.get().selection;
|
|
104
|
+
const pending = { ids: [], all: true, count: totalRef.current?.count ?? null };
|
|
105
|
+
store.patch({ selection: pending });
|
|
106
|
+
void answers.ids().then((ids) => {
|
|
107
|
+
if (ids)
|
|
108
|
+
return store.get().select(ids);
|
|
109
|
+
if (store.get().selection === pending)
|
|
110
|
+
store.patch({ selection: before });
|
|
111
|
+
});
|
|
114
112
|
},
|
|
115
|
-
error: (message) => toast?.(message),
|
|
116
113
|
});
|
|
117
|
-
|
|
118
|
-
|
|
114
|
+
return () => store.wire({ selectAll: undefined });
|
|
115
|
+
}, [answers, store]);
|
|
116
|
+
const hold = quickFilters?.hold;
|
|
117
|
+
const asked = JSON.stringify([requestSettingsOf(settings), hold?.length ? hold : null]);
|
|
118
|
+
const askedViews = JSON.stringify([{ ...requestSettingsOf(settings), filter: null }, hold?.length ? hold : null]);
|
|
119
|
+
const viewsList = views?.ready ? views.list : null;
|
|
120
|
+
const viewsKey = JSON.stringify(viewsList?.map((v) => [v.id, v.settings.filter]) ?? []);
|
|
121
|
+
useLayoutEffect(() => {
|
|
122
|
+
const counted = JSON.parse(viewsKey);
|
|
123
|
+
if (!answers || counted.length === 0 || !show)
|
|
124
|
+
return;
|
|
125
|
+
let current = true;
|
|
126
|
+
void answers.viewCounts(counted.map(([id, filter]) => ({ id, filter }))).then((counts) => {
|
|
127
|
+
if (current)
|
|
128
|
+
setViewCounts(counts);
|
|
129
|
+
});
|
|
130
|
+
return () => void (current = false);
|
|
131
|
+
}, [answers, viewsKey, show, askedViews]);
|
|
132
|
+
const quickKeys = (quickFilters?.fields ?? []).map((f) => f.key).join("\n");
|
|
133
|
+
const quickOpen = open === "quick";
|
|
134
|
+
useLayoutEffect(() => {
|
|
135
|
+
if (!answers || !quickOpen || quickKeys === "")
|
|
136
|
+
return;
|
|
137
|
+
let current = true;
|
|
138
|
+
void answers.counts("hold", quickKeys.split("\n")).then((counts) => {
|
|
139
|
+
if (current && counts)
|
|
140
|
+
setHoldCounts(Object.fromEntries(Object.entries(counts).map(([key, c]) => [key, c.counts])));
|
|
141
|
+
});
|
|
142
|
+
return () => void (current = false);
|
|
143
|
+
}, [answers, quickOpen, quickKeys, asked]);
|
|
144
|
+
const filterOpen = open !== null && FILTER_PANELS.has(open);
|
|
145
|
+
const textKeys = Object.values(fields ?? {}).filter((field) => field.type === "text").map((field) => field.key).join("\n");
|
|
119
146
|
useLayoutEffect(() => {
|
|
120
|
-
if (!
|
|
147
|
+
if (!answers || !filterOpen || textKeys === "")
|
|
121
148
|
return;
|
|
122
|
-
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
149
|
+
let current = true;
|
|
150
|
+
const keys = textKeys.split("\n");
|
|
151
|
+
void Promise.all(keys.map((key) => answers.values(key).then((answer) => [key, answer?.values ?? []]))).then((pairs) => {
|
|
152
|
+
if (current)
|
|
153
|
+
setValues(Object.fromEntries(pairs));
|
|
154
|
+
});
|
|
155
|
+
return () => void (current = false);
|
|
156
|
+
}, [answers, filterOpen, textKeys, asked]);
|
|
157
|
+
return useMemo(() => ({ viewCounts: show ? viewCounts : null, holdCounts, values }), [show, viewCounts, holdCounts, values]);
|
|
158
|
+
}
|
|
159
|
+
function withParts(answer, columns, parts) {
|
|
160
|
+
const rows = answer.groups.flatMap((group) => group.items);
|
|
161
|
+
const inHand = deriveGridSelectValues(columns, rows);
|
|
162
|
+
return {
|
|
163
|
+
...answer,
|
|
164
|
+
viewCounts: parts.viewCounts,
|
|
165
|
+
holdCounts: parts.holdCounts,
|
|
166
|
+
filterValues: { ...answer.filterValues, selectValues: { ...inHand, ...parts.values } },
|
|
167
|
+
};
|
|
128
168
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type FilterTree, type ListAnswerOf, type ListHold, type ListRequestSettings, type ListRow, type ListSettings } from "viewkitjs/fields/query";
|
|
2
|
+
import type { ListAnswerState } from "./answer.js";
|
|
3
|
+
import type { ListSource } from "./source.js";
|
|
4
|
+
export type ListAnswersController<Row extends ListRow> = {
|
|
5
|
+
/** The settings (and quick filters' hold) the rows answer; a change to their request parts asks again. */
|
|
6
|
+
ask: (settings: ListSettings, hold: ListHold | undefined) => void;
|
|
7
|
+
/** The next page of the ungrouped list; nothing while one is out or there is none. */
|
|
8
|
+
loadMore: () => void;
|
|
9
|
+
/** The next page of one group (its first, where its rows weren't sent); nothing while one is out or there is none. */
|
|
10
|
+
loadGroup: (key: string) => void;
|
|
11
|
+
/** Every matching row's id (the `ids` part); null on a refusal, a throw, or an answer a newer ask has outrun. */
|
|
12
|
+
ids: () => Promise<string[] | null>;
|
|
13
|
+
/** Counts of a quick filter's or the filter's values (the `counts` part). */
|
|
14
|
+
counts: (of: "filter" | "hold", fields: readonly string[]) => Promise<ListAnswerOf<"counts">["counts"] | null>;
|
|
15
|
+
/** One field's values and their labels (the `values` part). */
|
|
16
|
+
values: (field: string) => Promise<{
|
|
17
|
+
values: string[];
|
|
18
|
+
labels: Record<string, string>;
|
|
19
|
+
} | null>;
|
|
20
|
+
/** Each view's count under its own filter (the `view-counts` part). */
|
|
21
|
+
viewCounts: (views: readonly {
|
|
22
|
+
id: string;
|
|
23
|
+
filter: FilterTree;
|
|
24
|
+
}[]) => Promise<Record<string, number> | null>;
|
|
25
|
+
state: () => ListAnswerState<Row>;
|
|
26
|
+
};
|
|
27
|
+
/** The settings' parts a request carries: the filter, the sorts and the group, the search where there is one, and the
|
|
28
|
+
* row order where the settings hold one. */
|
|
29
|
+
export declare function requestSettingsOf(settings: ListSettings): ListRequestSettings;
|
|
30
|
+
export declare function createListAnswers<Row extends ListRow>(o: {
|
|
31
|
+
source: ListSource<Row>;
|
|
32
|
+
pageSize?: number;
|
|
33
|
+
/** Told after each change of `state()`. */
|
|
34
|
+
onChange: () => void;
|
|
35
|
+
/** A source that threw or rejected, or answered another part, in words a person reads. */
|
|
36
|
+
onError: (message: string) => void;
|
|
37
|
+
/** A group's name where the source sent none: the grouped field's label for its key, or null. */
|
|
38
|
+
nameGroup?: (group: NonNullable<ListRequestSettings["group"]>, key: string) => string | null;
|
|
39
|
+
}): ListAnswersController<Row>;
|