derive-dashboards 0.1.1 → 0.3.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 +5 -3
- package/dist/cache.mjs +2 -1
- package/dist/cli/add.mjs +130 -0
- package/dist/cli/cli.mjs +14 -12
- package/dist/cli/config.mjs +12 -1
- package/dist/cli/doctor.mjs +162 -0
- package/dist/cli/init.mjs +3 -2
- package/dist/cli/json.mjs +21 -0
- package/dist/cli/layers.mjs +33 -0
- package/dist/core/document/display.d.mts +63 -0
- package/dist/core/document/display.mjs +139 -0
- package/dist/core/document/layout.mjs +1 -1
- package/dist/core/document/view-layout.d.mts +14 -0
- package/dist/core/document/view-layout.mjs +157 -0
- package/dist/core/engine/filter.mjs +2 -2
- package/dist/core/engine/leaf.mjs +9 -4
- package/dist/core/engine/members.mjs +1 -1
- package/dist/core/engine/version.d.mts +5 -4
- package/dist/core/engine/version.mjs +5 -4
- package/dist/core/error/codes.d.mts +4 -1
- package/dist/core/error/codes.mjs +4 -1
- package/dist/core/index.d.mts +12 -8
- package/dist/core/index.mjs +11 -7
- package/dist/core/model/member.mjs +5 -2
- package/dist/core/model/types.d.mts +13 -13
- package/dist/core/model/validate/drill.mjs +6 -15
- package/dist/core/model/validate/fields.mjs +130 -0
- package/dist/core/model/validate/index.mjs +11 -3
- package/dist/core/model/validate/known-members.mjs +133 -0
- package/dist/core/model/validate/known-nested.mjs +85 -0
- package/dist/core/model/validate/known.mjs +94 -0
- package/dist/core/model/validate/members.mjs +7 -4
- package/dist/core/model/validate/source.mjs +8 -1
- package/dist/core/policy/bind-query.d.mts +12 -0
- package/dist/core/policy/bind-query.mjs +53 -0
- package/dist/core/query/alias.mjs +11 -1
- package/dist/core/query/drill/selection.d.mts +7 -2
- package/dist/core/query/drill/selection.mjs +18 -15
- package/dist/core/query/filter.d.mts +6 -0
- package/dist/core/query/picks.d.mts +8 -2
- package/dist/core/query/picks.mjs +11 -4
- package/dist/core/query/segments.mjs +2 -1
- package/dist/core/result/columns.d.mts +10 -1
- package/dist/core/result/columns.mjs +27 -5
- package/dist/core/result/time-axis.d.mts +23 -0
- package/dist/core/result/time-axis.mjs +72 -0
- package/dist/core/result/types.d.mts +36 -1
- package/dist/core/schema/aliases.d.mts +7 -0
- package/dist/core/schema/aliases.mjs +77 -0
- package/dist/core/schema/answer-parts.mjs +23 -1
- package/dist/core/schema/answers.d.mts +204 -0
- package/dist/core/schema/answers.mjs +2 -0
- package/dist/core/schema/index.d.mts +3 -2
- package/dist/core/schema/index.mjs +3 -2
- package/dist/core/schema/query.d.mts +81 -0
- package/dist/core/schema/query.mjs +9 -5
- package/dist/core/schema/tools.d.mts +2 -0
- package/dist/core/schema/tools.mjs +27 -1
- package/dist/core/time/bucket.mjs +8 -2
- package/dist/core/types/api.d.mts +7 -1
- package/dist/core/types/api.mjs +1 -1
- package/dist/dashboards.mjs +4 -0
- package/dist/drill-change.mjs +1 -1
- package/dist/drill-seal.mjs +1 -0
- package/dist/editing.mjs +2 -2
- package/dist/endpoints/dashboards.mjs +17 -2
- package/dist/endpoints/index.d.mts +15 -2
- package/dist/endpoints/meta.mjs +10 -1
- package/dist/execute.mjs +32 -17
- package/dist/handler.mjs +5 -1
- package/dist/index.mjs +6 -1
- package/dist/llms.txt +22 -0
- package/dist/mcp/format.mjs +42 -8
- package/dist/mcp/query.mjs +34 -4
- package/dist/postgres-store.mjs +1 -0
- package/dist/react/Canvas.mjs +28 -5
- package/dist/react/Cell.mjs +2 -1
- package/dist/react/Dashboard.d.mts +4 -0
- package/dist/react/Dashboard.mjs +27 -4
- package/dist/react/DashboardList.mjs +22 -22
- package/dist/react/DashboardListRow.mjs +49 -0
- package/dist/react/Discard.mjs +5 -2
- package/dist/react/Editing.mjs +19 -5
- package/dist/react/FilterBar.d.mts +7 -9
- package/dist/react/Grid.mjs +7 -19
- package/dist/react/Header.mjs +1 -1
- package/dist/react/History.mjs +5 -0
- package/dist/react/ReadFailed.mjs +23 -0
- package/dist/react/Spaces.mjs +15 -1
- package/dist/react/Tile.d.mts +2 -0
- package/dist/react/Tile.mjs +45 -10
- package/dist/react/_virtual/_rolldown/runtime.mjs +12 -0
- package/dist/react/arrange-steps.mjs +16 -13
- package/dist/react/arrange.mjs +10 -3
- package/dist/react/builder/Calcs.mjs +35 -11
- package/dist/react/builder/FilterEditor.mjs +196 -0
- package/dist/react/builder/Filters.mjs +172 -116
- package/dist/react/builder/Formula.mjs +82 -0
- package/dist/react/builder/Groups.mjs +1 -0
- package/dist/react/builder/Preview.mjs +1 -1
- package/dist/react/builder/RangeEditor.mjs +184 -0
- package/dist/react/builder/Sections.mjs +41 -6
- package/dist/react/builder/Time.mjs +138 -60
- package/dist/react/builder/Values.mjs +85 -0
- package/dist/react/builder/WidgetBuilder.mjs +50 -49
- package/dist/react/builder/base.d.mts +5 -0
- package/dist/react/builder/base.mjs +33 -8
- package/dist/react/builder/calc.mjs +15 -1
- package/dist/react/builder/escape.d.mts +1 -0
- package/dist/react/builder/escape.mjs +62 -0
- package/dist/react/builder/filter-tree.d.mts +9 -0
- package/dist/react/builder/filter-tree.mjs +186 -0
- package/dist/react/builder/formula-parse.d.mts +18 -0
- package/dist/react/builder/formula-parse.mjs +136 -0
- package/dist/react/builder/labels.d.mts +1 -1
- package/dist/react/builder/state.d.mts +2 -1
- package/dist/react/builder/state.mjs +23 -37
- package/dist/react/builder/useBuilder.d.mts +12 -1
- package/dist/react/builder/useBuilder.mjs +6 -3
- package/dist/react/builder/words.d.mts +59 -0
- package/dist/react/builder/words.mjs +73 -0
- package/dist/react/context.d.mts +15 -1
- package/dist/react/context.mjs +9 -3
- package/dist/react/drill/Bars.mjs +2 -2
- package/dist/react/drill/props.mjs +2 -1
- package/dist/react/drill/useDrill.d.mts +1 -1
- package/dist/react/drill/words.d.mts +3 -0
- package/dist/react/drill/words.mjs +2 -0
- package/dist/react/english.mjs +20 -5
- package/dist/react/filters/bar.d.mts +12 -0
- package/dist/react/filters/bar.mjs +135 -0
- package/dist/react/filters/calendar.mjs +193 -0
- package/dist/react/filters/chip.mjs +174 -0
- package/dist/react/filters/context.d.mts +22 -0
- package/dist/react/filters/context.mjs +78 -0
- package/dist/react/filters/dates.mjs +89 -0
- package/dist/react/filters/days.d.mts +36 -0
- package/dist/react/filters/days.mjs +87 -0
- package/dist/react/filters/field-step.mjs +81 -0
- package/dist/react/filters/hooks.d.mts +34 -0
- package/dist/react/filters/hooks.mjs +94 -0
- package/dist/react/filters/listbox.mjs +131 -0
- package/dist/react/filters/logic.d.mts +59 -0
- package/dist/react/filters/logic.mjs +109 -0
- package/dist/react/filters/menu-search.mjs +211 -0
- package/dist/react/filters/menu.mjs +85 -0
- package/dist/react/filters/number.mjs +127 -0
- package/dist/react/filters/numbers.d.mts +47 -0
- package/dist/react/filters/numbers.mjs +151 -0
- package/dist/react/filters/operator.mjs +104 -0
- package/dist/react/filters/phone.mjs +104 -0
- package/dist/react/filters/popover.d.mts +19 -0
- package/dist/react/filters/popover.mjs +153 -0
- package/dist/react/filters/rules.d.mts +8 -0
- package/dist/react/filters/rules.mjs +55 -0
- package/dist/react/filters/sheet.mjs +196 -0
- package/dist/react/filters/state.mjs +132 -0
- package/dist/react/filters/suggest.d.mts +47 -0
- package/dist/react/filters/suggest.mjs +97 -0
- package/dist/react/filters/values.mjs +166 -0
- package/dist/react/filters/words.d.mts +81 -0
- package/dist/react/filters/words.mjs +73 -0
- package/dist/react/format.mjs +11 -4
- package/dist/react/index.d.mts +15 -6
- package/dist/react/index.mjs +10 -4
- package/dist/react/messages.d.mts +34 -2
- package/dist/react/place.mjs +7 -3
- package/dist/react/renderers/index.d.mts +3 -21
- package/dist/react/renderers/index.mjs +68 -6
- package/dist/react/renderers/list.d.mts +2 -1
- package/dist/react/renderers/list.mjs +9 -12
- package/dist/react/renderers/props.d.mts +26 -0
- package/dist/react/scope.d.mts +4 -0
- package/dist/react/scope.mjs +19 -2
- package/dist/react/series-slots.d.mts +37 -0
- package/dist/react/series-slots.mjs +71 -0
- package/dist/react/series.d.mts +39 -9
- package/dist/react/series.mjs +144 -47
- package/dist/react/theme.css +930 -19
- package/dist/react/useBoard.d.mts +2 -0
- package/dist/react/useDashboard.d.mts +3 -0
- package/dist/react/useDashboard.mjs +6 -0
- package/dist/react/view-grid.mjs +16 -0
- package/dist/recharts/axes.mjs +28 -0
- package/dist/recharts/axis.mjs +84 -0
- package/dist/recharts/bar.d.mts +7 -0
- package/dist/recharts/bar.mjs +141 -0
- package/dist/recharts/charts.d.mts +4 -5
- package/dist/recharts/charts.mjs +2 -208
- package/dist/recharts/index.d.mts +3 -1
- package/dist/recharts/index.mjs +4 -1
- package/dist/recharts/line.d.mts +2 -0
- package/dist/recharts/line.mjs +112 -0
- package/dist/recharts/marks.mjs +47 -18
- package/dist/recharts/parts.mjs +103 -0
- package/dist/recharts/table.mjs +71 -0
- package/dist/records-read.mjs +1 -1
- package/dist/registry/filter-bar.json +26 -0
- package/dist/skills/derive-dashboards/SKILL.md +69 -0
- package/dist/store/apply.mjs +3 -1
- package/package.json +6 -2
- package/dist/react/FilterBar.mjs +0 -119
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { rangeOf, useBar, useWords, valuePickOf } from "./context.mjs";
|
|
2
|
+
import { PresetList } from "./dates.mjs";
|
|
3
|
+
import { isNumber, toggled } from "./logic.mjs";
|
|
4
|
+
import { NumberPicker } from "./number.mjs";
|
|
5
|
+
import { ValueList } from "./values.mjs";
|
|
6
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
+
// src/filters/field-step.tsx
|
|
8
|
+
function Back({ label, steps }) {
|
|
9
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
10
|
+
type: "button",
|
|
11
|
+
className: "dd-fb-back",
|
|
12
|
+
onClick: () => {
|
|
13
|
+
steps.go(null);
|
|
14
|
+
},
|
|
15
|
+
children: [
|
|
16
|
+
/* @__PURE__ */ jsx("span", {
|
|
17
|
+
"aria-hidden": true,
|
|
18
|
+
children: "←"
|
|
19
|
+
}),
|
|
20
|
+
" ",
|
|
21
|
+
label
|
|
22
|
+
]
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/** "Custom range…": closes the menu and opens the date chip, where the calendar is. */
|
|
26
|
+
function CustomRange({ id, steps }) {
|
|
27
|
+
const words = useWords();
|
|
28
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
29
|
+
type: "button",
|
|
30
|
+
className: "dd-fb-preset",
|
|
31
|
+
onClick: () => {
|
|
32
|
+
steps.close();
|
|
33
|
+
steps.openChip(id);
|
|
34
|
+
},
|
|
35
|
+
children: [words.customRange, "…"]
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/** The menu's second step: one field's picker, with a way back. */
|
|
39
|
+
function FieldStep({ field, steps }) {
|
|
40
|
+
const { fields, picks, setPick } = useBar();
|
|
41
|
+
const back = /* @__PURE__ */ jsx(Back, {
|
|
42
|
+
label: field.label,
|
|
43
|
+
steps
|
|
44
|
+
});
|
|
45
|
+
const done = (pick) => {
|
|
46
|
+
setPick(field.id, pick);
|
|
47
|
+
steps.close();
|
|
48
|
+
};
|
|
49
|
+
if (field.kind === "date_range") return /* @__PURE__ */ jsxs(Fragment, { children: [back, /* @__PURE__ */ jsx(PresetList, {
|
|
50
|
+
control: field,
|
|
51
|
+
pick: rangeOf(picks, field.id),
|
|
52
|
+
onPick: done,
|
|
53
|
+
extra: /* @__PURE__ */ jsx(CustomRange, {
|
|
54
|
+
id: field.id,
|
|
55
|
+
steps
|
|
56
|
+
})
|
|
57
|
+
})] });
|
|
58
|
+
if (field.kind !== "filter") return back;
|
|
59
|
+
const pick = valuePickOf(picks, field.id);
|
|
60
|
+
if (isNumber(field, fields)) return /* @__PURE__ */ jsxs(Fragment, { children: [back, /* @__PURE__ */ jsx(NumberPicker, {
|
|
61
|
+
control: field,
|
|
62
|
+
pick,
|
|
63
|
+
onPick: done
|
|
64
|
+
})] });
|
|
65
|
+
return /* @__PURE__ */ jsx(ValueList, {
|
|
66
|
+
control: field,
|
|
67
|
+
pick,
|
|
68
|
+
before: back,
|
|
69
|
+
onBack: () => {
|
|
70
|
+
steps.go(null);
|
|
71
|
+
},
|
|
72
|
+
onToggle: (v) => {
|
|
73
|
+
setPick(field.id, toggled(field, pick, v));
|
|
74
|
+
if (!field.multi) steps.close();
|
|
75
|
+
},
|
|
76
|
+
onClear: () => {
|
|
77
|
+
setPick(field.id, void 0);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
export { FieldStep };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Control, MetaMember, MetaResponse, Picks, Primitive } from "derive-dashboards/core";
|
|
2
|
+
export type FilterBarProps = {
|
|
3
|
+
controls: readonly Control[];
|
|
4
|
+
picks: Picks;
|
|
5
|
+
onChange: (picks: Picks) => void;
|
|
6
|
+
/** The dashboard's own time zone when it sets one; calendar days are read in it. */
|
|
7
|
+
timezone?: string | undefined;
|
|
8
|
+
};
|
|
9
|
+
/** A control's values as a bar lists them: what is known, whether a read is under way or failed. */
|
|
10
|
+
export type ValuesState = {
|
|
11
|
+
values: readonly Primitive[];
|
|
12
|
+
loading: boolean;
|
|
13
|
+
/** The last read failed (earlier values may still be shown); `retry` asks again. */
|
|
14
|
+
failed: boolean;
|
|
15
|
+
retry: () => void;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* A filter control's values: its own list, or for "lookup" the values its member has that this
|
|
19
|
+
* viewer can see, narrowed by `search` on the server. For any filter bar, the built-in one or yours.
|
|
20
|
+
*/
|
|
21
|
+
export declare function useControlValues(control: Extract<Control, {
|
|
22
|
+
kind: "filter";
|
|
23
|
+
}>, options?: {
|
|
24
|
+
search?: string;
|
|
25
|
+
limit?: number;
|
|
26
|
+
}): ValuesState;
|
|
27
|
+
/** A member in the viewer's meta: its own source's dimension, or one a join reaches. */
|
|
28
|
+
export declare function fieldOf(meta: MetaResponse | undefined, member: string): MetaMember | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* What the model says about each filter control's field: its type (a text list, a number, a yes
|
|
31
|
+
* or no), value tones, number buckets and unit. A filter bar draws the right picker from it, with
|
|
32
|
+
* nothing configured by the host. Undefined while loading, or for a viewer the meta is not open to.
|
|
33
|
+
*/
|
|
34
|
+
export declare function useFilterFields(controls: readonly Control[]): Readonly<Record<string, MetaMember | undefined>>;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { useDashboards } from "../context.mjs";
|
|
2
|
+
import { ScopeContext } from "../scope.mjs";
|
|
3
|
+
import { useContext, useEffect, useMemo, useState } from "react";
|
|
4
|
+
import useSWR from "swr";
|
|
5
|
+
// src/filters/hooks.ts
|
|
6
|
+
const LOOKUP_LIMIT = 24;
|
|
7
|
+
const noop = () => {};
|
|
8
|
+
const NONE = [];
|
|
9
|
+
/** A control's own list, searched; the same array while nothing changed, for effects. */
|
|
10
|
+
function useOwnValues(own, search) {
|
|
11
|
+
return useMemo(() => {
|
|
12
|
+
if (typeof own !== "object") return null;
|
|
13
|
+
const needle = search.toLowerCase();
|
|
14
|
+
return needle === "" ? own : own.filter((v) => String(v).toLowerCase().includes(needle));
|
|
15
|
+
}, [own, search]);
|
|
16
|
+
}
|
|
17
|
+
/** A lookup control's values from the server, searched there; none for a control's own list. */
|
|
18
|
+
function useLookup(control, search, limit) {
|
|
19
|
+
const { client } = useDashboards();
|
|
20
|
+
const scope = useContext(ScopeContext);
|
|
21
|
+
const draft = scope?.draft === true;
|
|
22
|
+
const key = typeof control.values === "object" || scope === null ? null : [
|
|
23
|
+
"dd-values",
|
|
24
|
+
scope.dashboardId,
|
|
25
|
+
control.id,
|
|
26
|
+
draft,
|
|
27
|
+
search,
|
|
28
|
+
limit
|
|
29
|
+
];
|
|
30
|
+
return useSWR(key, () => client.controlValues({
|
|
31
|
+
dashboardId: scope?.dashboardId ?? "",
|
|
32
|
+
controlId: control.id,
|
|
33
|
+
limit,
|
|
34
|
+
...search === "" ? {} : { search },
|
|
35
|
+
...draft ? { draft: true } : {}
|
|
36
|
+
}), { keepPreviousData: true });
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A filter control's values: its own list, or for "lookup" the values its member has that this
|
|
40
|
+
* viewer can see, narrowed by `search` on the server. For any filter bar, the built-in one or yours.
|
|
41
|
+
*/
|
|
42
|
+
function useControlValues(control, options = {}) {
|
|
43
|
+
const search = options.search ?? "";
|
|
44
|
+
const fetched = useLookup(control, search, options.limit ?? LOOKUP_LIMIT);
|
|
45
|
+
const ownValues = useOwnValues(control.values, search);
|
|
46
|
+
if (ownValues !== null) return {
|
|
47
|
+
values: ownValues,
|
|
48
|
+
loading: false,
|
|
49
|
+
failed: false,
|
|
50
|
+
retry: noop
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
values: fetched.data?.values ?? NONE,
|
|
54
|
+
loading: fetched.isLoading,
|
|
55
|
+
failed: fetched.error !== void 0,
|
|
56
|
+
retry: () => {
|
|
57
|
+
fetched.mutate();
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/** A member in the viewer's meta: its own source's dimension, or one a join reaches. */
|
|
62
|
+
function fieldOf(meta, member) {
|
|
63
|
+
const [sourceName, a, b] = member.split(".");
|
|
64
|
+
const source = meta?.sources.find((s) => s.name === sourceName);
|
|
65
|
+
if (source === void 0 || a === void 0) return void 0;
|
|
66
|
+
if (b === void 0) return source.dimensions.find((d) => d.id === member);
|
|
67
|
+
const join = source.joins.find((j) => j.name === a);
|
|
68
|
+
const target = meta?.sources.find((s) => s.name === join?.source);
|
|
69
|
+
return target?.dimensions.find((d) => d.id === `${target.name}.${b}`);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* What the model says about each filter control's field: its type (a text list, a number, a yes
|
|
73
|
+
* or no), value tones, number buckets and unit. A filter bar draws the right picker from it, with
|
|
74
|
+
* nothing configured by the host. Undefined while loading, or for a viewer the meta is not open to.
|
|
75
|
+
*/
|
|
76
|
+
function useFilterFields(controls) {
|
|
77
|
+
const { client } = useDashboards();
|
|
78
|
+
const { data } = useSWR(["dd-meta"], () => client.meta());
|
|
79
|
+
return Object.fromEntries(controls.map((c) => [c.id, c.kind === "filter" ? fieldOf(data, c.member) : void 0]));
|
|
80
|
+
}
|
|
81
|
+
/** A value that settles: it follows `value` once it has not changed for `ms`. */
|
|
82
|
+
function useDebounced(value, ms) {
|
|
83
|
+
const [out, setOut] = useState(value);
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
const t = setTimeout(() => {
|
|
86
|
+
setOut(value);
|
|
87
|
+
}, ms);
|
|
88
|
+
return () => {
|
|
89
|
+
clearTimeout(t);
|
|
90
|
+
};
|
|
91
|
+
}, [value, ms]);
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
export { fieldOf, useControlValues, useDebounced, useFilterFields };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { useId, useState } from "react";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
// src/filters/listbox.tsx
|
|
4
|
+
/** Where each key moves the active row, given the row now and how many there are. */
|
|
5
|
+
const MOVES = {
|
|
6
|
+
ArrowDown: (at, last) => Math.min(at + 1, last),
|
|
7
|
+
ArrowUp: (at) => Math.max(at - 1, 0)
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* The row the keyboard is on, apart from the chosen ones (as ARIA's listbox pattern asks). A new
|
|
11
|
+
* search starts at the first row, set while rendering, so no frame shows a stale row.
|
|
12
|
+
*/
|
|
13
|
+
function useActive(query, count) {
|
|
14
|
+
const [active, setActive] = useState(0);
|
|
15
|
+
const [searched, setSearched] = useState(query);
|
|
16
|
+
if (searched !== query) {
|
|
17
|
+
setSearched(query);
|
|
18
|
+
setActive(0);
|
|
19
|
+
}
|
|
20
|
+
return [Math.min(active, count - 1), setActive];
|
|
21
|
+
}
|
|
22
|
+
function Row({ id, option, active, multiselect, onHover }) {
|
|
23
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
24
|
+
id,
|
|
25
|
+
role: "option",
|
|
26
|
+
"aria-selected": option.selected ?? (multiselect ? false : active),
|
|
27
|
+
"data-active": active || void 0,
|
|
28
|
+
tabIndex: -1,
|
|
29
|
+
className: "dd-fb-option",
|
|
30
|
+
onMouseMove: () => {
|
|
31
|
+
if (!active) onHover();
|
|
32
|
+
},
|
|
33
|
+
onMouseDown: (e) => {
|
|
34
|
+
e.preventDefault();
|
|
35
|
+
},
|
|
36
|
+
ref: (el) => {
|
|
37
|
+
if (active) el?.scrollIntoView?.({ block: "nearest" });
|
|
38
|
+
},
|
|
39
|
+
onClick: option.onPick,
|
|
40
|
+
children: [option.selected === void 0 ? null : /* @__PURE__ */ jsx("span", {
|
|
41
|
+
className: "dd-fb-check",
|
|
42
|
+
"data-single": !multiselect || void 0,
|
|
43
|
+
"data-on": option.selected || void 0,
|
|
44
|
+
"aria-hidden": true
|
|
45
|
+
}), option.content]
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function Rows({ id, options, at, multiselect, setActive }) {
|
|
49
|
+
return options.map((o, i) => /* @__PURE__ */ jsxs("div", {
|
|
50
|
+
role: "presentation",
|
|
51
|
+
children: [o.group !== void 0 && o.group !== options[i - 1]?.group ? /* @__PURE__ */ jsx("div", {
|
|
52
|
+
className: "dd-fb-group",
|
|
53
|
+
"aria-hidden": true,
|
|
54
|
+
children: o.group
|
|
55
|
+
}) : null, /* @__PURE__ */ jsx(Row, {
|
|
56
|
+
id: `${id}-r${String(i)}`,
|
|
57
|
+
option: o,
|
|
58
|
+
active: i === at,
|
|
59
|
+
multiselect,
|
|
60
|
+
onHover: () => {
|
|
61
|
+
setActive(i);
|
|
62
|
+
}
|
|
63
|
+
})]
|
|
64
|
+
}, o.id));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* A search box over a list, the one keyboard pattern of the bar: type to search, arrows to
|
|
68
|
+
* move, Enter to pick, Backspace on an empty box to go back.
|
|
69
|
+
*/
|
|
70
|
+
/** The search box's keys: arrows move; Enter picks; Backspace on an empty box goes back. */
|
|
71
|
+
function keysFor(p, at, setActive) {
|
|
72
|
+
return (e) => {
|
|
73
|
+
if (e.nativeEvent.isComposing) return;
|
|
74
|
+
const move = MOVES[e.key];
|
|
75
|
+
if (move !== void 0 && p.options.length > 0) {
|
|
76
|
+
e.preventDefault();
|
|
77
|
+
setActive(move(at, p.options.length - 1));
|
|
78
|
+
} else if (e.key === "Enter") {
|
|
79
|
+
e.preventDefault();
|
|
80
|
+
p.options[at]?.onPick();
|
|
81
|
+
} else if (e.key === "Backspace" && p.query === "" && p.onBack !== void 0) {
|
|
82
|
+
e.preventDefault();
|
|
83
|
+
p.onBack();
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function Combobox(props) {
|
|
88
|
+
const { label, query, options, multiselect = false, busy = false } = props;
|
|
89
|
+
const id = useId();
|
|
90
|
+
const [at, setActive] = useActive(query, options.length);
|
|
91
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
92
|
+
className: "dd-fb-combobox",
|
|
93
|
+
children: [
|
|
94
|
+
props.before,
|
|
95
|
+
/* @__PURE__ */ jsx("input", {
|
|
96
|
+
className: "dd-fb-search",
|
|
97
|
+
role: "combobox",
|
|
98
|
+
"aria-expanded": true,
|
|
99
|
+
"aria-controls": `${id}-list`,
|
|
100
|
+
"aria-activedescendant": options[at] === void 0 ? void 0 : `${id}-r${String(at)}`,
|
|
101
|
+
"aria-autocomplete": "list",
|
|
102
|
+
"aria-label": label,
|
|
103
|
+
placeholder: props.placeholder,
|
|
104
|
+
value: query,
|
|
105
|
+
onChange: (e) => {
|
|
106
|
+
props.onQuery(e.target.value);
|
|
107
|
+
},
|
|
108
|
+
onKeyDown: keysFor(props, at, setActive)
|
|
109
|
+
}),
|
|
110
|
+
/* @__PURE__ */ jsx("div", {
|
|
111
|
+
id: `${id}-list`,
|
|
112
|
+
role: "listbox",
|
|
113
|
+
"aria-label": label,
|
|
114
|
+
"aria-multiselectable": multiselect || void 0,
|
|
115
|
+
"aria-busy": busy || void 0,
|
|
116
|
+
className: "dd-fb-list",
|
|
117
|
+
children: options.length === 0 ? /* @__PURE__ */ jsx("div", {
|
|
118
|
+
className: "dd-fb-empty",
|
|
119
|
+
children: props.empty
|
|
120
|
+
}) : /* @__PURE__ */ jsx(Rows, {
|
|
121
|
+
id,
|
|
122
|
+
options,
|
|
123
|
+
at,
|
|
124
|
+
multiselect,
|
|
125
|
+
setActive
|
|
126
|
+
})
|
|
127
|
+
})
|
|
128
|
+
]
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
export { Combobox };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Control, DateRange, MetaMember, Op, Picks, Primitive } from "derive-dashboards/core";
|
|
2
|
+
export type FilterControl = Extract<Control, {
|
|
3
|
+
kind: "filter";
|
|
4
|
+
}>;
|
|
5
|
+
export type DateControl = Extract<Control, {
|
|
6
|
+
kind: "date_range";
|
|
7
|
+
}>;
|
|
8
|
+
export type ValuePick = {
|
|
9
|
+
op: Op;
|
|
10
|
+
values: readonly Primitive[];
|
|
11
|
+
};
|
|
12
|
+
type RelativeRange$1 = Extract<DateRange, {
|
|
13
|
+
last: number;
|
|
14
|
+
}>;
|
|
15
|
+
export type Fields = Readonly<Record<string, MetaMember | undefined>>;
|
|
16
|
+
export declare const isNegative: (op: Op) => boolean;
|
|
17
|
+
/** A pick on "None": the field is empty (notSet), or with "is not", has any value (set). */
|
|
18
|
+
export declare const isNone: (pick: ValuePick) => boolean;
|
|
19
|
+
/** Whether a control offers "None": its operators include the null tests. */
|
|
20
|
+
export declare const offersNone: (control: FilterControl) => boolean;
|
|
21
|
+
/** The values a pick shows as chosen: None stands for itself. */
|
|
22
|
+
export declare const chosenOf: (pick: ValuePick | undefined) => readonly Primitive[];
|
|
23
|
+
/**
|
|
24
|
+
* The operator a control allows for values of one polarity and count: equals or notEquals for one
|
|
25
|
+
* (in or notIn when that is all it has), in or notIn for several; undefined when it has none.
|
|
26
|
+
* Every value pick the bar makes goes through here, so it never sends an operator the control
|
|
27
|
+
* does not list.
|
|
28
|
+
*/
|
|
29
|
+
export declare function valueOp(control: FilterControl, negative: boolean, count: number): Op | undefined;
|
|
30
|
+
/** Values as a pick the control allows; undefined when it cannot say this. */
|
|
31
|
+
export declare function valuePick(control: FilterControl, negative: boolean, values: readonly Primitive[]): ValuePick | undefined;
|
|
32
|
+
/** "None" as a pick (notSet), or "is not None" (set); undefined when the control lacks it. */
|
|
33
|
+
export declare function nonePick(control: FilterControl, negative: boolean): ValuePick | undefined;
|
|
34
|
+
/** Whether a control takes more than one value at once for this polarity. */
|
|
35
|
+
export declare const takesMany: (control: FilterControl, negative: boolean) => boolean;
|
|
36
|
+
/** Whether a control can say the other of "is" and "is not" for a pick as it stands. */
|
|
37
|
+
export declare function canFlip(control: FilterControl, pick: ValuePick): boolean;
|
|
38
|
+
/** The next pick after a value is toggled: None stands alone, and a value replaces it. A change
|
|
39
|
+
* the control cannot express leaves the pick as it was. */
|
|
40
|
+
export declare function toggled(control: FilterControl, pick: ValuePick | undefined, value: Primitive): ValuePick | undefined;
|
|
41
|
+
/** The same values with "is" turned to "is not", or back; unchanged where the control cannot. */
|
|
42
|
+
export declare const flipped: (control: FilterControl, pick: ValuePick, negative: boolean) => ValuePick;
|
|
43
|
+
/** Which of the four operator words (in `FilterWords`) a pick reads as. */
|
|
44
|
+
export type OpWord = "isWord" | "isNotWord" | "isAnyOf" | "isNoneOf";
|
|
45
|
+
export declare function opWord(negative: boolean, many: boolean): OpWord;
|
|
46
|
+
/** A pick's operator word, "is any of" once it holds more than one value; `negative` asks for the flip. */
|
|
47
|
+
export declare const pickOpWord: (pick: ValuePick, negative?: boolean) => OpWord;
|
|
48
|
+
/** A filter that takes a number range: a number field, or a control that offers only ranges (and equals). */
|
|
49
|
+
export declare function isNumber(control: FilterControl, fields: Fields): boolean;
|
|
50
|
+
/** What a change removed: the labels, and whether it cleared every filter; undefined if none. */
|
|
51
|
+
export declare function removedOf(controls: readonly Control[], before: Picks, after: Picks): {
|
|
52
|
+
labels: string[];
|
|
53
|
+
cleared: boolean;
|
|
54
|
+
} | undefined;
|
|
55
|
+
/** What a change did, in English words, for an Undo line; undefined when nothing was removed. */
|
|
56
|
+
export declare function changeText(controls: readonly Control[], before: Picks, after: Picks): string | undefined;
|
|
57
|
+
export declare function defaultFormat(_control: Control, v: Primitive): string;
|
|
58
|
+
export declare const listText: (control: Control, values: readonly Primitive[], format?: (c: Control, v: Primitive) => string) => string;
|
|
59
|
+
export { RelativeRange$1 as RelativeRange };
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// src/filters/logic.ts
|
|
2
|
+
const NEGATIVE = /* @__PURE__ */ new Set([
|
|
3
|
+
"notEquals",
|
|
4
|
+
"notIn",
|
|
5
|
+
"set"
|
|
6
|
+
]);
|
|
7
|
+
const isNegative = (op) => NEGATIVE.has(op);
|
|
8
|
+
/** A pick on "None": the field is empty (notSet), or with "is not", has any value (set). */
|
|
9
|
+
const isNone = (pick) => pick.op === "set" || pick.op === "notSet";
|
|
10
|
+
/** Whether a control offers "None": its operators include the null tests. */
|
|
11
|
+
const offersNone = (control) => control.ops.includes("notSet");
|
|
12
|
+
/** The values a pick shows as chosen: None stands for itself. */
|
|
13
|
+
const chosenOf = (pick) => pick === void 0 ? [] : isNone(pick) ? [null] : pick.values;
|
|
14
|
+
/**
|
|
15
|
+
* The operator a control allows for values of one polarity and count: equals or notEquals for one
|
|
16
|
+
* (in or notIn when that is all it has), in or notIn for several; undefined when it has none.
|
|
17
|
+
* Every value pick the bar makes goes through here, so it never sends an operator the control
|
|
18
|
+
* does not list.
|
|
19
|
+
*/
|
|
20
|
+
function valueOp(control, negative, count) {
|
|
21
|
+
const [one, many] = negative ? ["notEquals", "notIn"] : ["equals", "in"];
|
|
22
|
+
if (count > 1) return control.ops.includes(many) ? many : void 0;
|
|
23
|
+
if (control.ops.includes(one)) return one;
|
|
24
|
+
return control.ops.includes(many) ? many : void 0;
|
|
25
|
+
}
|
|
26
|
+
/** Values as a pick the control allows; undefined when it cannot say this. */
|
|
27
|
+
function valuePick(control, negative, values) {
|
|
28
|
+
const op = valueOp(control, negative, values.length);
|
|
29
|
+
return op === void 0 ? void 0 : {
|
|
30
|
+
op,
|
|
31
|
+
values
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** "None" as a pick (notSet), or "is not None" (set); undefined when the control lacks it. */
|
|
35
|
+
function nonePick(control, negative) {
|
|
36
|
+
const op = negative ? "set" : "notSet";
|
|
37
|
+
return control.ops.includes(op) ? {
|
|
38
|
+
op,
|
|
39
|
+
values: []
|
|
40
|
+
} : void 0;
|
|
41
|
+
}
|
|
42
|
+
/** Whether a control takes more than one value at once for this polarity. */
|
|
43
|
+
const takesMany = (control, negative) => control.multi && valueOp(control, negative, 2) !== void 0;
|
|
44
|
+
/** Whether a control can say the other of "is" and "is not" for a pick as it stands. */
|
|
45
|
+
function canFlip(control, pick) {
|
|
46
|
+
const negative = !isNegative(pick.op);
|
|
47
|
+
return isNone(pick) ? nonePick(control, negative) !== void 0 : valuePick(control, negative, pick.values) !== void 0;
|
|
48
|
+
}
|
|
49
|
+
/** The next pick after a value is toggled: None stands alone, and a value replaces it. A change
|
|
50
|
+
* the control cannot express leaves the pick as it was. */
|
|
51
|
+
function toggled(control, pick, value) {
|
|
52
|
+
const chosen = chosenOf(pick);
|
|
53
|
+
const negative = pick !== void 0 && isNegative(pick.op);
|
|
54
|
+
if (value === null) return chosen.includes(null) ? void 0 : nonePick(control, negative) ?? pick;
|
|
55
|
+
const values = chosen.filter((v) => v !== null);
|
|
56
|
+
const next = values.includes(value) ? values.filter((v) => v !== value) : takesMany(control, negative) ? [...values, value] : [value];
|
|
57
|
+
return next.length === 0 ? void 0 : valuePick(control, negative, next) ?? pick;
|
|
58
|
+
}
|
|
59
|
+
/** The same values with "is" turned to "is not", or back; unchanged where the control cannot. */
|
|
60
|
+
const flipped = (control, pick, negative) => (isNone(pick) ? nonePick(control, negative) : valuePick(control, negative, pick.values)) ?? pick;
|
|
61
|
+
function opWord(negative, many) {
|
|
62
|
+
if (negative) return many ? "isNoneOf" : "isNotWord";
|
|
63
|
+
return many ? "isAnyOf" : "isWord";
|
|
64
|
+
}
|
|
65
|
+
/** A pick's operator word, "is any of" once it holds more than one value; `negative` asks for the flip. */
|
|
66
|
+
const pickOpWord = (pick, negative = isNegative(pick.op)) => opWord(negative, !isNone(pick) && pick.values.length > 1);
|
|
67
|
+
const NUMBER_TYPES = /* @__PURE__ */ new Set([
|
|
68
|
+
"number",
|
|
69
|
+
"int",
|
|
70
|
+
"decimal"
|
|
71
|
+
]);
|
|
72
|
+
const NUMBER_OPS = /* @__PURE__ */ new Set([
|
|
73
|
+
"between",
|
|
74
|
+
"gte",
|
|
75
|
+
"gt",
|
|
76
|
+
"lt",
|
|
77
|
+
"lte"
|
|
78
|
+
]);
|
|
79
|
+
/** A filter that takes a number range: a number field, or a control that offers only ranges (and equals). */
|
|
80
|
+
function isNumber(control, fields) {
|
|
81
|
+
const type = fields[control.id]?.type;
|
|
82
|
+
if (type !== void 0) return NUMBER_TYPES.has(type);
|
|
83
|
+
return control.ops.some((op) => NUMBER_OPS.has(op)) && control.ops.every((op) => NUMBER_OPS.has(op) || op === "equals");
|
|
84
|
+
}
|
|
85
|
+
/** What a change removed: the labels, and whether it cleared every filter; undefined if none. */
|
|
86
|
+
function removedOf(controls, before, after) {
|
|
87
|
+
const removed = Object.keys(before).filter((id) => after[id] === void 0);
|
|
88
|
+
if (removed.length === 0) return void 0;
|
|
89
|
+
return {
|
|
90
|
+
labels: removed.map((id) => controls.find((c) => c.id === id)?.label ?? id),
|
|
91
|
+
cleared: Object.keys(after).length === 0 && removed.length > 1
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/** What a change did, in English words, for an Undo line; undefined when nothing was removed. */
|
|
95
|
+
function changeText(controls, before, after) {
|
|
96
|
+
const removed = removedOf(controls, before, after);
|
|
97
|
+
if (removed === void 0) return void 0;
|
|
98
|
+
if (removed.cleared) return "Filters cleared";
|
|
99
|
+
const { labels } = removed;
|
|
100
|
+
return `${labels.join(", ")} filter${labels.length > 1 ? "s" : ""} removed`;
|
|
101
|
+
}
|
|
102
|
+
function defaultFormat(_control, v) {
|
|
103
|
+
if (v === null) return "None";
|
|
104
|
+
if (typeof v === "boolean") return v ? "Yes" : "No";
|
|
105
|
+
const s = String(v);
|
|
106
|
+
return s.length === 0 ? s : s[0]?.toUpperCase() + s.slice(1);
|
|
107
|
+
}
|
|
108
|
+
const listText = (control, values, format = defaultFormat) => values.map((v) => format(control, v)).join(", ");
|
|
109
|
+
export { canFlip, changeText, chosenOf, defaultFormat, flipped, isNegative, isNone, isNumber, listText, nonePick, offersNone, opWord, pickOpWord, removedOf, takesMany, toggled, valueOp, valuePick };
|