@dloizides/ui-tables 1.19.2 → 1.21.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/dist/index.js CHANGED
@@ -5,6 +5,7 @@ var reactNative = require('react-native');
5
5
  var uiFeedback = require('@dloizides/ui-feedback');
6
6
  var jsxRuntime = require('react/jsx-runtime');
7
7
  var uiMotion = require('@dloizides/ui-motion');
8
+ var designTokens = require('@dloizides/design-tokens');
8
9
  var reactDom = require('react-dom');
9
10
  var uiForms = require('@dloizides/ui-forms');
10
11
 
@@ -179,13 +180,13 @@ var MENU_TOP_GAP = 4;
179
180
  var MENU_PAD_V = 4;
180
181
  var MENU_MIN_WIDTH = 80;
181
182
  var MENU_OPT_PAD_V = 7;
182
- var MENU_Z_INDEX = 1e3;
183
+ var MENU_Z_INDEX = designTokens.Z_INDEX.popover;
183
184
  var MENU_ELEVATION = 8;
184
185
  var MENU_BOX_SHADOW = "0px 2px 8px rgba(0, 0, 0, 0.15)";
185
186
  var RESULTS_FONT = 13;
186
187
  var HEAD_LETTER_SPACING = 0.4;
187
188
  var LABEL_LETTER_SPACING = 0.3;
188
- var STICKY_Z = 2;
189
+ var STICKY_Z = designTokens.Z_INDEX.sticky;
189
190
  var FULL_WIDTH = "100%";
190
191
  var SELECT_COL_WIDTH = MIN_TARGET_PX;
191
192
  var CHECKBOX_SIZE = 18;
@@ -1835,6 +1836,310 @@ function usePagedRows(rows, options = {}) {
1835
1836
  };
1836
1837
  }
1837
1838
 
1839
+ // src/StatusLedger/StatusTone.ts
1840
+ var StatusTone = /* @__PURE__ */ ((StatusTone2) => {
1841
+ StatusTone2["Bad"] = "bad";
1842
+ StatusTone2["Warn"] = "warn";
1843
+ StatusTone2["Active"] = "active";
1844
+ StatusTone2["Good"] = "good";
1845
+ StatusTone2["Neutral"] = "neutral";
1846
+ return StatusTone2;
1847
+ })(StatusTone || {});
1848
+
1849
+ // src/StatusLedger/constants.ts
1850
+ var STATUS_LEDGER_TEST_ID = "ui-status-ledger";
1851
+ var STATUS_COLUMN_KEY = "status";
1852
+ var ACTIONS_COLUMN_KEY = "actions";
1853
+ var ALL_VALUE = "";
1854
+ var TONE_RANK = {
1855
+ ["bad" /* Bad */]: 0,
1856
+ ["warn" /* Warn */]: 1,
1857
+ ["active" /* Active */]: 2,
1858
+ ["neutral" /* Neutral */]: 3,
1859
+ ["good" /* Good */]: 4
1860
+ };
1861
+ var TONE_GLYPH = {
1862
+ ["bad" /* Bad */]: "\u2715",
1863
+ ["warn" /* Warn */]: "!",
1864
+ ["active" /* Active */]: "\u25B8",
1865
+ ["neutral" /* Neutral */]: "\u2013",
1866
+ ["good" /* Good */]: "\u2713"
1867
+ };
1868
+ var ledgerSectionTestID = (testID, section) => `${testID}-section-${section}`;
1869
+ var ledgerSortDirectionTestID = (testID) => `${testID}-sort-direction`;
1870
+ var PILL_RADIUS2 = 999;
1871
+ var PILL_BORDER = 1;
1872
+ var PILL_PAD_H = 8;
1873
+ var PILL_PAD_V = 2;
1874
+ var PILL_GAP = 6;
1875
+ var PILL_FONT = 13;
1876
+ var PILL_WEIGHT = "600";
1877
+ var styles2 = reactNative.StyleSheet.create({
1878
+ pill: {
1879
+ flexDirection: "row",
1880
+ alignItems: "center",
1881
+ alignSelf: "flex-start",
1882
+ gap: PILL_GAP,
1883
+ borderWidth: PILL_BORDER,
1884
+ borderRadius: PILL_RADIUS2,
1885
+ paddingHorizontal: PILL_PAD_H,
1886
+ paddingVertical: PILL_PAD_V
1887
+ },
1888
+ text: { fontSize: PILL_FONT, fontWeight: PILL_WEIGHT }
1889
+ });
1890
+ function StatusPill({ label, tone, color, testID }) {
1891
+ return /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { accessibilityLabel: label, accessibilityRole: "text", style: [styles2.pill, { borderColor: color }], testID, children: [
1892
+ /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { "aria-hidden": true, style: [styles2.text, { color }], children: TONE_GLYPH[tone] }),
1893
+ /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style: [styles2.text, { color }], children: label })
1894
+ ] });
1895
+ }
1896
+
1897
+ // src/StatusLedger/SortDirection.ts
1898
+ var SortDirection = /* @__PURE__ */ ((SortDirection2) => {
1899
+ SortDirection2["Asc"] = "asc";
1900
+ SortDirection2["Desc"] = "desc";
1901
+ return SortDirection2;
1902
+ })(SortDirection || {});
1903
+
1904
+ // src/StatusLedger/utils/ledgerLogic.ts
1905
+ var BEFORE = -1;
1906
+ var AFTER = 1;
1907
+ var SAME = 0;
1908
+ var isMissing = (value) => value === null || value === void 0 || value === "";
1909
+ var compareValues = (a, b) => {
1910
+ if (typeof a === "number" && typeof b === "number") return a - b;
1911
+ return String(a).localeCompare(String(b), void 0, { numeric: true, sensitivity: "base" });
1912
+ };
1913
+ function filterLedgerRows(rows, filter, accessors) {
1914
+ const needle = filter.text.trim().toLocaleLowerCase();
1915
+ return rows.filter((row) => {
1916
+ const statusOk = filter.status === ALL_VALUE || accessors.getStatus(row) === filter.status;
1917
+ const groupOk = filter.group === ALL_VALUE || accessors.getGroup?.(row) === filter.group;
1918
+ const haystack = accessors.getSearchText?.(row) ?? "";
1919
+ const textOk = needle === "" || haystack.toLocaleLowerCase().includes(needle);
1920
+ return statusOk && groupOk && textOk;
1921
+ });
1922
+ }
1923
+ function sortLedgerRows(rows, sortValue, direction) {
1924
+ const sign = direction === "asc" /* Asc */ ? AFTER : BEFORE;
1925
+ return [...rows].sort((left, right) => {
1926
+ const a = sortValue(left);
1927
+ const b = sortValue(right);
1928
+ if (isMissing(a) && isMissing(b)) return SAME;
1929
+ if (isMissing(a)) return AFTER;
1930
+ if (isMissing(b)) return BEFORE;
1931
+ return sign * compareValues(a, b);
1932
+ });
1933
+ }
1934
+ var toneRank = (tone) => TONE_RANK[tone];
1935
+ var isAttentionTone = (tone) => tone === "bad" /* Bad */ || tone === "warn" /* Warn */;
1936
+ function splitNeedsLook(rows, needsLook, toneOf) {
1937
+ const attention = rows.filter(needsLook);
1938
+ const rest = rows.filter((row) => !needsLook(row));
1939
+ return { attention: sortLedgerRows(attention, (row) => toneRank(toneOf(row)), "asc" /* Asc */), rest };
1940
+ }
1941
+ function distinctValues(rows, pick) {
1942
+ const seen = /* @__PURE__ */ new Set();
1943
+ rows.forEach((row) => {
1944
+ const value = pick(row);
1945
+ if (value !== void 0 && value !== "") seen.add(value);
1946
+ });
1947
+ return [...seen];
1948
+ }
1949
+ var readText = (values, key) => {
1950
+ const value = values[key];
1951
+ return typeof value === "string" ? value : ALL_VALUE;
1952
+ };
1953
+ function useToneColor(toneColors) {
1954
+ const { theme } = uiFeedback.useUi();
1955
+ return React2.useCallback(
1956
+ (tone) => {
1957
+ const override = toneColors?.[tone];
1958
+ if (override !== void 0) return override;
1959
+ if (tone === "bad" /* Bad */) return theme.semantic.error["500"];
1960
+ if (tone === "active" /* Active */) return theme.palette.primary["500"];
1961
+ return tone === "good" /* Good */ ? theme.colors.textSecondary : theme.colors.text;
1962
+ },
1963
+ [toneColors, theme]
1964
+ );
1965
+ }
1966
+ function useLedgerView(props) {
1967
+ const { rows, columns, getStatus, statusLabel, statusTone, labels, getGroup, groupLabel, getSearchText, needsLook, renderActions, toneColors } = props;
1968
+ const [values, setValues] = React2.useState({});
1969
+ const [direction, setDirection] = React2.useState("asc" /* Asc */);
1970
+ const toneColor = useToneColor(toneColors);
1971
+ const toneOf = React2.useCallback((row) => statusTone(getStatus(row)), [statusTone, getStatus]);
1972
+ const allColumns = React2.useMemo(() => {
1973
+ const status = {
1974
+ key: STATUS_COLUMN_KEY,
1975
+ header: labels.status,
1976
+ sortValue: (row) => toneRank(toneOf(row)),
1977
+ render: (row) => /* @__PURE__ */ jsxRuntime.jsx(StatusPill, { color: toneColor(toneOf(row)), label: statusLabel(getStatus(row)), tone: toneOf(row) })
1978
+ };
1979
+ const actions = renderActions === void 0 ? [] : [{ key: ACTIONS_COLUMN_KEY, header: labels.actions, render: renderActions }];
1980
+ return [columns[0], status, ...columns.slice(1), ...actions].filter((c) => c !== void 0);
1981
+ }, [columns, labels, toneOf, toneColor, statusLabel, getStatus, renderActions]);
1982
+ const fields = React2.useMemo(() => {
1983
+ const all = { value: ALL_VALUE, label: labels.all };
1984
+ const statuses = distinctValues(rows, getStatus).map((s) => ({ value: s, label: statusLabel(s) }));
1985
+ const groups = getGroup === void 0 ? [] : distinctValues(rows, getGroup).map((g) => ({ value: g, label: groupLabel?.(g) ?? g }));
1986
+ const sortable = allColumns.filter((c) => "sortValue" in c && c.sortValue !== void 0).map((c) => ({ value: c.key, label: c.header }));
1987
+ const groupField = groups.length === 0 ? [] : [{ kind: "select", key: "group" /* Group */, label: labels.group, options: [all, ...groups] }];
1988
+ return [
1989
+ { kind: "text", key: "search" /* Search */, label: labels.search, grow: true },
1990
+ { kind: "select", key: "status" /* Status */, label: labels.status, options: [all, ...statuses] },
1991
+ ...groupField,
1992
+ { kind: "select", key: "sort" /* Sort */, label: labels.sortBy, options: sortable }
1993
+ ];
1994
+ }, [rows, getStatus, statusLabel, getGroup, groupLabel, labels, allColumns]);
1995
+ const visible = React2.useMemo(() => {
1996
+ const filter = { status: readText(values, "status" /* Status */), group: readText(values, "group" /* Group */), text: readText(values, "search" /* Search */) };
1997
+ const filtered = filterLedgerRows(rows, filter, { getStatus, getGroup, getSearchText });
1998
+ const sortColumn = allColumns.find((c) => c.key === readText(values, "sort" /* Sort */));
1999
+ return sortColumn?.sortValue === void 0 ? filtered : sortLedgerRows(filtered, sortColumn.sortValue, direction);
2000
+ }, [rows, values, getStatus, getGroup, getSearchText, allColumns, direction]);
2001
+ const split = React2.useMemo(() => splitNeedsLook(visible, needsLook ?? ((row) => isAttentionTone(toneOf(row))), toneOf), [visible, needsLook, toneOf]);
2002
+ const toggleDirection = React2.useCallback(() => setDirection((d) => d === "asc" /* Asc */ ? "desc" /* Desc */ : "asc" /* Asc */), []);
2003
+ const clear = React2.useCallback(() => setValues({}), []);
2004
+ const tableColumns = allColumns;
2005
+ return { values, setValues, direction, toggleDirection, clear, fields, visible, split, tableColumns };
2006
+ }
2007
+
2008
+ // src/StatusLedger/LedgerLayout.ts
2009
+ var LedgerLayout = /* @__PURE__ */ ((LedgerLayout2) => {
2010
+ LedgerLayout2["Auto"] = "auto";
2011
+ LedgerLayout2["Table"] = "table";
2012
+ LedgerLayout2["Feed"] = "feed";
2013
+ return LedgerLayout2;
2014
+ })(LedgerLayout || {});
2015
+ var GAP = 12;
2016
+ var HEADING_FONT = 15;
2017
+ var HEADING_WEIGHT = "700";
2018
+ var styles3 = reactNative.StyleSheet.create({
2019
+ root: { gap: GAP },
2020
+ heading: { fontSize: HEADING_FONT, fontWeight: HEADING_WEIGHT },
2021
+ note: { paddingVertical: GAP }
2022
+ });
2023
+ function LedgerSection({ title, rows, columns, emptyLabel, testID, ledger }) {
2024
+ const { theme } = uiFeedback.useUi();
2025
+ return /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { style: styles3.root, testID, children: [
2026
+ /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { accessibilityRole: "header", style: [styles3.heading, { color: theme.colors.text }], children: `${title} \xB7 ${rows.length}` }),
2027
+ rows.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style: [styles3.note, { color: theme.colors.textSecondary }], children: emptyLabel }) : /* @__PURE__ */ jsxRuntime.jsx(
2028
+ DataTable,
2029
+ {
2030
+ columns,
2031
+ getRowAccessibilityLabel: ledger.getRowAccessibilityLabel,
2032
+ keyExtractor: ledger.keyExtractor,
2033
+ onRowPress: ledger.onRowPress,
2034
+ rows,
2035
+ stackBreakpoint: ledger.breakpoint ?? CARD_STACK_BREAKPOINT
2036
+ }
2037
+ )
2038
+ ] });
2039
+ }
2040
+ function StatusLedger(props) {
2041
+ const { labels, layout = "auto" /* Auto */, breakpoint = CARD_STACK_BREAKPOINT, loading, testID = STATUS_LEDGER_TEST_ID } = props;
2042
+ const { width } = reactNative.useWindowDimensions();
2043
+ const view = useLedgerView(props);
2044
+ const feed = layout === "feed" /* Feed */ || layout === "auto" /* Auto */ && width < breakpoint;
2045
+ const ascending = view.direction === "asc" /* Asc */;
2046
+ const directionButton = /* @__PURE__ */ jsxRuntime.jsx(
2047
+ ActionButton,
2048
+ {
2049
+ hint: labels.sortDirectionHint,
2050
+ label: ascending ? labels.sortAscending : labels.sortDescending,
2051
+ onPress: view.toggleDirection,
2052
+ testID: ledgerSortDirectionTestID(testID),
2053
+ tone: "ghost"
2054
+ }
2055
+ );
2056
+ return /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { style: styles3.root, testID, children: [
2057
+ /* @__PURE__ */ jsxRuntime.jsx(
2058
+ Filters,
2059
+ {
2060
+ actions: directionButton,
2061
+ clearHint: labels.clearHint,
2062
+ clearLabel: labels.clear,
2063
+ fields: view.fields,
2064
+ onChange: view.setValues,
2065
+ onClear: view.clear,
2066
+ resultsCount: view.visible.length,
2067
+ resultsLabel: labels.results,
2068
+ testID: `${testID}-filters`,
2069
+ values: view.values
2070
+ }
2071
+ ),
2072
+ feed && !loading ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
2073
+ /* @__PURE__ */ jsxRuntime.jsx(LedgerSection, { columns: view.tableColumns, emptyLabel: labels.allClear, ledger: props, rows: view.split.attention, testID: ledgerSectionTestID(testID, "attention"), title: labels.needsLook }),
2074
+ /* @__PURE__ */ jsxRuntime.jsx(LedgerSection, { columns: view.tableColumns, emptyLabel: labels.empty, ledger: props, rows: view.split.rest, testID: ledgerSectionTestID(testID, "rest"), title: labels.everythingElse })
2075
+ ] }) : /* @__PURE__ */ jsxRuntime.jsx(
2076
+ DataTable,
2077
+ {
2078
+ columns: view.tableColumns,
2079
+ emptyLabel: labels.empty,
2080
+ getRowAccessibilityLabel: props.getRowAccessibilityLabel,
2081
+ keyExtractor: props.keyExtractor,
2082
+ loading,
2083
+ onRowPress: props.onRowPress,
2084
+ rows: view.visible,
2085
+ stackBreakpoint: feed ? breakpoint : 0,
2086
+ testID: `${testID}-table`
2087
+ }
2088
+ )
2089
+ ] });
2090
+ }
2091
+
2092
+ // src/StatusLedger/utils/relativeTime.ts
2093
+ var MS_PER_SECOND = 1e3;
2094
+ var SECONDS_PER_MINUTE = 60;
2095
+ var MINUTES_PER_HOUR = 60;
2096
+ var HOURS_PER_DAY = 24;
2097
+ var MS_PER_MINUTE = MS_PER_SECOND * SECONDS_PER_MINUTE;
2098
+ var MS_PER_HOUR = MS_PER_MINUTE * MINUTES_PER_HOUR;
2099
+ var MS_PER_DAY = MS_PER_HOUR * HOURS_PER_DAY;
2100
+ var COUNT_TOKEN = /\{\{n\}\}/g;
2101
+ function toEpochMs(value) {
2102
+ if (value === null || value === void 0 || value === "") return null;
2103
+ const ms = value instanceof Date ? value.getTime() : typeof value === "number" ? value : Date.parse(value);
2104
+ return Number.isFinite(ms) ? ms : null;
2105
+ }
2106
+ var fill = (template, count) => template.replace(COUNT_TOKEN, String(count));
2107
+ function formatRelativeTime(value, nowMs, strings) {
2108
+ const ms = toEpochMs(value);
2109
+ if (ms === null) return strings.never;
2110
+ const delta = nowMs - ms;
2111
+ const units = delta < 0 && strings.future !== void 0 ? strings.future : strings;
2112
+ const distance = Math.abs(delta);
2113
+ if (distance < MS_PER_MINUTE) return strings.justNow;
2114
+ if (distance < MS_PER_HOUR) return fill(units.minutes, Math.floor(distance / MS_PER_MINUTE));
2115
+ if (distance < MS_PER_DAY) return fill(units.hours, Math.floor(distance / MS_PER_HOUR));
2116
+ return fill(units.days, Math.floor(distance / MS_PER_DAY));
2117
+ }
2118
+ var styles4 = reactNative.StyleSheet.create({
2119
+ target: { minHeight: MIN_TARGET_PX, minWidth: MIN_TARGET_PX, justifyContent: "center" }
2120
+ });
2121
+ var defaultAbsolute = (ms) => new Date(ms).toLocaleString();
2122
+ function RelativeTime({ value, nowMs, strings, hint, formatAbsolute = defaultAbsolute, testID }) {
2123
+ const { theme } = uiFeedback.useUi();
2124
+ const [showAbsolute, setShowAbsolute] = React2.useState(false);
2125
+ const ms = toEpochMs(value);
2126
+ const relative = formatRelativeTime(value, nowMs, strings);
2127
+ const absolute = ms === null ? relative : formatAbsolute(ms);
2128
+ return /* @__PURE__ */ jsxRuntime.jsx(
2129
+ reactNative.Pressable,
2130
+ {
2131
+ accessibilityHint: hint,
2132
+ accessibilityLabel: absolute,
2133
+ accessibilityRole: "button",
2134
+ disabled: ms === null,
2135
+ onPress: () => setShowAbsolute((current) => !current),
2136
+ style: styles4.target,
2137
+ testID,
2138
+ children: /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style: { color: theme.colors.text }, children: showAbsolute ? absolute : relative })
2139
+ }
2140
+ );
2141
+ }
2142
+
1838
2143
  Object.defineProperty(exports, "DEFAULT_TYPEAHEAD_MAX_SUGGESTIONS", {
1839
2144
  enumerable: true,
1840
2145
  get: function () { return uiForms.DEFAULT_TYPEAHEAD_MAX_SUGGESTIONS; }
@@ -1860,18 +2165,34 @@ exports.FILTERS_I18N = FILTERS_I18N;
1860
2165
  exports.FILTERS_TEST_ID = FILTERS_TEST_ID;
1861
2166
  exports.FilterBar = FilterBar;
1862
2167
  exports.Filters = Filters;
2168
+ exports.LedgerLayout = LedgerLayout;
1863
2169
  exports.Pager = Pager;
2170
+ exports.RelativeTime = RelativeTime;
2171
+ exports.STATUS_LEDGER_TEST_ID = STATUS_LEDGER_TEST_ID;
2172
+ exports.SortDirection = SortDirection;
1864
2173
  exports.StatCard = StatCard;
2174
+ exports.StatusLedger = StatusLedger;
2175
+ exports.StatusPill = StatusPill;
2176
+ exports.StatusTone = StatusTone;
1865
2177
  exports.TABLE_I18N = TABLE_I18N;
1866
2178
  exports.TABLE_TEST_IDS = TABLE_TEST_IDS;
1867
2179
  exports.applyTestID = applyTestID;
1868
2180
  exports.clearTestID = clearTestID;
2181
+ exports.distinctValues = distinctValues;
1869
2182
  exports.fieldTestID = fieldTestID;
2183
+ exports.filterLedgerRows = filterLedgerRows;
2184
+ exports.formatRelativeTime = formatRelativeTime;
2185
+ exports.isAttentionTone = isAttentionTone;
2186
+ exports.ledgerSectionTestID = ledgerSectionTestID;
2187
+ exports.ledgerSortDirectionTestID = ledgerSortDirectionTestID;
1870
2188
  exports.rowDetailTestID = rowDetailTestID;
1871
2189
  exports.rowFadeTestID = rowFadeTestID;
1872
2190
  exports.rowSelectTestID = rowSelectTestID;
1873
2191
  exports.rowTestID = rowTestID;
1874
2192
  exports.selectAllTestID = selectAllTestID;
2193
+ exports.sortLedgerRows = sortLedgerRows;
2194
+ exports.splitNeedsLook = splitNeedsLook;
2195
+ exports.toEpochMs = toEpochMs;
1875
2196
  exports.useFilterDraft = useFilterDraft;
1876
2197
  exports.usePagedRows = usePagedRows;
1877
2198
  //# sourceMappingURL=index.js.map