@sybilion/uilib 1.8.1 → 1.8.2
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/esm/src/components/ui/Chart/components/ChartAxes.js +4 -4
- package/dist/esm/src/components/ui/TimeRangeControls/TimeRangeControls.js +18 -4
- package/dist/esm/types/src/components/ui/TimeRangeControls/TimeRangeControls.d.ts +9 -3
- package/dist/esm/types/src/components/ui/TimeRangeControls/TimeRangeControls.test.d.ts +1 -0
- package/package.json +1 -1
- package/src/components/ui/Chart/components/ChartAxes.test.tsx +11 -0
- package/src/components/ui/Chart/components/ChartAxes.tsx +6 -25
- package/src/components/ui/TimeRangeControls/AGENT.md +1 -1
- package/src/components/ui/TimeRangeControls/TimeRangeControls.test.tsx +40 -0
- package/src/components/ui/TimeRangeControls/TimeRangeControls.tsx +46 -35
|
@@ -38,10 +38,10 @@ formatNumber: formatNumber$1 = formatNumber, xMin, xMax, yMin, yMax, autoScaleYA
|
|
|
38
38
|
// Determine Y-axis domain
|
|
39
39
|
let yDomain;
|
|
40
40
|
if (autoScaleYAxis) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
];
|
|
41
|
+
// recharts' own fit: nice round ticks just around the data, so a series
|
|
42
|
+
// moving 1000–1010 fills the chart (scaling the values by 0.95/1.05 left
|
|
43
|
+
// it flat) and a negative one is padded too.
|
|
44
|
+
yDomain = ['auto', 'auto'];
|
|
45
45
|
}
|
|
46
46
|
else if (yMin !== undefined && yMax !== undefined) {
|
|
47
47
|
yDomain = [yMin, yMax];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
2
2
|
import { memo } from 'react';
|
|
3
3
|
import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem } from '../Select/Select.js';
|
|
4
4
|
import { ToggleGroup, ToggleGroupItem } from '../ToggleGroup/ToggleGroup.js';
|
|
@@ -10,9 +10,23 @@ function toggleValueForTimeRange(timeRange) {
|
|
|
10
10
|
? timeRange
|
|
11
11
|
: '';
|
|
12
12
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
/**
|
|
14
|
+
* The preset ranges as a select. A range that is not a preset (one dragged on
|
|
15
|
+
* the chart) shows "Custom" rather than a preset it is not.
|
|
16
|
+
*/
|
|
17
|
+
const TimeRangeSelect = memo(({ timeRange, onTimeRangeChange, loading }) => (jsxs(Select
|
|
18
|
+
// An empty value shows the placeholder; undefined would leave the
|
|
19
|
+
// select uncontrolled and keep the last preset picked.
|
|
20
|
+
, {
|
|
21
|
+
// An empty value shows the placeholder; undefined would leave the
|
|
22
|
+
// select uncontrolled and keep the last preset picked.
|
|
23
|
+
value: toggleValueForTimeRange(timeRange), onValueChange: onTimeRangeChange, disabled: loading, children: [jsx(SelectTrigger, { className: S.timeRangeSelectTrigger, size: "sm", "aria-label": "Time range", children: jsx(SelectValue, { placeholder: "Custom" }) }), jsx(SelectContent, { className: S.timeRangeSelectContent, children: TIME_RANGES.map(range => (jsx(SelectItem, { value: range, className: S.timeRangeSelectItem, children: range }, range))) })] })));
|
|
16
24
|
TimeRangeSelect.displayName = 'TimeRangeSelect';
|
|
25
|
+
/**
|
|
26
|
+
* The preset ranges as a toggle row from 768px up, and as a select below it
|
|
27
|
+
* (CSS shows one of the two), so the control stays on a phone.
|
|
28
|
+
*/
|
|
29
|
+
const TimeRangeControls = memo(({ timeRange, onTimeRangeChange, loading }) => (jsxs("div", { className: S.timeRangeContainer, children: [jsx(ToggleGroup, { type: "single", value: toggleValueForTimeRange(timeRange), onValueChange: onTimeRangeChange, variant: "outline", disabled: loading, className: S.timeRangeToggleGroup, children: TIME_RANGES.map(range => (jsx(ToggleGroupItem, { value: range, className: S.timeRangeToggleItem, children: range }, range))) }), jsx(TimeRangeSelect, { timeRange: timeRange, onTimeRangeChange: onTimeRangeChange, loading: loading })] })));
|
|
30
|
+
TimeRangeControls.displayName = 'TimeRangeControls';
|
|
17
31
|
|
|
18
32
|
export { TimeRangeControls, TimeRangeSelect };
|
|
@@ -3,7 +3,13 @@ export type TimeRangeControlsProps = {
|
|
|
3
3
|
onTimeRangeChange: (range: string) => void;
|
|
4
4
|
loading?: boolean;
|
|
5
5
|
};
|
|
6
|
+
/**
|
|
7
|
+
* The preset ranges as a select. A range that is not a preset (one dragged on
|
|
8
|
+
* the chart) shows "Custom" rather than a preset it is not.
|
|
9
|
+
*/
|
|
10
|
+
export declare const TimeRangeSelect: import("react").MemoExoticComponent<({ timeRange, onTimeRangeChange, loading }: TimeRangeControlsProps) => import("react/jsx-runtime").JSX.Element>;
|
|
11
|
+
/**
|
|
12
|
+
* The preset ranges as a toggle row from 768px up, and as a select below it
|
|
13
|
+
* (CSS shows one of the two), so the control stays on a phone.
|
|
14
|
+
*/
|
|
6
15
|
export declare const TimeRangeControls: import("react").MemoExoticComponent<({ timeRange, onTimeRangeChange, loading }: TimeRangeControlsProps) => import("react/jsx-runtime").JSX.Element>;
|
|
7
|
-
export declare const TimeRangeSelect: import("react").MemoExoticComponent<({ timeRange, onTimeRangeChange, loading, }: TimeRangeControlsProps & {
|
|
8
|
-
loading: boolean;
|
|
9
|
-
}) => import("react/jsx-runtime").JSX.Element>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -47,3 +47,14 @@ describe('ChartAxes y-axis numbers', () => {
|
|
|
47
47
|
expect(own).toHaveBeenCalled();
|
|
48
48
|
});
|
|
49
49
|
});
|
|
50
|
+
|
|
51
|
+
describe('ChartAxes auto-scaled y range', () => {
|
|
52
|
+
it("uses recharts' own fit around the data, not the values scaled", () => {
|
|
53
|
+
yAxisProps.length = 0;
|
|
54
|
+
render(<ChartAxes formatDate={formatDate} autoScaleYAxis />);
|
|
55
|
+
expect((yAxisProps.at(-1) as { domain?: unknown })?.domain).toEqual([
|
|
56
|
+
'auto',
|
|
57
|
+
'auto',
|
|
58
|
+
]);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useMemo, useRef } from 'react';
|
|
1
|
+
import { type ComponentProps, useMemo, useRef } from 'react';
|
|
2
2
|
|
|
3
3
|
import { Label, XAxis, YAxis } from 'recharts';
|
|
4
4
|
|
|
@@ -81,32 +81,13 @@ export function ChartAxes({
|
|
|
81
81
|
}, [hide, yMin, yMax, autoScaleYAxis, formatNumber]);
|
|
82
82
|
|
|
83
83
|
// Determine Y-axis domain
|
|
84
|
-
let yDomain:
|
|
85
|
-
| [
|
|
86
|
-
(
|
|
87
|
-
| number
|
|
88
|
-
| string
|
|
89
|
-
| 'auto'
|
|
90
|
-
| 'dataMin'
|
|
91
|
-
| 'dataMax'
|
|
92
|
-
| ((dataMin: number) => number)
|
|
93
|
-
),
|
|
94
|
-
(
|
|
95
|
-
| number
|
|
96
|
-
| string
|
|
97
|
-
| 'auto'
|
|
98
|
-
| 'dataMin'
|
|
99
|
-
| 'dataMax'
|
|
100
|
-
| ((dataMax: number) => number)
|
|
101
|
-
),
|
|
102
|
-
]
|
|
103
|
-
| undefined;
|
|
84
|
+
let yDomain: ComponentProps<typeof YAxis>['domain'];
|
|
104
85
|
|
|
105
86
|
if (autoScaleYAxis) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
];
|
|
87
|
+
// recharts' own fit: nice round ticks just around the data, so a series
|
|
88
|
+
// moving 1000–1010 fills the chart (scaling the values by 0.95/1.05 left
|
|
89
|
+
// it flat) and a negative one is padded too.
|
|
90
|
+
yDomain = ['auto', 'auto'];
|
|
110
91
|
} else if (yMin !== undefined && yMax !== undefined) {
|
|
111
92
|
yDomain = [yMin, yMax];
|
|
112
93
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
TimeRangeControls renders a row of preset time range toggles, such as one month or one year, for switching the window of a chart. Control it with timeRange and onTimeRangeChange, and pass loading to disable the controls while data refreshes.
|
|
1
|
+
TimeRangeControls renders a row of preset time range toggles, such as one month or one year, for switching the window of a chart. Control it with timeRange and onTimeRangeChange, and pass loading to disable the controls while data refreshes. Below 768px the same presets show as a select instead of the toggle row, so the control stays on a phone; a range that is not a preset (one dragged on the chart) selects no toggle and shows "Custom" in the select.
|
|
2
2
|
|
|
3
3
|
## When to use
|
|
4
4
|
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { render, screen } from '@testing-library/react';
|
|
2
|
+
|
|
3
|
+
import { TimeRangeControls } from './TimeRangeControls';
|
|
4
|
+
|
|
5
|
+
const noop = () => {};
|
|
6
|
+
|
|
7
|
+
describe('TimeRangeControls', () => {
|
|
8
|
+
it('renders the toggle row and the select, so a phone still has the control', () => {
|
|
9
|
+
render(<TimeRangeControls timeRange="1y" onTimeRangeChange={noop} />);
|
|
10
|
+
expect(screen.getByRole('group')).toBeTruthy();
|
|
11
|
+
const select = screen.getByRole('combobox', { name: 'Time range' });
|
|
12
|
+
expect(select.textContent).toContain('1y');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('says "Custom" for a range that is not a preset', () => {
|
|
16
|
+
const { rerender } = render(
|
|
17
|
+
<TimeRangeControls timeRange="1y" onTimeRangeChange={noop} />,
|
|
18
|
+
);
|
|
19
|
+
rerender(
|
|
20
|
+
<TimeRangeControls
|
|
21
|
+
timeRange="2024-01-01_2024-06-01"
|
|
22
|
+
onTimeRangeChange={noop}
|
|
23
|
+
/>,
|
|
24
|
+
);
|
|
25
|
+
const select = screen.getByRole('combobox', { name: 'Time range' });
|
|
26
|
+
expect(select.textContent).toContain('Custom');
|
|
27
|
+
expect(select.textContent).not.toContain('1y');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('disables both while loading', () => {
|
|
31
|
+
render(
|
|
32
|
+
<TimeRangeControls timeRange="1y" onTimeRangeChange={noop} loading />,
|
|
33
|
+
);
|
|
34
|
+
expect(
|
|
35
|
+
screen.getByRole('combobox', { name: 'Time range' }).hasAttribute(
|
|
36
|
+
'disabled',
|
|
37
|
+
),
|
|
38
|
+
).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -27,50 +27,25 @@ export type TimeRangeControlsProps = {
|
|
|
27
27
|
loading?: boolean;
|
|
28
28
|
};
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
type="single"
|
|
35
|
-
value={toggleValueForTimeRange(timeRange)}
|
|
36
|
-
onValueChange={onTimeRangeChange}
|
|
37
|
-
variant="outline"
|
|
38
|
-
disabled={loading}
|
|
39
|
-
className={S.timeRangeToggleGroup}
|
|
40
|
-
>
|
|
41
|
-
{TIME_RANGES.map(range => (
|
|
42
|
-
<ToggleGroupItem
|
|
43
|
-
key={range}
|
|
44
|
-
value={range}
|
|
45
|
-
className={S.timeRangeToggleItem}
|
|
46
|
-
>
|
|
47
|
-
{range}
|
|
48
|
-
</ToggleGroupItem>
|
|
49
|
-
))}
|
|
50
|
-
</ToggleGroup>
|
|
51
|
-
</div>
|
|
52
|
-
),
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
TimeRangeControls.displayName = 'TimeRangeControls';
|
|
56
|
-
|
|
30
|
+
/**
|
|
31
|
+
* The preset ranges as a select. A range that is not a preset (one dragged on
|
|
32
|
+
* the chart) shows "Custom" rather than a preset it is not.
|
|
33
|
+
*/
|
|
57
34
|
export const TimeRangeSelect = memo(
|
|
58
|
-
({
|
|
59
|
-
timeRange,
|
|
60
|
-
onTimeRangeChange,
|
|
61
|
-
loading,
|
|
62
|
-
}: TimeRangeControlsProps & { loading: boolean }) => (
|
|
35
|
+
({ timeRange, onTimeRangeChange, loading }: TimeRangeControlsProps) => (
|
|
63
36
|
<Select
|
|
64
|
-
value
|
|
37
|
+
// An empty value shows the placeholder; undefined would leave the
|
|
38
|
+
// select uncontrolled and keep the last preset picked.
|
|
39
|
+
value={toggleValueForTimeRange(timeRange)}
|
|
65
40
|
onValueChange={onTimeRangeChange}
|
|
66
41
|
disabled={loading}
|
|
67
42
|
>
|
|
68
43
|
<SelectTrigger
|
|
69
44
|
className={S.timeRangeSelectTrigger}
|
|
70
45
|
size="sm"
|
|
71
|
-
aria-label="
|
|
46
|
+
aria-label="Time range"
|
|
72
47
|
>
|
|
73
|
-
<SelectValue placeholder="
|
|
48
|
+
<SelectValue placeholder="Custom" />
|
|
74
49
|
</SelectTrigger>
|
|
75
50
|
|
|
76
51
|
<SelectContent className={S.timeRangeSelectContent}>
|
|
@@ -89,3 +64,39 @@ export const TimeRangeSelect = memo(
|
|
|
89
64
|
);
|
|
90
65
|
|
|
91
66
|
TimeRangeSelect.displayName = 'TimeRangeSelect';
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The preset ranges as a toggle row from 768px up, and as a select below it
|
|
70
|
+
* (CSS shows one of the two), so the control stays on a phone.
|
|
71
|
+
*/
|
|
72
|
+
export const TimeRangeControls = memo(
|
|
73
|
+
({ timeRange, onTimeRangeChange, loading }: TimeRangeControlsProps) => (
|
|
74
|
+
<div className={S.timeRangeContainer}>
|
|
75
|
+
<ToggleGroup
|
|
76
|
+
type="single"
|
|
77
|
+
value={toggleValueForTimeRange(timeRange)}
|
|
78
|
+
onValueChange={onTimeRangeChange}
|
|
79
|
+
variant="outline"
|
|
80
|
+
disabled={loading}
|
|
81
|
+
className={S.timeRangeToggleGroup}
|
|
82
|
+
>
|
|
83
|
+
{TIME_RANGES.map(range => (
|
|
84
|
+
<ToggleGroupItem
|
|
85
|
+
key={range}
|
|
86
|
+
value={range}
|
|
87
|
+
className={S.timeRangeToggleItem}
|
|
88
|
+
>
|
|
89
|
+
{range}
|
|
90
|
+
</ToggleGroupItem>
|
|
91
|
+
))}
|
|
92
|
+
</ToggleGroup>
|
|
93
|
+
<TimeRangeSelect
|
|
94
|
+
timeRange={timeRange}
|
|
95
|
+
onTimeRangeChange={onTimeRangeChange}
|
|
96
|
+
loading={loading}
|
|
97
|
+
/>
|
|
98
|
+
</div>
|
|
99
|
+
),
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
TimeRangeControls.displayName = 'TimeRangeControls';
|