@terreno/ui 57.7.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/Avatar.js +2 -2
- package/dist/Avatar.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/Button.js +2 -2
- package/dist/Button.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 +128 -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/DateTimeActionSheet.js +2 -2
- package/dist/DateTimeActionSheet.js.map +1 -1
- package/dist/DateTimeField.js +3 -3
- package/dist/DateTimeField.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/DropdownPanel.d.ts.map +1 -1
- package/dist/DropdownPanel.js +1 -1
- package/dist/DropdownPanel.js.map +1 -1
- 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/HeightField.js +2 -2
- package/dist/HeightField.js.map +1 -1
- package/dist/IconButton.d.ts.map +1 -1
- package/dist/IconButton.js +9 -6
- package/dist/IconButton.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/MediaQuery.d.ts +10 -0
- package/dist/MediaQuery.d.ts.map +1 -1
- package/dist/MediaQuery.js +13 -1
- package/dist/MediaQuery.js.map +1 -1
- package/dist/MultiselectField.js +2 -2
- package/dist/MultiselectField.js.map +1 -1
- package/dist/RadioField.js +2 -2
- package/dist/RadioField.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/SplitPage.d.ts +1 -1
- package/dist/SplitPage.d.ts.map +1 -1
- package/dist/SplitPage.js +126 -21
- package/dist/SplitPage.js.map +1 -1
- 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/UnifiedAddressAutoComplete.js +2 -2
- package/dist/UnifiedAddressAutoComplete.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/fieldElements/FieldTitle.js +2 -2
- package/dist/fieldElements/FieldTitle.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/Avatar.tsx +2 -2
- package/src/BarChart.test.tsx +119 -0
- package/src/BarChart.tsx +94 -20
- package/src/Button.test.tsx +7 -7
- package/src/Button.tsx +2 -2
- package/src/ChartCard.test.tsx +79 -0
- package/src/ChartCard.tsx +80 -0
- package/src/Common.ts +119 -2
- package/src/DashboardGrid.test.tsx +98 -1
- package/src/DashboardGrid.tsx +71 -20
- package/src/DateTimeActionSheet.tsx +4 -4
- package/src/DateTimeField.test.tsx +5 -4
- package/src/DateTimeField.tsx +3 -3
- package/src/DonutChart.test.tsx +73 -0
- package/src/DonutChart.tsx +87 -14
- package/src/DropdownMenuItem.tsx +48 -0
- package/src/DropdownPanel.test.tsx +12 -0
- package/src/DropdownPanel.tsx +8 -1
- 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/HeightField.tsx +2 -2
- package/src/IconButton.test.tsx +63 -0
- package/src/IconButton.tsx +10 -5
- package/src/LineChart.test.tsx +104 -0
- package/src/LineChart.tsx +154 -58
- package/src/MediaQuery.ts +14 -1
- package/src/MultiselectField.tsx +2 -2
- package/src/RadioField.tsx +2 -2
- 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/SplitPage.test.tsx +449 -11
- package/src/SplitPage.tsx +255 -21
- 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/UnifiedAddressAutoComplete.test.tsx +4 -4
- package/src/UnifiedAddressAutoComplete.tsx +2 -2
- package/src/WebDropdownMenu.tsx +3 -7
- package/src/__snapshots__/DropdownPanel.test.tsx.snap +3 -1
- package/src/bunSetup.ts +3 -1
- 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/fieldElements/FieldTitle.tsx +2 -2
- package/src/index.tsx +6 -0
- package/src/lazyBoundaries/heavyOptionalExports.tsx +21 -0
- package/src/resolveDocumentBodyPortalTarget.ts +11 -0
|
@@ -18,7 +18,7 @@ import {renderWithTheme, setupComponentTest, teardownComponentTest} from "./test
|
|
|
18
18
|
|
|
19
19
|
const setDesktop = () => {
|
|
20
20
|
mock.module("./MediaQuery", () => ({
|
|
21
|
-
|
|
21
|
+
isNarrowViewport: () => false,
|
|
22
22
|
mediaQuery: () => "lg" as const,
|
|
23
23
|
mediaQueryLargerThan: () => true,
|
|
24
24
|
mediaQuerySmallerThan: () => false,
|
|
@@ -27,7 +27,7 @@ const setDesktop = () => {
|
|
|
27
27
|
|
|
28
28
|
const setMobile = () => {
|
|
29
29
|
mock.module("./MediaQuery", () => ({
|
|
30
|
-
|
|
30
|
+
isNarrowViewport: () => true,
|
|
31
31
|
mediaQuery: () => "xs" as const,
|
|
32
32
|
mediaQueryLargerThan: () => false,
|
|
33
33
|
mediaQuerySmallerThan: () => true,
|
|
@@ -35,11 +35,12 @@ const setMobile = () => {
|
|
|
35
35
|
};
|
|
36
36
|
|
|
37
37
|
// Restore MediaQuery to bunSetup defaults after all tests to prevent cross-file pollution.
|
|
38
|
-
// bunSetup mocks:
|
|
38
|
+
// bunSetup mocks: isNarrowViewport → false, mediaQueryLargerThan → false, mediaQuerySmallerThan → false.
|
|
39
39
|
const restoreDefault = () => {
|
|
40
40
|
mock.module("./MediaQuery", () => ({
|
|
41
|
-
|
|
41
|
+
isNarrowViewport: mock(() => false),
|
|
42
42
|
mediaQueryLargerThan: mock(() => false),
|
|
43
|
+
mediaQuerySmallerThan: mock(() => false),
|
|
43
44
|
}));
|
|
44
45
|
};
|
|
45
46
|
|
package/src/DateTimeField.tsx
CHANGED
|
@@ -11,7 +11,7 @@ import {FieldError} from "./fieldElements/FieldError";
|
|
|
11
11
|
import {FieldHelperText} from "./fieldElements/FieldHelperText";
|
|
12
12
|
import {FieldTitle} from "./fieldElements/FieldTitle";
|
|
13
13
|
import {IconButton} from "./IconButton";
|
|
14
|
-
import {
|
|
14
|
+
import {isNarrowViewport} from "./MediaQuery";
|
|
15
15
|
import {SelectField} from "./SelectField";
|
|
16
16
|
import {Text} from "./Text";
|
|
17
17
|
import {useTheme} from "./Theme";
|
|
@@ -454,7 +454,7 @@ export const DateTimeField: FC<DateTimeFieldProps> = ({
|
|
|
454
454
|
|
|
455
455
|
const [parentWidth, setParentWidth] = useState<number | null>(null);
|
|
456
456
|
const parentIsLessThanBreakpointOrIsMobile =
|
|
457
|
-
(parentWidth !== null && parentWidth < breakpoint) ||
|
|
457
|
+
(parentWidth !== null && parentWidth < breakpoint) || isNarrowViewport();
|
|
458
458
|
|
|
459
459
|
// We need to store the pending value in a ref because the state changes don't trigger
|
|
460
460
|
// immediately, so onBlur may use stale values.
|
|
@@ -904,7 +904,7 @@ export const DateTimeField: FC<DateTimeFieldProps> = ({
|
|
|
904
904
|
onRef: (el: TextInput | null, i: number) => (inputRefs.current[i] = el),
|
|
905
905
|
};
|
|
906
906
|
|
|
907
|
-
const isMobile =
|
|
907
|
+
const isMobile = isNarrowViewport();
|
|
908
908
|
const isMobileTimeOnly = isMobile && type === "time";
|
|
909
909
|
const isMobileDatetime = isMobile && type === "datetime";
|
|
910
910
|
const showDateSection = type === "date" || type === "datetime";
|
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
|
+
};
|
|
@@ -295,6 +295,18 @@ describe("DropdownPanel trigger", () => {
|
|
|
295
295
|
expect(getByTestId("f.panel")).toBeTruthy();
|
|
296
296
|
});
|
|
297
297
|
|
|
298
|
+
it("hugs the trigger so the panel anchors to it, not to the surrounding row", () => {
|
|
299
|
+
// The wrapper is what gets measured. A stretched wrapper starts at the edge of the
|
|
300
|
+
// whole row, which anchors the panel far from a trigger aligned anywhere else.
|
|
301
|
+
const {getByTestId} = renderWithTheme(
|
|
302
|
+
<DropdownPanel testID="f">
|
|
303
|
+
<Text>Body</Text>
|
|
304
|
+
</DropdownPanel>
|
|
305
|
+
);
|
|
306
|
+
const wrapper = getByTestId("f").props.children[0];
|
|
307
|
+
expect(wrapper.props.style.alignSelf).toBe("flex-start");
|
|
308
|
+
});
|
|
309
|
+
|
|
298
310
|
it("stretches the trigger wrapper when fullWidth is set", () => {
|
|
299
311
|
const {getByTestId} = renderWithTheme(
|
|
300
312
|
<DropdownPanel fullWidth testID="f">
|
package/src/DropdownPanel.tsx
CHANGED
|
@@ -346,7 +346,14 @@ export const DropdownPanel: FC<DropdownPanelProps> = ({
|
|
|
346
346
|
|
|
347
347
|
return (
|
|
348
348
|
<View style={{position: "relative", width: fullWidth ? "100%" : undefined}} testID={testID}>
|
|
349
|
-
|
|
349
|
+
{/* The wrapper is what gets measured, so it must hug the trigger. A stretched
|
|
350
|
+
wrapper anchors the panel to the start of the whole row instead — which looks
|
|
351
|
+
right only while the trigger happens to sit at that edge. */}
|
|
352
|
+
<View
|
|
353
|
+
collapsable={false}
|
|
354
|
+
ref={triggerRef}
|
|
355
|
+
style={{alignSelf: fullWidth ? "stretch" : "flex-start"}}
|
|
356
|
+
>
|
|
350
357
|
{renderTrigger ? renderTrigger({isOpen: Boolean(open), toggle}) : renderDefaultTrigger()}
|
|
351
358
|
</View>
|
|
352
359
|
{Boolean(open) && renderOverlay()}
|
|
@@ -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
|
});
|
package/src/FilePickerButton.tsx
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import {DateTime} from "luxon";
|
|
2
2
|
import React, {useCallback, useState} from "react";
|
|
3
|
+
import {Platform} from "react-native";
|
|
3
4
|
|
|
4
5
|
import {Box} from "./Box";
|
|
5
|
-
import {
|
|
6
|
+
import {DropdownMenuItem} from "./DropdownMenuItem";
|
|
7
|
+
import {DropdownPanel} from "./DropdownPanel";
|
|
6
8
|
import {IconButton} from "./IconButton";
|
|
7
|
-
import {Modal} from "./Modal";
|
|
8
9
|
|
|
9
10
|
export interface SelectedFile {
|
|
10
11
|
mimeType: string;
|
|
@@ -19,16 +20,87 @@ export interface FilePickerButtonProps {
|
|
|
19
20
|
testID?: string;
|
|
20
21
|
}
|
|
21
22
|
|
|
23
|
+
const DOCUMENT_MIME_TYPES = ["application/pdf", "text/plain", "text/csv", "application/json"];
|
|
24
|
+
|
|
25
|
+
const readFileAsDataUrl = (file: File): Promise<string> =>
|
|
26
|
+
new Promise((resolve, reject) => {
|
|
27
|
+
const reader = new FileReader();
|
|
28
|
+
reader.onerror = (): void => reject(reader.error ?? new Error("Failed to read file"));
|
|
29
|
+
reader.onload = (): void => resolve(String(reader.result));
|
|
30
|
+
reader.readAsDataURL(file);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Opens the browser file dialog. Must run synchronously inside the press handler: browsers
|
|
35
|
+
* only open a file input from a trusted user gesture, which an awaited import would lose.
|
|
36
|
+
* Files resolve as `data:` URLs so they outlive the page, unlike `blob:` object URLs.
|
|
37
|
+
*/
|
|
38
|
+
const pickFilesOnWeb = ({
|
|
39
|
+
accept,
|
|
40
|
+
multiple,
|
|
41
|
+
}: {
|
|
42
|
+
accept: string;
|
|
43
|
+
multiple: boolean;
|
|
44
|
+
}): Promise<SelectedFile[]> =>
|
|
45
|
+
new Promise((resolve) => {
|
|
46
|
+
const input = document.createElement("input");
|
|
47
|
+
input.type = "file";
|
|
48
|
+
input.accept = accept;
|
|
49
|
+
input.multiple = multiple;
|
|
50
|
+
input.style.display = "none";
|
|
51
|
+
const cleanup = (): void => {
|
|
52
|
+
input.remove();
|
|
53
|
+
};
|
|
54
|
+
input.addEventListener("change", async () => {
|
|
55
|
+
const files = Array.from(input.files ?? []);
|
|
56
|
+
cleanup();
|
|
57
|
+
try {
|
|
58
|
+
const selected = await Promise.all(
|
|
59
|
+
files.map(async (file) => ({
|
|
60
|
+
mimeType: file.type || "application/octet-stream",
|
|
61
|
+
name: file.name,
|
|
62
|
+
uri: await readFileAsDataUrl(file),
|
|
63
|
+
}))
|
|
64
|
+
);
|
|
65
|
+
resolve(selected);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
console.warn("Failed to read selected files", error);
|
|
68
|
+
resolve([]);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
input.addEventListener("cancel", () => {
|
|
72
|
+
cleanup();
|
|
73
|
+
resolve([]);
|
|
74
|
+
});
|
|
75
|
+
document.body.appendChild(input);
|
|
76
|
+
input.click();
|
|
77
|
+
});
|
|
78
|
+
|
|
22
79
|
export const FilePickerButton = ({
|
|
23
80
|
disabled = false,
|
|
24
81
|
multiple = false,
|
|
25
82
|
onFilesSelected,
|
|
26
83
|
testID,
|
|
27
84
|
}: FilePickerButtonProps): React.ReactElement => {
|
|
28
|
-
const [
|
|
85
|
+
const [isMenuOpen, setIsMenuOpen] = useState(false);
|
|
86
|
+
const triggerTestID = testID ?? "file-picker-button";
|
|
87
|
+
|
|
88
|
+
const handleWebPick = useCallback(
|
|
89
|
+
async (accept: string): Promise<void> => {
|
|
90
|
+
const files = await pickFilesOnWeb({accept, multiple});
|
|
91
|
+
if (files.length > 0) {
|
|
92
|
+
onFilesSelected(files);
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
[multiple, onFilesSelected]
|
|
96
|
+
);
|
|
29
97
|
|
|
30
98
|
const handlePickImage = useCallback(async () => {
|
|
31
|
-
|
|
99
|
+
setIsMenuOpen(false);
|
|
100
|
+
if (Platform.OS === "web") {
|
|
101
|
+
await handleWebPick("image/*");
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
32
104
|
const ImagePicker = await import("expo-image-picker");
|
|
33
105
|
const result = await ImagePicker.launchImageLibraryAsync({
|
|
34
106
|
allowsMultipleSelection: multiple,
|
|
@@ -44,14 +116,18 @@ export const FilePickerButton = ({
|
|
|
44
116
|
}));
|
|
45
117
|
onFilesSelected(files);
|
|
46
118
|
}
|
|
47
|
-
}, [multiple, onFilesSelected]);
|
|
119
|
+
}, [handleWebPick, multiple, onFilesSelected]);
|
|
48
120
|
|
|
49
121
|
const handlePickDocument = useCallback(async () => {
|
|
50
|
-
|
|
122
|
+
setIsMenuOpen(false);
|
|
123
|
+
if (Platform.OS === "web") {
|
|
124
|
+
await handleWebPick(DOCUMENT_MIME_TYPES.join(","));
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
51
127
|
const DocumentPicker = await import("expo-document-picker");
|
|
52
128
|
const result = await DocumentPicker.getDocumentAsync({
|
|
53
129
|
multiple,
|
|
54
|
-
type:
|
|
130
|
+
type: DOCUMENT_MIME_TYPES,
|
|
55
131
|
});
|
|
56
132
|
|
|
57
133
|
if (!result.canceled && result.assets.length > 0) {
|
|
@@ -62,28 +138,40 @@ export const FilePickerButton = ({
|
|
|
62
138
|
}));
|
|
63
139
|
onFilesSelected(files);
|
|
64
140
|
}
|
|
65
|
-
}, [multiple, onFilesSelected]);
|
|
141
|
+
}, [handleWebPick, multiple, onFilesSelected]);
|
|
66
142
|
|
|
67
143
|
return (
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
144
|
+
<DropdownPanel
|
|
145
|
+
align="auto"
|
|
146
|
+
isOpen={isMenuOpen}
|
|
147
|
+
onOpenChange={setIsMenuOpen}
|
|
148
|
+
renderTrigger={({toggle}) => (
|
|
149
|
+
<IconButton
|
|
150
|
+
accessibilityLabel="Attach file"
|
|
151
|
+
disabled={disabled}
|
|
152
|
+
iconName="paperclip"
|
|
153
|
+
onClick={toggle}
|
|
154
|
+
testID={triggerTestID}
|
|
155
|
+
/>
|
|
156
|
+
)}
|
|
157
|
+
showActionButtons={false}
|
|
158
|
+
testID={`${triggerTestID}-menu`}
|
|
159
|
+
width={200}
|
|
160
|
+
>
|
|
161
|
+
<Box gap={1}>
|
|
162
|
+
<DropdownMenuItem
|
|
163
|
+
iconName="image"
|
|
164
|
+
label="Photo Library"
|
|
165
|
+
onClick={handlePickImage}
|
|
166
|
+
testID={`${triggerTestID}-photo`}
|
|
167
|
+
/>
|
|
168
|
+
<DropdownMenuItem
|
|
169
|
+
iconName="file"
|
|
170
|
+
label="Document"
|
|
171
|
+
onClick={handlePickDocument}
|
|
172
|
+
testID={`${triggerTestID}-document`}
|
|
173
|
+
/>
|
|
174
|
+
</Box>
|
|
175
|
+
</DropdownPanel>
|
|
88
176
|
);
|
|
89
177
|
};
|