@terreno/ui 57.21.0 → 58.0.0-beta.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/.ai/guidelines/core.md +1 -1
- package/CHANGELOG.md +43 -0
- package/dist/ActionSheet.d.ts +9 -9
- package/dist/ActionSheet.d.ts.map +1 -1
- package/dist/ActionSheet.js +11 -4
- package/dist/ActionSheet.js.map +1 -1
- package/dist/AnnouncementNavigator.d.ts +7 -1
- package/dist/AnnouncementNavigator.d.ts.map +1 -1
- package/dist/AnnouncementNavigator.js +15 -16
- package/dist/AnnouncementNavigator.js.map +1 -1
- package/dist/Avatar.d.ts.map +1 -1
- package/dist/Avatar.js +1 -1
- package/dist/Avatar.js.map +1 -1
- package/dist/Box.d.ts.map +1 -1
- package/dist/Box.js +3 -1
- package/dist/Box.js.map +1 -1
- package/dist/Common.d.ts +16 -12
- package/dist/Common.d.ts.map +1 -1
- package/dist/Common.js.map +1 -1
- package/dist/ConsentFormScreen.d.ts.map +1 -1
- package/dist/ConsentFormScreen.js.map +1 -1
- package/dist/ConsentNavigator.d.ts +5 -3
- package/dist/ConsentNavigator.d.ts.map +1 -1
- package/dist/ConsentNavigator.js +4 -4
- package/dist/ConsentNavigator.js.map +1 -1
- package/dist/CustomSelectField.js.map +1 -1
- package/dist/DataTable.d.ts.map +1 -1
- package/dist/DataTable.js +37 -12
- package/dist/DataTable.js.map +1 -1
- package/dist/DateTimeField.js.map +1 -1
- package/dist/DropdownPanel.d.ts +0 -5
- package/dist/DropdownPanel.d.ts.map +1 -1
- package/dist/DropdownPanel.js +0 -5
- package/dist/DropdownPanel.js.map +1 -1
- package/dist/FilterAccordion.d.ts.map +1 -1
- package/dist/FilterAccordion.js.map +1 -1
- package/dist/FilterBoolean.d.ts.map +1 -1
- package/dist/FilterBoolean.js.map +1 -1
- package/dist/FilterSelectMenu.js +1 -1
- package/dist/FilterSelectMenu.js.map +1 -1
- package/dist/GPTChat.d.ts.map +1 -1
- package/dist/GPTChat.js.map +1 -1
- package/dist/Heading.d.ts.map +1 -1
- package/dist/Heading.js +6 -12
- package/dist/Heading.js.map +1 -1
- package/dist/HeightField.d.ts.map +1 -1
- package/dist/HeightField.js +1 -1
- package/dist/HeightField.js.map +1 -1
- package/dist/Hyperlink.d.ts.map +1 -1
- package/dist/Hyperlink.js +6 -1
- package/dist/Hyperlink.js.map +1 -1
- package/dist/Image.d.ts +1 -1
- package/dist/MarkdownEditorField.d.ts.map +1 -1
- package/dist/MarkdownEditorField.js +1 -1
- package/dist/MarkdownEditorField.js.map +1 -1
- package/dist/OfflineBanner.d.ts +3 -2
- package/dist/OfflineBanner.d.ts.map +1 -1
- package/dist/OfflineBanner.js +3 -2
- package/dist/OfflineBanner.js.map +1 -1
- package/dist/OtpField.d.ts.map +1 -1
- package/dist/OtpField.js.map +1 -1
- package/dist/PickerSelect.d.ts +4 -4
- package/dist/PickerSelect.d.ts.map +1 -1
- package/dist/PickerSelect.js.map +1 -1
- package/dist/ProvisionalConsentSigner.d.ts +3 -2
- package/dist/ProvisionalConsentSigner.d.ts.map +1 -1
- package/dist/ProvisionalConsentSigner.js +2 -2
- package/dist/ProvisionalConsentSigner.js.map +1 -1
- package/dist/SidebarNavigation.js +2 -2
- package/dist/SidebarNavigation.js.map +1 -1
- package/dist/SidebarNavigation.native.js +2 -2
- package/dist/SidebarNavigation.native.js.map +1 -1
- package/dist/Skeleton.js.map +1 -1
- package/dist/Text.d.ts.map +1 -1
- package/dist/Text.js +27 -50
- package/dist/Text.js.map +1 -1
- package/dist/TextField.d.ts.map +1 -1
- package/dist/TextField.js.map +1 -1
- package/dist/ThumbsUpDownFeedback.d.ts.map +1 -1
- package/dist/ThumbsUpDownFeedback.js.map +1 -1
- package/dist/ToastNotifications.d.ts.map +1 -1
- package/dist/ToastNotifications.js.map +1 -1
- package/dist/Tooltip.d.ts.map +1 -1
- package/dist/Tooltip.js.map +1 -1
- package/dist/Unifier.d.ts +1 -1
- package/dist/WebAddressAutocomplete.js.map +1 -1
- package/dist/WebDropdownMenu.d.ts +3 -3
- package/dist/WebDropdownMenu.d.ts.map +1 -1
- package/dist/WebDropdownMenu.js +1 -1
- package/dist/WebDropdownMenu.js.map +1 -1
- package/dist/announcementPlatform.d.ts +5 -0
- package/dist/announcementPlatform.d.ts.map +1 -1
- package/dist/announcementPlatform.js +9 -0
- package/dist/announcementPlatform.js.map +1 -1
- package/dist/asks/AskCard.d.ts +2 -2
- package/dist/asks/AskCard.d.ts.map +1 -1
- package/dist/dataTableColumnWidths.d.ts +76 -0
- package/dist/dataTableColumnWidths.d.ts.map +1 -0
- package/dist/dataTableColumnWidths.js +181 -0
- package/dist/dataTableColumnWidths.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/login/LoginScreen.js.map +1 -1
- package/dist/requestClient.d.ts +71 -0
- package/dist/requestClient.d.ts.map +1 -0
- package/dist/requestClient.js +139 -0
- package/dist/requestClient.js.map +1 -0
- package/dist/security/PasswordCard.d.ts.map +1 -1
- package/dist/security/PasswordCard.js.map +1 -1
- package/dist/security/TwoFactorSettingsCard.d.ts.map +1 -1
- package/dist/security/TwoFactorSettingsCard.js +1 -1
- package/dist/security/TwoFactorSettingsCard.js.map +1 -1
- package/dist/signUp/SignUpScreen.js.map +1 -1
- package/dist/table/Table.d.ts.map +1 -1
- package/dist/table/Table.js.map +1 -1
- package/dist/table/TableTitle.d.ts.map +1 -1
- package/dist/table/TableTitle.js +2 -1
- package/dist/table/TableTitle.js.map +1 -1
- package/dist/textFontSizes.d.ts +12 -0
- package/dist/textFontSizes.d.ts.map +1 -0
- package/dist/textFontSizes.js +21 -0
- package/dist/textFontSizes.js.map +1 -0
- package/dist/useAcknowledgeAnnouncement.d.ts +8 -63
- package/dist/useAcknowledgeAnnouncement.d.ts.map +1 -1
- package/dist/useAcknowledgeAnnouncement.js +31 -80
- package/dist/useAcknowledgeAnnouncement.js.map +1 -1
- package/dist/useAnnouncements.d.ts +14 -47
- package/dist/useAnnouncements.d.ts.map +1 -1
- package/dist/useAnnouncements.js +30 -57
- package/dist/useAnnouncements.js.map +1 -1
- package/dist/useConsentForms.d.ts +6 -38
- package/dist/useConsentForms.d.ts.map +1 -1
- package/dist/useConsentForms.js +25 -48
- package/dist/useConsentForms.js.map +1 -1
- package/dist/useProvisionalConsent.d.ts +7 -58
- package/dist/useProvisionalConsent.d.ts.map +1 -1
- package/dist/useProvisionalConsent.js +57 -67
- package/dist/useProvisionalConsent.js.map +1 -1
- package/dist/useSubmitConsent.d.ts +9 -39
- package/dist/useSubmitConsent.d.ts.map +1 -1
- package/dist/useSubmitConsent.js +10 -39
- package/dist/useSubmitConsent.js.map +1 -1
- package/package.json +38 -38
- package/src/ActionSheet.test.tsx +4 -4
- package/src/ActionSheet.tsx +18 -5
- package/src/AnnouncementNavigator.test.tsx +215 -315
- package/src/AnnouncementNavigator.tsx +25 -20
- package/src/Avatar.tsx +2 -9
- package/src/Box.test.tsx +3 -3
- package/src/Box.tsx +4 -2
- package/src/Common.ts +19 -15
- package/src/ConsentFormScreen.tsx +2 -1
- package/src/ConsentNavigator.test.tsx +166 -242
- package/src/ConsentNavigator.tsx +9 -13
- package/src/CustomSelectField.tsx +3 -3
- package/src/DataTable.test.tsx +69 -1
- package/src/DataTable.tsx +72 -19
- package/src/DateTimeField.tsx +4 -4
- package/src/DropdownPanel.tsx +0 -6
- package/src/FilterAccordion.tsx +3 -3
- package/src/FilterBoolean.tsx +3 -3
- package/src/FilterSelectMenu.tsx +1 -1
- package/src/GPTChat.tsx +10 -9
- package/src/Heading.tsx +7 -14
- package/src/HeightField.tsx +9 -2
- package/src/Hyperlink.tsx +6 -3
- package/src/Link.test.tsx +2 -2
- package/src/MarkdownEditorField.tsx +10 -2
- package/src/MarkdownView.test.tsx +10 -6
- package/src/Modal.test.tsx +3 -1
- package/src/ModalSheet.test.tsx +6 -6
- package/src/OfflineBanner.tsx +3 -2
- package/src/OtpField.tsx +2 -1
- package/src/PickerSelect.tsx +3 -2
- package/src/ProvisionalConsentSigner.test.tsx +140 -113
- package/src/ProvisionalConsentSigner.tsx +5 -4
- package/src/SidebarNavigation.native.tsx +2 -2
- package/src/SidebarNavigation.tsx +2 -2
- package/src/Skeleton.tsx +1 -1
- package/src/TapToEdit.test.tsx +2 -2
- package/src/Text.test.tsx +1 -1
- package/src/Text.tsx +25 -51
- package/src/TextField.tsx +2 -1
- package/src/ThumbsUpDownFeedback.tsx +3 -3
- package/src/ToastNotifications.tsx +3 -2
- package/src/ToastNotifications.webModal.test.tsx +4 -1
- package/src/Tooltip.tsx +2 -1
- package/src/UserInactivity.test.tsx +2 -3
- package/src/WebAddressAutocomplete.tsx +3 -3
- package/src/WebDropdownMenu.tsx +16 -5
- package/src/__snapshots__/DataTable.test.tsx.snap +13 -0
- package/src/announcementPlatform.ts +10 -0
- package/src/asks/AskCard.test.tsx +14 -12
- package/src/asks/AskCard.tsx +2 -2
- package/src/bunSetup.ts +42 -0
- package/src/dataTableColumnWidths.test.ts +266 -0
- package/src/dataTableColumnWidths.ts +272 -0
- package/src/index.tsx +9 -2
- package/src/login/LoginScreen.tsx +2 -2
- package/src/requestClient.test.ts +288 -0
- package/src/requestClient.ts +228 -0
- package/src/security/PasswordCard.tsx +7 -5
- package/src/security/TwoFactorSettingsCard.tsx +10 -3
- package/src/signUp/SignUpScreen.tsx +2 -2
- package/src/table/Table.tsx +1 -2
- package/src/table/TableTitle.tsx +2 -1
- package/src/test-utils.tsx +76 -0
- package/src/textFontSizes.ts +31 -0
- package/src/useAcknowledgeAnnouncement.test.ts +101 -193
- package/src/useAcknowledgeAnnouncement.ts +69 -147
- package/src/useAnnouncements.test.ts +122 -186
- package/src/useAnnouncements.ts +49 -105
- package/src/useConsentForms.test.ts +59 -112
- package/src/useConsentForms.ts +37 -87
- package/src/useProvisionalConsent.test.ts +74 -150
- package/src/useProvisionalConsent.ts +79 -125
- package/src/useSubmitConsent.test.ts +59 -85
- package/src/useSubmitConsent.ts +22 -77
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import {describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {Platform} from "react-native";
|
|
3
|
+
|
|
4
|
+
import type {DataTableCellData, DataTableColumn} from "./Common";
|
|
5
|
+
import {
|
|
6
|
+
createDataTableTextMeasurer,
|
|
7
|
+
DATA_TABLE_AUTO_CUSTOM_CONTENT_WIDTH,
|
|
8
|
+
DATA_TABLE_AUTO_MAX_COLUMN_WIDTH,
|
|
9
|
+
DATA_TABLE_AUTO_MIN_COLUMN_WIDTH,
|
|
10
|
+
DATA_TABLE_AUTO_WIDTH_SAMPLE_ROWS,
|
|
11
|
+
DATA_TABLE_CELL_HORIZONTAL_PADDING,
|
|
12
|
+
estimateDataTableTextWidth,
|
|
13
|
+
fitDataTableColumnWidths,
|
|
14
|
+
getDataTableNaturalWidths,
|
|
15
|
+
type MeasureDataTableText,
|
|
16
|
+
} from "./dataTableColumnWidths";
|
|
17
|
+
|
|
18
|
+
// One pixel per character keeps expected widths easy to read.
|
|
19
|
+
const measureText: MeasureDataTableText = ({text}) => text.length;
|
|
20
|
+
const formatDate = (value: unknown): string => `date:${String(value)}`;
|
|
21
|
+
const PAD = DATA_TABLE_CELL_HORIZONTAL_PADDING;
|
|
22
|
+
|
|
23
|
+
const naturalWidths = (
|
|
24
|
+
columns: DataTableColumn[],
|
|
25
|
+
data: DataTableCellData[][],
|
|
26
|
+
extra: Partial<Parameters<typeof getDataTableNaturalWidths>[0]> = {}
|
|
27
|
+
): number[] =>
|
|
28
|
+
getDataTableNaturalWidths({
|
|
29
|
+
columns,
|
|
30
|
+
data,
|
|
31
|
+
defaultTextSize: "md",
|
|
32
|
+
formatDate,
|
|
33
|
+
measureText,
|
|
34
|
+
...extra,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe("getDataTableNaturalWidths", () => {
|
|
38
|
+
it("keeps an explicit width", () => {
|
|
39
|
+
const widths = naturalWidths(
|
|
40
|
+
[{columnType: "text", title: "Name", width: 222}],
|
|
41
|
+
[[{value: "x".repeat(500)}]]
|
|
42
|
+
);
|
|
43
|
+
expect(widths).toEqual([222]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("sizes to the content when it is wider than the title", () => {
|
|
47
|
+
const content = "c".repeat(120);
|
|
48
|
+
const widths = naturalWidths([{columnType: "text", title: "Name"}], [[{value: content}]]);
|
|
49
|
+
expect(widths).toEqual([120 + PAD]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("sizes to the title and its header icons when they are wider than the content", () => {
|
|
53
|
+
const title = "t".repeat(100);
|
|
54
|
+
const widths = naturalWidths(
|
|
55
|
+
[{columnType: "text", infoModalText: "Help", sortable: true, title}],
|
|
56
|
+
[[{value: "a"}]]
|
|
57
|
+
);
|
|
58
|
+
// Sort icon (24) and info icon (26) sit next to the title.
|
|
59
|
+
expect(widths).toEqual([100 + 24 + 26 + PAD]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("clamps to the minimum and maximum auto widths", () => {
|
|
63
|
+
const widths = naturalWidths(
|
|
64
|
+
[
|
|
65
|
+
{columnType: "text", title: "A"},
|
|
66
|
+
{columnType: "text", title: "B"},
|
|
67
|
+
],
|
|
68
|
+
[[{value: "a"}, {value: "b".repeat(2000)}]]
|
|
69
|
+
);
|
|
70
|
+
expect(widths).toEqual([DATA_TABLE_AUTO_MIN_COLUMN_WIDTH, DATA_TABLE_AUTO_MAX_COLUMN_WIDTH]);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("measures dates as they are formatted", () => {
|
|
74
|
+
const widths = naturalWidths(
|
|
75
|
+
[{columnType: "date", title: "D"}],
|
|
76
|
+
[[{value: "2026-01-01T00:00:00.000Z".repeat(4)}]]
|
|
77
|
+
);
|
|
78
|
+
expect(widths).toEqual([formatDate("2026-01-01T00:00:00.000Z".repeat(4)).length + PAD]);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("gives a custom column a default content width when its value is not text", () => {
|
|
82
|
+
const widths = naturalWidths([{columnType: "pill", title: "P"}], [[{value: {a: 1}}]], {
|
|
83
|
+
customColumnTypes: new Set(["pill"]),
|
|
84
|
+
});
|
|
85
|
+
expect(widths).toEqual([DATA_TABLE_AUTO_CUSTOM_CONTENT_WIDTH + PAD]);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("samples at most a fixed number of rows, spread across the data", () => {
|
|
89
|
+
const spy = mock(measureText);
|
|
90
|
+
const data = Array.from({length: 10_000}, (_, index) => [{value: `row ${index}`}]);
|
|
91
|
+
naturalWidths([{columnType: "text", title: "Name"}], data, {measureText: spy});
|
|
92
|
+
// One call for the header, one per sampled row.
|
|
93
|
+
expect(spy).toHaveBeenCalledTimes(DATA_TABLE_AUTO_WIDTH_SAMPLE_ROWS + 1);
|
|
94
|
+
const measuredRows = spy.mock.calls.slice(1).map(([args]) => args.text);
|
|
95
|
+
// 10,000 rows / 50 samples = every 200th row, so the last sample is row 9,800.
|
|
96
|
+
expect(measuredRows[0]).toBe("row 0");
|
|
97
|
+
expect(measuredRows.at(-1)).toBe("row 9800");
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe("getDataTableNaturalWidths header and cell extras", () => {
|
|
102
|
+
it("makes room for the filter button only on web when filters are wired up", () => {
|
|
103
|
+
const column: DataTableColumn = {
|
|
104
|
+
columnType: "text",
|
|
105
|
+
filter: {field: "status", kind: "text"},
|
|
106
|
+
title: "t".repeat(100),
|
|
107
|
+
};
|
|
108
|
+
const originalOS = Platform.OS;
|
|
109
|
+
try {
|
|
110
|
+
Platform.OS = "web";
|
|
111
|
+
expect(naturalWidths([column], [[{value: "a"}]], {hasFilters: true})).toEqual([
|
|
112
|
+
100 + 40 + PAD,
|
|
113
|
+
]);
|
|
114
|
+
expect(naturalWidths([column], [[{value: "a"}]], {hasFilters: false})).toEqual([100 + PAD]);
|
|
115
|
+
Platform.OS = "ios";
|
|
116
|
+
expect(naturalWidths([column], [[{value: "a"}]], {hasFilters: true})).toEqual([100 + PAD]);
|
|
117
|
+
} finally {
|
|
118
|
+
Platform.OS = originalOS;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("sizes a boolean column to its icon rather than its value", () => {
|
|
123
|
+
const widths = naturalWidths(
|
|
124
|
+
[{columnType: "boolean", title: "B"}],
|
|
125
|
+
[[{value: "a very long value that is never drawn as text"}]]
|
|
126
|
+
);
|
|
127
|
+
expect(widths).toEqual([DATA_TABLE_AUTO_MIN_COLUMN_WIDTH]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("skips missing cells and non-text values in plain columns", () => {
|
|
131
|
+
const widths = naturalWidths([{columnType: "text", title: "T"}], [[], [{value: {a: 1}}]]);
|
|
132
|
+
expect(widths).toEqual([DATA_TABLE_AUTO_MIN_COLUMN_WIDTH]);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
interface FakeCanvas {
|
|
137
|
+
fonts: string[];
|
|
138
|
+
texts: string[];
|
|
139
|
+
getContext: (kind: string) => unknown;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const fakeCanvas = (width: number): FakeCanvas => {
|
|
143
|
+
const canvas: FakeCanvas = {
|
|
144
|
+
fonts: [],
|
|
145
|
+
getContext: () => ({
|
|
146
|
+
set font(value: string) {
|
|
147
|
+
canvas.fonts.push(value);
|
|
148
|
+
},
|
|
149
|
+
measureText: (text: string) => {
|
|
150
|
+
canvas.texts.push(text);
|
|
151
|
+
return {width};
|
|
152
|
+
},
|
|
153
|
+
}),
|
|
154
|
+
texts: [],
|
|
155
|
+
};
|
|
156
|
+
return canvas;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
describe("createDataTableTextMeasurer", () => {
|
|
160
|
+
const header = {fontSize: 10, isHeader: true, text: "Name"};
|
|
161
|
+
const cell = {fontSize: 16, isHeader: false, text: "Ada Lovelace"};
|
|
162
|
+
|
|
163
|
+
it("estimates when not on web", () => {
|
|
164
|
+
const getDocument = mock(() => ({createElement: () => fakeCanvas(99)}));
|
|
165
|
+
const measure = createDataTableTextMeasurer({getDocument, isWeb: () => false});
|
|
166
|
+
expect(measure(cell)).toBe(estimateDataTableTextWidth(cell));
|
|
167
|
+
expect(getDocument).not.toHaveBeenCalled();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("measures with one cached canvas on web, using the header and cell fonts", () => {
|
|
171
|
+
const canvas = fakeCanvas(41.2);
|
|
172
|
+
const createElement = mock(() => canvas);
|
|
173
|
+
const measure = createDataTableTextMeasurer({
|
|
174
|
+
getDocument: () => ({createElement}),
|
|
175
|
+
isWeb: () => true,
|
|
176
|
+
});
|
|
177
|
+
expect(measure(header)).toBe(42);
|
|
178
|
+
expect(measure(cell)).toBe(42);
|
|
179
|
+
expect(createElement).toHaveBeenCalledTimes(1);
|
|
180
|
+
expect(canvas.fonts).toEqual([
|
|
181
|
+
"700 10px text-bold, Nunito, sans-serif",
|
|
182
|
+
"400 16px text-regular, Nunito, sans-serif",
|
|
183
|
+
]);
|
|
184
|
+
// Headers render uppercase, so they are measured that way.
|
|
185
|
+
expect(canvas.texts).toEqual(["NAME", "Ada Lovelace"]);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("falls back to the estimate when the canvas is a stub that measures 0", () => {
|
|
189
|
+
const measure = createDataTableTextMeasurer({
|
|
190
|
+
getDocument: () => ({createElement: () => fakeCanvas(0)}),
|
|
191
|
+
isWeb: () => true,
|
|
192
|
+
});
|
|
193
|
+
expect(measure(cell)).toBe(estimateDataTableTextWidth(cell));
|
|
194
|
+
expect(measure({...cell, text: ""})).toBe(0);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("estimates when there is no document, no canvas context, or canvas creation throws", () => {
|
|
198
|
+
const noDocument = createDataTableTextMeasurer({
|
|
199
|
+
getDocument: () => undefined,
|
|
200
|
+
isWeb: () => true,
|
|
201
|
+
});
|
|
202
|
+
const noContext = createDataTableTextMeasurer({
|
|
203
|
+
getDocument: () => ({createElement: () => ({})}),
|
|
204
|
+
isWeb: () => true,
|
|
205
|
+
});
|
|
206
|
+
const getDocument = mock(() => ({
|
|
207
|
+
createElement: (): unknown => {
|
|
208
|
+
throw new Error("no canvas");
|
|
209
|
+
},
|
|
210
|
+
}));
|
|
211
|
+
const throwing = createDataTableTextMeasurer({getDocument, isWeb: () => true});
|
|
212
|
+
for (const measure of [noDocument, noContext, throwing]) {
|
|
213
|
+
expect(measure(cell)).toBe(estimateDataTableTextWidth(cell));
|
|
214
|
+
}
|
|
215
|
+
// A failed lookup is cached rather than retried on every cell.
|
|
216
|
+
throwing(cell);
|
|
217
|
+
expect(getDocument).toHaveBeenCalledTimes(1);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("defaults to the global document and Platform.OS", () => {
|
|
221
|
+
const measure = createDataTableTextMeasurer();
|
|
222
|
+
expect(measure(cell)).toBeGreaterThan(0);
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describe("fitDataTableColumnWidths", () => {
|
|
227
|
+
it("keeps natural widths before the container is measured", () => {
|
|
228
|
+
const columns: DataTableColumn[] = [{columnType: "text", title: "A"}];
|
|
229
|
+
expect(fitDataTableColumnWidths({availableWidth: 0, columns, naturalWidths: [100]})).toEqual([
|
|
230
|
+
100,
|
|
231
|
+
]);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("grows auto columns in proportion to fill the container exactly", () => {
|
|
235
|
+
const columns: DataTableColumn[] = [
|
|
236
|
+
{columnType: "text", title: "A"},
|
|
237
|
+
{columnType: "text", title: "B", width: 100},
|
|
238
|
+
{columnType: "text", title: "C"},
|
|
239
|
+
];
|
|
240
|
+
const widths = fitDataTableColumnWidths({
|
|
241
|
+
availableWidth: 1001,
|
|
242
|
+
columns,
|
|
243
|
+
naturalWidths: [100, 100, 300],
|
|
244
|
+
});
|
|
245
|
+
expect(widths[1]).toBe(100);
|
|
246
|
+
expect(widths[0]).toBe(100 + Math.floor((501 * 100) / 400));
|
|
247
|
+
expect(widths.reduce((sum, width) => sum + width, 0)).toBe(1001);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it("keeps natural widths when the columns overflow the container", () => {
|
|
251
|
+
const columns: DataTableColumn[] = [
|
|
252
|
+
{columnType: "text", title: "A"},
|
|
253
|
+
{columnType: "text", title: "B"},
|
|
254
|
+
];
|
|
255
|
+
expect(
|
|
256
|
+
fitDataTableColumnWidths({availableWidth: 300, columns, naturalWidths: [200, 200]})
|
|
257
|
+
).toEqual([200, 200]);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("never changes explicit widths, even with room to spare", () => {
|
|
261
|
+
const columns: DataTableColumn[] = [{columnType: "text", title: "A", width: 120}];
|
|
262
|
+
expect(fitDataTableColumnWidths({availableWidth: 900, columns, naturalWidths: [120]})).toEqual([
|
|
263
|
+
120,
|
|
264
|
+
]);
|
|
265
|
+
});
|
|
266
|
+
});
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import {Platform} from "react-native";
|
|
2
|
+
|
|
3
|
+
import type {DataTableCellData, DataTableColumn} from "./Common";
|
|
4
|
+
import {textFontSizes} from "./textFontSizes";
|
|
5
|
+
|
|
6
|
+
// Layout values DataTable renders with. DataTable.tsx and TableTitle import these, so the width
|
|
7
|
+
// estimate below and the rendered table cannot drift apart.
|
|
8
|
+
|
|
9
|
+
/** Padding on every side of each header and body cell. */
|
|
10
|
+
export const DATA_TABLE_CELL_PADDING = 16;
|
|
11
|
+
/** Horizontal padding on each header and body cell (both sides). */
|
|
12
|
+
export const DATA_TABLE_CELL_HORIZONTAL_PADDING = DATA_TABLE_CELL_PADDING * 2;
|
|
13
|
+
/** Font size of the uppercase, bold column title in the header. */
|
|
14
|
+
export const DATA_TABLE_HEADER_FONT_SIZE = 10;
|
|
15
|
+
/** Diameter of the round sort button in a sortable column's header. */
|
|
16
|
+
export const DATA_TABLE_SORT_BUTTON_SIZE = 16;
|
|
17
|
+
/** Gap between the header title and the sort button. */
|
|
18
|
+
export const DATA_TABLE_SORT_BUTTON_MARGIN = 8;
|
|
19
|
+
/** Width of the fixed "more" column drawn when `moreContentComponent` is set. */
|
|
20
|
+
export const DATA_TABLE_MORE_COLUMN_WIDTH = 48;
|
|
21
|
+
/** Border around the table body. */
|
|
22
|
+
export const DATA_TABLE_BORDER_WIDTH = 1;
|
|
23
|
+
|
|
24
|
+
/** Narrowest an auto-sized column starts at, before any fill is added. */
|
|
25
|
+
export const DATA_TABLE_AUTO_MIN_COLUMN_WIDTH = 80;
|
|
26
|
+
/** Widest an auto-sized column starts at, before any fill is added. Longer text wraps or clips. */
|
|
27
|
+
export const DATA_TABLE_AUTO_MAX_COLUMN_WIDTH = 360;
|
|
28
|
+
/** Rows read per column when estimating content width. Spread evenly across the data. */
|
|
29
|
+
export const DATA_TABLE_AUTO_WIDTH_SAMPLE_ROWS = 50;
|
|
30
|
+
/** Content width assumed for a custom cell whose value is not a string or number. */
|
|
31
|
+
export const DATA_TABLE_AUTO_CUSTOM_CONTENT_WIDTH = 120;
|
|
32
|
+
|
|
33
|
+
const HEADER_SORT_ICON_WIDTH = DATA_TABLE_SORT_BUTTON_SIZE + DATA_TABLE_SORT_BUTTON_MARGIN;
|
|
34
|
+
// InfoModalIcon and DataTableColumnFilterWeb size themselves; these are their rendered widths.
|
|
35
|
+
const HEADER_INFO_ICON_WIDTH = 26;
|
|
36
|
+
const HEADER_FILTER_ICON_WIDTH = 40;
|
|
37
|
+
const BOOLEAN_ICON_WIDTH = 20;
|
|
38
|
+
|
|
39
|
+
// Font families registered by TerrenoFontProvider, as Text picks them for sm/md cells and
|
|
40
|
+
// TableTitle's bold header.
|
|
41
|
+
const HEADER_FONT_FAMILY = "text-bold";
|
|
42
|
+
const CELL_FONT_FAMILY = "text-regular";
|
|
43
|
+
|
|
44
|
+
// Average glyph width as a share of font size, for Nunito. Native has no synchronous text
|
|
45
|
+
// measurement, so it estimates; web measures with a canvas.
|
|
46
|
+
const REGULAR_CHAR_WIDTH_RATIO = 0.56;
|
|
47
|
+
const BOLD_UPPERCASE_CHAR_WIDTH_RATIO = 0.72;
|
|
48
|
+
|
|
49
|
+
export type DataTableTextSize = "sm" | "md" | "lg";
|
|
50
|
+
|
|
51
|
+
export interface MeasureDataTableTextArgs {
|
|
52
|
+
text: string;
|
|
53
|
+
fontSize: number;
|
|
54
|
+
isHeader: boolean;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type MeasureDataTableText = (args: MeasureDataTableTextArgs) => number;
|
|
58
|
+
|
|
59
|
+
export const estimateDataTableTextWidth: MeasureDataTableText = ({text, fontSize, isHeader}) => {
|
|
60
|
+
const ratio = isHeader ? BOLD_UPPERCASE_CHAR_WIDTH_RATIO : REGULAR_CHAR_WIDTH_RATIO;
|
|
61
|
+
return Math.ceil(text.length * fontSize * ratio);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
interface CanvasTextContext {
|
|
65
|
+
font: string;
|
|
66
|
+
measureText: (text: string) => {width: number};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface CanvasDocument {
|
|
70
|
+
createElement?: (tag: string) => unknown;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface CreateDataTableTextMeasurerArgs {
|
|
74
|
+
/** Document to create the measuring canvas from. Defaults to the global document. */
|
|
75
|
+
getDocument?: () => CanvasDocument | undefined;
|
|
76
|
+
/** Whether to measure with a canvas. Defaults to `Platform.OS === "web"`. */
|
|
77
|
+
isWeb?: () => boolean;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const getGlobalDocument = (): CanvasDocument | undefined =>
|
|
81
|
+
(globalThis as {document?: CanvasDocument}).document;
|
|
82
|
+
|
|
83
|
+
const isWebPlatform = (): boolean => Platform.OS === "web";
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Builds a measurer that uses one shared canvas on web (no layout or reflow) and estimates
|
|
87
|
+
* everywhere else, or when no canvas is available.
|
|
88
|
+
*/
|
|
89
|
+
export const createDataTableTextMeasurer = ({
|
|
90
|
+
getDocument = getGlobalDocument,
|
|
91
|
+
isWeb = isWebPlatform,
|
|
92
|
+
}: CreateDataTableTextMeasurerArgs = {}): MeasureDataTableText => {
|
|
93
|
+
let canvasContext: CanvasTextContext | null | undefined;
|
|
94
|
+
|
|
95
|
+
const getCanvasContext = (): CanvasTextContext | null => {
|
|
96
|
+
if (canvasContext !== undefined) {
|
|
97
|
+
return canvasContext;
|
|
98
|
+
}
|
|
99
|
+
const doc = getDocument();
|
|
100
|
+
if (!doc?.createElement) {
|
|
101
|
+
canvasContext = null;
|
|
102
|
+
return canvasContext;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
const canvas = doc.createElement("canvas") as {getContext?: (kind: string) => unknown};
|
|
106
|
+
canvasContext = (canvas.getContext?.("2d") as CanvasTextContext | null) ?? null;
|
|
107
|
+
} catch {
|
|
108
|
+
canvasContext = null;
|
|
109
|
+
}
|
|
110
|
+
return canvasContext;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
return (args) => {
|
|
114
|
+
const context = isWeb() ? getCanvasContext() : null;
|
|
115
|
+
if (!context) {
|
|
116
|
+
return estimateDataTableTextWidth(args);
|
|
117
|
+
}
|
|
118
|
+
const family = args.isHeader ? HEADER_FONT_FAMILY : CELL_FONT_FAMILY;
|
|
119
|
+
const weight = args.isHeader ? 700 : 400;
|
|
120
|
+
context.font = `${weight} ${args.fontSize}px ${family}, Nunito, sans-serif`;
|
|
121
|
+
const measured = context.measureText(args.isHeader ? args.text.toUpperCase() : args.text).width;
|
|
122
|
+
// Fall back if the canvas is a stub (jsdom returns 0).
|
|
123
|
+
if (!measured && args.text.length > 0) {
|
|
124
|
+
return estimateDataTableTextWidth(args);
|
|
125
|
+
}
|
|
126
|
+
return Math.ceil(measured);
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/** Default measurer: canvas on web, estimate on native. */
|
|
131
|
+
export const measureDataTableTextWidth: MeasureDataTableText = createDataTableTextMeasurer();
|
|
132
|
+
|
|
133
|
+
const sampleRowIndexes = (rowCount: number): number[] => {
|
|
134
|
+
if (rowCount <= DATA_TABLE_AUTO_WIDTH_SAMPLE_ROWS) {
|
|
135
|
+
return Array.from({length: rowCount}, (_, index) => index);
|
|
136
|
+
}
|
|
137
|
+
const stride = rowCount / DATA_TABLE_AUTO_WIDTH_SAMPLE_ROWS;
|
|
138
|
+
return Array.from({length: DATA_TABLE_AUTO_WIDTH_SAMPLE_ROWS}, (_, index) =>
|
|
139
|
+
Math.floor(index * stride)
|
|
140
|
+
);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const cellText = (
|
|
144
|
+
column: DataTableColumn,
|
|
145
|
+
cell: DataTableCellData,
|
|
146
|
+
formatDate: (value: unknown) => string
|
|
147
|
+
): string | undefined => {
|
|
148
|
+
const {value} = cell;
|
|
149
|
+
if (column.columnType === "date") {
|
|
150
|
+
return formatDate(value);
|
|
151
|
+
}
|
|
152
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
153
|
+
return String(value);
|
|
154
|
+
}
|
|
155
|
+
return undefined;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const clampWidth = (width: number): number =>
|
|
159
|
+
Math.min(DATA_TABLE_AUTO_MAX_COLUMN_WIDTH, Math.max(DATA_TABLE_AUTO_MIN_COLUMN_WIDTH, width));
|
|
160
|
+
|
|
161
|
+
export interface GetDataTableNaturalWidthsArgs {
|
|
162
|
+
columns: DataTableColumn[];
|
|
163
|
+
data: DataTableCellData[][];
|
|
164
|
+
defaultTextSize: DataTableTextSize;
|
|
165
|
+
formatDate: (value: unknown) => string;
|
|
166
|
+
/** Column types drawn by `customColumnComponentMap`; their content is not text we can measure. */
|
|
167
|
+
customColumnTypes?: Set<string>;
|
|
168
|
+
hasFilters?: boolean;
|
|
169
|
+
measureText?: MeasureDataTableText;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Width each column wants on its own: the wider of its header and its sampled content, clamped
|
|
174
|
+
* to a sane range. Columns with an explicit `width` keep it. Runs only when columns or data change.
|
|
175
|
+
*/
|
|
176
|
+
export const getDataTableNaturalWidths = ({
|
|
177
|
+
columns,
|
|
178
|
+
data,
|
|
179
|
+
defaultTextSize,
|
|
180
|
+
formatDate,
|
|
181
|
+
customColumnTypes,
|
|
182
|
+
hasFilters = false,
|
|
183
|
+
measureText = measureDataTableTextWidth,
|
|
184
|
+
}: GetDataTableNaturalWidthsArgs): number[] => {
|
|
185
|
+
const rowIndexes = sampleRowIndexes(data.length);
|
|
186
|
+
|
|
187
|
+
return columns.map((column, columnIndex) => {
|
|
188
|
+
if (column.width !== undefined) {
|
|
189
|
+
return column.width;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
let headerWidth = column.title
|
|
193
|
+
? measureText({fontSize: DATA_TABLE_HEADER_FONT_SIZE, isHeader: true, text: column.title})
|
|
194
|
+
: 0;
|
|
195
|
+
if (column.sortable) {
|
|
196
|
+
headerWidth += HEADER_SORT_ICON_WIDTH;
|
|
197
|
+
}
|
|
198
|
+
if (column.infoModalText) {
|
|
199
|
+
headerWidth += HEADER_INFO_ICON_WIDTH;
|
|
200
|
+
}
|
|
201
|
+
if (column.filter && hasFilters && Platform.OS === "web") {
|
|
202
|
+
headerWidth += HEADER_FILTER_ICON_WIDTH;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
let contentWidth = 0;
|
|
206
|
+
const isCustom = customColumnTypes?.has(column.columnType) ?? false;
|
|
207
|
+
if (column.columnType === "boolean" && !isCustom) {
|
|
208
|
+
contentWidth = BOOLEAN_ICON_WIDTH;
|
|
209
|
+
} else {
|
|
210
|
+
for (const rowIndex of rowIndexes) {
|
|
211
|
+
const cell = data[rowIndex]?.[columnIndex];
|
|
212
|
+
if (!cell) {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const text = cellText(column, cell, formatDate);
|
|
216
|
+
if (text === undefined) {
|
|
217
|
+
if (isCustom) {
|
|
218
|
+
contentWidth = Math.max(contentWidth, DATA_TABLE_AUTO_CUSTOM_CONTENT_WIDTH);
|
|
219
|
+
}
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const fontSize = textFontSizes[cell.textSize ?? defaultTextSize].size;
|
|
223
|
+
contentWidth = Math.max(contentWidth, measureText({fontSize, isHeader: false, text}));
|
|
224
|
+
if (contentWidth >= DATA_TABLE_AUTO_MAX_COLUMN_WIDTH) {
|
|
225
|
+
// Already at the cap; the rest of the sample cannot change the result.
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return clampWidth(Math.max(headerWidth, contentWidth) + DATA_TABLE_CELL_HORIZONTAL_PADDING);
|
|
232
|
+
});
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
export interface FitDataTableColumnWidthsArgs {
|
|
236
|
+
columns: DataTableColumn[];
|
|
237
|
+
naturalWidths: number[];
|
|
238
|
+
/** Width the columns can fill. 0 (not measured yet) keeps the natural widths. */
|
|
239
|
+
availableWidth: number;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Grows auto-sized columns to fill the container, in proportion to their natural widths. When
|
|
244
|
+
* the columns already overflow, they keep their natural widths and the table scrolls
|
|
245
|
+
* horizontally. Columns with an explicit `width` never change.
|
|
246
|
+
*/
|
|
247
|
+
export const fitDataTableColumnWidths = ({
|
|
248
|
+
columns,
|
|
249
|
+
naturalWidths,
|
|
250
|
+
availableWidth,
|
|
251
|
+
}: FitDataTableColumnWidthsArgs): number[] => {
|
|
252
|
+
const autoIndexes = columns.flatMap((column, index) =>
|
|
253
|
+
column.width === undefined ? [index] : []
|
|
254
|
+
);
|
|
255
|
+
const totalNatural = naturalWidths.reduce((sum, width) => sum + width, 0);
|
|
256
|
+
const extra = Math.floor(availableWidth - totalNatural);
|
|
257
|
+
if (autoIndexes.length === 0 || availableWidth <= 0 || extra <= 0) {
|
|
258
|
+
return naturalWidths;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const autoNatural = autoIndexes.reduce((sum, index) => sum + naturalWidths[index], 0);
|
|
262
|
+
const widths = [...naturalWidths];
|
|
263
|
+
let assigned = 0;
|
|
264
|
+
for (const index of autoIndexes) {
|
|
265
|
+
const share = Math.floor((extra * naturalWidths[index]) / autoNatural);
|
|
266
|
+
widths[index] += share;
|
|
267
|
+
assigned += share;
|
|
268
|
+
}
|
|
269
|
+
// Rounding leftovers go to the last auto column so the row is exactly the container width.
|
|
270
|
+
widths[autoIndexes[autoIndexes.length - 1]] += extra - assigned;
|
|
271
|
+
return widths;
|
|
272
|
+
};
|
package/src/index.tsx
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
// Re-export React Native style types for
|
|
2
|
-
export type {
|
|
1
|
+
// Re-export React Native style and host-instance types for consumer projects.
|
|
2
|
+
export type {
|
|
3
|
+
ScrollViewInstance,
|
|
4
|
+
StyleProp,
|
|
5
|
+
TextInputInstance,
|
|
6
|
+
ViewInstance,
|
|
7
|
+
ViewStyle,
|
|
8
|
+
} from "react-native";
|
|
3
9
|
export * from "./Accordion";
|
|
4
10
|
export * from "./ActionSheet";
|
|
5
11
|
export * from "./AddressField";
|
|
@@ -138,6 +144,7 @@ export type * from "./ProvisionalConsentSigner";
|
|
|
138
144
|
export * from "./pdfHtmlTemplate";
|
|
139
145
|
export * from "./Radio";
|
|
140
146
|
export * from "./RadioField";
|
|
147
|
+
export type {TerrenoRequestArgs, TerrenoRequestClient} from "./requestClient";
|
|
141
148
|
export type * from "./Scorecard";
|
|
142
149
|
export * from "./ScrollView";
|
|
143
150
|
export * from "./SectionDivider";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type {FC, ReactElement, ReactNode} from "react";
|
|
2
2
|
import {useCallback, useEffect, useRef, useState} from "react";
|
|
3
|
-
import {Platform, Pressable, type
|
|
3
|
+
import {Platform, Pressable, type TextInputInstance, View} from "react-native";
|
|
4
4
|
|
|
5
5
|
import {Avatar} from "../Avatar";
|
|
6
6
|
import {Box} from "../Box";
|
|
@@ -276,7 +276,7 @@ export const LoginScreen: FC<LoginScreenProps> = ({
|
|
|
276
276
|
}, []);
|
|
277
277
|
|
|
278
278
|
const formRef = useRef<FormContainerHandle>(null);
|
|
279
|
-
const inputRefs = useRef<Record<string,
|
|
279
|
+
const inputRefs = useRef<Record<string, TextInputInstance | null>>({});
|
|
280
280
|
const isSubmittingRef = useRef(false);
|
|
281
281
|
|
|
282
282
|
const handlePasskeyPress = useCallback(async (): Promise<void> => {
|