@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
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
|
-
|
|
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 [
|
|
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
|
-
|
|
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
|
|
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({
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
125
|
+
const chart = (
|
|
79
126
|
<ChartFrame
|
|
80
127
|
accessibilityLabel={summaryLabel}
|
|
81
128
|
emptyText={emptyText}
|
|
82
|
-
isEmpty={
|
|
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
|
-
{
|
|
119
|
-
<Path
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
{
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
-
|
|
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
|
|
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
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type {FC} from "react";
|
|
2
|
+
import {useCallback, useState} from "react";
|
|
3
|
+
import {Path, Svg} from "react-native-svg";
|
|
4
|
+
|
|
5
|
+
import {Box} from "./Box";
|
|
6
|
+
import type {LayoutChangeEvent, SparklineChartProps} from "./Common";
|
|
7
|
+
import {getLinePath} from "./charts/paths";
|
|
8
|
+
import {createCartesianScales} from "./charts/scales";
|
|
9
|
+
import {getChartPaint} from "./charts/theme";
|
|
10
|
+
import type {ChartPlot} from "./charts/types/chartTypes";
|
|
11
|
+
import {useTheme} from "./Theme";
|
|
12
|
+
import {resolveTestID, toTestProps} from "./testing/resolveTestId";
|
|
13
|
+
|
|
14
|
+
const DEFAULT_HEIGHT = 48;
|
|
15
|
+
const DEFAULT_WIDTH = 200;
|
|
16
|
+
const PLOT_INSET = 4;
|
|
17
|
+
|
|
18
|
+
export const SparklineChart: FC<SparklineChartProps> = ({
|
|
19
|
+
accessibilityLabel = "Sparkline chart",
|
|
20
|
+
comparisonData = [],
|
|
21
|
+
data,
|
|
22
|
+
height = DEFAULT_HEIGHT,
|
|
23
|
+
testID,
|
|
24
|
+
}) => {
|
|
25
|
+
const {theme} = useTheme();
|
|
26
|
+
const paint = getChartPaint(theme);
|
|
27
|
+
const [chartWidth, setChartWidth] = useState(DEFAULT_WIDTH);
|
|
28
|
+
|
|
29
|
+
const handleLayout = useCallback((event: LayoutChangeEvent): void => {
|
|
30
|
+
const nextWidth = event.nativeEvent.layout.width;
|
|
31
|
+
if (nextWidth > 0) {
|
|
32
|
+
setChartWidth(nextWidth);
|
|
33
|
+
}
|
|
34
|
+
}, []);
|
|
35
|
+
|
|
36
|
+
const allPoints = [...data, ...comparisonData];
|
|
37
|
+
const plot: ChartPlot = {
|
|
38
|
+
height: Math.max(height - PLOT_INSET * 2, 1),
|
|
39
|
+
left: PLOT_INSET,
|
|
40
|
+
top: PLOT_INSET,
|
|
41
|
+
width: Math.max(chartWidth - PLOT_INSET * 2, 1),
|
|
42
|
+
};
|
|
43
|
+
const scales = createCartesianScales({includeZero: false, plot, points: allPoints});
|
|
44
|
+
const currentPath = getLinePath({points: data, scales});
|
|
45
|
+
const comparisonPath = getLinePath({points: comparisonData, scales});
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<Box
|
|
49
|
+
accessibilityLabel={accessibilityLabel}
|
|
50
|
+
height={height}
|
|
51
|
+
minWidth={0}
|
|
52
|
+
onLayout={handleLayout}
|
|
53
|
+
overflow="hidden"
|
|
54
|
+
width="100%"
|
|
55
|
+
{...toTestProps(testID)}
|
|
56
|
+
>
|
|
57
|
+
{allPoints.length > 0 ? (
|
|
58
|
+
<Svg height={height} width={chartWidth}>
|
|
59
|
+
{comparisonPath ? (
|
|
60
|
+
<Path
|
|
61
|
+
d={comparisonPath}
|
|
62
|
+
fill="none"
|
|
63
|
+
opacity={0.6}
|
|
64
|
+
stroke={paint.series}
|
|
65
|
+
strokeDasharray="4 4"
|
|
66
|
+
strokeWidth={2}
|
|
67
|
+
testID={resolveTestID(testID, "comparison")}
|
|
68
|
+
/>
|
|
69
|
+
) : null}
|
|
70
|
+
{currentPath ? (
|
|
71
|
+
<Path
|
|
72
|
+
d={currentPath}
|
|
73
|
+
fill="none"
|
|
74
|
+
stroke={paint.series}
|
|
75
|
+
strokeWidth={2}
|
|
76
|
+
testID={resolveTestID(testID, "current")}
|
|
77
|
+
/>
|
|
78
|
+
) : null}
|
|
79
|
+
</Svg>
|
|
80
|
+
) : null}
|
|
81
|
+
</Box>
|
|
82
|
+
);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export type {SparklineChartProps} from "./Common";
|
package/src/TextField.test.tsx
CHANGED
|
@@ -790,6 +790,22 @@ describe("TextField", () => {
|
|
|
790
790
|
).not.toThrow();
|
|
791
791
|
});
|
|
792
792
|
|
|
793
|
+
it("clears the measured grow height after the controlled value is emptied", async () => {
|
|
794
|
+
const {getByDisplayValue, rerender} = renderWithTheme(
|
|
795
|
+
<TextField grow multiline onChange={mockOnChange} value="long note" />
|
|
796
|
+
);
|
|
797
|
+
fireEvent(getByDisplayValue("long note"), "contentSizeChange", {
|
|
798
|
+
nativeEvent: {contentSize: {height: 120}},
|
|
799
|
+
});
|
|
800
|
+
|
|
801
|
+
rerender(<TextField grow multiline onChange={mockOnChange} value="" />);
|
|
802
|
+
await act(async () => {});
|
|
803
|
+
rerender(<TextField grow multiline onChange={mockOnChange} value="n" />);
|
|
804
|
+
|
|
805
|
+
const styles = [getByDisplayValue("n").props.style].flat(Infinity);
|
|
806
|
+
assert.isTrue(styles.some((style) => style?.height === 40));
|
|
807
|
+
});
|
|
808
|
+
|
|
793
809
|
it("invokes the onFocus callback when the input is focused", () => {
|
|
794
810
|
const {getByDisplayValue} = renderWithTheme(
|
|
795
811
|
<TextField onChange={mockOnChange} onFocus={mockOnFocus} value="" />
|
package/src/TextField.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {getCalendars} from "expo-localization";
|
|
2
|
-
import {type FC, useCallback, useMemo, useRef, useState} from "react";
|
|
2
|
+
import {type FC, useCallback, useEffect, useMemo, useRef, useState} from "react";
|
|
3
3
|
import {
|
|
4
4
|
type DimensionValue,
|
|
5
5
|
type KeyboardTypeOptions,
|
|
@@ -89,6 +89,7 @@ export const TextField: FC<TextFieldProps> = ({
|
|
|
89
89
|
multiline,
|
|
90
90
|
rows = 1,
|
|
91
91
|
grow,
|
|
92
|
+
maxHeight,
|
|
92
93
|
returnKeyType,
|
|
93
94
|
onBlur,
|
|
94
95
|
onFocus,
|
|
@@ -113,6 +114,13 @@ export const TextField: FC<TextFieldProps> = ({
|
|
|
113
114
|
const [isValueRevealed, setIsValueRevealed] = useState(false);
|
|
114
115
|
const textInputRef = useRef<TextInput | null>(null);
|
|
115
116
|
|
|
117
|
+
// Clear stale measured height so the next typed character starts at the default row height.
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
if (grow && !value) {
|
|
120
|
+
setHeight(rows * 40);
|
|
121
|
+
}
|
|
122
|
+
}, [grow, rows, value]);
|
|
123
|
+
|
|
116
124
|
const isPasswordField = type === "password";
|
|
117
125
|
const hasVisibilityToggle = isPasswordField && showVisibilityToggle;
|
|
118
126
|
|
|
@@ -164,7 +172,9 @@ export const TextField: FC<TextFieldProps> = ({
|
|
|
164
172
|
|
|
165
173
|
const calculatedHeight: DimensionValue = useMemo(() => {
|
|
166
174
|
if (grow) {
|
|
167
|
-
|
|
175
|
+
// React Native Web never reports a smaller content size, so an emptied field resets.
|
|
176
|
+
const grownHeight = value ? Math.max(40, height) : rows * 40;
|
|
177
|
+
return maxHeight ? Math.min(grownHeight, maxHeight) : grownHeight;
|
|
168
178
|
} else if (multiline) {
|
|
169
179
|
return height || "100%";
|
|
170
180
|
} else {
|
|
@@ -172,7 +182,7 @@ export const TextField: FC<TextFieldProps> = ({
|
|
|
172
182
|
// single-line inputs need extra vertical room beyond 16px text.
|
|
173
183
|
return Platform.OS === "ios" ? 24 : 22;
|
|
174
184
|
}
|
|
175
|
-
}, [grow, height, multiline]);
|
|
185
|
+
}, [grow, height, maxHeight, multiline, rows, value]);
|
|
176
186
|
|
|
177
187
|
const defaultTextInputStyles = useMemo(() => {
|
|
178
188
|
const style: StyleProp<TextStyleWithOutline> = {
|