@malloy-publisher/sdk 0.2.7 → 0.3.1
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 +180 -204
- package/dist/DocumentStorageProvider-BeBD0uWc.cjs.js +134 -0
- package/dist/DocumentStorageProvider-tGvodI8f.es.js +24268 -0
- package/dist/RenderedResult-CPi6uQWq.cjs.js +115 -0
- package/dist/RenderedResult-mDE-4u8S.es.js +542 -0
- package/dist/{ServerProvider-xXW1HJOJ.cjs.js → ServerProvider-CH71GsoL.cjs.js} +1 -1
- package/dist/{ServerProvider-ksC5Su_x.es.js → ServerProvider-X2Ma0HRs.es.js} +324 -359
- package/dist/builder/index.cjs.js +105 -0
- package/dist/builder/index.d.ts +2 -0
- package/dist/builder/index.es.js +9561 -0
- package/dist/builder-entry.d.ts +8 -0
- package/dist/client/api.d.ts +13 -1
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +1 -1
- package/dist/components/ApiErrorDisplay.d.ts +1 -2
- package/dist/components/Dashboard/Dashboard.d.ts +23 -16
- package/dist/components/Dashboard/DashboardGrid.d.ts +67 -0
- package/dist/components/Dashboard/DashboardTile.d.ts +6 -1
- package/dist/components/Dashboard/ExploreDialog.d.ts +18 -0
- package/dist/components/Dashboard/RowsDialog.d.ts +47 -0
- package/dist/components/Dashboard/TileCard.d.ts +44 -0
- package/dist/components/Dashboard/index.d.ts +2 -0
- package/dist/components/Dashboard/promoteMeasureRow.d.ts +40 -0
- package/dist/components/Dashboard/telemetry.d.ts +43 -0
- package/dist/components/DashboardBuilder/AddTileDialog.d.ts +35 -0
- package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +29 -0
- package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +112 -0
- package/dist/components/DashboardBuilder/DashboardEditor.d.ts +32 -0
- package/dist/components/DashboardBuilder/DiffDialog.d.ts +15 -0
- package/dist/components/DashboardBuilder/DrillDialog.d.ts +15 -0
- package/dist/components/DashboardBuilder/FilterDialog.d.ts +54 -0
- package/dist/components/DashboardBuilder/FilterStrip.d.ts +17 -0
- package/dist/components/DashboardBuilder/SettingsPopover.d.ts +20 -0
- package/dist/components/DashboardBuilder/TileFrame.d.ts +42 -0
- package/dist/components/DashboardBuilder/TileMenu.d.ts +24 -0
- package/dist/components/DashboardBuilder/catalog.d.ts +62 -0
- package/dist/components/DashboardBuilder/controls.d.ts +157 -0
- package/dist/components/DashboardBuilder/diff.d.ts +35 -0
- package/dist/components/DashboardBuilder/document.d.ts +218 -0
- package/dist/components/DashboardBuilder/layout.d.ts +71 -0
- package/dist/components/DashboardBuilder/malloyText.d.ts +45 -0
- package/dist/components/DashboardBuilder/preview.d.ts +60 -0
- package/dist/components/DashboardBuilder/readDocument.d.ts +106 -0
- package/dist/components/DashboardBuilder/sortable.d.ts +51 -0
- package/dist/components/DashboardBuilder/spliceDocument.d.ts +44 -0
- package/dist/components/DashboardBuilder/testing/fixtures.d.ts +10 -0
- package/dist/components/DashboardBuilder/useBuilderShortcuts.d.ts +22 -0
- package/dist/components/DashboardBuilder/useDashboardEditor.d.ts +63 -0
- package/dist/components/DashboardBuilder/useDraft.d.ts +13 -0
- package/dist/components/DashboardBuilder/useFilterForm.d.ts +65 -0
- package/dist/components/DashboardBuilder/useTileReorder.d.ts +22 -0
- package/dist/components/DashboardBuilder/useTileResize.d.ts +29 -0
- package/dist/components/DocumentStorage/BrowserDocumentStorage.d.ts +14 -0
- package/dist/components/DocumentStorage/DocumentStorage.d.ts +58 -0
- package/dist/components/DocumentStorage/DocumentStorageProvider.d.ts +25 -0
- package/dist/components/DocumentStorage/index.d.ts +3 -0
- package/dist/components/FloatingIconButton.d.ts +16 -0
- package/dist/components/Model/index.d.ts +0 -1
- package/dist/components/Prose.d.ts +46 -0
- package/dist/components/RenderedResult/RenderedResult.d.ts +12 -0
- package/dist/components/RenderedResult/ResultContainer.d.ts +7 -1
- package/dist/components/RenderedResult/ResultPanel.d.ts +30 -0
- package/dist/components/RenderedResult/index.d.ts +2 -0
- package/dist/components/RenderedResult/resultSizing.d.ts +183 -0
- package/dist/components/drill/index.d.ts +2 -1
- package/dist/components/drill/resolveDrill.d.ts +22 -0
- package/dist/components/drill/useDrill.d.ts +11 -2
- package/dist/components/drill/useDrillSelf.d.ts +46 -0
- package/dist/components/given/filterValue.d.ts +63 -0
- package/dist/components/given/index.d.ts +1 -0
- package/dist/components/given/useGivenUrlParams.d.ts +39 -0
- package/dist/components/index.d.ts +3 -3
- package/dist/components/styles.d.ts +3 -7
- package/dist/core-8NVBCLZh.cjs.js +148 -0
- package/dist/{core-BY7vB5gN.es.js → core-B8oMW4sm.es.js} +3540 -2335
- package/dist/hooks/index.d.ts +3 -1
- package/dist/hooks/useDocumentControls.d.ts +70 -0
- package/dist/hooks/useQueryResult.d.ts +40 -0
- package/dist/hooks/useSuggestOptions.d.ts +28 -12
- package/dist/index.cjs.js +34 -1
- package/dist/index.es.js +21487 -79
- package/dist/sdk.css +1 -1
- package/dist/styles.css +0 -1
- package/dist/utils/malloyLiteral.d.ts +11 -0
- package/package.json +25 -14
- package/src/builder-entry.ts +55 -0
- package/src/components/ApiErrorDisplay.tsx +1 -1
- package/src/components/Connections/EditConnectionDialog.tsx +96 -86
- package/src/components/Dashboard/Dashboard.spec.tsx +7 -10
- package/src/components/Dashboard/Dashboard.tsx +184 -229
- package/src/components/Dashboard/DashboardGrid.tsx +124 -0
- package/src/components/Dashboard/DashboardTile.spec.tsx +30 -6
- package/src/components/Dashboard/DashboardTile.tsx +67 -101
- package/src/components/Dashboard/ExploreDialog.tsx +84 -0
- package/src/components/Dashboard/RowsDialog.spec.ts +42 -0
- package/src/components/Dashboard/RowsDialog.tsx +234 -0
- package/src/components/Dashboard/TileCard.tsx +120 -0
- package/src/components/Dashboard/index.ts +2 -0
- package/src/components/Dashboard/promoteMeasureRow.spec.ts +207 -0
- package/src/components/Dashboard/promoteMeasureRow.ts +160 -0
- package/src/components/Dashboard/telemetry.ts +44 -0
- package/src/components/Dashboard/tileGridColumn.spec.ts +1 -1
- package/src/components/DashboardBuilder/AddTileDialog.tsx +249 -0
- package/src/components/DashboardBuilder/BuilderToolbar.tsx +270 -0
- package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +858 -0
- package/src/components/DashboardBuilder/DashboardBuilder.tsx +653 -0
- package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +211 -0
- package/src/components/DashboardBuilder/DashboardEditor.tsx +493 -0
- package/src/components/DashboardBuilder/DiffDialog.tsx +129 -0
- package/src/components/DashboardBuilder/DrillDialog.tsx +281 -0
- package/src/components/DashboardBuilder/FilterDialog.tsx +581 -0
- package/src/components/DashboardBuilder/FilterStrip.tsx +125 -0
- package/src/components/DashboardBuilder/SettingsPopover.tsx +168 -0
- package/src/components/DashboardBuilder/TileFrame.tsx +380 -0
- package/src/components/DashboardBuilder/TileMenu.tsx +188 -0
- package/src/components/DashboardBuilder/catalog.spec.ts +215 -0
- package/src/components/DashboardBuilder/catalog.ts +225 -0
- package/src/components/DashboardBuilder/controls.spec.ts +243 -0
- package/src/components/DashboardBuilder/controls.ts +398 -0
- package/src/components/DashboardBuilder/diff.spec.ts +44 -0
- package/src/components/DashboardBuilder/diff.ts +93 -0
- package/src/components/DashboardBuilder/document.ts +209 -0
- package/src/components/DashboardBuilder/keepRowStructure.spec.ts +84 -0
- package/src/components/DashboardBuilder/layout.ts +143 -0
- package/src/components/DashboardBuilder/malloyText.spec.ts +84 -0
- package/src/components/DashboardBuilder/malloyText.ts +151 -0
- package/src/components/DashboardBuilder/moveIntoGap.spec.ts +90 -0
- package/src/components/DashboardBuilder/preview.spec.ts +149 -0
- package/src/components/DashboardBuilder/preview.ts +172 -0
- package/src/components/DashboardBuilder/readDocument.spec.ts +375 -0
- package/src/components/DashboardBuilder/readDocument.ts +467 -0
- package/src/components/DashboardBuilder/sortable.tsx +151 -0
- package/src/components/DashboardBuilder/spliceDocument.spec.ts +1108 -0
- package/src/components/DashboardBuilder/spliceDocument.ts +1098 -0
- package/src/components/DashboardBuilder/testing/fixtures.ts +48 -0
- package/src/components/DashboardBuilder/useBuilderShortcuts.ts +88 -0
- package/src/components/DashboardBuilder/useDashboardEditor.spec.ts +255 -0
- package/src/components/DashboardBuilder/useDashboardEditor.ts +193 -0
- package/src/components/DashboardBuilder/useDraft.ts +43 -0
- package/src/components/DashboardBuilder/useFilterForm.spec.tsx +144 -0
- package/src/components/DashboardBuilder/useFilterForm.ts +352 -0
- package/src/components/DashboardBuilder/useTileReorder.spec.tsx +97 -0
- package/src/components/DashboardBuilder/useTileReorder.ts +115 -0
- package/src/components/DashboardBuilder/useTileResize.spec.tsx +121 -0
- package/src/components/DashboardBuilder/useTileResize.ts +110 -0
- package/src/components/DashboardBuilder/withGaps.spec.ts +96 -0
- package/src/components/DocumentStorage/BrowserDocumentStorage.spec.ts +81 -0
- package/src/components/DocumentStorage/BrowserDocumentStorage.ts +105 -0
- package/src/components/DocumentStorage/DocumentStorage.ts +73 -0
- package/src/components/DocumentStorage/DocumentStorageProvider.tsx +58 -0
- package/src/components/DocumentStorage/index.ts +16 -0
- package/src/components/Environment/About.tsx +4 -36
- package/src/components/FloatingIconButton.tsx +41 -0
- package/src/components/Model/Model.tsx +6 -23
- package/src/components/Model/ModelCell.tsx +34 -72
- package/src/components/Model/index.ts +0 -1
- package/src/components/Notebook/Notebook.tsx +27 -156
- package/src/components/Notebook/NotebookCell.tsx +37 -191
- package/src/components/Prose.spec.tsx +85 -0
- package/src/components/Prose.tsx +179 -0
- package/src/components/QueryResult/QueryResult.tsx +15 -43
- package/src/components/RenderedResult/RenderedResult.tsx +86 -76
- package/src/components/RenderedResult/ResultContainer.tsx +25 -36
- package/src/components/RenderedResult/ResultPanel.spec.tsx +49 -0
- package/src/components/RenderedResult/ResultPanel.tsx +73 -0
- package/src/components/RenderedResult/index.ts +16 -0
- package/src/components/RenderedResult/resultSizing.spec.ts +249 -0
- package/src/components/RenderedResult/resultSizing.ts +308 -0
- package/src/components/ResultsDialog.tsx +2 -1
- package/src/components/drill/index.ts +6 -0
- package/src/components/drill/resolveDrill.ts +22 -1
- package/src/components/drill/useDrill.spec.tsx +94 -0
- package/src/components/drill/useDrill.tsx +52 -12
- package/src/components/drill/useDrillSelf.spec.tsx +64 -0
- package/src/components/drill/useDrillSelf.ts +110 -0
- package/src/components/given/GivenInput.spec.tsx +196 -2
- package/src/components/given/GivenInput.tsx +300 -25
- package/src/components/given/GivensPanel.tsx +1 -1
- package/src/components/given/filterValue.spec.ts +112 -0
- package/src/components/given/filterValue.ts +188 -0
- package/src/components/given/index.ts +4 -0
- package/src/components/given/useGivenUrlParams.spec.tsx +74 -0
- package/src/components/given/useGivenUrlParams.ts +87 -0
- package/src/components/index.ts +11 -3
- package/src/components/styles.ts +0 -37
- package/src/hooks/index.ts +13 -1
- package/src/hooks/useDocumentControls.ts +170 -0
- package/src/hooks/useGivensState.spec.ts +158 -0
- package/src/hooks/useGivensState.ts +114 -23
- package/src/hooks/useQueryResult.ts +82 -0
- package/src/hooks/useSuggestOptions.dom.spec.ts +60 -1
- package/src/hooks/useSuggestOptions.ts +94 -51
- package/src/utils/malloyLiteral.spec.ts +43 -0
- package/src/utils/malloyLiteral.ts +76 -0
- package/vite.config.ts +5 -1
- package/dist/components/AnalyzePackageButton.d.ts +0 -6
- package/dist/components/Workbook/BrowserWorkbookStorage.d.ts +0 -9
- package/dist/components/Workbook/EditableMalloyCell.d.ts +0 -11
- package/dist/components/Workbook/ModelPicker.d.ts +0 -11
- package/dist/components/Workbook/MutableCell.d.ts +0 -17
- package/dist/components/Workbook/Workbook.d.ts +0 -7
- package/dist/components/Workbook/WorkbookList.d.ts +0 -7
- package/dist/components/Workbook/WorkbookManager.d.ts +0 -84
- package/dist/components/Workbook/WorkbookStorage.d.ts +0 -17
- package/dist/components/Workbook/WorkbookStorageProvider.d.ts +0 -12
- package/dist/components/Workbook/index.d.ts +0 -7
- package/dist/core-CWKmWEg2.cjs.js +0 -148
- package/dist/index-B0WyZAUA.es.js +0 -66412
- package/dist/index-kxVD1RjY.cjs.js +0 -356
- package/dist/markdown-editor.css +0 -341
- package/src/components/AnalyzePackageButton.tsx +0 -319
- package/src/components/Workbook/BrowserWorkbookStorage.ts +0 -64
- package/src/components/Workbook/EditableMalloyCell.tsx +0 -49
- package/src/components/Workbook/ModelPicker.tsx +0 -134
- package/src/components/Workbook/MutableCell.tsx +0 -458
- package/src/components/Workbook/Workbook.tsx +0 -501
- package/src/components/Workbook/WorkbookList.tsx +0 -110
- package/src/components/Workbook/WorkbookManager.ts +0 -214
- package/src/components/Workbook/WorkbookStorage.ts +0 -35
- package/src/components/Workbook/WorkbookStorageProvider.tsx +0 -40
- package/src/components/Workbook/index.ts +0 -10
|
@@ -72,6 +72,27 @@ export interface DrillClickPayload {
|
|
|
72
72
|
* a modifier was held (cmd/ctrl to open in a new tab).
|
|
73
73
|
*/
|
|
74
74
|
event?: MouseEvent;
|
|
75
|
+
/**
|
|
76
|
+
* Where the click came from, as the host names it — for a dashboard, the
|
|
77
|
+
* tile expression whose result was clicked. Set by the host when it wraps
|
|
78
|
+
* a binding per tile; the renderer never fills it. What "the rows behind
|
|
79
|
+
* this value" runs against is read from here.
|
|
80
|
+
*/
|
|
81
|
+
context?: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* A click asking for the rows behind a value, on a surface that offers them.
|
|
86
|
+
* No tag is involved: any grouped value has rows behind it.
|
|
87
|
+
*/
|
|
88
|
+
export interface DrillRowsRequest {
|
|
89
|
+
/** The clicked field's name, as the result spells it. */
|
|
90
|
+
field: string;
|
|
91
|
+
rawValue: unknown;
|
|
92
|
+
/** The value as it read on screen. */
|
|
93
|
+
label: string;
|
|
94
|
+
/** {@link DrillClickPayload.context}, when the host set one. */
|
|
95
|
+
context?: string;
|
|
75
96
|
}
|
|
76
97
|
|
|
77
98
|
/** The literal `to=self`: filter the current view instead of leaving it. */
|
|
@@ -232,7 +253,7 @@ export function drillValueToFilter(value: unknown): string | undefined {
|
|
|
232
253
|
}
|
|
233
254
|
|
|
234
255
|
/** How the clicked value read on screen, for a menu label. */
|
|
235
|
-
function drillValueLabel(value: unknown): string {
|
|
256
|
+
export function drillValueLabel(value: unknown): string {
|
|
236
257
|
if (value instanceof Date) return drillValueToFilter(value) ?? "";
|
|
237
258
|
return String(value);
|
|
238
259
|
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { act, renderHook } from "@testing-library/react";
|
|
5
|
+
import { describe, expect, it, mock } from "bun:test";
|
|
6
|
+
import type { DrillField, DrillTagReader } from "./resolveDrill";
|
|
7
|
+
import { useDrill } from "./useDrill";
|
|
8
|
+
|
|
9
|
+
const tag = (drill?: { to: string[]; given?: string }): DrillTagReader => ({
|
|
10
|
+
tag: (...at: string[]) =>
|
|
11
|
+
drill && at[0] === "drill" ? tag(drill) : undefined,
|
|
12
|
+
text: (...at: string[]) =>
|
|
13
|
+
at[0] === "given"
|
|
14
|
+
? drill?.given
|
|
15
|
+
: at[0] === "to"
|
|
16
|
+
? drill?.to[0]
|
|
17
|
+
: undefined,
|
|
18
|
+
textArray: (...at: string[]) => (at[0] === "to" ? drill?.to : undefined),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const dimension = (
|
|
22
|
+
name: string,
|
|
23
|
+
drill?: { to: string[]; given?: string },
|
|
24
|
+
): DrillField => ({ name, tag: tag(drill), wasDimension: () => true });
|
|
25
|
+
const measure = (name: string): DrillField => ({
|
|
26
|
+
name,
|
|
27
|
+
tag: tag(),
|
|
28
|
+
wasDimension: () => false,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe("useDrill: the rows behind a value", () => {
|
|
32
|
+
it("opens the rows for a grouped value that has no drill, and marks it clickable", () => {
|
|
33
|
+
const onRows = mock(() => {});
|
|
34
|
+
const { result } = renderHook(() => useDrill({ onRows }));
|
|
35
|
+
expect(result.current.drill.canDrill(dimension("cat"))).toBe(true);
|
|
36
|
+
expect(result.current.drill.canDrill(measure("revenue"))).toBe(false);
|
|
37
|
+
act(() =>
|
|
38
|
+
result.current.drill.onClick({
|
|
39
|
+
field: dimension("cat"),
|
|
40
|
+
value: "Jeans",
|
|
41
|
+
context: "overview -> by_cat",
|
|
42
|
+
}),
|
|
43
|
+
);
|
|
44
|
+
expect(onRows).toHaveBeenCalledWith({
|
|
45
|
+
field: "cat",
|
|
46
|
+
rawValue: "Jeans",
|
|
47
|
+
label: "Jeans",
|
|
48
|
+
context: "overview -> by_cat",
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("does nothing for a header, a measure, or an empty cell", () => {
|
|
53
|
+
const onRows = mock(() => {});
|
|
54
|
+
const { result } = renderHook(() => useDrill({ onRows }));
|
|
55
|
+
act(() => {
|
|
56
|
+
result.current.drill.onClick({
|
|
57
|
+
field: dimension("cat"),
|
|
58
|
+
value: "Jeans",
|
|
59
|
+
isHeader: true,
|
|
60
|
+
});
|
|
61
|
+
result.current.drill.onClick({ field: measure("revenue"), value: 3 });
|
|
62
|
+
result.current.drill.onClick({ field: dimension("cat"), value: null });
|
|
63
|
+
});
|
|
64
|
+
expect(onRows).not.toHaveBeenCalled();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("leaves a drilled value to its drill: one destination still navigates at once", () => {
|
|
68
|
+
const onRows = mock(() => {});
|
|
69
|
+
const onSelf = mock(() => {});
|
|
70
|
+
const { result } = renderHook(() => useDrill({ onRows, onSelf }));
|
|
71
|
+
act(() =>
|
|
72
|
+
result.current.drill.onClick({
|
|
73
|
+
field: dimension("cat", { to: ["self"], given: "CATEGORY" }),
|
|
74
|
+
value: "Jeans",
|
|
75
|
+
context: "overview -> by_cat",
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
expect(onSelf).toHaveBeenCalledWith("CATEGORY", "Jeans");
|
|
79
|
+
expect(onRows).not.toHaveBeenCalled();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("still navigates straight away when rows are not on offer", () => {
|
|
83
|
+
const onSelf = mock(() => {});
|
|
84
|
+
const { result } = renderHook(() => useDrill({ onSelf }));
|
|
85
|
+
expect(result.current.drill.canDrill(dimension("cat"))).toBe(false);
|
|
86
|
+
act(() =>
|
|
87
|
+
result.current.drill.onClick({
|
|
88
|
+
field: dimension("cat", { to: ["self"], given: "CATEGORY" }),
|
|
89
|
+
value: "Jeans",
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
expect(onSelf).toHaveBeenCalledWith("CATEGORY", "Jeans");
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -8,11 +8,14 @@ import {
|
|
|
8
8
|
DRILL_SELF,
|
|
9
9
|
drillDestinations,
|
|
10
10
|
drillGivenName,
|
|
11
|
+
drillValueLabel,
|
|
12
|
+
drillValueToFilter,
|
|
11
13
|
humanizeSlug,
|
|
12
14
|
resolveDrill,
|
|
13
15
|
type DrillClickPayload,
|
|
14
16
|
type DrillIntent,
|
|
15
17
|
type DrillNavigation,
|
|
18
|
+
type DrillRowsRequest,
|
|
16
19
|
} from "./resolveDrill";
|
|
17
20
|
|
|
18
21
|
export interface UseDrillOptions {
|
|
@@ -43,6 +46,15 @@ export interface UseDrillOptions {
|
|
|
43
46
|
* cannot know which document the reader is in, so the surface names itself.
|
|
44
47
|
*/
|
|
45
48
|
selfLabel?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Show the rows behind a grouped value. With it, EVERY dimension cell is
|
|
51
|
+
* clickable: one with no `# drill` this surface can honour opens the rows;
|
|
52
|
+
* one with a drill behaves exactly as it does without this option, since a
|
|
53
|
+
* drill is the author's own instruction for that value. Omitted on a
|
|
54
|
+
* surface with nowhere to show them, and cells then click only where a tag
|
|
55
|
+
* says so.
|
|
56
|
+
*/
|
|
57
|
+
onRows?: (request: DrillRowsRequest) => void;
|
|
46
58
|
}
|
|
47
59
|
|
|
48
60
|
/**
|
|
@@ -70,11 +82,12 @@ export interface UseDrillResult {
|
|
|
70
82
|
}
|
|
71
83
|
|
|
72
84
|
interface DrillMenuState {
|
|
73
|
-
intent
|
|
85
|
+
intent?: DrillIntent;
|
|
74
86
|
destinations: string[];
|
|
75
87
|
event?: MouseEvent;
|
|
76
88
|
top: number;
|
|
77
89
|
left: number;
|
|
90
|
+
label: string;
|
|
78
91
|
}
|
|
79
92
|
|
|
80
93
|
/**
|
|
@@ -91,6 +104,7 @@ export function useDrill({
|
|
|
91
104
|
onSelf,
|
|
92
105
|
selfLabel = "Filter this view",
|
|
93
106
|
canSelf,
|
|
107
|
+
onRows,
|
|
94
108
|
}: UseDrillOptions): UseDrillResult {
|
|
95
109
|
// A cell inside the renderer's own DOM is the anchor, and this component
|
|
96
110
|
// holds no ref to it, so the menu is positioned from the click coordinates.
|
|
@@ -128,35 +142,60 @@ export function useDrill({
|
|
|
128
142
|
[onNavigate, onSelf],
|
|
129
143
|
);
|
|
130
144
|
|
|
145
|
+
// The rows behind this click, when the surface shows rows and the click is
|
|
146
|
+
// on a grouped value. A header names the field and has no value; a measure
|
|
147
|
+
// has no rows of its own; an empty cell is more likely a misclick.
|
|
148
|
+
const rowsBehind = useCallback(
|
|
149
|
+
(payload: DrillClickPayload): DrillRowsRequest | undefined => {
|
|
150
|
+
if (!onRows || payload.isHeader || !payload.field) return undefined;
|
|
151
|
+
if (!(payload.field.wasDimension?.() ?? false)) return undefined;
|
|
152
|
+
if (drillValueToFilter(payload.value) === undefined) return undefined;
|
|
153
|
+
return {
|
|
154
|
+
field: payload.field.name,
|
|
155
|
+
rawValue: payload.value,
|
|
156
|
+
label: drillValueLabel(payload.value),
|
|
157
|
+
...(payload.context === undefined
|
|
158
|
+
? {}
|
|
159
|
+
: { context: payload.context }),
|
|
160
|
+
};
|
|
161
|
+
},
|
|
162
|
+
[onRows],
|
|
163
|
+
);
|
|
164
|
+
|
|
131
165
|
const onClick = useCallback(
|
|
132
166
|
(payload: DrillClickPayload) => {
|
|
133
167
|
// Runs on every cell click, so "not a drill" is the ordinary answer.
|
|
134
168
|
const intent = resolveDrill(payload);
|
|
135
|
-
|
|
136
|
-
|
|
169
|
+
const rows = rowsBehind(payload);
|
|
137
170
|
// Offer only what this surface can actually do, so a menu never lists a
|
|
138
171
|
// destination that would be a no-op when picked.
|
|
139
|
-
const destinations = honorable(intent.to, intent.given);
|
|
140
|
-
if (destinations.length === 0)
|
|
141
|
-
|
|
172
|
+
const destinations = intent ? honorable(intent.to, intent.given) : [];
|
|
173
|
+
if (destinations.length === 0) {
|
|
174
|
+
if (rows) onRows?.(rows);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (destinations.length === 1 && intent) {
|
|
142
178
|
go(intent, destinations[0], payload.event);
|
|
143
179
|
return;
|
|
144
180
|
}
|
|
145
181
|
setMenu({
|
|
146
|
-
intent,
|
|
182
|
+
...(intent ? { intent } : {}),
|
|
147
183
|
destinations,
|
|
148
184
|
event: payload.event,
|
|
149
185
|
top: payload.event?.clientY ?? 0,
|
|
150
186
|
left: payload.event?.clientX ?? 0,
|
|
187
|
+
label: intent?.label ?? rows?.label ?? "",
|
|
151
188
|
});
|
|
152
189
|
},
|
|
153
|
-
[go, honorable],
|
|
190
|
+
[go, honorable, onRows, rowsBehind],
|
|
154
191
|
);
|
|
155
192
|
|
|
156
193
|
const canDrill = useCallback(
|
|
157
194
|
(field: DrillField) =>
|
|
158
|
-
honorable(drillDestinations(field), drillGivenName(field)).length >
|
|
159
|
-
|
|
195
|
+
honorable(drillDestinations(field), drillGivenName(field)).length >
|
|
196
|
+
0 ||
|
|
197
|
+
(onRows !== undefined && (field.wasDimension?.() ?? false)),
|
|
198
|
+
[honorable, onRows],
|
|
160
199
|
);
|
|
161
200
|
|
|
162
201
|
const drill = useMemo<DrillBinding>(
|
|
@@ -176,7 +215,7 @@ export function useDrill({
|
|
|
176
215
|
anchorPosition={{ top: menu?.top ?? 0, left: menu?.left ?? 0 }}
|
|
177
216
|
>
|
|
178
217
|
<ListSubheader sx={{ lineHeight: "32px" }}>
|
|
179
|
-
{menu ? `${menu.
|
|
218
|
+
{menu ? `${menu.label} →` : ""}
|
|
180
219
|
</ListSubheader>
|
|
181
220
|
{/* Deduplicated: `# drill { to=[overview, overview] }` is a typo
|
|
182
221
|
rather than a request for two identical rows, and React keys
|
|
@@ -188,7 +227,8 @@ export function useDrill({
|
|
|
188
227
|
key={destination}
|
|
189
228
|
onClick={() => {
|
|
190
229
|
close();
|
|
191
|
-
if (menu
|
|
230
|
+
if (menu?.intent)
|
|
231
|
+
go(menu.intent, destination, menu.event);
|
|
192
232
|
}}
|
|
193
233
|
>
|
|
194
234
|
{destination === DRILL_SELF
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { afterEach, describe, expect, it, mock, spyOn } from "bun:test";
|
|
5
|
+
import { renderHook } from "@testing-library/react";
|
|
6
|
+
import type { GivenValue } from "../../hooks/givenValue";
|
|
7
|
+
import { useDrillSelf } from "./useDrillSelf";
|
|
8
|
+
|
|
9
|
+
const TYPES = new Map<string, string | undefined>([
|
|
10
|
+
["REGION", "filter<string>"],
|
|
11
|
+
["MIN_AMOUNT", "number"],
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
function hook(setGiven = mock((_n: string, _v: GivenValue) => {})) {
|
|
15
|
+
const view = renderHook(() =>
|
|
16
|
+
useDrillSelf({
|
|
17
|
+
declaredTypes: TYPES,
|
|
18
|
+
setGiven,
|
|
19
|
+
documentName: "overview",
|
|
20
|
+
}),
|
|
21
|
+
);
|
|
22
|
+
return { ...view.result.current, setGiven };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe("useDrillSelf", () => {
|
|
26
|
+
let warn: ReturnType<typeof spyOn> | undefined;
|
|
27
|
+
afterEach(() => warn?.mockRestore());
|
|
28
|
+
|
|
29
|
+
it("resolves a tag's name as declared, or by folding case", () => {
|
|
30
|
+
const { resolveGiven, canSelf } = hook();
|
|
31
|
+
expect(resolveGiven("REGION")).toBe("REGION");
|
|
32
|
+
expect(resolveGiven("region")).toBe("REGION");
|
|
33
|
+
expect(resolveGiven("Min_Amount")).toBe("MIN_AMOUNT");
|
|
34
|
+
expect(resolveGiven("STATE")).toBeUndefined();
|
|
35
|
+
expect(canSelf("region")).toBe(true);
|
|
36
|
+
expect(canSelf("STATE")).toBe(false);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("sets the given under the DECLARED name, encoded for its type", () => {
|
|
40
|
+
const { onSelf, setGiven } = hook();
|
|
41
|
+
onSelf("region", "West");
|
|
42
|
+
expect(setGiven).toHaveBeenCalledWith("REGION", "West");
|
|
43
|
+
onSelf("min_amount", 42);
|
|
44
|
+
expect(setGiven).toHaveBeenLastCalledWith("MIN_AMOUNT", 42);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("declines, aloud, a name the document does not declare", () => {
|
|
48
|
+
warn = spyOn(console, "warn").mockImplementation(() => {});
|
|
49
|
+
const { onSelf, setGiven } = hook();
|
|
50
|
+
onSelf("STATE", "CA");
|
|
51
|
+
expect(setGiven).not.toHaveBeenCalled();
|
|
52
|
+
expect(warn).toHaveBeenCalledWith(expect.stringContaining("'overview'"));
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("declines, aloud, a value the given's type cannot hold", () => {
|
|
56
|
+
warn = spyOn(console, "warn").mockImplementation(() => {});
|
|
57
|
+
const { onSelf, setGiven } = hook();
|
|
58
|
+
onSelf("MIN_AMOUNT", "not a number");
|
|
59
|
+
expect(setGiven).not.toHaveBeenCalled();
|
|
60
|
+
expect(warn).toHaveBeenCalledWith(
|
|
61
|
+
expect.stringContaining("Drill declined"),
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { useCallback, useMemo } from "react";
|
|
5
|
+
import type { GivenValue } from "../../hooks/givenValue";
|
|
6
|
+
import { encodeDrillValue } from "./resolveDrill";
|
|
7
|
+
|
|
8
|
+
export interface UseDrillSelfOptions {
|
|
9
|
+
/** Declared type per given name: what this document can set. */
|
|
10
|
+
declaredTypes: ReadonlyMap<string, string | undefined>;
|
|
11
|
+
/** The control row's setter, from `useGivensState`. */
|
|
12
|
+
setGiven: (name: string, value: GivenValue) => void;
|
|
13
|
+
/**
|
|
14
|
+
* How the document names itself in the warning for a tag that names a given
|
|
15
|
+
* it does not declare: a dashboard's slug, a notebook's path.
|
|
16
|
+
*/
|
|
17
|
+
documentName: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface UseDrillSelfResult {
|
|
21
|
+
/** The declared given a drill tag's name refers to, or undefined. */
|
|
22
|
+
resolveGiven: (given: string) => string | undefined;
|
|
23
|
+
/** For `useDrill`: whether a `to=self` on this given can be honoured. */
|
|
24
|
+
canSelf: (given: string) => boolean;
|
|
25
|
+
/** For `useDrill`: set the given to the clicked value, encoded for its type. */
|
|
26
|
+
onSelf: (given: string, rawValue: unknown) => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The `# drill { to=self }` half of a document's drill wiring: which of its
|
|
31
|
+
* givens a tag can set, and setting one from a clicked cell.
|
|
32
|
+
*
|
|
33
|
+
* Shared by the dashboard and the notebook so one tag behaves identically on
|
|
34
|
+
* both surfaces, which used to be two copies of this code with the same
|
|
35
|
+
* warning string in each. Three rules live here:
|
|
36
|
+
*
|
|
37
|
+
* - **Names fold case.** `# drill` with no `given=` falls back to the
|
|
38
|
+
* DIMENSION's spelling, conventionally lower_snake, while givens are
|
|
39
|
+
* conventionally SHOUTED; neither convention can be assumed, so a tag naming
|
|
40
|
+
* `region` finds a declared `REGION` and vice versa. First declaration wins
|
|
41
|
+
* when a model declares both spellings.
|
|
42
|
+
* - **The value is encoded against the DECLARED type**, which is knowable here
|
|
43
|
+
* and is not knowable at the click: `useDrill` hands over the raw cell value
|
|
44
|
+
* for exactly this reason. Passing it straight to `setGiven` skipped the
|
|
45
|
+
* encoder, so a clicked date reached a `number` given as epoch milliseconds
|
|
46
|
+
* and a filter value went unescaped. It is set under the name the MODEL
|
|
47
|
+
* declares, so it reaches the URL and the request under the one name the
|
|
48
|
+
* server knows.
|
|
49
|
+
* - **A refusal is said aloud.** `canSelf` checks only that the name resolves,
|
|
50
|
+
* so a `given=` naming a type the clicked value cannot become still paints a
|
|
51
|
+
* whole column clickable and would then drop every click in silence. Both
|
|
52
|
+
* refusals warn, because each is an authoring mistake with no other symptom.
|
|
53
|
+
*/
|
|
54
|
+
export function useDrillSelf({
|
|
55
|
+
declaredTypes,
|
|
56
|
+
setGiven,
|
|
57
|
+
documentName,
|
|
58
|
+
}: UseDrillSelfOptions): UseDrillSelfResult {
|
|
59
|
+
const givenNamesByFold = useMemo(() => {
|
|
60
|
+
const byFold = new Map<string, string>();
|
|
61
|
+
for (const name of declaredTypes.keys()) {
|
|
62
|
+
if (!byFold.has(name.toLowerCase()))
|
|
63
|
+
byFold.set(name.toLowerCase(), name);
|
|
64
|
+
}
|
|
65
|
+
return byFold;
|
|
66
|
+
}, [declaredTypes]);
|
|
67
|
+
|
|
68
|
+
const resolveGiven = useCallback(
|
|
69
|
+
(given: string) =>
|
|
70
|
+
declaredTypes.has(given)
|
|
71
|
+
? given
|
|
72
|
+
: givenNamesByFold.get(given.toLowerCase()),
|
|
73
|
+
[declaredTypes, givenNamesByFold],
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
// Asked before a cell is painted as drillable, so the affordance matches
|
|
77
|
+
// what a click can do: sending a given the document cannot bind would fail
|
|
78
|
+
// every query on the page.
|
|
79
|
+
const canSelf = useCallback(
|
|
80
|
+
(given: string) => resolveGiven(given) !== undefined,
|
|
81
|
+
[resolveGiven],
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const onSelf = useCallback(
|
|
85
|
+
(given: string, rawValue: unknown) => {
|
|
86
|
+
const declared = resolveGiven(given);
|
|
87
|
+
if (declared === undefined) {
|
|
88
|
+
console.warn(
|
|
89
|
+
`# drill { to=self } tried to set '${given}', which ` +
|
|
90
|
+
`'${documentName}' does not declare as a given. Name the ` +
|
|
91
|
+
`given with 'given=' on the drill tag.`,
|
|
92
|
+
);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const declaredType = declaredTypes.get(declared);
|
|
96
|
+
const value = encodeDrillValue(rawValue, declaredType);
|
|
97
|
+
if (value === undefined) {
|
|
98
|
+
console.warn(
|
|
99
|
+
`Drill declined: ${JSON.stringify(rawValue)} cannot be a value for given "${declared}"` +
|
|
100
|
+
(declaredType ? ` of type ${declaredType}` : ""),
|
|
101
|
+
);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
setGiven(declared, value);
|
|
105
|
+
},
|
|
106
|
+
[declaredTypes, documentName, resolveGiven, setGiven],
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
return { resolveGiven, canSelf, onSelf };
|
|
110
|
+
}
|
|
@@ -209,7 +209,11 @@ describe("GivenInput: a control= the widget cannot honour falls through", () =>
|
|
|
209
209
|
// rejects, and a `number` given would get a string `givensToRequest` forwards
|
|
210
210
|
// verbatim for Malloy to refuse. Nothing populates `control` yet, so these
|
|
211
211
|
// pin the refusal before the slice that starts populating it.
|
|
212
|
-
|
|
212
|
+
// Autocomplete pickers only. The time-range control's dropdown is a
|
|
213
|
+
// combobox too, on a `<div>`; the picker this suite is about is an `<input>`.
|
|
214
|
+
const pickerCount = () =>
|
|
215
|
+
screen.queryAllByRole("combobox").filter((el) => el.tagName === "INPUT")
|
|
216
|
+
.length;
|
|
213
217
|
|
|
214
218
|
for (const type of ["string", "filter<string>"]) {
|
|
215
219
|
it(`renders a picker for ${type}`, () => {
|
|
@@ -259,6 +263,54 @@ describe("GivenInput: a slider only claims a filter it can represent", () => {
|
|
|
259
263
|
onChange={() => {}}
|
|
260
264
|
/>,
|
|
261
265
|
);
|
|
266
|
+
// Two handles, the lower at the bound and the upper at the ceiling.
|
|
267
|
+
const sliders = screen.queryAllByRole("slider") as HTMLInputElement[];
|
|
268
|
+
expect(sliders.map((s) => s.value)).toEqual(["5", "10"]);
|
|
269
|
+
expect(screen.getByText("≥ 5")).toBeDefined();
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("renders a closed range on both handles", () => {
|
|
273
|
+
render(
|
|
274
|
+
<GivenInput
|
|
275
|
+
given={{
|
|
276
|
+
name: "n",
|
|
277
|
+
type: "filter<number>",
|
|
278
|
+
rangeMin: 0,
|
|
279
|
+
rangeMax: 10,
|
|
280
|
+
}}
|
|
281
|
+
value="[2 to 7]"
|
|
282
|
+
onChange={() => {}}
|
|
283
|
+
/>,
|
|
284
|
+
);
|
|
285
|
+
const sliders = screen.queryAllByRole("slider") as HTMLInputElement[];
|
|
286
|
+
expect(sliders.map((s) => s.value)).toEqual(["2", "7"]);
|
|
287
|
+
expect(screen.getByText("2 to 7")).toBeDefined();
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("still refuses a half-open range", () => {
|
|
291
|
+
render(
|
|
292
|
+
<GivenInput
|
|
293
|
+
given={{
|
|
294
|
+
name: "n",
|
|
295
|
+
type: "filter<number>",
|
|
296
|
+
rangeMin: 0,
|
|
297
|
+
rangeMax: 10,
|
|
298
|
+
}}
|
|
299
|
+
value="(2 to 7]"
|
|
300
|
+
onChange={() => {}}
|
|
301
|
+
/>,
|
|
302
|
+
);
|
|
303
|
+
expect(screen.queryAllByRole("slider")).toHaveLength(0);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it("keeps one handle for a plain number given", () => {
|
|
307
|
+
render(
|
|
308
|
+
<GivenInput
|
|
309
|
+
given={{ name: "n", type: "number", rangeMin: 0, rangeMax: 10 }}
|
|
310
|
+
value={4}
|
|
311
|
+
onChange={() => {}}
|
|
312
|
+
/>,
|
|
313
|
+
);
|
|
262
314
|
expect(screen.queryAllByRole("slider")).toHaveLength(1);
|
|
263
315
|
});
|
|
264
316
|
|
|
@@ -330,6 +382,7 @@ describe("GivenInput: the whole type x control matrix", () => {
|
|
|
330
382
|
// the property that matters most is the last assertion: no combination falls
|
|
331
383
|
// through every branch and renders nothing at all.
|
|
332
384
|
const widget = () => {
|
|
385
|
+
if (screen.queryAllByTestId("time-range").length) return "timerange";
|
|
333
386
|
if (screen.queryAllByRole("combobox").length) return "picker";
|
|
334
387
|
if (screen.queryAllByRole("slider").length) return "slider";
|
|
335
388
|
if (screen.queryAllByRole("checkbox").length) return "checkbox";
|
|
@@ -407,9 +460,31 @@ describe("GivenInput: the whole type x control matrix", () => {
|
|
|
407
460
|
// A filter the picker cannot represent falls through to the text box, so
|
|
408
461
|
// the author's filter stays visible rather than being re-encoded.
|
|
409
462
|
expect(show("filter<string>", "select", false, "-Nike")).toBe("text");
|
|
410
|
-
// A lower
|
|
463
|
+
// A lower bound and a closed range both fit the handles; `1 to 5` is not
|
|
464
|
+
// number-filter syntax at all and stays visible as written.
|
|
411
465
|
expect(show("filter<number>", undefined, true, ">= 5")).toBe("slider");
|
|
466
|
+
expect(show("filter<number>", undefined, true, "[1 to 5]")).toBe(
|
|
467
|
+
"slider",
|
|
468
|
+
);
|
|
412
469
|
expect(show("filter<number>", undefined, true, "1 to 5")).toBe("text");
|
|
470
|
+
// A date filter gets the time-range control for a window or a day range,
|
|
471
|
+
// the single day picker for one day, and the text box for the rest.
|
|
472
|
+
expect(show("filter<date>", undefined, false, undefined)).toBe(
|
|
473
|
+
"timerange",
|
|
474
|
+
);
|
|
475
|
+
expect(show("filter<date>", undefined, false, "7 days")).toBe(
|
|
476
|
+
"timerange",
|
|
477
|
+
);
|
|
478
|
+
expect(
|
|
479
|
+
show(
|
|
480
|
+
"filter<timestamp>",
|
|
481
|
+
undefined,
|
|
482
|
+
false,
|
|
483
|
+
"2024-01-01 to 2024-02-01",
|
|
484
|
+
),
|
|
485
|
+
).toBe("timerange");
|
|
486
|
+
expect(show("filter<date>", undefined, false, "2024-01-15")).toBe("text");
|
|
487
|
+
expect(show("filter<date>", undefined, false, "last month")).toBe("text");
|
|
413
488
|
});
|
|
414
489
|
|
|
415
490
|
// One test per type rather than one test for the whole matrix. The matrix is
|
|
@@ -687,3 +762,122 @@ describe("GivenInput: a date renders its UTC day, whatever zone the runner is in
|
|
|
687
762
|
},
|
|
688
763
|
);
|
|
689
764
|
});
|
|
765
|
+
|
|
766
|
+
describe("GivenInput: the time-range control for a date filter", () => {
|
|
767
|
+
const given: Given = { name: "PERIOD", type: "filter<timestamp>" };
|
|
768
|
+
const dropdown = () =>
|
|
769
|
+
screen
|
|
770
|
+
.getAllByRole("combobox")
|
|
771
|
+
.find((el) => el.tagName !== "INPUT") as HTMLElement;
|
|
772
|
+
|
|
773
|
+
it("shows an unset given as blank, with no revert", () => {
|
|
774
|
+
render(
|
|
775
|
+
<GivenInput given={given} value={undefined} onChange={() => {}} />,
|
|
776
|
+
);
|
|
777
|
+
expect(screen.getByTestId("time-range")).toBeDefined();
|
|
778
|
+
expect(clearButtons()).toHaveLength(0);
|
|
779
|
+
expect(screen.queryAllByLabelText("From")).toHaveLength(0);
|
|
780
|
+
});
|
|
781
|
+
|
|
782
|
+
it("shows the preset a value spells, however it is spelled", () => {
|
|
783
|
+
render(<GivenInput given={given} value="30 day" onChange={() => {}} />);
|
|
784
|
+
expect(dropdown().textContent).toBe("Last 30 days");
|
|
785
|
+
expect(clearButtons()).toHaveLength(1);
|
|
786
|
+
});
|
|
787
|
+
|
|
788
|
+
it("commits a picked preset in Malloy's filter grammar", () => {
|
|
789
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
790
|
+
render(
|
|
791
|
+
<GivenInput given={given} value={undefined} onChange={onChange} />,
|
|
792
|
+
);
|
|
793
|
+
fireEvent.mouseDown(dropdown());
|
|
794
|
+
fireEvent.click(screen.getByRole("option", { name: "Last 7 days" }));
|
|
795
|
+
expect(onChange).toHaveBeenCalledWith("7 days");
|
|
796
|
+
});
|
|
797
|
+
|
|
798
|
+
it("opens two day pickers for a custom range, committing nothing yet", () => {
|
|
799
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
800
|
+
render(
|
|
801
|
+
<GivenInput given={given} value={undefined} onChange={onChange} />,
|
|
802
|
+
);
|
|
803
|
+
fireEvent.mouseDown(dropdown());
|
|
804
|
+
fireEvent.click(screen.getByRole("option", { name: "Custom range" }));
|
|
805
|
+
expect(screen.getByLabelText("From")).toBeDefined();
|
|
806
|
+
expect(screen.getByLabelText("To")).toBeDefined();
|
|
807
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
it("shows a day range as the inclusive days it selects", () => {
|
|
811
|
+
render(
|
|
812
|
+
<GivenInput
|
|
813
|
+
given={given}
|
|
814
|
+
value="2024-01-01 to 2024-02-01"
|
|
815
|
+
onChange={() => {}}
|
|
816
|
+
/>,
|
|
817
|
+
);
|
|
818
|
+
expect(dropdown().textContent).toBe("Custom range");
|
|
819
|
+
expect((screen.getByLabelText("From") as HTMLInputElement).value).toBe(
|
|
820
|
+
"01/01/2024",
|
|
821
|
+
);
|
|
822
|
+
expect((screen.getByLabelText("To") as HTMLInputElement).value).toBe(
|
|
823
|
+
"01/31/2024",
|
|
824
|
+
);
|
|
825
|
+
});
|
|
826
|
+
|
|
827
|
+
it("shows the empty filter as an override it can revert", () => {
|
|
828
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
829
|
+
render(<GivenInput given={given} value="" onChange={onChange} />);
|
|
830
|
+
expect(dropdown().textContent).toBe("Any time");
|
|
831
|
+
fireEvent.click(clearButtons()[0]);
|
|
832
|
+
expect(onChange).toHaveBeenCalledWith(null);
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
it("leaves a filter it cannot show in the text box, as written", () => {
|
|
836
|
+
render(
|
|
837
|
+
<GivenInput given={given} value="last month" onChange={() => {}} />,
|
|
838
|
+
);
|
|
839
|
+
expect(screen.queryAllByTestId("time-range")).toHaveLength(0);
|
|
840
|
+
expect((screen.getByLabelText("PERIOD") as HTMLInputElement).value).toBe(
|
|
841
|
+
"last month",
|
|
842
|
+
);
|
|
843
|
+
});
|
|
844
|
+
|
|
845
|
+
it("keeps the single day picker for one day", () => {
|
|
846
|
+
render(
|
|
847
|
+
<GivenInput given={given} value="2024-01-15" onChange={() => {}} />,
|
|
848
|
+
);
|
|
849
|
+
expect(screen.queryAllByTestId("time-range")).toHaveLength(0);
|
|
850
|
+
expect(screen.getByLabelText("PERIOD")).toBeDefined();
|
|
851
|
+
});
|
|
852
|
+
});
|
|
853
|
+
|
|
854
|
+
describe("GivenInput: the range slider's commits", () => {
|
|
855
|
+
const given: Given = {
|
|
856
|
+
name: "n",
|
|
857
|
+
type: "filter<number>",
|
|
858
|
+
rangeMin: 0,
|
|
859
|
+
rangeMax: 10,
|
|
860
|
+
};
|
|
861
|
+
const handles = () => screen.getAllByRole("slider") as HTMLInputElement[];
|
|
862
|
+
|
|
863
|
+
it("writes a closed range when the upper handle is inside the track", () => {
|
|
864
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
865
|
+
render(<GivenInput given={given} value="[2 to 7]" onChange={onChange} />);
|
|
866
|
+
fireEvent.change(handles()[1], { target: { value: "6" } });
|
|
867
|
+
expect(onChange).toHaveBeenCalledWith("[2 to 6]");
|
|
868
|
+
});
|
|
869
|
+
|
|
870
|
+
it("collapses to a threshold once the upper handle reaches the ceiling", () => {
|
|
871
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
872
|
+
render(<GivenInput given={given} value="[2 to 7]" onChange={onChange} />);
|
|
873
|
+
fireEvent.change(handles()[1], { target: { value: "10" } });
|
|
874
|
+
expect(onChange).toHaveBeenCalledWith(">= 2");
|
|
875
|
+
});
|
|
876
|
+
|
|
877
|
+
it("reverts when both handles are at the ends", () => {
|
|
878
|
+
const onChange = mock((_next: GivenValue) => {});
|
|
879
|
+
render(<GivenInput given={given} value=">= 2" onChange={onChange} />);
|
|
880
|
+
fireEvent.change(handles()[0], { target: { value: "0" } });
|
|
881
|
+
expect(onChange).toHaveBeenCalledWith(null);
|
|
882
|
+
});
|
|
883
|
+
});
|