@terreno/ui 57.8.0 → 57.10.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.
Files changed (269) hide show
  1. package/README.md +2 -0
  2. package/dist/AreaChart.d.ts +1 -1
  3. package/dist/AreaChart.d.ts.map +1 -1
  4. package/dist/AreaChart.js +70 -22
  5. package/dist/AreaChart.js.map +1 -1
  6. package/dist/BarChart.d.ts.map +1 -1
  7. package/dist/BarChart.js +55 -13
  8. package/dist/BarChart.js.map +1 -1
  9. package/dist/Button.d.ts.map +1 -1
  10. package/dist/Button.js +19 -4
  11. package/dist/Button.js.map +1 -1
  12. package/dist/ChartCard.d.ts +5 -0
  13. package/dist/ChartCard.d.ts.map +1 -0
  14. package/dist/ChartCard.js +18 -0
  15. package/dist/ChartCard.js.map +1 -0
  16. package/dist/Common.d.ts +147 -6
  17. package/dist/Common.d.ts.map +1 -1
  18. package/dist/Common.js.map +1 -1
  19. package/dist/DashboardGrid.d.ts +6 -2
  20. package/dist/DashboardGrid.d.ts.map +1 -1
  21. package/dist/DashboardGrid.js +43 -10
  22. package/dist/DashboardGrid.js.map +1 -1
  23. package/dist/DateTimeField.d.ts.map +1 -1
  24. package/dist/DateTimeField.js +90 -17
  25. package/dist/DateTimeField.js.map +1 -1
  26. package/dist/DonutChart.d.ts.map +1 -1
  27. package/dist/DonutChart.js +20 -10
  28. package/dist/DonutChart.js.map +1 -1
  29. package/dist/DropdownMenuItem.d.ts +16 -0
  30. package/dist/DropdownMenuItem.d.ts.map +1 -0
  31. package/dist/DropdownMenuItem.js +11 -0
  32. package/dist/DropdownMenuItem.js.map +1 -0
  33. package/dist/FilePickerButton.d.ts +15 -1
  34. package/dist/FilePickerButton.d.ts.map +1 -1
  35. package/dist/FilePickerButton.js +90 -13
  36. package/dist/FilePickerButton.js.map +1 -1
  37. package/dist/GPTChat.d.ts +57 -1
  38. package/dist/GPTChat.d.ts.map +1 -1
  39. package/dist/GPTChat.js +301 -20
  40. package/dist/GPTChat.js.map +1 -1
  41. package/dist/HtmlFrame.d.ts +11 -0
  42. package/dist/HtmlFrame.d.ts.map +1 -0
  43. package/dist/HtmlFrame.js +29 -0
  44. package/dist/HtmlFrame.js.map +1 -0
  45. package/dist/LineChart.d.ts +1 -1
  46. package/dist/LineChart.d.ts.map +1 -1
  47. package/dist/LineChart.js +62 -21
  48. package/dist/LineChart.js.map +1 -1
  49. package/dist/Scorecard.d.ts +5 -0
  50. package/dist/Scorecard.d.ts.map +1 -0
  51. package/dist/Scorecard.js +11 -0
  52. package/dist/Scorecard.js.map +1 -0
  53. package/dist/SparklineChart.d.ts +5 -0
  54. package/dist/SparklineChart.d.ts.map +1 -0
  55. package/dist/SparklineChart.js +35 -0
  56. package/dist/SparklineChart.js.map +1 -0
  57. package/dist/Text.d.ts +1 -1
  58. package/dist/Text.d.ts.map +1 -1
  59. package/dist/Text.js +2 -2
  60. package/dist/Text.js.map +1 -1
  61. package/dist/TextField.d.ts.map +1 -1
  62. package/dist/TextField.js +12 -4
  63. package/dist/TextField.js.map +1 -1
  64. package/dist/ToastNotifications.d.ts.map +1 -1
  65. package/dist/ToastNotifications.js +29 -6
  66. package/dist/ToastNotifications.js.map +1 -1
  67. package/dist/WebDropdownMenu.d.ts +2 -1
  68. package/dist/WebDropdownMenu.d.ts.map +1 -1
  69. package/dist/WebDropdownMenu.js +2 -6
  70. package/dist/WebDropdownMenu.js.map +1 -1
  71. package/dist/asks/AskCard.d.ts +34 -0
  72. package/dist/asks/AskCard.d.ts.map +1 -0
  73. package/dist/asks/AskCard.js +91 -0
  74. package/dist/asks/AskCard.js.map +1 -0
  75. package/dist/asks/AskChoice.d.ts +11 -0
  76. package/dist/asks/AskChoice.d.ts.map +1 -0
  77. package/dist/asks/AskChoice.js +146 -0
  78. package/dist/asks/AskChoice.js.map +1 -0
  79. package/dist/asks/AskConfirm.d.ts +15 -0
  80. package/dist/asks/AskConfirm.d.ts.map +1 -0
  81. package/dist/asks/AskConfirm.js +17 -0
  82. package/dist/asks/AskConfirm.js.map +1 -0
  83. package/dist/asks/AskFiles.d.ts +18 -0
  84. package/dist/asks/AskFiles.d.ts.map +1 -0
  85. package/dist/asks/AskFiles.js +149 -0
  86. package/dist/asks/AskFiles.js.map +1 -0
  87. package/dist/asks/AskForm.d.ts +27 -0
  88. package/dist/asks/AskForm.d.ts.map +1 -0
  89. package/dist/asks/AskForm.js +146 -0
  90. package/dist/asks/AskForm.js.map +1 -0
  91. package/dist/asks/AskMarkdown.d.ts +25 -0
  92. package/dist/asks/AskMarkdown.d.ts.map +1 -0
  93. package/dist/asks/AskMarkdown.js +73 -0
  94. package/dist/asks/AskMarkdown.js.map +1 -0
  95. package/dist/asks/SimpleAskCard.d.ts +23 -0
  96. package/dist/asks/SimpleAskCard.d.ts.map +1 -0
  97. package/dist/asks/SimpleAskCard.js +32 -0
  98. package/dist/asks/SimpleAskCard.js.map +1 -0
  99. package/dist/asks/askControls.d.ts +33 -0
  100. package/dist/asks/askControls.d.ts.map +1 -0
  101. package/dist/asks/askControls.js +30 -0
  102. package/dist/asks/askControls.js.map +1 -0
  103. package/dist/asks/askFileRefs.d.ts +26 -0
  104. package/dist/asks/askFileRefs.d.ts.map +1 -0
  105. package/dist/asks/askFileRefs.js +75 -0
  106. package/dist/asks/askFileRefs.js.map +1 -0
  107. package/dist/asks/askFormDraft.d.ts +60 -0
  108. package/dist/asks/askFormDraft.d.ts.map +1 -0
  109. package/dist/asks/askFormDraft.js +224 -0
  110. package/dist/asks/askFormDraft.js.map +1 -0
  111. package/dist/asks/askSummary.d.ts +10 -0
  112. package/dist/asks/askSummary.d.ts.map +1 -0
  113. package/dist/asks/askSummary.js +138 -0
  114. package/dist/asks/askSummary.js.map +1 -0
  115. package/dist/asks/askTypes.d.ts +21 -0
  116. package/dist/asks/askTypes.d.ts.map +1 -0
  117. package/dist/asks/askTypes.js +2 -0
  118. package/dist/asks/askTypes.js.map +1 -0
  119. package/dist/asks/simpleCardButtonVariants.d.ts +5 -0
  120. package/dist/asks/simpleCardButtonVariants.d.ts.map +1 -0
  121. package/dist/asks/simpleCardButtonVariants.js +8 -0
  122. package/dist/asks/simpleCardButtonVariants.js.map +1 -0
  123. package/dist/blocks/BlocksError.d.ts +8 -0
  124. package/dist/blocks/BlocksError.d.ts.map +1 -0
  125. package/dist/blocks/BlocksError.js +14 -0
  126. package/dist/blocks/BlocksError.js.map +1 -0
  127. package/dist/blocks/BlocksView.d.ts +10 -0
  128. package/dist/blocks/BlocksView.d.ts.map +1 -0
  129. package/dist/blocks/BlocksView.js +90 -0
  130. package/dist/blocks/BlocksView.js.map +1 -0
  131. package/dist/blocks/blockRenderers.d.ts +25 -0
  132. package/dist/blocks/blockRenderers.d.ts.map +1 -0
  133. package/dist/blocks/blockRenderers.js +136 -0
  134. package/dist/blocks/blockRenderers.js.map +1 -0
  135. package/dist/blocks/datasetToPoints.d.ts +22 -0
  136. package/dist/blocks/datasetToPoints.d.ts.map +1 -0
  137. package/dist/blocks/datasetToPoints.js +28 -0
  138. package/dist/blocks/datasetToPoints.js.map +1 -0
  139. package/dist/blocks/useBlockSelections.d.ts +6 -0
  140. package/dist/blocks/useBlockSelections.d.ts.map +1 -0
  141. package/dist/blocks/useBlockSelections.js +10 -0
  142. package/dist/blocks/useBlockSelections.js.map +1 -0
  143. package/dist/blocks/useResolvedDatasets.d.ts +18 -0
  144. package/dist/blocks/useResolvedDatasets.d.ts.map +1 -0
  145. package/dist/blocks/useResolvedDatasets.js +59 -0
  146. package/dist/blocks/useResolvedDatasets.js.map +1 -0
  147. package/dist/blocks/useResolvedImages.d.ts +15 -0
  148. package/dist/blocks/useResolvedImages.d.ts.map +1 -0
  149. package/dist/blocks/useResolvedImages.js +66 -0
  150. package/dist/blocks/useResolvedImages.js.map +1 -0
  151. package/dist/charts/ChartFacadeContainer.d.ts +10 -0
  152. package/dist/charts/ChartFacadeContainer.d.ts.map +1 -0
  153. package/dist/charts/ChartFacadeContainer.js +10 -0
  154. package/dist/charts/ChartFacadeContainer.js.map +1 -0
  155. package/dist/charts/ChartFrame.d.ts +4 -0
  156. package/dist/charts/ChartFrame.d.ts.map +1 -1
  157. package/dist/charts/ChartFrame.js +4 -2
  158. package/dist/charts/ChartFrame.js.map +1 -1
  159. package/dist/charts/layout.d.ts +9 -2
  160. package/dist/charts/layout.d.ts.map +1 -1
  161. package/dist/charts/layout.js +28 -6
  162. package/dist/charts/layout.js.map +1 -1
  163. package/dist/charts/scales.d.ts +3 -1
  164. package/dist/charts/scales.d.ts.map +1 -1
  165. package/dist/charts/scales.js +7 -7
  166. package/dist/charts/scales.js.map +1 -1
  167. package/dist/charts/series.d.ts +15 -0
  168. package/dist/charts/series.d.ts.map +1 -0
  169. package/dist/charts/series.js +19 -0
  170. package/dist/charts/series.js.map +1 -0
  171. package/dist/charts/types/chartTypes.d.ts +7 -0
  172. package/dist/charts/types/chartTypes.d.ts.map +1 -1
  173. package/dist/dashboardGridLayout.d.ts +6 -0
  174. package/dist/dashboardGridLayout.d.ts.map +1 -1
  175. package/dist/dashboardGridLayout.js +19 -3
  176. package/dist/dashboardGridLayout.js.map +1 -1
  177. package/dist/index.d.ts +9 -1
  178. package/dist/index.d.ts.map +1 -1
  179. package/dist/index.js +4 -1
  180. package/dist/index.js.map +1 -1
  181. package/dist/lazyBoundaries/heavyOptionalExports.d.ts +15 -0
  182. package/dist/lazyBoundaries/heavyOptionalExports.d.ts.map +1 -1
  183. package/dist/lazyBoundaries/heavyOptionalExports.js +10 -0
  184. package/dist/lazyBoundaries/heavyOptionalExports.js.map +1 -1
  185. package/dist/resolveDocumentBodyPortalTarget.d.ts +7 -0
  186. package/dist/resolveDocumentBodyPortalTarget.d.ts.map +1 -0
  187. package/dist/resolveDocumentBodyPortalTarget.js +12 -0
  188. package/dist/resolveDocumentBodyPortalTarget.js.map +1 -0
  189. package/package.json +2 -1
  190. package/src/AIRequestExplorer.test.tsx +2 -2
  191. package/src/AreaChart.test.tsx +81 -0
  192. package/src/AreaChart.tsx +171 -62
  193. package/src/BarChart.test.tsx +119 -0
  194. package/src/BarChart.tsx +94 -20
  195. package/src/Button.test.tsx +72 -0
  196. package/src/Button.tsx +34 -4
  197. package/src/ChartCard.test.tsx +79 -0
  198. package/src/ChartCard.tsx +80 -0
  199. package/src/Common.ts +136 -2
  200. package/src/DashboardGrid.test.tsx +98 -1
  201. package/src/DashboardGrid.tsx +71 -20
  202. package/src/DateTimeField.test.tsx +185 -2
  203. package/src/DateTimeField.tsx +106 -16
  204. package/src/DonutChart.test.tsx +78 -10
  205. package/src/DonutChart.tsx +87 -14
  206. package/src/DropdownMenuItem.tsx +48 -0
  207. package/src/FilePickerButton.test.tsx +116 -9
  208. package/src/FilePickerButton.tsx +177 -23
  209. package/src/GPTChat.test.tsx +891 -8
  210. package/src/GPTChat.tsx +713 -45
  211. package/src/HtmlFrame.tsx +60 -0
  212. package/src/LineChart.test.tsx +109 -10
  213. package/src/LineChart.tsx +154 -58
  214. package/src/Scorecard.test.tsx +65 -0
  215. package/src/Scorecard.tsx +45 -0
  216. package/src/SparklineChart.test.tsx +69 -0
  217. package/src/SparklineChart.tsx +85 -0
  218. package/src/Text.test.tsx +22 -0
  219. package/src/Text.tsx +10 -1
  220. package/src/TextField.test.tsx +16 -0
  221. package/src/TextField.tsx +13 -3
  222. package/src/ToastNotifications.tsx +33 -6
  223. package/src/ToastNotifications.webModal.test.tsx +285 -0
  224. package/src/WebDropdownMenu.tsx +3 -7
  225. package/src/asks/AskCard.test.tsx +1428 -0
  226. package/src/asks/AskCard.tsx +240 -0
  227. package/src/asks/AskChoice.tsx +368 -0
  228. package/src/asks/AskConfirm.tsx +42 -0
  229. package/src/asks/AskFiles.test.tsx +347 -0
  230. package/src/asks/AskFiles.tsx +246 -0
  231. package/src/asks/AskForm.tsx +334 -0
  232. package/src/asks/AskMarkdown.tsx +176 -0
  233. package/src/asks/SimpleAskCard.test.tsx +253 -0
  234. package/src/asks/SimpleAskCard.tsx +82 -0
  235. package/src/asks/askControls.tsx +98 -0
  236. package/src/asks/askFileRefs.test.ts +155 -0
  237. package/src/asks/askFileRefs.ts +98 -0
  238. package/src/asks/askFormDraft.test.ts +347 -0
  239. package/src/asks/askFormDraft.ts +309 -0
  240. package/src/asks/askSummary.test.ts +255 -0
  241. package/src/asks/askSummary.ts +160 -0
  242. package/src/asks/askTypes.ts +25 -0
  243. package/src/asks/asksUseTerrenoComponents.test.ts +55 -0
  244. package/src/asks/simpleCardButtonVariants.ts +14 -0
  245. package/src/blocks/BlocksError.tsx +28 -0
  246. package/src/blocks/BlocksView.test.tsx +607 -0
  247. package/src/blocks/BlocksView.tsx +125 -0
  248. package/src/blocks/blockRenderers.tsx +288 -0
  249. package/src/blocks/datasetToPoints.test.ts +31 -0
  250. package/src/blocks/datasetToPoints.ts +48 -0
  251. package/src/blocks/useBlockSelections.ts +13 -0
  252. package/src/blocks/useResolvedDatasets.ts +78 -0
  253. package/src/blocks/useResolvedImages.ts +81 -0
  254. package/src/bunSetup.ts +1 -0
  255. package/src/charts/ChartFacadeContainer.tsx +35 -0
  256. package/src/charts/ChartFrame.tsx +39 -1
  257. package/src/charts/layout.test.ts +22 -1
  258. package/src/charts/layout.ts +37 -4
  259. package/src/charts/scales.test.ts +33 -0
  260. package/src/charts/scales.ts +10 -6
  261. package/src/charts/series.ts +43 -0
  262. package/src/charts/types/chartTypes.ts +8 -0
  263. package/src/dashboardGridLayout.test.ts +59 -1
  264. package/src/dashboardGridLayout.ts +30 -3
  265. package/src/index.tsx +13 -0
  266. package/src/isolated/HtmlFrame.isolated.tsx +54 -0
  267. package/src/lazyBoundaries/heavyOptionalExports.test.tsx +2 -0
  268. package/src/lazyBoundaries/heavyOptionalExports.tsx +35 -0
  269. package/src/resolveDocumentBodyPortalTarget.ts +11 -0
@@ -0,0 +1,60 @@
1
+ import React, {useRef} from "react";
2
+ import {Platform} from "react-native";
3
+ import WebView from "react-native-webview";
4
+
5
+ import {Box} from "./Box";
6
+ import type {HtmlFrameProps} from "./Common";
7
+
8
+ export type {HtmlFrameProps} from "./Common";
9
+
10
+ const HTML_FRAME_HEIGHTS = {lg: 640, md: 400, sm: 240} as const;
11
+
12
+ const CSP =
13
+ "default-src 'none'; img-src data:; style-src 'unsafe-inline'; font-src data:; form-action 'none'; base-uri 'none'";
14
+
15
+ /** The document a frame loads. The CSP meta tag is first so it applies to the preview. */
16
+ export const htmlFrameSrcDoc = (html: string): string =>
17
+ `<meta http-equiv="Content-Security-Policy" content="${CSP}">${html}`;
18
+
19
+ /**
20
+ * Sandboxed preview of agent HTML. Web uses an iframe with an empty sandbox.
21
+ * Native uses a WebView with JavaScript off and navigation blocked after the first load.
22
+ */
23
+ export const HtmlFrame: React.FC<HtmlFrameProps> = ({height = "md", html, title}) => {
24
+ const srcDoc = htmlFrameSrcDoc(html);
25
+ const pixels = HTML_FRAME_HEIGHTS[height];
26
+ const didLoad = useRef(false);
27
+
28
+ if (Platform.OS === "web") {
29
+ return (
30
+ <Box height={pixels} testID="html-frame-web" width="100%">
31
+ <iframe
32
+ referrerPolicy="no-referrer"
33
+ sandbox=""
34
+ srcDoc={srcDoc}
35
+ style={{border: 0, height: "100%", width: "100%"}}
36
+ title={title ?? "Agent-generated preview"}
37
+ />
38
+ </Box>
39
+ );
40
+ }
41
+
42
+ return (
43
+ <Box height={pixels} testID="html-frame-native" width="100%">
44
+ <WebView
45
+ dataDetectorTypes="none"
46
+ incognito
47
+ javaScriptEnabled={false}
48
+ onShouldStartLoadWithRequest={() => {
49
+ if (didLoad.current) {
50
+ return false;
51
+ }
52
+ didLoad.current = true;
53
+ return true;
54
+ }}
55
+ source={{html: srcDoc}}
56
+ style={{flex: 1}}
57
+ />
58
+ </Box>
59
+ );
60
+ };
@@ -1,6 +1,6 @@
1
- import {describe, expect, it, spyOn} from "bun:test";
1
+ import {describe, expect, it} from "bun:test";
2
2
  import {act, fireEvent} from "@testing-library/react-native";
3
- import {Linking} from "react-native";
3
+ import {assert} from "chai";
4
4
 
5
5
  import {LineChart} from "./LineChart";
6
6
  import {renderWithTheme} from "./test-utils";
@@ -11,7 +11,110 @@ const POINTS = [
11
11
  {label: "C", value: 100},
12
12
  ];
13
13
 
14
+ const SERIES = [
15
+ {data: POINTS, id: "rank", label: "Search lost IS (rank)"},
16
+ {
17
+ data: POINTS.map((point) => ({...point, value: point.value / 2})),
18
+ id: "share",
19
+ label: "Search impr. share",
20
+ },
21
+ {
22
+ data: POINTS.map((point) => ({...point, value: point.value / 4})),
23
+ id: "budget",
24
+ label: "Search lost IS (budget)",
25
+ },
26
+ ];
27
+
14
28
  describe("LineChart", () => {
29
+ it("applies explicit tick rotation and chart-card header shortcuts", async (): Promise<void> => {
30
+ let pressCount = 0;
31
+ const {getByTestId, getByText} = renderWithTheme(
32
+ <LineChart
33
+ data={POINTS}
34
+ onPeriodPress={(): void => {
35
+ pressCount += 1;
36
+ }}
37
+ periodLabel="Last 30 days"
38
+ testID="chart"
39
+ title="Search share"
40
+ xTickPolicy="rotate"
41
+ />
42
+ );
43
+ const tickStyle = getByTestId("chart.xtick.0").props.style;
44
+ const tickStyles = Array.isArray(tickStyle) ? tickStyle : [tickStyle];
45
+
46
+ assert.isTrue(tickStyles.some((style) => Array.isArray(style?.transform)));
47
+ assert.exists(getByText("Search share"));
48
+ assert.exists(getByText("Last 30 days"));
49
+ fireEvent.press(getByTestId("chart.card.period-clickable"));
50
+ await new Promise((resolve) => setTimeout(resolve, 0));
51
+ assert.equal(pressCount, 1);
52
+ });
53
+
54
+ it("renders one path and legend item per named series", () => {
55
+ const {getByTestId, getByText} = renderWithTheme(
56
+ <LineChart data={[]} series={SERIES} testID="chart" />
57
+ );
58
+
59
+ for (let index = 0; index < SERIES.length; index += 1) {
60
+ assert.exists(getByTestId(`chart.series.${index}.path`));
61
+ assert.exists(getByTestId(`chart.series.${index}.marker.0`));
62
+ assert.exists(getByTestId(`chart.legend.${index}`));
63
+ assert.exists(getByTestId(`chart.legend.${index}.swatch`));
64
+ const legendStyle = getByTestId(`chart.legend.${index}.item`).props.style;
65
+ const legendStyles = Array.isArray(legendStyle) ? legendStyle : [legendStyle];
66
+ assert.isTrue(
67
+ legendStyles.some(
68
+ (style: {flexGrow?: number; minWidth?: number} | undefined) =>
69
+ style?.flexGrow === 1 && style.minWidth === 0
70
+ )
71
+ );
72
+ assert.exists(getByText(SERIES[index]?.label ?? ""));
73
+ }
74
+ assert.isBelow(
75
+ getByTestId("chart.series.0.marker.2").props.cy,
76
+ getByTestId("chart.series.2.marker.2").props.cy
77
+ );
78
+ });
79
+
80
+ it("identifies the active series in a multi-series tooltip", async (): Promise<void> => {
81
+ const {getByTestId, getByText} = renderWithTheme(
82
+ <LineChart data={[]} series={SERIES} testID="chart" />
83
+ );
84
+
85
+ await act(async () => {
86
+ fireEvent.press(getByTestId("chart.series.1.point.1-clickable"));
87
+ await new Promise((resolve) => setTimeout(resolve, 0));
88
+ });
89
+
90
+ assert.exists(getByText("Search impr. share — B: 25"));
91
+ });
92
+
93
+ it("renders a dotted comparison path on the shared scale", () => {
94
+ const {getByTestId} = renderWithTheme(
95
+ <LineChart comparisonData={POINTS} data={POINTS} testID="chart" />
96
+ );
97
+
98
+ assert.isString(getByTestId("chart.comparison").props.strokeDasharray);
99
+ });
100
+
101
+ it("renders comparison-only data instead of the empty state", () => {
102
+ const {getByTestId, queryByText} = renderWithTheme(
103
+ <LineChart comparisonData={POINTS} data={[]} testID="chart" />
104
+ );
105
+
106
+ assert.exists(getByTestId("chart.comparison"));
107
+ assert.notExists(queryByText("No data"));
108
+ });
109
+
110
+ it("shows the empty state when every named series is empty", () => {
111
+ const {getByText} = renderWithTheme(
112
+ <LineChart data={POINTS} series={[{data: [], id: "empty", label: "Empty"}]} />
113
+ );
114
+
115
+ assert.exists(getByText("No data"));
116
+ });
117
+
15
118
  it("renders one mark testID per point", () => {
16
119
  const {getByTestId, queryByTestId} = renderWithTheme(
17
120
  <LineChart data={POINTS} testID="chart" />
@@ -150,17 +253,13 @@ describe("LineChart", () => {
150
253
  );
151
254
  });
152
255
 
153
- it("does not open URLs from x-axis labels", async () => {
154
- const openURLSpy = spyOn(Linking, "openURL").mockImplementation(() => Promise.resolve(true));
256
+ it("does not open URLs from x-axis labels", () => {
155
257
  const {getByText} = renderWithTheme(
156
258
  <LineChart data={[{label: "https://evil.example", value: 10}]} />
157
259
  );
158
260
 
159
- await act(async () => {
160
- fireEvent.press(getByText("https://evil.example"));
161
- });
162
-
163
- expect(openURLSpy).not.toHaveBeenCalled();
164
- openURLSpy.mockRestore();
261
+ // The suite shares one Linking mock, so a spy on openURL also records calls
262
+ // from other files. A label is inert when it has no press handler.
263
+ expect(getByText("https://evil.example").props.onPress).toBeUndefined();
165
264
  });
166
265
  });
package/src/LineChart.tsx CHANGED
@@ -4,17 +4,21 @@ import {Circle, Path, Svg, Line as SvgLine} from "react-native-svg";
4
4
 
5
5
  import {Box} from "./Box";
6
6
  import type {LayoutChangeEvent, LineChartProps} from "./Common";
7
+ import {ChartFacadeContainer} from "./charts/ChartFacadeContainer";
7
8
  import {ChartFrame} from "./charts/ChartFrame";
8
9
  import {
10
+ CHART_ROTATED_X_AXIS_HEIGHT,
9
11
  CHART_X_AXIS_HEIGHT,
10
12
  getChartAxisWidth,
11
13
  getChartPlot,
12
14
  getPlotHeight,
13
15
  getXTickStyle,
14
16
  getYTickStyle,
17
+ shouldRotateChartXTicks,
15
18
  } from "./charts/layout";
16
19
  import {getLinePath} from "./charts/paths";
17
20
  import {createCartesianScales, getYTickValues} from "./charts/scales";
21
+ import {getAllSeriesPoints, getAxisPoints, resolveChartSeries} from "./charts/series";
18
22
  import {getChartPaint} from "./charts/theme";
19
23
  import type {ChartPoint} from "./charts/types/chartTypes";
20
24
  import {Text} from "./Text";
@@ -28,27 +32,41 @@ const MARK_HIT_SIZE = 24;
28
32
  const formatChartTooltip = ({
29
33
  formatValue,
30
34
  point,
35
+ seriesLabel,
31
36
  }: {
32
37
  formatValue: (value: number) => string;
33
38
  point: ChartPoint;
39
+ seriesLabel?: string;
34
40
  }): string => {
35
- return `${point.label}: ${formatValue(point.value)}`;
41
+ const prefix = seriesLabel ? `${seriesLabel} — ` : "";
42
+ return `${prefix}${point.label}: ${formatValue(point.value)}`;
36
43
  };
37
44
 
45
+ interface ActivePoint {
46
+ pointIndex: number;
47
+ seriesIndex: number;
48
+ }
49
+
38
50
  export const LineChart: FC<LineChartProps> = ({
39
51
  accessibilityLabel,
52
+ comparisonData = [],
40
53
  data,
41
54
  emptyText = "No data",
42
55
  formatValue = String,
43
56
  height = DEFAULT_HEIGHT,
44
57
  legendLabel,
45
58
  loading = false,
59
+ onPeriodPress,
60
+ periodLabel,
61
+ series,
46
62
  testID,
63
+ title,
64
+ xTickPolicy = "auto",
47
65
  }) => {
48
66
  const {theme} = useTheme();
49
67
  const paint = getChartPaint(theme);
50
68
  const [chartWidth, setChartWidth] = useState(DEFAULT_WIDTH);
51
- const [activePointIndex, setActivePointIndex] = useState<number | undefined>(undefined);
69
+ const [activePoint, setActivePoint] = useState<ActivePoint | undefined>(undefined);
52
70
 
53
71
  const handleLayout = useCallback((event: LayoutChangeEvent): void => {
54
72
  const nextWidth = event.nativeEvent.layout.width;
@@ -57,29 +75,59 @@ export const LineChart: FC<LineChartProps> = ({
57
75
  }
58
76
  }, []);
59
77
 
60
- const handleMarkPress = useCallback((pointIndex: number): void => {
61
- setActivePointIndex(pointIndex);
78
+ const handleMarkPress = useCallback((seriesIndex: number, pointIndex: number): void => {
79
+ setActivePoint({pointIndex, seriesIndex});
62
80
  }, []);
63
81
 
82
+ const resolvedSeries = resolveChartSeries({data, legendLabel, series});
83
+ const allPoints = getAllSeriesPoints({comparisonData, series: resolvedSeries});
84
+ const axisPoints = getAxisPoints({comparisonData, series: resolvedSeries});
85
+ const coloredSeries = resolvedSeries.map((entry, index) => ({
86
+ ...entry,
87
+ color: entry.color ?? paint.slices[index % paint.slices.length] ?? paint.series,
88
+ }));
64
89
  const axisWidth = getChartAxisWidth(chartWidth);
65
- const plotHeight = getPlotHeight({hasLegend: Boolean(legendLabel), height});
90
+ const legendItems =
91
+ series && series.length > 0
92
+ ? coloredSeries.map((entry) => ({color: entry.color, label: entry.label}))
93
+ : undefined;
94
+ const isXTickRotated = shouldRotateChartXTicks({
95
+ labelCount: axisPoints.length,
96
+ policy: xTickPolicy,
97
+ });
98
+ const xAxisHeight = isXTickRotated ? CHART_ROTATED_X_AXIS_HEIGHT : CHART_X_AXIS_HEIGHT;
99
+ const plotHeight = getPlotHeight({
100
+ hasLegend: Boolean(legendLabel) || Boolean(legendItems?.length),
101
+ height,
102
+ xAxisHeight,
103
+ });
66
104
  const plot = getChartPlot({chartWidth, height: plotHeight});
67
105
  const plotWidth = chartWidth - axisWidth;
68
- const scales = createCartesianScales({plot, points: data});
69
- const linePath = getLinePath({points: data, scales});
70
- const yTicks = getYTickValues(data);
71
- const activePoint = activePointIndex === undefined ? undefined : data[activePointIndex];
72
- const tooltipText = activePoint
73
- ? formatChartTooltip({formatValue, point: activePoint})
106
+ const scales = createCartesianScales({
107
+ plot,
108
+ points: allPoints,
109
+ xLabels: axisPoints.map((point) => point.label),
110
+ });
111
+ const comparisonPath = getLinePath({points: comparisonData, scales});
112
+ const yTicks = getYTickValues(allPoints);
113
+ const activeSeries = activePoint ? coloredSeries[activePoint.seriesIndex] : undefined;
114
+ const selectedPoint = activePoint ? activeSeries?.data[activePoint.pointIndex] : undefined;
115
+ const tooltipText = selectedPoint
116
+ ? formatChartTooltip({
117
+ formatValue,
118
+ point: selectedPoint,
119
+ seriesLabel: series && series.length > 0 ? activeSeries?.label : undefined,
120
+ })
74
121
  : undefined;
75
122
  const summaryLabel =
76
123
  accessibilityLabel ?? (legendLabel ? `${legendLabel} line chart` : "Line chart");
77
124
 
78
- return (
125
+ const chart = (
79
126
  <ChartFrame
80
127
  accessibilityLabel={summaryLabel}
81
128
  emptyText={emptyText}
82
- isEmpty={data.length === 0}
129
+ isEmpty={allPoints.length === 0}
130
+ legendItems={legendItems}
83
131
  legendLabel={legendLabel}
84
132
  loading={loading}
85
133
  testID={testID}
@@ -115,66 +163,103 @@ export const LineChart: FC<LineChartProps> = ({
115
163
  />
116
164
  );
117
165
  })}
118
- {linePath ? (
119
- <Path d={linePath} fill="none" stroke={paint.series} strokeWidth={2} />
120
- ) : null}
121
- {data.map((point) => (
122
- <Circle
123
- cx={scales.xCenter(point.label)}
124
- cy={scales.y(point.value)}
125
- fill={paint.series}
126
- key={`dot-${point.label}`}
127
- r={4}
166
+ {comparisonPath ? (
167
+ <Path
168
+ d={comparisonPath}
169
+ fill="none"
170
+ opacity={0.6}
171
+ stroke={paint.series}
172
+ strokeDasharray="4 4"
173
+ strokeWidth={2}
174
+ testID={resolveTestID(testID, "comparison")}
128
175
  />
129
- ))}
176
+ ) : null}
177
+ {coloredSeries.map((entry, seriesIndex) => {
178
+ const linePath = getLinePath({points: entry.data, scales});
179
+ return linePath ? (
180
+ <Path
181
+ d={linePath}
182
+ fill="none"
183
+ key={`line-${entry.id}`}
184
+ stroke={entry.color}
185
+ strokeDasharray={entry.stroke === "dotted" ? "4 4" : undefined}
186
+ strokeWidth={2}
187
+ testID={resolveTestID(testID, `series.${seriesIndex}.path`)}
188
+ />
189
+ ) : null;
190
+ })}
191
+ {coloredSeries.flatMap((entry, seriesIndex) =>
192
+ entry.data.map((point, pointIndex) => (
193
+ <Circle
194
+ cx={scales.xCenter(point.label)}
195
+ cy={scales.y(point.value)}
196
+ fill={entry.color}
197
+ key={`dot-${entry.id}-${point.label}`}
198
+ r={4}
199
+ testID={resolveTestID(testID, `series.${seriesIndex}.marker.${pointIndex}`)}
200
+ />
201
+ ))
202
+ )}
130
203
  </Svg>
131
- {data.map((point, index) => {
132
- const onPress = (): void => {
133
- handleMarkPress(index);
134
- };
135
- return (
136
- <Box
137
- accessibilityHint={`Show value for ${point.label}`}
138
- accessibilityLabel={`${point.label}: ${formatValue(point.value)}`}
139
- dangerouslySetInlineStyle={{
140
- __style: {
141
- height: MARK_HIT_SIZE,
142
- left: scales.xCenter(point.label) - MARK_HIT_SIZE / 2,
143
- position: "absolute",
144
- top: scales.y(point.value) - MARK_HIT_SIZE / 2,
145
- width: MARK_HIT_SIZE,
146
- },
147
- }}
148
- key={`mark-${point.label}`}
149
- onClick={onPress}
150
- onHoverStart={onPress}
151
- testID={resolveTestID(testID, `point.${index}`)}
152
- />
153
- );
154
- })}
204
+ {coloredSeries.flatMap((entry, seriesIndex) =>
205
+ entry.data.map((point, pointIndex) => {
206
+ const onPress = (): void => {
207
+ handleMarkPress(seriesIndex, pointIndex);
208
+ };
209
+ const markTestID =
210
+ series && series.length > 0
211
+ ? `series.${seriesIndex}.point.${pointIndex}`
212
+ : `point.${pointIndex}`;
213
+ return (
214
+ <Box
215
+ accessibilityHint={`Show value for ${point.label}`}
216
+ accessibilityLabel={
217
+ series && series.length > 0
218
+ ? `${entry.label} — ${point.label}: ${formatValue(point.value)}`
219
+ : `${point.label}: ${formatValue(point.value)}`
220
+ }
221
+ dangerouslySetInlineStyle={{
222
+ __style: {
223
+ height: MARK_HIT_SIZE,
224
+ left: scales.xCenter(point.label) - MARK_HIT_SIZE / 2,
225
+ position: "absolute",
226
+ top: scales.y(point.value) - MARK_HIT_SIZE / 2,
227
+ width: MARK_HIT_SIZE,
228
+ },
229
+ }}
230
+ key={`mark-${entry.id}-${point.label}`}
231
+ onClick={onPress}
232
+ onHoverStart={onPress}
233
+ testID={resolveTestID(testID, markTestID)}
234
+ />
235
+ );
236
+ })
237
+ )}
155
238
  </Box>
156
239
  </Box>
157
240
  <Box direction="row">
158
241
  <Box width={axisWidth} />
159
- <Box
160
- flex="grow"
161
- height={CHART_X_AXIS_HEIGHT}
162
- minWidth={0}
163
- overflow="hidden"
164
- position="relative"
165
- >
166
- {data.map((point, index) => (
242
+ <Box flex="grow" height={xAxisHeight} minWidth={0} overflow="hidden" position="relative">
243
+ {axisPoints.map((point, index) => (
167
244
  <Box
168
245
  dangerouslySetInlineStyle={{
169
246
  __style: getXTickStyle({
170
247
  bandwidth: scales.bandwidth,
248
+ isRotated: isXTickRotated,
171
249
  xCenter: scales.xCenter(point.label),
172
250
  }),
173
251
  }}
174
252
  key={`xtick-${point.label}`}
175
253
  testID={resolveTestID(testID, `xtick.${index}`)}
176
254
  >
177
- <Text align="center" color="secondaryDark" size="sm" skipLinking truncate>
255
+ <Text
256
+ align="center"
257
+ color="secondaryDark"
258
+ numberOfLines={1}
259
+ size="sm"
260
+ skipLinking
261
+ truncate={!isXTickRotated}
262
+ >
178
263
  {point.label}
179
264
  </Text>
180
265
  </Box>
@@ -184,7 +269,18 @@ export const LineChart: FC<LineChartProps> = ({
184
269
  </Box>
185
270
  </ChartFrame>
186
271
  );
272
+
273
+ return (
274
+ <ChartFacadeContainer
275
+ onPeriodPress={onPeriodPress}
276
+ periodLabel={periodLabel}
277
+ testID={testID}
278
+ title={title}
279
+ >
280
+ {chart}
281
+ </ChartFacadeContainer>
282
+ );
187
283
  };
188
284
 
189
285
  export type {LineChartProps} from "./Common";
190
- export type {ChartPoint} from "./charts/types/chartTypes";
286
+ export type {ChartPoint, ChartSeries} from "./charts/types/chartTypes";
@@ -0,0 +1,65 @@
1
+ import {describe, it} from "bun:test";
2
+ import {assert} from "chai";
3
+ import {Scorecard as RootScorecard} from "./index";
4
+ import {Scorecard} from "./Scorecard";
5
+ import {renderWithTheme} from "./test-utils";
6
+
7
+ const CURRENT = [
8
+ {label: "Mon", value: 12},
9
+ {label: "Tue", value: 18},
10
+ {label: "Wed", value: 9},
11
+ ];
12
+
13
+ const COMPARISON = [
14
+ {label: "Mon", value: 10},
15
+ {label: "Tue", value: 11},
16
+ {label: "Wed", value: 15},
17
+ ];
18
+
19
+ describe("Scorecard", () => {
20
+ it("renders a formatted metric with current and comparison sparkline paths", () => {
21
+ const {getByTestId, getByText} = renderWithTheme(
22
+ <Scorecard
23
+ comparisonData={COMPARISON}
24
+ formatValue={(value): string => `$${value}`}
25
+ sparklineData={CURRENT}
26
+ testID="cost-scorecard"
27
+ title="Cost"
28
+ value={569}
29
+ />
30
+ );
31
+
32
+ assert.exists(getByText("Cost"));
33
+ assert.exists(getByText("$569"));
34
+ assert.exists(getByTestId("cost-scorecard.sparkline.current"));
35
+ assert.exists(getByTestId("cost-scorecard.sparkline.comparison"));
36
+ });
37
+
38
+ it("renders a string metric without requiring sparkline data", () => {
39
+ const {getByText, queryByTestId} = renderWithTheme(
40
+ <Scorecard sparklineData={[]} testID="status-scorecard" title="Status" value="Healthy" />
41
+ );
42
+
43
+ assert.exists(getByText("Healthy"));
44
+ assert.notExists(queryByTestId("status-scorecard.sparkline"));
45
+ });
46
+
47
+ it("uses default numeric formatting and omits comparison when it is not provided", () => {
48
+ const {getByText, getByTestId, queryByTestId} = renderWithTheme(
49
+ <Scorecard
50
+ sparklineData={CURRENT}
51
+ testID="conversion-scorecard"
52
+ title="Conversions"
53
+ value={7}
54
+ />
55
+ );
56
+
57
+ assert.exists(getByText("7"));
58
+ assert.exists(getByTestId("conversion-scorecard.sparkline.current"));
59
+ assert.notExists(queryByTestId("conversion-scorecard.sparkline.comparison"));
60
+ });
61
+
62
+ it("is available from the package root lazy boundary", () => {
63
+ assert.isFunction(RootScorecard);
64
+ });
65
+ });
@@ -0,0 +1,45 @@
1
+ import type {FC} from "react";
2
+
3
+ import {Box} from "./Box";
4
+ import {ChartCard} from "./ChartCard";
5
+ import type {ScorecardProps} from "./Common";
6
+ import {Heading} from "./Heading";
7
+ import {SparklineChart} from "./SparklineChart";
8
+ import {resolveTestID} from "./testing/resolveTestId";
9
+
10
+ export const Scorecard: FC<ScorecardProps> = ({
11
+ comparisonData,
12
+ formatValue = String,
13
+ onPeriodPress,
14
+ periodLabel,
15
+ sparklineData,
16
+ testID,
17
+ title,
18
+ value,
19
+ }) => {
20
+ const displayValue = typeof value === "number" ? formatValue(value) : value;
21
+
22
+ return (
23
+ <ChartCard
24
+ onPeriodPress={onPeriodPress}
25
+ periodLabel={periodLabel}
26
+ testID={testID}
27
+ title={title}
28
+ >
29
+ <Box gap={2} minWidth={0} width="100%">
30
+ <Heading size="xl" testID={resolveTestID(testID, "value")}>
31
+ {displayValue}
32
+ </Heading>
33
+ {sparklineData && sparklineData.length > 0 ? (
34
+ <SparklineChart
35
+ comparisonData={comparisonData}
36
+ data={sparklineData}
37
+ testID={resolveTestID(testID, "sparkline")}
38
+ />
39
+ ) : null}
40
+ </Box>
41
+ </ChartCard>
42
+ );
43
+ };
44
+
45
+ export type {ScorecardProps} from "./Common";
@@ -0,0 +1,69 @@
1
+ import {describe, it} from "bun:test";
2
+ import {fireEvent} from "@testing-library/react-native";
3
+ import {assert} from "chai";
4
+
5
+ import {SparklineChart as RootSparklineChart} from "./index";
6
+ import {SparklineChart} from "./SparklineChart";
7
+ import {Text} from "./Text";
8
+ import {renderWithTheme} from "./test-utils";
9
+
10
+ const CURRENT = [
11
+ {label: "Mon", value: 12},
12
+ {label: "Tue", value: 18},
13
+ {label: "Wed", value: 9},
14
+ ];
15
+
16
+ const COMPARISON = [
17
+ {label: "Mon", value: 10},
18
+ {label: "Tue", value: 11},
19
+ {label: "Wed", value: 15},
20
+ ];
21
+
22
+ const getPathXCoordinates = (path: string): number[] => {
23
+ return Array.from(path.matchAll(/[ML]([0-9.]+),/g), (match) => Number(match[1]));
24
+ };
25
+
26
+ describe("SparklineChart", () => {
27
+ it("renders current and dotted comparison paths without chart chrome", () => {
28
+ const {getByTestId, queryByTestId, UNSAFE_queryAllByType} = renderWithTheme(
29
+ <SparklineChart comparisonData={COMPARISON} data={CURRENT} testID="revenue-sparkline" />
30
+ );
31
+
32
+ const current = getByTestId("revenue-sparkline.current");
33
+ const comparison = getByTestId("revenue-sparkline.comparison");
34
+
35
+ assert.isUndefined(current.props.strokeDasharray);
36
+ assert.isString(comparison.props.strokeDasharray);
37
+ assert.deepEqual(getPathXCoordinates(current.props.d), getPathXCoordinates(comparison.props.d));
38
+ assert.lengthOf(getPathXCoordinates(current.props.d), CURRENT.length);
39
+ assert.lengthOf(UNSAFE_queryAllByType(Text), 0);
40
+ assert.notExists(queryByTestId("revenue-sparkline.tooltip"));
41
+ assert.notExists(queryByTestId("revenue-sparkline.legend"));
42
+ });
43
+
44
+ it("renders an empty frame without throwing", () => {
45
+ const {getByTestId, queryByTestId} = renderWithTheme(
46
+ <SparklineChart data={[]} testID="empty-sparkline" />
47
+ );
48
+
49
+ assert.exists(getByTestId("empty-sparkline"));
50
+ assert.notExists(queryByTestId("empty-sparkline.current"));
51
+ assert.notExists(queryByTestId("empty-sparkline.comparison"));
52
+ });
53
+
54
+ it("is available from the package root lazy boundary", () => {
55
+ assert.isFunction(RootSparklineChart);
56
+ });
57
+
58
+ it("redraws from the measured container width", () => {
59
+ const {getByTestId} = renderWithTheme(
60
+ <SparklineChart comparisonData={COMPARISON} data={CURRENT} testID="measured-sparkline" />
61
+ );
62
+
63
+ fireEvent(getByTestId("measured-sparkline"), "layout", {
64
+ nativeEvent: {layout: {height: 48, width: 80}},
65
+ });
66
+
67
+ assert.exists(getByTestId("measured-sparkline.current"));
68
+ });
69
+ });