@terreno/ui 57.8.0 → 57.9.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/AreaChart.d.ts +1 -1
- package/dist/AreaChart.d.ts.map +1 -1
- package/dist/AreaChart.js +70 -22
- package/dist/AreaChart.js.map +1 -1
- package/dist/BarChart.d.ts.map +1 -1
- package/dist/BarChart.js +55 -13
- package/dist/BarChart.js.map +1 -1
- package/dist/ChartCard.d.ts +5 -0
- package/dist/ChartCard.d.ts.map +1 -0
- package/dist/ChartCard.js +18 -0
- package/dist/ChartCard.js.map +1 -0
- package/dist/Common.d.ts +79 -5
- package/dist/Common.d.ts.map +1 -1
- package/dist/Common.js.map +1 -1
- package/dist/DashboardGrid.d.ts +6 -2
- package/dist/DashboardGrid.d.ts.map +1 -1
- package/dist/DashboardGrid.js +43 -10
- package/dist/DashboardGrid.js.map +1 -1
- package/dist/DonutChart.d.ts.map +1 -1
- package/dist/DonutChart.js +20 -10
- package/dist/DonutChart.js.map +1 -1
- package/dist/DropdownMenuItem.d.ts +16 -0
- package/dist/DropdownMenuItem.d.ts.map +1 -0
- package/dist/DropdownMenuItem.js +11 -0
- package/dist/DropdownMenuItem.js.map +1 -0
- package/dist/FilePickerButton.d.ts.map +1 -1
- package/dist/FilePickerButton.js +70 -10
- package/dist/FilePickerButton.js.map +1 -1
- package/dist/GPTChat.d.ts.map +1 -1
- package/dist/GPTChat.js +103 -14
- package/dist/GPTChat.js.map +1 -1
- package/dist/LineChart.d.ts +1 -1
- package/dist/LineChart.d.ts.map +1 -1
- package/dist/LineChart.js +62 -21
- package/dist/LineChart.js.map +1 -1
- package/dist/Scorecard.d.ts +5 -0
- package/dist/Scorecard.d.ts.map +1 -0
- package/dist/Scorecard.js +11 -0
- package/dist/Scorecard.js.map +1 -0
- package/dist/SparklineChart.d.ts +5 -0
- package/dist/SparklineChart.d.ts.map +1 -0
- package/dist/SparklineChart.js +35 -0
- package/dist/SparklineChart.js.map +1 -0
- package/dist/TextField.d.ts.map +1 -1
- package/dist/TextField.js +12 -4
- package/dist/TextField.js.map +1 -1
- package/dist/ToastNotifications.d.ts.map +1 -1
- package/dist/ToastNotifications.js +29 -6
- package/dist/ToastNotifications.js.map +1 -1
- package/dist/WebDropdownMenu.d.ts +2 -1
- package/dist/WebDropdownMenu.d.ts.map +1 -1
- package/dist/WebDropdownMenu.js +2 -6
- package/dist/WebDropdownMenu.js.map +1 -1
- package/dist/charts/ChartFacadeContainer.d.ts +10 -0
- package/dist/charts/ChartFacadeContainer.d.ts.map +1 -0
- package/dist/charts/ChartFacadeContainer.js +10 -0
- package/dist/charts/ChartFacadeContainer.js.map +1 -0
- package/dist/charts/ChartFrame.d.ts +4 -0
- package/dist/charts/ChartFrame.d.ts.map +1 -1
- package/dist/charts/ChartFrame.js +4 -2
- package/dist/charts/ChartFrame.js.map +1 -1
- package/dist/charts/layout.d.ts +9 -2
- package/dist/charts/layout.d.ts.map +1 -1
- package/dist/charts/layout.js +28 -6
- package/dist/charts/layout.js.map +1 -1
- package/dist/charts/scales.d.ts +3 -1
- package/dist/charts/scales.d.ts.map +1 -1
- package/dist/charts/scales.js +7 -7
- package/dist/charts/scales.js.map +1 -1
- package/dist/charts/series.d.ts +15 -0
- package/dist/charts/series.d.ts.map +1 -0
- package/dist/charts/series.js +19 -0
- package/dist/charts/series.js.map +1 -0
- package/dist/charts/types/chartTypes.d.ts +7 -0
- package/dist/charts/types/chartTypes.d.ts.map +1 -1
- package/dist/dashboardGridLayout.d.ts +6 -0
- package/dist/dashboardGridLayout.d.ts.map +1 -1
- package/dist/dashboardGridLayout.js +19 -3
- package/dist/dashboardGridLayout.js.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts +9 -0
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.js +6 -0
- package/dist/lazyBoundaries/heavyOptionalExports.js.map +1 -1
- package/dist/resolveDocumentBodyPortalTarget.d.ts +7 -0
- package/dist/resolveDocumentBodyPortalTarget.d.ts.map +1 -0
- package/dist/resolveDocumentBodyPortalTarget.js +12 -0
- package/dist/resolveDocumentBodyPortalTarget.js.map +1 -0
- package/package.json +1 -1
- package/src/AIRequestExplorer.test.tsx +2 -2
- package/src/AreaChart.test.tsx +81 -0
- package/src/AreaChart.tsx +171 -62
- package/src/BarChart.test.tsx +119 -0
- package/src/BarChart.tsx +94 -20
- package/src/ChartCard.test.tsx +79 -0
- package/src/ChartCard.tsx +80 -0
- package/src/Common.ts +69 -2
- package/src/DashboardGrid.test.tsx +98 -1
- package/src/DashboardGrid.tsx +71 -20
- package/src/DonutChart.test.tsx +73 -0
- package/src/DonutChart.tsx +87 -14
- package/src/DropdownMenuItem.tsx +48 -0
- package/src/FilePickerButton.test.tsx +60 -6
- package/src/FilePickerButton.tsx +116 -28
- package/src/GPTChat.test.tsx +133 -6
- package/src/GPTChat.tsx +196 -38
- package/src/LineChart.test.tsx +104 -0
- package/src/LineChart.tsx +154 -58
- package/src/Scorecard.test.tsx +65 -0
- package/src/Scorecard.tsx +45 -0
- package/src/SparklineChart.test.tsx +69 -0
- package/src/SparklineChart.tsx +85 -0
- package/src/TextField.test.tsx +16 -0
- package/src/TextField.tsx +13 -3
- package/src/ToastNotifications.tsx +33 -6
- package/src/ToastNotifications.webModal.test.tsx +285 -0
- package/src/WebDropdownMenu.tsx +3 -7
- package/src/bunSetup.ts +1 -0
- package/src/charts/ChartFacadeContainer.tsx +35 -0
- package/src/charts/ChartFrame.tsx +39 -1
- package/src/charts/layout.test.ts +22 -1
- package/src/charts/layout.ts +37 -4
- package/src/charts/scales.test.ts +33 -0
- package/src/charts/scales.ts +10 -6
- package/src/charts/series.ts +43 -0
- package/src/charts/types/chartTypes.ts +8 -0
- package/src/dashboardGridLayout.test.ts +59 -1
- package/src/dashboardGridLayout.ts +30 -3
- package/src/index.tsx +6 -0
- package/src/lazyBoundaries/heavyOptionalExports.tsx +21 -0
- package/src/resolveDocumentBodyPortalTarget.ts +11 -0
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import {describe, expect, it} from "bun:test";
|
|
2
2
|
import {act, fireEvent} from "@testing-library/react-native";
|
|
3
|
+
import {assert} from "chai";
|
|
4
|
+
import {memo} from "react";
|
|
3
5
|
|
|
4
6
|
import {Box} from "./Box";
|
|
5
7
|
import {getSpacing} from "./Common";
|
|
6
|
-
import {DashboardGrid} from "./DashboardGrid";
|
|
8
|
+
import {DashboardGrid, DashboardGridItem} from "./DashboardGrid";
|
|
7
9
|
import {getDashboardCellWidth} from "./dashboardGridLayout";
|
|
10
|
+
import {sharedResponsiveBreakpointStore} from "./ResponsiveBreakpoint";
|
|
8
11
|
import {renderWithTheme} from "./test-utils";
|
|
9
12
|
|
|
13
|
+
const MemoDashboardGridItem = memo(DashboardGridItem);
|
|
14
|
+
|
|
10
15
|
describe("DashboardGrid", () => {
|
|
11
16
|
it("keeps child testIDs", () => {
|
|
12
17
|
const {getByTestId} = renderWithTheme(
|
|
@@ -72,4 +77,96 @@ describe("DashboardGrid", () => {
|
|
|
72
77
|
width: expectedWidth,
|
|
73
78
|
});
|
|
74
79
|
});
|
|
80
|
+
|
|
81
|
+
it("lets an item span responsive column units without changing plain children", async () => {
|
|
82
|
+
const {getByTestId} = renderWithTheme(
|
|
83
|
+
<DashboardGrid columns={{lg: 4, md: 4, sm: 4}} gap={4} testID="grid">
|
|
84
|
+
<DashboardGridItem span={{lg: 2, md: 2, sm: 2}} testID="wide-item">
|
|
85
|
+
<Box>Wide</Box>
|
|
86
|
+
</DashboardGridItem>
|
|
87
|
+
<Box testID="plain-item">Plain</Box>
|
|
88
|
+
</DashboardGrid>
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
await act(async () => {
|
|
92
|
+
fireEvent(getByTestId("grid"), "layout", {
|
|
93
|
+
nativeEvent: {layout: {height: 80, width: 332}},
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const baseWidth = getDashboardCellWidth({
|
|
98
|
+
columnCount: 4,
|
|
99
|
+
gapPx: getSpacing(4),
|
|
100
|
+
rowWidth: 332,
|
|
101
|
+
});
|
|
102
|
+
assert.include(getByTestId("grid.cell.0").props.style, {
|
|
103
|
+
width: baseWidth * 2 + getSpacing(4),
|
|
104
|
+
});
|
|
105
|
+
assert.include(getByTestId("grid.cell.1").props.style, {width: baseWidth});
|
|
106
|
+
assert.exists(getByTestId("wide-item"));
|
|
107
|
+
assert.exists(getByTestId("plain-item"));
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("resolves asymmetric spans at each responsive breakpoint", async () => {
|
|
111
|
+
const result = renderWithTheme(
|
|
112
|
+
<DashboardGrid columns={{lg: 4, md: 4, sm: 4}} gap={4} testID="grid">
|
|
113
|
+
<DashboardGridItem span={{lg: 3, md: 2, sm: 1}}>
|
|
114
|
+
<Box>Responsive</Box>
|
|
115
|
+
</DashboardGridItem>
|
|
116
|
+
</DashboardGrid>
|
|
117
|
+
);
|
|
118
|
+
await act(async () => {
|
|
119
|
+
fireEvent(result.getByTestId("grid"), "layout", {
|
|
120
|
+
nativeEvent: {layout: {height: 80, width: 332}},
|
|
121
|
+
});
|
|
122
|
+
sharedResponsiveBreakpointStore.updateWidth(319);
|
|
123
|
+
});
|
|
124
|
+
const baseWidth = getDashboardCellWidth({
|
|
125
|
+
columnCount: 4,
|
|
126
|
+
gapPx: getSpacing(4),
|
|
127
|
+
rowWidth: 332,
|
|
128
|
+
});
|
|
129
|
+
assert.equal(result.getByTestId("grid.cell.0").props.style.width, baseWidth);
|
|
130
|
+
|
|
131
|
+
act((): void => {
|
|
132
|
+
sharedResponsiveBreakpointStore.updateWidth(375);
|
|
133
|
+
});
|
|
134
|
+
assert.equal(
|
|
135
|
+
result.getByTestId("grid.cell.0").props.style.width,
|
|
136
|
+
baseWidth * 2 + getSpacing(4)
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
act((): void => {
|
|
140
|
+
sharedResponsiveBreakpointStore.updateWidth(600);
|
|
141
|
+
});
|
|
142
|
+
assert.equal(
|
|
143
|
+
result.getByTestId("grid.cell.0").props.style.width,
|
|
144
|
+
baseWidth * 3 + getSpacing(4) * 2
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
result.unmount();
|
|
148
|
+
sharedResponsiveBreakpointStore.updateWidth(375);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("keeps span metadata through React memo wrappers", async () => {
|
|
152
|
+
const {getByTestId} = renderWithTheme(
|
|
153
|
+
<DashboardGrid columns={{lg: 4, md: 4, sm: 4}} gap={4} testID="grid">
|
|
154
|
+
<MemoDashboardGridItem span={{lg: 2, md: 2, sm: 2}}>
|
|
155
|
+
<Box>Memo item</Box>
|
|
156
|
+
</MemoDashboardGridItem>
|
|
157
|
+
</DashboardGrid>
|
|
158
|
+
);
|
|
159
|
+
await act(async () => {
|
|
160
|
+
fireEvent(getByTestId("grid"), "layout", {
|
|
161
|
+
nativeEvent: {layout: {height: 80, width: 332}},
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
const baseWidth = getDashboardCellWidth({
|
|
165
|
+
columnCount: 4,
|
|
166
|
+
gapPx: getSpacing(4),
|
|
167
|
+
rowWidth: 332,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
assert.equal(getByTestId("grid.cell.0").props.style.width, baseWidth * 2 + getSpacing(4));
|
|
171
|
+
});
|
|
75
172
|
});
|
package/src/DashboardGrid.tsx
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import type {FC, ReactNode} from "react";
|
|
2
|
-
import {Children, useCallback, useState} from "react";
|
|
2
|
+
import {Children, isValidElement, useCallback, useState} from "react";
|
|
3
3
|
|
|
4
4
|
import {Box} from "./Box";
|
|
5
|
-
import type {DashboardGridProps, LayoutChangeEvent} from "./Common";
|
|
5
|
+
import type {DashboardGridItemProps, DashboardGridProps, LayoutChangeEvent} from "./Common";
|
|
6
6
|
import {getSpacing} from "./Common";
|
|
7
|
-
import {
|
|
7
|
+
import {getDashboardSpanCellBoxStyle} from "./dashboardGridLayout";
|
|
8
8
|
import {useResponsiveBreakpoint} from "./ResponsiveBreakpoint";
|
|
9
9
|
import {resolveTestID} from "./testing/resolveTestId";
|
|
10
10
|
|
|
11
11
|
const DEFAULT_COLUMNS = {lg: 3, md: 2, sm: 1};
|
|
12
12
|
|
|
13
|
+
type MarkedDashboardGridItem = FC<DashboardGridItemProps> & {
|
|
14
|
+
isDashboardGridItem?: boolean;
|
|
15
|
+
};
|
|
16
|
+
|
|
13
17
|
const resolveColumnCount = ({
|
|
14
18
|
breakpoint,
|
|
15
19
|
columns,
|
|
@@ -26,6 +30,44 @@ const resolveColumnCount = ({
|
|
|
26
30
|
return columns.sm;
|
|
27
31
|
};
|
|
28
32
|
|
|
33
|
+
const resolveItemSpan = ({
|
|
34
|
+
breakpoint,
|
|
35
|
+
span,
|
|
36
|
+
}: {
|
|
37
|
+
breakpoint: "xs" | "sm" | "md" | "lg" | "xl";
|
|
38
|
+
span?: DashboardGridItemProps["span"];
|
|
39
|
+
}): number => {
|
|
40
|
+
if (!span) {
|
|
41
|
+
return 1;
|
|
42
|
+
}
|
|
43
|
+
if (breakpoint === "lg" || breakpoint === "xl") {
|
|
44
|
+
return span.lg ?? span.md ?? span.sm ?? 1;
|
|
45
|
+
}
|
|
46
|
+
if (breakpoint === "md") {
|
|
47
|
+
return span.md ?? span.sm ?? 1;
|
|
48
|
+
}
|
|
49
|
+
return span.sm ?? 1;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const DashboardGridItem: MarkedDashboardGridItem = ({children, testID}) => {
|
|
53
|
+
return (
|
|
54
|
+
<Box minWidth={0} testID={testID} width="100%">
|
|
55
|
+
{children}
|
|
56
|
+
</Box>
|
|
57
|
+
);
|
|
58
|
+
};
|
|
59
|
+
DashboardGridItem.isDashboardGridItem = true;
|
|
60
|
+
|
|
61
|
+
const isDashboardGridItemType = (type: unknown): boolean => {
|
|
62
|
+
if (typeof type === "function") {
|
|
63
|
+
return (type as MarkedDashboardGridItem).isDashboardGridItem === true;
|
|
64
|
+
}
|
|
65
|
+
if (type && typeof type === "object" && "type" in type) {
|
|
66
|
+
return isDashboardGridItemType(type.type);
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
};
|
|
70
|
+
|
|
29
71
|
export const DashboardGrid: FC<DashboardGridProps> = ({
|
|
30
72
|
children,
|
|
31
73
|
columns = DEFAULT_COLUMNS,
|
|
@@ -36,11 +78,7 @@ export const DashboardGrid: FC<DashboardGridProps> = ({
|
|
|
36
78
|
const breakpoint = useResponsiveBreakpoint({enabled: true});
|
|
37
79
|
const [rowWidth, setRowWidth] = useState(0);
|
|
38
80
|
const columnCount = Math.max(resolveColumnCount({breakpoint, columns}), 1);
|
|
39
|
-
const
|
|
40
|
-
columnCount,
|
|
41
|
-
gapPx: getSpacing(gap),
|
|
42
|
-
rowWidth,
|
|
43
|
-
});
|
|
81
|
+
const gapPx = getSpacing(gap);
|
|
44
82
|
|
|
45
83
|
const handleLayout = useCallback((event: LayoutChangeEvent): void => {
|
|
46
84
|
const nextWidth = event.nativeEvent.layout.width;
|
|
@@ -51,19 +89,32 @@ export const DashboardGrid: FC<DashboardGridProps> = ({
|
|
|
51
89
|
|
|
52
90
|
return (
|
|
53
91
|
<Box direction="row" gap={gap} onLayout={handleLayout} testID={testID} width="100%" wrap>
|
|
54
|
-
{renderedChildren.map((child: ReactNode, index: number) =>
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
92
|
+
{renderedChildren.map((child: ReactNode, index: number) => {
|
|
93
|
+
const itemSpan =
|
|
94
|
+
isValidElement<DashboardGridItemProps>(child) && isDashboardGridItemType(child.type)
|
|
95
|
+
? child.props.span
|
|
96
|
+
: undefined;
|
|
97
|
+
const span = resolveItemSpan({breakpoint, span: itemSpan});
|
|
98
|
+
const cellStyle = getDashboardSpanCellBoxStyle({
|
|
99
|
+
columnCount,
|
|
100
|
+
gapPx,
|
|
101
|
+
rowWidth,
|
|
102
|
+
span,
|
|
103
|
+
});
|
|
104
|
+
return (
|
|
105
|
+
<Box
|
|
106
|
+
dangerouslySetInlineStyle={{
|
|
107
|
+
__style: cellStyle,
|
|
108
|
+
}}
|
|
109
|
+
key={`dashboard-cell-${index}`}
|
|
110
|
+
testID={resolveTestID(testID, `cell.${index}`)}
|
|
111
|
+
>
|
|
112
|
+
{child}
|
|
113
|
+
</Box>
|
|
114
|
+
);
|
|
115
|
+
})}
|
|
65
116
|
</Box>
|
|
66
117
|
);
|
|
67
118
|
};
|
|
68
119
|
|
|
69
|
-
export type {DashboardGridProps} from "./Common";
|
|
120
|
+
export type {DashboardGridItemProps, DashboardGridProps} from "./Common";
|
package/src/DonutChart.test.tsx
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {describe, expect, it, spyOn} from "bun:test";
|
|
2
2
|
import {act, fireEvent} from "@testing-library/react-native";
|
|
3
|
+
import {assert} from "chai";
|
|
3
4
|
import {Linking} from "react-native";
|
|
4
5
|
|
|
5
6
|
import {DonutChart} from "./DonutChart";
|
|
@@ -11,6 +12,78 @@ const POINTS = [
|
|
|
11
12
|
];
|
|
12
13
|
|
|
13
14
|
describe("DonutChart", () => {
|
|
15
|
+
it("renders center copy and default percent shares in the legend", () => {
|
|
16
|
+
const {getAllByText, getByTestId, getByText} = renderWithTheme(
|
|
17
|
+
<DonutChart centerTitle="Cost" centerValue="$1.15K" data={POINTS} testID="chart" />
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
assert.exists(getByText("$1.15K"));
|
|
21
|
+
assert.exists(getByText("Cost"));
|
|
22
|
+
assert.exists(getByTestId("chart.center"));
|
|
23
|
+
assert.exists(getByTestId("chart.center.value"));
|
|
24
|
+
assert.exists(getByTestId("chart.center.title"));
|
|
25
|
+
assert.equal(getByTestId("chart.share.0").props.children, "50%");
|
|
26
|
+
assert.lengthOf(getAllByText("50%"), 2);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("supports custom share formatting and a single 100% slice", () => {
|
|
30
|
+
const {getByTestId, getByText} = renderWithTheme(
|
|
31
|
+
<DonutChart
|
|
32
|
+
centerTitle="Conversions"
|
|
33
|
+
centerValue="7.00"
|
|
34
|
+
data={[{label: "Mobile phones", value: 7}]}
|
|
35
|
+
formatShare={(value, total): string => `${value} of ${total}`}
|
|
36
|
+
testID="chart"
|
|
37
|
+
/>
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
assert.isNotEmpty(getByTestId("chart.slice.0").props.d);
|
|
41
|
+
assert.exists(getByText("7 of 7"));
|
|
42
|
+
assert.exists(getByText("7.00"));
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("formats one full slice as 100% and zero totals as 0%", () => {
|
|
46
|
+
const full = renderWithTheme(
|
|
47
|
+
<DonutChart data={[{label: "Mobile phones", value: 7}]} testID="full-chart" />
|
|
48
|
+
);
|
|
49
|
+
assert.equal(full.getByTestId("full-chart.share.0").props.children, "100%");
|
|
50
|
+
|
|
51
|
+
const emptyTotal = renderWithTheme(
|
|
52
|
+
<DonutChart
|
|
53
|
+
data={[
|
|
54
|
+
{label: "A", value: 0},
|
|
55
|
+
{label: "B", value: -1},
|
|
56
|
+
]}
|
|
57
|
+
testID="zero-chart"
|
|
58
|
+
/>
|
|
59
|
+
);
|
|
60
|
+
assert.equal(emptyTotal.getByTestId("zero-chart.share.0").props.children, "0%");
|
|
61
|
+
assert.equal(emptyTotal.getByTestId("zero-chart.share.1").props.children, "0%");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("renders chart-card header shortcuts and omits them without title", async (): Promise<void> => {
|
|
65
|
+
let pressCount = 0;
|
|
66
|
+
const titled = renderWithTheme(
|
|
67
|
+
<DonutChart
|
|
68
|
+
data={POINTS}
|
|
69
|
+
onPeriodPress={(): void => {
|
|
70
|
+
pressCount += 1;
|
|
71
|
+
}}
|
|
72
|
+
periodLabel="Last 30 days"
|
|
73
|
+
testID="chart"
|
|
74
|
+
title="Cost by Device"
|
|
75
|
+
/>
|
|
76
|
+
);
|
|
77
|
+
assert.exists(titled.getByText("Cost by Device"));
|
|
78
|
+
assert.exists(titled.getByText("Last 30 days"));
|
|
79
|
+
fireEvent.press(titled.getByTestId("chart.card.period-clickable"));
|
|
80
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
81
|
+
assert.equal(pressCount, 1);
|
|
82
|
+
|
|
83
|
+
const plain = renderWithTheme(<DonutChart data={POINTS} testID="plain-chart" />);
|
|
84
|
+
assert.notExists(plain.queryByTestId("plain-chart.card"));
|
|
85
|
+
});
|
|
86
|
+
|
|
14
87
|
it("renders one mark testID per slice", () => {
|
|
15
88
|
const {getByTestId, queryByTestId} = renderWithTheme(
|
|
16
89
|
<DonutChart data={POINTS} testID="chart" />
|
package/src/DonutChart.tsx
CHANGED
|
@@ -4,11 +4,13 @@ import {Path, Svg} from "react-native-svg";
|
|
|
4
4
|
|
|
5
5
|
import {Box} from "./Box";
|
|
6
6
|
import type {DonutChartProps, LayoutChangeEvent} from "./Common";
|
|
7
|
+
import {ChartFacadeContainer} from "./charts/ChartFacadeContainer";
|
|
7
8
|
import {ChartFrame} from "./charts/ChartFrame";
|
|
8
9
|
import {getDonutSize} from "./charts/layout";
|
|
9
10
|
import {getDonutSliceAngles, getDonutSliceHitCenter, getDonutSlicePath} from "./charts/paths";
|
|
10
11
|
import {getChartPaint} from "./charts/theme";
|
|
11
12
|
import type {ChartPoint} from "./charts/types/chartTypes";
|
|
13
|
+
import {Heading} from "./Heading";
|
|
12
14
|
import {Text} from "./Text";
|
|
13
15
|
import {useTheme} from "./Theme";
|
|
14
16
|
import {resolveTestID} from "./testing/resolveTestId";
|
|
@@ -16,6 +18,13 @@ import {resolveTestID} from "./testing/resolveTestId";
|
|
|
16
18
|
const DEFAULT_SIZE = 220;
|
|
17
19
|
const MARK_HIT_SIZE = 24;
|
|
18
20
|
|
|
21
|
+
const formatDefaultShare = (value: number, total: number): string => {
|
|
22
|
+
if (total <= 0) {
|
|
23
|
+
return "0%";
|
|
24
|
+
}
|
|
25
|
+
return `${Math.round((Math.max(value, 0) / total) * 100)}%`;
|
|
26
|
+
};
|
|
27
|
+
|
|
19
28
|
const formatChartTooltip = ({
|
|
20
29
|
formatValue,
|
|
21
30
|
point,
|
|
@@ -38,12 +47,18 @@ const getSliceAngles = (
|
|
|
38
47
|
|
|
39
48
|
export const DonutChart: FC<DonutChartProps> = ({
|
|
40
49
|
accessibilityLabel,
|
|
50
|
+
centerTitle,
|
|
51
|
+
centerValue,
|
|
41
52
|
data,
|
|
42
53
|
emptyText = "No data",
|
|
54
|
+
formatShare = formatDefaultShare,
|
|
43
55
|
formatValue = String,
|
|
44
56
|
height = DEFAULT_SIZE,
|
|
45
57
|
loading = false,
|
|
58
|
+
onPeriodPress,
|
|
59
|
+
periodLabel,
|
|
46
60
|
testID,
|
|
61
|
+
title,
|
|
47
62
|
}) => {
|
|
48
63
|
const {theme} = useTheme();
|
|
49
64
|
const paint = getChartPaint(theme);
|
|
@@ -66,13 +81,14 @@ export const DonutChart: FC<DonutChartProps> = ({
|
|
|
66
81
|
const outerRadius = Math.max(size / 2 - 8, 1);
|
|
67
82
|
const innerRadius = outerRadius * 0.55;
|
|
68
83
|
const slices = getSliceAngles(data);
|
|
84
|
+
const total = data.reduce((sum, point) => sum + Math.max(point.value, 0), 0);
|
|
69
85
|
const activePoint = activePointIndex === undefined ? undefined : data[activePointIndex];
|
|
70
86
|
const tooltipText = activePoint
|
|
71
87
|
? formatChartTooltip({formatValue, point: activePoint})
|
|
72
88
|
: undefined;
|
|
73
89
|
const summaryLabel = accessibilityLabel ?? "Donut chart";
|
|
74
90
|
|
|
75
|
-
|
|
91
|
+
const chart = (
|
|
76
92
|
<ChartFrame
|
|
77
93
|
accessibilityLabel={summaryLabel}
|
|
78
94
|
emptyText={emptyText}
|
|
@@ -100,10 +116,39 @@ export const DonutChart: FC<DonutChartProps> = ({
|
|
|
100
116
|
})}
|
|
101
117
|
fill={slice.point.color ?? paint.slices[index % paint.slices.length]}
|
|
102
118
|
key={`slice-${slice.point.label}`}
|
|
119
|
+
testID={resolveTestID(testID, `slice.${index}`)}
|
|
103
120
|
transform={`translate(${center}, ${center})`}
|
|
104
121
|
/>
|
|
105
122
|
))}
|
|
106
123
|
</Svg>
|
|
124
|
+
{centerValue || centerTitle ? (
|
|
125
|
+
<Box
|
|
126
|
+
alignItems="center"
|
|
127
|
+
dangerouslySetInlineStyle={{__style: {left: 0, top: 0}}}
|
|
128
|
+
height={size}
|
|
129
|
+
justifyContent="center"
|
|
130
|
+
position="absolute"
|
|
131
|
+
testID={resolveTestID(testID, "center")}
|
|
132
|
+
width={size}
|
|
133
|
+
>
|
|
134
|
+
{centerValue ? (
|
|
135
|
+
<Heading align="center" size="sm" testID={resolveTestID(testID, "center.value")}>
|
|
136
|
+
{centerValue}
|
|
137
|
+
</Heading>
|
|
138
|
+
) : null}
|
|
139
|
+
{centerTitle ? (
|
|
140
|
+
<Text
|
|
141
|
+
align="center"
|
|
142
|
+
color="secondaryDark"
|
|
143
|
+
size="sm"
|
|
144
|
+
skipLinking
|
|
145
|
+
testID={resolveTestID(testID, "center.title")}
|
|
146
|
+
>
|
|
147
|
+
{centerTitle}
|
|
148
|
+
</Text>
|
|
149
|
+
) : null}
|
|
150
|
+
</Box>
|
|
151
|
+
) : null}
|
|
107
152
|
{slices.map((slice, index) => {
|
|
108
153
|
const onPress = (): void => {
|
|
109
154
|
handleMarkPress(index);
|
|
@@ -134,19 +179,36 @@ export const DonutChart: FC<DonutChartProps> = ({
|
|
|
134
179
|
</Box>
|
|
135
180
|
<Box direction="column" gap={1} padding={2}>
|
|
136
181
|
{data.map((point, index) => (
|
|
137
|
-
<Box
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
182
|
+
<Box
|
|
183
|
+
direction="row"
|
|
184
|
+
gap={2}
|
|
185
|
+
justifyContent="between"
|
|
186
|
+
key={`legend-${point.label}`}
|
|
187
|
+
minWidth={0}
|
|
188
|
+
width="100%"
|
|
189
|
+
>
|
|
190
|
+
<Box alignItems="center" direction="row" gap={2} minWidth={0}>
|
|
191
|
+
<Box
|
|
192
|
+
dangerouslySetInlineStyle={{
|
|
193
|
+
__style: {
|
|
194
|
+
backgroundColor: point.color ?? paint.slices[index % paint.slices.length],
|
|
195
|
+
height: 12,
|
|
196
|
+
width: 12,
|
|
197
|
+
},
|
|
198
|
+
}}
|
|
199
|
+
testID={resolveTestID(testID, `swatch.${index}`)}
|
|
200
|
+
/>
|
|
201
|
+
<Text size="sm" skipLinking truncate>
|
|
202
|
+
{point.label}
|
|
203
|
+
</Text>
|
|
204
|
+
</Box>
|
|
205
|
+
<Text
|
|
206
|
+
color="secondaryDark"
|
|
207
|
+
size="sm"
|
|
208
|
+
skipLinking
|
|
209
|
+
testID={resolveTestID(testID, `share.${index}`)}
|
|
210
|
+
>
|
|
211
|
+
{formatShare(point.value, total)}
|
|
150
212
|
</Text>
|
|
151
213
|
</Box>
|
|
152
214
|
))}
|
|
@@ -154,6 +216,17 @@ export const DonutChart: FC<DonutChartProps> = ({
|
|
|
154
216
|
</Box>
|
|
155
217
|
</ChartFrame>
|
|
156
218
|
);
|
|
219
|
+
|
|
220
|
+
return (
|
|
221
|
+
<ChartFacadeContainer
|
|
222
|
+
onPeriodPress={onPeriodPress}
|
|
223
|
+
periodLabel={periodLabel}
|
|
224
|
+
testID={testID}
|
|
225
|
+
title={title}
|
|
226
|
+
>
|
|
227
|
+
{chart}
|
|
228
|
+
</ChartFacadeContainer>
|
|
229
|
+
);
|
|
157
230
|
};
|
|
158
231
|
|
|
159
232
|
export type {DonutChartProps} from "./Common";
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type React from "react";
|
|
2
|
+
import {Pressable} from "react-native";
|
|
3
|
+
|
|
4
|
+
import {Box} from "./Box";
|
|
5
|
+
import type {IconName, IconProps, TextColor} from "./Common";
|
|
6
|
+
import {Icon} from "./Icon";
|
|
7
|
+
import {Text} from "./Text";
|
|
8
|
+
|
|
9
|
+
export interface DropdownMenuItemProps {
|
|
10
|
+
accessibilityLabel?: string;
|
|
11
|
+
/** Text and icon color. Defaults to the dark secondary text color. */
|
|
12
|
+
color?: TextColor;
|
|
13
|
+
iconName: IconName;
|
|
14
|
+
/** Icon style. Defaults to the outlined `regular` glyph. */
|
|
15
|
+
iconType?: IconProps["type"];
|
|
16
|
+
label: string;
|
|
17
|
+
onClick: () => void;
|
|
18
|
+
testID?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A single icon + label row for an action menu rendered inside a `DropdownPanel`. */
|
|
22
|
+
export const DropdownMenuItem = ({
|
|
23
|
+
accessibilityLabel,
|
|
24
|
+
color = "secondaryDark",
|
|
25
|
+
iconName,
|
|
26
|
+
iconType = "regular",
|
|
27
|
+
label,
|
|
28
|
+
onClick,
|
|
29
|
+
testID,
|
|
30
|
+
}: DropdownMenuItemProps): React.ReactElement => {
|
|
31
|
+
// A plain Pressable keeps onClick synchronous with the press, which web file pickers need.
|
|
32
|
+
return (
|
|
33
|
+
<Pressable
|
|
34
|
+
accessibilityHint={`Press to ${label.toLowerCase()}`}
|
|
35
|
+
accessibilityLabel={accessibilityLabel ?? label}
|
|
36
|
+
accessibilityRole="menuitem"
|
|
37
|
+
onPress={onClick}
|
|
38
|
+
testID={testID}
|
|
39
|
+
>
|
|
40
|
+
<Box alignItems="center" direction="row" gap={2} paddingX={2} paddingY={2} rounding="md">
|
|
41
|
+
<Icon color={color} iconName={iconName} size="sm" type={iconType} />
|
|
42
|
+
<Text color={color} size="sm">
|
|
43
|
+
{label}
|
|
44
|
+
</Text>
|
|
45
|
+
</Box>
|
|
46
|
+
</Pressable>
|
|
47
|
+
);
|
|
48
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {afterAll, beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
2
|
import {fireEvent, waitFor} from "@testing-library/react-native";
|
|
3
|
-
import {Pressable} from "react-native";
|
|
3
|
+
import {Platform, Pressable} from "react-native";
|
|
4
4
|
|
|
5
5
|
// Override the IconButton mock so the onClick fires when pressed.
|
|
6
6
|
mock.module("./IconButton", () => ({
|
|
@@ -83,17 +83,20 @@ describe("FilePickerButton", () => {
|
|
|
83
83
|
expect(getByTestId("file-picker-button").props.accessibilityState).toEqual({disabled: true});
|
|
84
84
|
});
|
|
85
85
|
|
|
86
|
-
it("opens
|
|
86
|
+
it("opens an anchored dropdown with both options instead of a modal", () => {
|
|
87
87
|
const {getByText} = openModal();
|
|
88
|
-
expect(getByText("Attach")).toBeTruthy();
|
|
89
88
|
expect(getByText("Photo Library")).toBeTruthy();
|
|
90
89
|
expect(getByText("Document")).toBeTruthy();
|
|
91
90
|
});
|
|
92
91
|
|
|
93
|
-
it("
|
|
94
|
-
const {getByLabelText} = renderWithTheme(
|
|
92
|
+
it("closes the dropdown from the backdrop", () => {
|
|
93
|
+
const {getByLabelText, getByTestId, queryByText} = renderWithTheme(
|
|
94
|
+
<FilePickerButton onFilesSelected={() => {}} />
|
|
95
|
+
);
|
|
95
96
|
fireEvent.press(getByLabelText("Attach file"));
|
|
96
|
-
|
|
97
|
+
expect(queryByText("Document")).toBeTruthy();
|
|
98
|
+
fireEvent.press(getByTestId("file-picker-button-menu.backdrop"));
|
|
99
|
+
expect(queryByText("Document")).toBeNull();
|
|
97
100
|
});
|
|
98
101
|
|
|
99
102
|
it("maps picked images and applies default mime type and name", async () => {
|
|
@@ -164,4 +167,55 @@ describe("FilePickerButton", () => {
|
|
|
164
167
|
await waitFor(() => expect(getDocumentAsync).toHaveBeenCalledTimes(2));
|
|
165
168
|
expect(empty.files).toHaveLength(0);
|
|
166
169
|
});
|
|
170
|
+
|
|
171
|
+
it("opens a browser file input for documents on web and returns data URLs", async () => {
|
|
172
|
+
const listeners: Record<string, () => void | Promise<void>> = {};
|
|
173
|
+
const file = {name: "notes.txt", type: "text/plain"};
|
|
174
|
+
const input = {
|
|
175
|
+
accept: "",
|
|
176
|
+
addEventListener: (event: string, handler: () => void): void => {
|
|
177
|
+
listeners[event] = handler;
|
|
178
|
+
},
|
|
179
|
+
click: mock(() => {}),
|
|
180
|
+
files: [file],
|
|
181
|
+
multiple: false,
|
|
182
|
+
remove: mock(() => {}),
|
|
183
|
+
style: {display: ""},
|
|
184
|
+
type: "",
|
|
185
|
+
};
|
|
186
|
+
const globals = globalThis as unknown as Record<string, unknown>;
|
|
187
|
+
const originalDocument = globals.document;
|
|
188
|
+
const originalFileReader = globals.FileReader;
|
|
189
|
+
const originalOS = Platform.OS;
|
|
190
|
+
|
|
191
|
+
const {files, getByText} = openModal();
|
|
192
|
+
globals.document = {
|
|
193
|
+
body: {appendChild: mock(() => {})},
|
|
194
|
+
createElement: (): typeof input => input,
|
|
195
|
+
};
|
|
196
|
+
globals.FileReader = class {
|
|
197
|
+
onerror: (() => void) | null = null;
|
|
198
|
+
onload: (() => void) | null = null;
|
|
199
|
+
result = "data:text/plain;base64,aGk=";
|
|
200
|
+
readAsDataURL(): void {
|
|
201
|
+
this.onload?.();
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
Platform.OS = "web";
|
|
205
|
+
try {
|
|
206
|
+
fireEvent.press(getByText("Document"));
|
|
207
|
+
expect(input.click).toHaveBeenCalledTimes(1);
|
|
208
|
+
expect(input.accept).toBe("application/pdf,text/plain,text/csv,application/json");
|
|
209
|
+
await listeners.change?.();
|
|
210
|
+
await waitFor(() => expect(files).toHaveLength(1));
|
|
211
|
+
expect(files[0]).toEqual([
|
|
212
|
+
{mimeType: "text/plain", name: "notes.txt", uri: "data:text/plain;base64,aGk="},
|
|
213
|
+
]);
|
|
214
|
+
expect(getDocumentAsync).not.toHaveBeenCalled();
|
|
215
|
+
} finally {
|
|
216
|
+
Platform.OS = originalOS;
|
|
217
|
+
globals.document = originalDocument;
|
|
218
|
+
globals.FileReader = originalFileReader;
|
|
219
|
+
}
|
|
220
|
+
});
|
|
167
221
|
});
|