@faast-app/ui 0.0.0-stage → 0.7.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 +13 -2
- package/dist/components/Alert.d.ts +30 -0
- package/dist/components/Alert.js +19 -0
- package/dist/components/AppShell.d.ts +63 -0
- package/dist/components/AppShell.js +80 -0
- package/dist/components/Avatar.d.ts +18 -0
- package/dist/components/Avatar.js +21 -0
- package/dist/components/Badge.d.ts +24 -0
- package/dist/components/Badge.js +10 -0
- package/dist/components/Button.d.ts +71 -0
- package/dist/components/Button.js +40 -0
- package/dist/components/Card.d.ts +23 -0
- package/dist/components/Card.js +17 -0
- package/dist/components/Chip.d.ts +16 -0
- package/dist/components/Chip.js +9 -0
- package/dist/components/Choice.d.ts +38 -0
- package/dist/components/Choice.js +53 -0
- package/dist/components/Dialog.d.ts +44 -0
- package/dist/components/Dialog.js +51 -0
- package/dist/components/EmptyState.d.ts +16 -0
- package/dist/components/EmptyState.js +8 -0
- package/dist/components/FileDropArea.d.ts +62 -0
- package/dist/components/FileDropArea.js +90 -0
- package/dist/components/FormField.d.ts +35 -0
- package/dist/components/FormField.js +32 -0
- package/dist/components/HelpMenu.d.ts +46 -0
- package/dist/components/HelpMenu.js +104 -0
- package/dist/components/Icon.d.ts +24 -0
- package/dist/components/Icon.js +195 -0
- package/dist/components/Input.d.ts +37 -0
- package/dist/components/Input.js +31 -0
- package/dist/components/KpiStrip.d.ts +32 -0
- package/dist/components/KpiStrip.js +14 -0
- package/dist/components/LockScreen.d.ts +34 -0
- package/dist/components/LockScreen.js +22 -0
- package/dist/components/Logo.d.ts +27 -0
- package/dist/components/Logo.js +12 -0
- package/dist/components/Navigation.d.ts +37 -0
- package/dist/components/Navigation.js +29 -0
- package/dist/components/OptionList.d.ts +27 -0
- package/dist/components/OptionList.js +18 -0
- package/dist/components/OtpInput.d.ts +31 -0
- package/dist/components/OtpInput.js +102 -0
- package/dist/components/Pagination.d.ts +31 -0
- package/dist/components/Pagination.js +41 -0
- package/dist/components/PasswordField.d.ts +32 -0
- package/dist/components/PasswordField.js +29 -0
- package/dist/components/SegmentedControl.d.ts +35 -0
- package/dist/components/SegmentedControl.js +52 -0
- package/dist/components/Select.d.ts +15 -0
- package/dist/components/Select.js +10 -0
- package/dist/components/SessionTimeoutDialog.d.ts +39 -0
- package/dist/components/SessionTimeoutDialog.js +62 -0
- package/dist/components/Spinner.d.ts +25 -0
- package/dist/components/Spinner.js +15 -0
- package/dist/components/StatTiles.d.ts +35 -0
- package/dist/components/StatTiles.js +12 -0
- package/dist/components/Stepper.d.ts +26 -0
- package/dist/components/Stepper.js +26 -0
- package/dist/components/StickyActionBar.d.ts +28 -0
- package/dist/components/StickyActionBar.js +12 -0
- package/dist/components/Table.d.ts +69 -0
- package/dist/components/Table.js +73 -0
- package/dist/components/TenantMark.d.ts +27 -0
- package/dist/components/TenantMark.js +15 -0
- package/dist/components/Timeline.d.ts +31 -0
- package/dist/components/Timeline.js +18 -0
- package/dist/components/Toast.d.ts +43 -0
- package/dist/components/Toast.js +46 -0
- package/dist/components/Toggletip.d.ts +26 -0
- package/dist/components/Toggletip.js +47 -0
- package/dist/components/Tooltip.d.ts +24 -0
- package/dist/components/Tooltip.js +73 -0
- package/dist/components/Tour.d.ts +66 -0
- package/dist/components/Tour.js +161 -0
- package/dist/components/TourOffer.d.ts +22 -0
- package/dist/components/TourOffer.js +33 -0
- package/dist/components/WelcomeDialog.d.ts +45 -0
- package/dist/components/WelcomeDialog.js +38 -0
- package/dist/data-table/components/ClientDataTable.d.ts +22 -0
- package/dist/data-table/components/ClientDataTable.js +29 -0
- package/dist/data-table/components/DataTableFooter.d.ts +11 -0
- package/dist/data-table/components/DataTableFooter.js +16 -0
- package/dist/data-table/components/DataTableView.d.ts +9 -0
- package/dist/data-table/components/DataTableView.js +95 -0
- package/dist/data-table/components/ServerDataTable.d.ts +16 -0
- package/dist/data-table/components/ServerDataTable.js +17 -0
- package/dist/data-table/core/columnVisibility.d.ts +15 -0
- package/dist/data-table/core/columnVisibility.js +34 -0
- package/dist/data-table/core/constants.d.ts +20 -0
- package/dist/data-table/core/constants.js +21 -0
- package/dist/data-table/core/filters/registry.d.ts +33 -0
- package/dist/data-table/core/filters/registry.js +393 -0
- package/dist/data-table/core/filters/types.d.ts +88 -0
- package/dist/data-table/core/filters/types.js +1 -0
- package/dist/data-table/core/group.d.ts +15 -0
- package/dist/data-table/core/group.js +73 -0
- package/dist/data-table/core/paginate.d.ts +13 -0
- package/dist/data-table/core/paginate.js +27 -0
- package/dist/data-table/core/query.d.ts +65 -0
- package/dist/data-table/core/query.js +138 -0
- package/dist/data-table/core/search.d.ts +10 -0
- package/dist/data-table/core/search.js +35 -0
- package/dist/data-table/core/selection.d.ts +24 -0
- package/dist/data-table/core/selection.js +59 -0
- package/dist/data-table/core/sort.d.ts +14 -0
- package/dist/data-table/core/sort.js +41 -0
- package/dist/data-table/core/text.d.ts +5 -0
- package/dist/data-table/core/text.js +27 -0
- package/dist/data-table/core/types.d.ts +40 -0
- package/dist/data-table/core/types.js +1 -0
- package/dist/data-table/core/url.d.ts +43 -0
- package/dist/data-table/core/url.js +194 -0
- package/dist/data-table/hooks/common.d.ts +14 -0
- package/dist/data-table/hooks/common.js +30 -0
- package/dist/data-table/hooks/small-hooks.d.ts +12 -0
- package/dist/data-table/hooks/small-hooks.js +111 -0
- package/dist/data-table/hooks/useClientDataTable.d.ts +35 -0
- package/dist/data-table/hooks/useClientDataTable.js +311 -0
- package/dist/data-table/hooks/useServerDataTable.d.ts +38 -0
- package/dist/data-table/hooks/useServerDataTable.js +209 -0
- package/dist/data-table/hooks/useServerRows.d.ts +27 -0
- package/dist/data-table/hooks/useServerRows.js +82 -0
- package/dist/data-table/hooks/useTableState.d.ts +39 -0
- package/dist/data-table/hooks/useTableState.js +219 -0
- package/dist/data-table/index.d.ts +42 -0
- package/dist/data-table/index.js +22 -0
- package/dist/data-table/labels.d.ts +77 -0
- package/dist/data-table/labels.js +77 -0
- package/dist/data-table/toolbar/ActiveFilters.d.ts +10 -0
- package/dist/data-table/toolbar/ActiveFilters.js +53 -0
- package/dist/data-table/toolbar/BulkActionBar.d.ts +15 -0
- package/dist/data-table/toolbar/BulkActionBar.js +22 -0
- package/dist/data-table/toolbar/ColumnVisibilityDialog.d.ts +7 -0
- package/dist/data-table/toolbar/ColumnVisibilityDialog.js +15 -0
- package/dist/data-table/toolbar/DataTableToolbar.d.ts +19 -0
- package/dist/data-table/toolbar/DataTableToolbar.js +12 -0
- package/dist/data-table/toolbar/FilterFields.d.ts +11 -0
- package/dist/data-table/toolbar/FilterFields.js +58 -0
- package/dist/data-table/toolbar/FiltersControl.d.ts +34 -0
- package/dist/data-table/toolbar/FiltersControl.js +70 -0
- package/dist/data-table/toolbar/PageSizeSelect.d.ts +7 -0
- package/dist/data-table/toolbar/PageSizeSelect.js +12 -0
- package/dist/data-table/toolbar/QuickFilters.d.ts +7 -0
- package/dist/data-table/toolbar/QuickFilters.js +9 -0
- package/dist/data-table/toolbar/ResultsCount.d.ts +7 -0
- package/dist/data-table/toolbar/ResultsCount.js +9 -0
- package/dist/data-table/toolbar/SearchField.d.ts +36 -0
- package/dist/data-table/toolbar/SearchField.js +64 -0
- package/dist/data-table/toolbar/SortSelect.d.ts +10 -0
- package/dist/data-table/toolbar/SortSelect.js +30 -0
- package/dist/data-table/types.d.ts +334 -0
- package/dist/data-table/types.js +1 -0
- package/dist/index.d.ts +91 -0
- package/dist/index.js +49 -0
- package/dist/styles.css +4668 -0
- package/dist/theme/ThemeProvider.d.ts +33 -0
- package/dist/theme/ThemeProvider.js +77 -0
- package/dist/theme/ThemeToggle.d.ts +16 -0
- package/dist/theme/ThemeToggle.js +27 -0
- package/dist/utils/cx.d.ts +2 -0
- package/dist/utils/cx.js +4 -0
- package/dist/utils/devWarn.d.ts +2 -0
- package/dist/utils/devWarn.js +10 -0
- package/dist/utils/tourGeometry.d.ts +61 -0
- package/dist/utils/tourGeometry.js +135 -0
- package/dist/utils/useFloatingPosition.d.ts +9 -0
- package/dist/utils/useFloatingPosition.js +45 -0
- package/dist/utils/useModalDialog.d.ts +7 -0
- package/dist/utils/useModalDialog.js +49 -0
- package/dist/utils/useTourAnchorRect.d.ts +6 -0
- package/dist/utils/useTourAnchorRect.js +40 -0
- package/package.json +50 -4
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export const PAGE_SIZE_OPTIONS = [10, 25, 50, 100];
|
|
2
|
+
export const DEFAULT_PAGE_SIZE = 10;
|
|
3
|
+
/** Hard maximum rows per page. Nothing in the data tables renders more. */
|
|
4
|
+
export const MAX_PAGE_SIZE = 100;
|
|
5
|
+
/** Idle time before typing in the search field is applied. */
|
|
6
|
+
export const SEARCH_DEBOUNCE_MS = 300;
|
|
7
|
+
/** The "Cargando…" announcement waits this long so fast loads stay silent. */
|
|
8
|
+
export const LOADING_ANNOUNCE_DELAY_MS = 400;
|
|
9
|
+
export const MAX_SEARCH_LENGTH = 100;
|
|
10
|
+
/** Ids of columns, filters, groups and quick filters (URLs, storage keys, React keys). */
|
|
11
|
+
export const ID_PATTERN = /^[a-z][a-z0-9_-]{0,31}$/;
|
|
12
|
+
/** Values of select/multiselect options. */
|
|
13
|
+
export const OPTION_VALUE_PATTERN = /^[A-Za-z0-9_.-]{1,64}$/;
|
|
14
|
+
export const MAX_MULTISELECT_VALUES = 20;
|
|
15
|
+
export const MAX_URL_VALUE_LENGTH = 256;
|
|
16
|
+
export const MAX_URL_PARAMS_READ = 50;
|
|
17
|
+
/** Reserved quick filter id meaning "no quick filter". */
|
|
18
|
+
export const ALL_QUICK_FILTER = 'all';
|
|
19
|
+
export function isPageSize(value) {
|
|
20
|
+
return PAGE_SIZE_OPTIONS.includes(value);
|
|
21
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { FilterError, FilterValues } from '../types.js';
|
|
2
|
+
import type { ChipFormat, CoreFilterDef } from './types.js';
|
|
3
|
+
/** True for real calendar dates written `YYYY-MM-DD` (2026-02-30 is rejected). */
|
|
4
|
+
export declare function isCalendarDate(value: unknown): value is string;
|
|
5
|
+
/** Default number parser: `123`, `123.45`, `123,45`; no grouping separators. */
|
|
6
|
+
export declare function parseNumberInput(input: string): number | null;
|
|
7
|
+
export declare function isFilterId(id: string): boolean;
|
|
8
|
+
export declare function isValidOptionValue(value: string): boolean;
|
|
9
|
+
/** Is the value "no filter"? Empty values are never applied nor serialised. */
|
|
10
|
+
export declare function isFilterValueEmpty<TRow>(def: CoreFilterDef<TRow>, value: unknown): boolean;
|
|
11
|
+
/** Client matching of one filter. Empty values match everything. */
|
|
12
|
+
export declare function matchesFilter<TRow>(def: CoreFilterDef<TRow>, row: TRow, value: unknown): boolean;
|
|
13
|
+
/** Returns a field error, or `undefined` when the (possibly draft) value can be applied. */
|
|
14
|
+
export declare function validateFilterValue<TRow>(def: CoreFilterDef<TRow>, value: unknown): FilterError | undefined;
|
|
15
|
+
/** Cleans a valid draft value into the applied shape; `undefined` when it is empty. */
|
|
16
|
+
export declare function normalizeFilterValue<TRow>(def: CoreFilterDef<TRow>, value: unknown): unknown;
|
|
17
|
+
/** Wire format of one applied (valid, non-empty) value. */
|
|
18
|
+
export declare function serializeFilterValue<TRow>(def: CoreFilterDef<TRow>, value: unknown): string;
|
|
19
|
+
/**
|
|
20
|
+
* Parses one wire value. Never throws; anything unexpected yields `undefined`. The result is
|
|
21
|
+
* validated against the definition (options, calendar dates, bounds, ranges).
|
|
22
|
+
*/
|
|
23
|
+
export declare function parseFilterValue<TRow>(def: CoreFilterDef<TRow>, raw: string): unknown;
|
|
24
|
+
/** Text of a chip: `Label: value`. Rendered as text only. */
|
|
25
|
+
export declare function filterChipText<TRow>(def: CoreFilterDef<TRow>, value: unknown, format: ChipFormat): string;
|
|
26
|
+
/** Applied filters (non-empty, declared) as `{ def, value }` pairs in definition order. */
|
|
27
|
+
export declare function activeFilters<TRow>(defs: readonly CoreFilterDef<TRow>[], values: FilterValues): {
|
|
28
|
+
def: CoreFilterDef<TRow>;
|
|
29
|
+
value: unknown;
|
|
30
|
+
}[];
|
|
31
|
+
/** Keeps only declared, valid, non-empty values in a fresh prototype-less object. */
|
|
32
|
+
export declare function sanitizeFilterValues<TRow>(defs: readonly CoreFilterDef<TRow>[], values: FilterValues): FilterValues;
|
|
33
|
+
export declare function filterValuesEqual<TRow>(defs: readonly CoreFilterDef<TRow>[], a: FilterValues, b: FilterValues): boolean;
|
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
import { ID_PATTERN, MAX_MULTISELECT_VALUES, MAX_SEARCH_LENGTH, OPTION_VALUE_PATTERN, } from '../constants.js';
|
|
2
|
+
import { emptyFilterValues } from '../query.js';
|
|
3
|
+
import { normalizeText, sanitizeText } from '../text.js';
|
|
4
|
+
const DATE_PATTERN = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
5
|
+
const NUMBER_PATTERN = /^-?\d{1,15}(\.\d{1,10})?$/;
|
|
6
|
+
/** True for real calendar dates written `YYYY-MM-DD` (2026-02-30 is rejected). */
|
|
7
|
+
export function isCalendarDate(value) {
|
|
8
|
+
if (typeof value !== 'string')
|
|
9
|
+
return false;
|
|
10
|
+
const m = DATE_PATTERN.exec(value);
|
|
11
|
+
if (!m)
|
|
12
|
+
return false;
|
|
13
|
+
const year = Number(m[1]);
|
|
14
|
+
const month = Number(m[2]);
|
|
15
|
+
const day = Number(m[3]);
|
|
16
|
+
if (month < 1 || month > 12 || day < 1)
|
|
17
|
+
return false;
|
|
18
|
+
const leap = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
|
|
19
|
+
const days = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
20
|
+
return day <= (days[month - 1] ?? 0);
|
|
21
|
+
}
|
|
22
|
+
/** Default number parser: `123`, `123.45`, `123,45`; no grouping separators. */
|
|
23
|
+
export function parseNumberInput(input) {
|
|
24
|
+
const text = input.trim().replace(',', '.');
|
|
25
|
+
if (!NUMBER_PATTERN.test(text))
|
|
26
|
+
return null;
|
|
27
|
+
const n = Number(text);
|
|
28
|
+
return Number.isFinite(n) ? n : null;
|
|
29
|
+
}
|
|
30
|
+
export function isFilterId(id) {
|
|
31
|
+
return ID_PATTERN.test(id);
|
|
32
|
+
}
|
|
33
|
+
const isRecord = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
34
|
+
const optionValues = (options) => new Set(options.map((o) => o.value));
|
|
35
|
+
export function isValidOptionValue(value) {
|
|
36
|
+
return OPTION_VALUE_PATTERN.test(value);
|
|
37
|
+
}
|
|
38
|
+
function numberOf(def, raw) {
|
|
39
|
+
if (raw === undefined || raw === '')
|
|
40
|
+
return undefined;
|
|
41
|
+
if (typeof raw === 'number')
|
|
42
|
+
return Number.isFinite(raw) ? raw : null;
|
|
43
|
+
return (def.parse ?? parseNumberInput)(raw);
|
|
44
|
+
}
|
|
45
|
+
function hasExponent(n) {
|
|
46
|
+
return /e/i.test(String(n));
|
|
47
|
+
}
|
|
48
|
+
/** Is the value "no filter"? Empty values are never applied nor serialised. */
|
|
49
|
+
export function isFilterValueEmpty(def, value) {
|
|
50
|
+
if (value === undefined || value === null)
|
|
51
|
+
return true;
|
|
52
|
+
switch (def.kind) {
|
|
53
|
+
case 'text':
|
|
54
|
+
return typeof value !== 'string' || normalizeText(value) === '';
|
|
55
|
+
case 'select':
|
|
56
|
+
return typeof value !== 'string' || value === '';
|
|
57
|
+
case 'multiselect':
|
|
58
|
+
return !Array.isArray(value) || value.length === 0;
|
|
59
|
+
case 'dateRange': {
|
|
60
|
+
if (!isRecord(value))
|
|
61
|
+
return true;
|
|
62
|
+
return !value.from && !value.to;
|
|
63
|
+
}
|
|
64
|
+
case 'numberRange': {
|
|
65
|
+
if (!isRecord(value))
|
|
66
|
+
return true;
|
|
67
|
+
const empty = (v) => v === undefined || v === null || v === '';
|
|
68
|
+
return empty(value.min) && empty(value.max);
|
|
69
|
+
}
|
|
70
|
+
case 'boolean':
|
|
71
|
+
return value !== true;
|
|
72
|
+
case 'custom':
|
|
73
|
+
return def.isEmpty(value);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** Client matching of one filter. Empty values match everything. */
|
|
77
|
+
export function matchesFilter(def, row, value) {
|
|
78
|
+
if (isFilterValueEmpty(def, value))
|
|
79
|
+
return true;
|
|
80
|
+
switch (def.kind) {
|
|
81
|
+
case 'text':
|
|
82
|
+
return normalizeText(def.value(row)).includes(normalizeText(value));
|
|
83
|
+
case 'select':
|
|
84
|
+
return def.value(row) === value;
|
|
85
|
+
case 'multiselect': {
|
|
86
|
+
const actual = def.value(row);
|
|
87
|
+
const list = typeof actual === 'string' ? [actual] : actual;
|
|
88
|
+
return value.some((v) => list.includes(v));
|
|
89
|
+
}
|
|
90
|
+
case 'dateRange': {
|
|
91
|
+
const { from, to } = value;
|
|
92
|
+
const date = def.value(row);
|
|
93
|
+
if (!date)
|
|
94
|
+
return false;
|
|
95
|
+
if (from && date < from)
|
|
96
|
+
return false;
|
|
97
|
+
if (to && date > to)
|
|
98
|
+
return false;
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
case 'numberRange': {
|
|
102
|
+
const { min, max } = value;
|
|
103
|
+
const n = def.value(row);
|
|
104
|
+
if (n === null || n === undefined || Number.isNaN(n))
|
|
105
|
+
return false;
|
|
106
|
+
if (min !== undefined && n < min)
|
|
107
|
+
return false;
|
|
108
|
+
if (max !== undefined && n > max)
|
|
109
|
+
return false;
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
case 'boolean':
|
|
113
|
+
return def.value(row) === true;
|
|
114
|
+
case 'custom':
|
|
115
|
+
return def.match ? def.match(row, value) : true;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/** Returns a field error, or `undefined` when the (possibly draft) value can be applied. */
|
|
119
|
+
export function validateFilterValue(def, value) {
|
|
120
|
+
if (isFilterValueEmpty(def, value))
|
|
121
|
+
return undefined;
|
|
122
|
+
switch (def.kind) {
|
|
123
|
+
case 'text':
|
|
124
|
+
return value.length > (def.maxLength ?? MAX_SEARCH_LENGTH)
|
|
125
|
+
? { code: 'too-long' }
|
|
126
|
+
: undefined;
|
|
127
|
+
case 'select':
|
|
128
|
+
return optionValues(def.options).has(value) ? undefined : { code: 'invalid-option' };
|
|
129
|
+
case 'multiselect': {
|
|
130
|
+
const allowed = optionValues(def.options);
|
|
131
|
+
const list = value;
|
|
132
|
+
if (list.length > MAX_MULTISELECT_VALUES)
|
|
133
|
+
return { code: 'too-long' };
|
|
134
|
+
return list.every((v) => typeof v === 'string' && allowed.has(v))
|
|
135
|
+
? undefined
|
|
136
|
+
: { code: 'invalid-option' };
|
|
137
|
+
}
|
|
138
|
+
case 'dateRange': {
|
|
139
|
+
const { from, to } = value;
|
|
140
|
+
if ((from && !isCalendarDate(from)) || (to && !isCalendarDate(to))) {
|
|
141
|
+
return { code: 'invalid-date' };
|
|
142
|
+
}
|
|
143
|
+
if (from && to && from > to)
|
|
144
|
+
return { code: 'invalid-range' };
|
|
145
|
+
if ((from && def.min && from < def.min) || (to && def.max && to > def.max)) {
|
|
146
|
+
return { code: 'out-of-bounds' };
|
|
147
|
+
}
|
|
148
|
+
if ((from && def.max && from > def.max) || (to && def.min && to < def.min)) {
|
|
149
|
+
return { code: 'out-of-bounds' };
|
|
150
|
+
}
|
|
151
|
+
return undefined;
|
|
152
|
+
}
|
|
153
|
+
case 'numberRange': {
|
|
154
|
+
const raw = value;
|
|
155
|
+
const min = numberOf(def, raw.min);
|
|
156
|
+
const max = numberOf(def, raw.max);
|
|
157
|
+
if (min === null || max === null)
|
|
158
|
+
return { code: 'invalid-number' };
|
|
159
|
+
if ((min !== undefined && hasExponent(min)) || (max !== undefined && hasExponent(max))) {
|
|
160
|
+
return { code: 'invalid-number' };
|
|
161
|
+
}
|
|
162
|
+
if (min !== undefined && max !== undefined && min > max)
|
|
163
|
+
return { code: 'invalid-range' };
|
|
164
|
+
for (const n of [min, max]) {
|
|
165
|
+
if (n === undefined)
|
|
166
|
+
continue;
|
|
167
|
+
if ((def.min !== undefined && n < def.min) || (def.max !== undefined && n > def.max)) {
|
|
168
|
+
return { code: 'out-of-bounds' };
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
case 'boolean':
|
|
174
|
+
return undefined;
|
|
175
|
+
case 'custom': {
|
|
176
|
+
const message = def.validate?.(value);
|
|
177
|
+
return message ? { code: 'custom', message } : undefined;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/** Cleans a valid draft value into the applied shape; `undefined` when it is empty. */
|
|
182
|
+
export function normalizeFilterValue(def, value) {
|
|
183
|
+
if (isFilterValueEmpty(def, value))
|
|
184
|
+
return undefined;
|
|
185
|
+
switch (def.kind) {
|
|
186
|
+
case 'text':
|
|
187
|
+
return sanitizeText(value, def.maxLength ?? MAX_SEARCH_LENGTH);
|
|
188
|
+
case 'select':
|
|
189
|
+
case 'custom':
|
|
190
|
+
return value;
|
|
191
|
+
case 'multiselect':
|
|
192
|
+
return [...new Set(value)];
|
|
193
|
+
case 'dateRange': {
|
|
194
|
+
const { from, to } = value;
|
|
195
|
+
const out = {};
|
|
196
|
+
if (from)
|
|
197
|
+
out.from = from;
|
|
198
|
+
if (to)
|
|
199
|
+
out.to = to;
|
|
200
|
+
return out;
|
|
201
|
+
}
|
|
202
|
+
case 'numberRange': {
|
|
203
|
+
const raw = value;
|
|
204
|
+
const min = numberOf(def, raw.min);
|
|
205
|
+
const max = numberOf(def, raw.max);
|
|
206
|
+
const out = {};
|
|
207
|
+
if (min !== undefined && min !== null)
|
|
208
|
+
out.min = min;
|
|
209
|
+
if (max !== undefined && max !== null)
|
|
210
|
+
out.max = max;
|
|
211
|
+
return out;
|
|
212
|
+
}
|
|
213
|
+
case 'boolean':
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/** Wire format of one applied (valid, non-empty) value. */
|
|
218
|
+
export function serializeFilterValue(def, value) {
|
|
219
|
+
switch (def.kind) {
|
|
220
|
+
case 'text':
|
|
221
|
+
case 'select':
|
|
222
|
+
return String(value);
|
|
223
|
+
case 'multiselect':
|
|
224
|
+
return value.join(',');
|
|
225
|
+
case 'dateRange': {
|
|
226
|
+
const { from, to } = value;
|
|
227
|
+
return `${from ?? ''}..${to ?? ''}`;
|
|
228
|
+
}
|
|
229
|
+
case 'numberRange': {
|
|
230
|
+
const { min, max } = value;
|
|
231
|
+
return `${min ?? ''}..${max ?? ''}`;
|
|
232
|
+
}
|
|
233
|
+
case 'boolean':
|
|
234
|
+
return '1';
|
|
235
|
+
case 'custom':
|
|
236
|
+
return def.serialize(value);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
function splitRange(raw) {
|
|
240
|
+
const parts = raw.split('..');
|
|
241
|
+
if (parts.length !== 2)
|
|
242
|
+
return undefined;
|
|
243
|
+
return [parts[0], parts[1]];
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Parses one wire value. Never throws; anything unexpected yields `undefined`. The result is
|
|
247
|
+
* validated against the definition (options, calendar dates, bounds, ranges).
|
|
248
|
+
*/
|
|
249
|
+
export function parseFilterValue(def, raw) {
|
|
250
|
+
let value;
|
|
251
|
+
try {
|
|
252
|
+
switch (def.kind) {
|
|
253
|
+
case 'text':
|
|
254
|
+
value = sanitizeText(raw, def.maxLength ?? MAX_SEARCH_LENGTH);
|
|
255
|
+
break;
|
|
256
|
+
case 'select':
|
|
257
|
+
value = raw;
|
|
258
|
+
break;
|
|
259
|
+
case 'multiselect': {
|
|
260
|
+
const parts = raw.split(',').filter((p) => p !== '');
|
|
261
|
+
if (parts.length === 0 || parts.length > MAX_MULTISELECT_VALUES)
|
|
262
|
+
return undefined;
|
|
263
|
+
value = [...new Set(parts)];
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
case 'dateRange': {
|
|
267
|
+
const range = splitRange(raw);
|
|
268
|
+
if (!range)
|
|
269
|
+
return undefined;
|
|
270
|
+
value = { ...(range[0] ? { from: range[0] } : {}), ...(range[1] ? { to: range[1] } : {}) };
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
case 'numberRange': {
|
|
274
|
+
const range = splitRange(raw);
|
|
275
|
+
if (!range)
|
|
276
|
+
return undefined;
|
|
277
|
+
const parse = (s) => (s === '' ? undefined : parseNumberInput(s));
|
|
278
|
+
const min = parse(range[0]);
|
|
279
|
+
const max = parse(range[1]);
|
|
280
|
+
if (min === null || max === null)
|
|
281
|
+
return undefined;
|
|
282
|
+
value = { ...(min !== undefined ? { min } : {}), ...(max !== undefined ? { max } : {}) };
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
case 'boolean':
|
|
286
|
+
if (raw !== '1')
|
|
287
|
+
return undefined;
|
|
288
|
+
value = true;
|
|
289
|
+
break;
|
|
290
|
+
case 'custom':
|
|
291
|
+
value = def.parse(raw);
|
|
292
|
+
if (value === undefined)
|
|
293
|
+
return undefined;
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
if (isFilterValueEmpty(def, value) || validateFilterValue(def, value))
|
|
297
|
+
return undefined;
|
|
298
|
+
return normalizeFilterValue(def, value);
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
return undefined;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
function ddmmyyyy(iso) {
|
|
305
|
+
const [y, m, d] = iso.split('-');
|
|
306
|
+
return `${d}/${m}/${y}`;
|
|
307
|
+
}
|
|
308
|
+
/** Text of a chip: `Label: value`. Rendered as text only. */
|
|
309
|
+
export function filterChipText(def, value, format) {
|
|
310
|
+
let text;
|
|
311
|
+
switch (def.kind) {
|
|
312
|
+
case 'text':
|
|
313
|
+
text = String(value);
|
|
314
|
+
break;
|
|
315
|
+
case 'select':
|
|
316
|
+
text = def.options.find((o) => o.value === value)?.label ?? String(value);
|
|
317
|
+
break;
|
|
318
|
+
case 'multiselect':
|
|
319
|
+
text = value
|
|
320
|
+
.map((v) => def.options.find((o) => o.value === v)?.label ?? v)
|
|
321
|
+
.join(', ');
|
|
322
|
+
break;
|
|
323
|
+
case 'dateRange': {
|
|
324
|
+
const { from, to } = value;
|
|
325
|
+
text =
|
|
326
|
+
from && to
|
|
327
|
+
? format.rangeBetween(ddmmyyyy(from), ddmmyyyy(to))
|
|
328
|
+
: from
|
|
329
|
+
? format.rangeFrom(ddmmyyyy(from))
|
|
330
|
+
: format.rangeTo(ddmmyyyy(to));
|
|
331
|
+
break;
|
|
332
|
+
}
|
|
333
|
+
case 'numberRange': {
|
|
334
|
+
const { min, max } = value;
|
|
335
|
+
const f = format.formatNumber ?? String;
|
|
336
|
+
text =
|
|
337
|
+
min !== undefined && max !== undefined
|
|
338
|
+
? format.rangeBetween(f(min), f(max))
|
|
339
|
+
: min !== undefined
|
|
340
|
+
? format.rangeFrom(f(min))
|
|
341
|
+
: format.rangeTo(f(max));
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
case 'boolean':
|
|
345
|
+
text = format.yes;
|
|
346
|
+
break;
|
|
347
|
+
case 'custom':
|
|
348
|
+
text = def.chipLabel(value);
|
|
349
|
+
break;
|
|
350
|
+
}
|
|
351
|
+
return `${def.label}: ${text}`;
|
|
352
|
+
}
|
|
353
|
+
/** Applied filters (non-empty, declared) as `{ def, value }` pairs in definition order. */
|
|
354
|
+
export function activeFilters(defs, values) {
|
|
355
|
+
const out = [];
|
|
356
|
+
for (const def of defs) {
|
|
357
|
+
const value = values[def.id];
|
|
358
|
+
if (!isFilterValueEmpty(def, value))
|
|
359
|
+
out.push({ def, value });
|
|
360
|
+
}
|
|
361
|
+
return out;
|
|
362
|
+
}
|
|
363
|
+
/** Keeps only declared, valid, non-empty values in a fresh prototype-less object. */
|
|
364
|
+
export function sanitizeFilterValues(defs, values) {
|
|
365
|
+
const out = emptyFilterValues();
|
|
366
|
+
const target = out;
|
|
367
|
+
for (const def of defs) {
|
|
368
|
+
if (!Object.prototype.hasOwnProperty.call(values, def.id))
|
|
369
|
+
continue;
|
|
370
|
+
const value = values[def.id];
|
|
371
|
+
if (validateFilterValue(def, value))
|
|
372
|
+
continue;
|
|
373
|
+
const normalized = normalizeFilterValue(def, value);
|
|
374
|
+
if (normalized !== undefined)
|
|
375
|
+
target[def.id] = normalized;
|
|
376
|
+
}
|
|
377
|
+
return out;
|
|
378
|
+
}
|
|
379
|
+
export function filterValuesEqual(defs, a, b) {
|
|
380
|
+
for (const def of defs) {
|
|
381
|
+
const ea = isFilterValueEmpty(def, a[def.id]);
|
|
382
|
+
const eb = isFilterValueEmpty(def, b[def.id]);
|
|
383
|
+
if (ea !== eb)
|
|
384
|
+
return false;
|
|
385
|
+
if (ea)
|
|
386
|
+
continue;
|
|
387
|
+
const va = normalizeFilterValue(def, a[def.id]);
|
|
388
|
+
const vb = normalizeFilterValue(def, b[def.id]);
|
|
389
|
+
if (JSON.stringify(va) !== JSON.stringify(vb))
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
return true;
|
|
393
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { FilterError } from '../types.js';
|
|
2
|
+
/** Colour of the dot before a multiselect option (the status tones of the library). */
|
|
3
|
+
export type FilterOptionTone = 'neutral' | 'success' | 'warning' | 'danger' | 'info' | 'violet';
|
|
4
|
+
export interface FilterOption {
|
|
5
|
+
/** Pattern `^[A-Za-z0-9_.-]{1,64}$`. */
|
|
6
|
+
value: string;
|
|
7
|
+
label: string;
|
|
8
|
+
/** Multiselect shown as chips: a coloured dot before the label. Colour is never the only signal. */
|
|
9
|
+
tone?: FilterOptionTone;
|
|
10
|
+
}
|
|
11
|
+
export interface DateRangeValue {
|
|
12
|
+
/** Calendar dates `YYYY-MM-DD`. */
|
|
13
|
+
from?: string;
|
|
14
|
+
to?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface NumberRangeValue {
|
|
17
|
+
min?: number;
|
|
18
|
+
max?: number;
|
|
19
|
+
}
|
|
20
|
+
/** What a number range field may hold while the user types: numbers or raw text. */
|
|
21
|
+
export interface DraftNumberRangeValue {
|
|
22
|
+
min?: number | string;
|
|
23
|
+
max?: number | string;
|
|
24
|
+
}
|
|
25
|
+
export interface FilterValueMap {
|
|
26
|
+
text: string;
|
|
27
|
+
select: string;
|
|
28
|
+
multiselect: readonly string[];
|
|
29
|
+
dateRange: DateRangeValue;
|
|
30
|
+
numberRange: NumberRangeValue;
|
|
31
|
+
boolean: boolean;
|
|
32
|
+
}
|
|
33
|
+
export interface FilterDefBase {
|
|
34
|
+
/** Same id rules as columns: `^[a-z][a-z0-9_-]{0,31}$`. */
|
|
35
|
+
id: string;
|
|
36
|
+
/** Field label and chip prefix. */
|
|
37
|
+
label: string;
|
|
38
|
+
}
|
|
39
|
+
/** Filter definition without anything React-related (the public `FilterDef` extends it). */
|
|
40
|
+
export interface CoreCustomFilterDef<TRow, V> extends FilterDefBase {
|
|
41
|
+
kind: 'custom';
|
|
42
|
+
isEmpty: (value: V) => boolean;
|
|
43
|
+
/** Client only. */
|
|
44
|
+
match?: (row: TRow, value: V) => boolean;
|
|
45
|
+
/** URL-safe, at most 256 characters. */
|
|
46
|
+
serialize: (value: V) => string;
|
|
47
|
+
/** Must return `undefined` for anything unexpected. */
|
|
48
|
+
parse: (raw: string) => V | undefined;
|
|
49
|
+
chipLabel: (value: V) => string;
|
|
50
|
+
validate?: (value: V) => string | undefined;
|
|
51
|
+
}
|
|
52
|
+
export type AnyCoreCustomFilterDef<TRow> = CoreCustomFilterDef<TRow, any>;
|
|
53
|
+
export type CoreFilterDef<TRow> = (FilterDefBase & {
|
|
54
|
+
kind: 'text';
|
|
55
|
+
value: (row: TRow) => string;
|
|
56
|
+
maxLength?: number;
|
|
57
|
+
}) | (FilterDefBase & {
|
|
58
|
+
kind: 'select';
|
|
59
|
+
options: readonly FilterOption[];
|
|
60
|
+
value: (row: TRow) => string;
|
|
61
|
+
}) | (FilterDefBase & {
|
|
62
|
+
kind: 'multiselect';
|
|
63
|
+
options: readonly FilterOption[];
|
|
64
|
+
value: (row: TRow) => string | readonly string[];
|
|
65
|
+
}) | (FilterDefBase & {
|
|
66
|
+
kind: 'dateRange';
|
|
67
|
+
value: (row: TRow) => string | null;
|
|
68
|
+
min?: string;
|
|
69
|
+
max?: string;
|
|
70
|
+
}) | (FilterDefBase & {
|
|
71
|
+
kind: 'numberRange';
|
|
72
|
+
value: (row: TRow) => number | null;
|
|
73
|
+
parse?: (input: string) => number | null;
|
|
74
|
+
min?: number;
|
|
75
|
+
max?: number;
|
|
76
|
+
}) | (FilterDefBase & {
|
|
77
|
+
kind: 'boolean';
|
|
78
|
+
value: (row: TRow) => boolean;
|
|
79
|
+
}) | AnyCoreCustomFilterDef<TRow>;
|
|
80
|
+
/** Text pieces the chips need; the data table labels provide them in Spanish. */
|
|
81
|
+
export interface ChipFormat {
|
|
82
|
+
yes: string;
|
|
83
|
+
rangeFrom: (value: string) => string;
|
|
84
|
+
rangeTo: (value: string) => string;
|
|
85
|
+
rangeBetween: (from: string, to: string) => string;
|
|
86
|
+
formatNumber?: (value: number) => string;
|
|
87
|
+
}
|
|
88
|
+
export type { FilterError };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { PageGroup } from './types.js';
|
|
2
|
+
export type GroupOrder = readonly string[] | ((a: string, b: string) => number);
|
|
3
|
+
/**
|
|
4
|
+
* Orders rows by group key (explicit `order` first, unknown keys after, by collator) keeping
|
|
5
|
+
* the incoming order inside each group. Stable; never mutates the input.
|
|
6
|
+
*/
|
|
7
|
+
export declare function orderByGroup<TRow>(rows: readonly TRow[], keyOf: (row: TRow) => string, order: GroupOrder | undefined, collator: Intl.Collator): TRow[];
|
|
8
|
+
/**
|
|
9
|
+
* Splits the rows of one page (`ordered.slice(start, end)`) into groups and flags the groups
|
|
10
|
+
* that continue from the previous page or onto the next one. `ordered` is the whole result in
|
|
11
|
+
* display order, already ordered by group.
|
|
12
|
+
*/
|
|
13
|
+
export declare function groupPageRows<TRow>(ordered: readonly TRow[], start: number, end: number, keyOf: (row: TRow) => string, labelOf: (key: string) => string): PageGroup<TRow>[];
|
|
14
|
+
/** Distinct group keys in order of appearance. */
|
|
15
|
+
export declare function groupKeysOf<TRow>(ordered: readonly TRow[], keyOf: (row: TRow) => string): string[];
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Orders rows by group key (explicit `order` first, unknown keys after, by collator) keeping
|
|
3
|
+
* the incoming order inside each group. Stable; never mutates the input.
|
|
4
|
+
*/
|
|
5
|
+
export function orderByGroup(rows, keyOf, order, collator) {
|
|
6
|
+
const compareKeys = (a, b) => {
|
|
7
|
+
if (typeof order === 'function')
|
|
8
|
+
return order(a, b);
|
|
9
|
+
if (order) {
|
|
10
|
+
const ia = order.indexOf(a);
|
|
11
|
+
const ib = order.indexOf(b);
|
|
12
|
+
if (ia !== -1 || ib !== -1) {
|
|
13
|
+
if (ia === -1)
|
|
14
|
+
return 1;
|
|
15
|
+
if (ib === -1)
|
|
16
|
+
return -1;
|
|
17
|
+
return ia - ib;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return collator.compare(a, b);
|
|
21
|
+
};
|
|
22
|
+
const entries = rows.map((row, index) => ({ row, index, key: keyOf(row) }));
|
|
23
|
+
entries.sort((a, b) => compareKeys(a.key, b.key) || a.index - b.index);
|
|
24
|
+
return entries.map((entry) => entry.row);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Splits the rows of one page (`ordered.slice(start, end)`) into groups and flags the groups
|
|
28
|
+
* that continue from the previous page or onto the next one. `ordered` is the whole result in
|
|
29
|
+
* display order, already ordered by group.
|
|
30
|
+
*/
|
|
31
|
+
export function groupPageRows(ordered, start, end, keyOf, labelOf) {
|
|
32
|
+
const groups = [];
|
|
33
|
+
for (let i = start; i < end; i++) {
|
|
34
|
+
const row = ordered[i];
|
|
35
|
+
const key = keyOf(row);
|
|
36
|
+
const last = groups[groups.length - 1];
|
|
37
|
+
if (last && last.key === key) {
|
|
38
|
+
;
|
|
39
|
+
last.rows.push(row);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
groups.push({
|
|
43
|
+
key,
|
|
44
|
+
label: labelOf(key),
|
|
45
|
+
rows: [row],
|
|
46
|
+
continuesFromPreviousPage: false,
|
|
47
|
+
continuesOnNextPage: false,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const first = groups[0];
|
|
52
|
+
const lastGroup = groups[groups.length - 1];
|
|
53
|
+
if (first && start > 0 && keyOf(ordered[start - 1]) === first.key) {
|
|
54
|
+
first.continuesFromPreviousPage = true;
|
|
55
|
+
}
|
|
56
|
+
if (lastGroup && end < ordered.length && keyOf(ordered[end]) === lastGroup.key) {
|
|
57
|
+
lastGroup.continuesOnNextPage = true;
|
|
58
|
+
}
|
|
59
|
+
return groups;
|
|
60
|
+
}
|
|
61
|
+
/** Distinct group keys in order of appearance. */
|
|
62
|
+
export function groupKeysOf(ordered, keyOf) {
|
|
63
|
+
const keys = [];
|
|
64
|
+
const seen = new Set();
|
|
65
|
+
for (const row of ordered) {
|
|
66
|
+
const key = keyOf(row);
|
|
67
|
+
if (!seen.has(key)) {
|
|
68
|
+
seen.add(key);
|
|
69
|
+
keys.push(key);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return keys;
|
|
73
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare function pageCount(rowCount: number, pageSize: number): number;
|
|
2
|
+
/** Clamps a 1-based page into `[1, pageCount]`; non-integers fall back to 1. */
|
|
3
|
+
export declare function clampPage(page: number, count: number): number;
|
|
4
|
+
export declare function pageBounds(page: number, pageSize: number, rowCount: number): {
|
|
5
|
+
start: number;
|
|
6
|
+
end: number;
|
|
7
|
+
};
|
|
8
|
+
export declare function slicePage<T>(rows: readonly T[], page: number, pageSize: number): T[];
|
|
9
|
+
/** 1-based inclusive range of the rows on a page; `{0, 0}` when empty. */
|
|
10
|
+
export declare function rangeOf(page: number, pageSize: number, rowCount: number): {
|
|
11
|
+
from: number;
|
|
12
|
+
to: number;
|
|
13
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export function pageCount(rowCount, pageSize) {
|
|
2
|
+
if (!Number.isFinite(rowCount) || rowCount <= 0 || pageSize <= 0)
|
|
3
|
+
return 1;
|
|
4
|
+
return Math.max(1, Math.ceil(rowCount / pageSize));
|
|
5
|
+
}
|
|
6
|
+
/** Clamps a 1-based page into `[1, pageCount]`; non-integers fall back to 1. */
|
|
7
|
+
export function clampPage(page, count) {
|
|
8
|
+
if (!Number.isInteger(page))
|
|
9
|
+
return 1;
|
|
10
|
+
return Math.min(Math.max(1, page), Math.max(1, count));
|
|
11
|
+
}
|
|
12
|
+
export function pageBounds(page, pageSize, rowCount) {
|
|
13
|
+
const start = Math.min((page - 1) * pageSize, Math.max(0, rowCount));
|
|
14
|
+
const end = Math.min(start + pageSize, rowCount);
|
|
15
|
+
return { start, end };
|
|
16
|
+
}
|
|
17
|
+
export function slicePage(rows, page, pageSize) {
|
|
18
|
+
const { start, end } = pageBounds(page, pageSize, rows.length);
|
|
19
|
+
return rows.slice(start, end);
|
|
20
|
+
}
|
|
21
|
+
/** 1-based inclusive range of the rows on a page; `{0, 0}` when empty. */
|
|
22
|
+
export function rangeOf(page, pageSize, rowCount) {
|
|
23
|
+
if (rowCount <= 0)
|
|
24
|
+
return { from: 0, to: 0 };
|
|
25
|
+
const { start, end } = pageBounds(page, pageSize, rowCount);
|
|
26
|
+
return { from: start + 1, to: end };
|
|
27
|
+
}
|