@hero-design/rn 8.100.2 → 8.101.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.
Files changed (36) hide show
  1. package/.turbo/turbo-build.log +3 -3
  2. package/CHANGELOG.md +6 -0
  3. package/es/index.js +109 -25
  4. package/lib/index.js +109 -25
  5. package/package.json +1 -1
  6. package/src/components/Chart/ColumnChart/ColumnChartContent.tsx +19 -3
  7. package/src/components/Chart/ColumnChart/Segment.tsx +1 -1
  8. package/src/components/Chart/ColumnChart/StackedSegment.tsx +10 -6
  9. package/src/components/Chart/ColumnChart/__tests__/Segment.spec.tsx +1 -1
  10. package/src/components/Chart/ColumnChart/__tests__/__snapshots__/StackedSegment.spec.tsx.snap +6 -21
  11. package/src/components/Chart/ColumnChart/__tests__/__snapshots__/index.spec.tsx.snap +999 -6
  12. package/src/components/Chart/ColumnChart/__tests__/index.spec.tsx +107 -0
  13. package/src/components/Chart/ColumnChart/index.tsx +15 -0
  14. package/src/components/Chart/Line/Line.tsx +5 -2
  15. package/src/components/Chart/Line/__tests__/Line.spec.tsx +13 -6
  16. package/src/components/Chart/Line/__tests__/__snapshots__/Line.spec.tsx.snap +1 -1
  17. package/src/components/Chart/Line/__tests__/__snapshots__/index.spec.tsx.snap +1464 -4
  18. package/src/components/Chart/Line/__tests__/index.spec.tsx +95 -1
  19. package/src/components/Chart/Line/index.tsx +14 -2
  20. package/src/components/Chart/shared/__tests__/utils.spec.ts +16 -0
  21. package/src/components/Chart/shared/constants.ts +4 -0
  22. package/src/components/Chart/shared/hooks/useCustomColor.ts +84 -0
  23. package/src/components/Chart/shared/utils.ts +14 -0
  24. package/src/components/Chart/types.ts +32 -0
  25. package/stats/8.100.2/rn-stats.html +1 -3
  26. package/stats/8.101.0/rn-stats.html +4844 -0
  27. package/types/components/Chart/ColumnChart/ColumnChartContent.d.ts +5 -1
  28. package/types/components/Chart/ColumnChart/StackedSegment.d.ts +4 -0
  29. package/types/components/Chart/ColumnChart/index.d.ts +8 -2
  30. package/types/components/Chart/Line/Line.d.ts +3 -1
  31. package/types/components/Chart/Line/index.d.ts +8 -2
  32. package/types/components/Chart/index.d.ts +2 -2
  33. package/types/components/Chart/shared/constants.d.ts +2 -0
  34. package/types/components/Chart/shared/hooks/useCustomColor.d.ts +22 -0
  35. package/types/components/Chart/shared/utils.d.ts +11 -0
  36. package/types/components/Chart/types.d.ts +14 -1
@@ -1,7 +1,12 @@
1
1
  import React from 'react';
2
+ import { mobileVisualisationPalette } from '@hero-design/colors';
2
3
  import { fireEvent } from '@testing-library/react-native';
3
4
  import renderWithTheme from '../../../../testHelpers/renderWithTheme';
4
5
  import ColumnChart, { ColumnChartProps } from '../index';
6
+ import {
7
+ ERROR_COLOR_NOT_FOUND,
8
+ ERROR_COLOR_NUMBER_MISMATCH,
9
+ } from '../../shared/constants';
5
10
 
6
11
  const baseData: ColumnChartProps['data'] = [
7
12
  {
@@ -131,4 +136,106 @@ describe('ColumnChart', () => {
131
136
  xIndex: 0,
132
137
  });
133
138
  });
139
+
140
+ it('renders chart with custom colors', () => {
141
+ const { getByTestId, getByText, toJSON } = renderWithTheme(
142
+ <ColumnChart
143
+ data={baseData}
144
+ xAxisConfig={xAxisConfig}
145
+ testID="column-chart"
146
+ styleConfig={{
147
+ series: [
148
+ { label: 'Series 1', color: 'greenLight' },
149
+ { label: 'Series 2', color: 'blueLight' },
150
+ ],
151
+ }}
152
+ />
153
+ );
154
+
155
+ // Simulate the onLayout event
156
+ fireEvent(getByTestId('column-chart'), 'layout', {
157
+ nativeEvent: {
158
+ layout: { x: 0, y: 0, width: 100, height: 200 },
159
+ },
160
+ });
161
+
162
+ expect(
163
+ getByTestId('column-segment-A-Series 1').props.accessibilityLabel
164
+ ).toContain(`color ${mobileVisualisationPalette.greenLight}`);
165
+ expect(
166
+ getByTestId('column-segment-B-Series 2').props.accessibilityLabel
167
+ ).toContain(`color ${mobileVisualisationPalette.blueLight}`);
168
+
169
+ expect(toJSON()).toMatchSnapshot();
170
+ expect(getByText('A')).toBeVisible();
171
+ expect(getByText('B')).toBeVisible();
172
+ expect(getByText('C')).toBeVisible();
173
+ });
174
+
175
+ it('should throw error if styleConfig.series does not have enough colors', () => {
176
+ expect(() =>
177
+ renderWithTheme(
178
+ <ColumnChart
179
+ data={baseData}
180
+ xAxisConfig={xAxisConfig}
181
+ testID="column-chart"
182
+ styleConfig={{
183
+ series: [{ label: 'Series 1' }],
184
+ }}
185
+ />
186
+ )
187
+ ).toThrow(ERROR_COLOR_NUMBER_MISMATCH);
188
+
189
+ expect(() =>
190
+ renderWithTheme(
191
+ <ColumnChart
192
+ data={baseData}
193
+ xAxisConfig={xAxisConfig}
194
+ testID="column-chart"
195
+ styleConfig={{
196
+ series: [
197
+ { label: 'Series 1', color: 'greenLight' },
198
+ { label: 'Series 2', color: 'blueLight' },
199
+ { label: 'Series 3', color: 'redLight' },
200
+ ],
201
+ }}
202
+ />
203
+ )
204
+ ).toThrow(ERROR_COLOR_NUMBER_MISMATCH);
205
+
206
+ expect(() =>
207
+ renderWithTheme(
208
+ <ColumnChart
209
+ data={baseData}
210
+ xAxisConfig={xAxisConfig}
211
+ testID="column-chart"
212
+ styleConfig={{
213
+ series: [
214
+ { label: 'Series 1', color: undefined },
215
+ { label: 'Series 2', color: 'blueLight' },
216
+ ],
217
+ }}
218
+ />
219
+ )
220
+ ).toThrow(ERROR_COLOR_NUMBER_MISMATCH);
221
+ });
222
+
223
+ it('should throw error if color is not found in the palette', () => {
224
+ expect(() =>
225
+ renderWithTheme(
226
+ <ColumnChart
227
+ data={baseData}
228
+ xAxisConfig={xAxisConfig}
229
+ testID="column-chart"
230
+ styleConfig={{
231
+ series: [
232
+ // @ts-expect-error: This is a test for error handling
233
+ { label: 'Series 1', color: 'mauve' },
234
+ { label: 'Series 2', color: 'blueLight' },
235
+ ],
236
+ }}
237
+ />
238
+ )
239
+ ).toThrow(ERROR_COLOR_NOT_FOUND.replace('{color}', 'mauve'));
240
+ });
134
241
  });
@@ -10,12 +10,14 @@ import {
10
10
  DataValue,
11
11
  HeaderConfig,
12
12
  Series,
13
+ StyleConfig,
13
14
  XAxisConfig,
14
15
  YAxisConfig,
15
16
  } from '../types';
16
17
  import ColumnChartContent from './ColumnChartContent';
17
18
  import { createNiceScale } from '../shared/niceNumbers';
18
19
  import { StyledColumnChartWrapper } from './StyledColumnChart';
20
+ import useCustomColor from '../shared/hooks/useCustomColor';
19
21
 
20
22
  /**
21
23
  * Props for the ColumnChart component.
@@ -69,6 +71,12 @@ export interface ColumnChartProps {
69
71
  seriesIndex: number;
70
72
  xIndex: number;
71
73
  }) => void;
74
+ /**
75
+ * * styleConfig use to custom the style of the chart.
76
+ * * styleConfig must be an object:
77
+ * * color?: use to custom the legend colors.
78
+ */
79
+ styleConfig?: StyleConfig;
72
80
  }
73
81
 
74
82
  /**
@@ -102,7 +110,13 @@ const ColumnChart = ({
102
110
  headerConfig,
103
111
  emptyText,
104
112
  onBarPress,
113
+ styleConfig,
105
114
  }: ColumnChartProps) => {
115
+ const colorScale = useCustomColor({
116
+ data,
117
+ seriesConfig: styleConfig?.series,
118
+ });
119
+
106
120
  const xLabels = useMemo(() => {
107
121
  return xAxisConfig.labels && xAxisConfig.labels.length > 0
108
122
  ? xAxisConfig.labels
@@ -205,6 +219,7 @@ const ColumnChart = ({
205
219
  xAxisConfig={calculatedXAxisConfig}
206
220
  yAxisConfig={calculatedYAxisConfig}
207
221
  onBarPress={onBarPress}
222
+ colorScale={colorScale}
208
223
  />
209
224
  )}
210
225
  />
@@ -23,6 +23,8 @@ type LineProps = {
23
23
  coordinates: AxisCoordinates;
24
24
  /** Color for the line */
25
25
  color?: string;
26
+ /** Test ID for the line */
27
+ testID?: string;
26
28
  };
27
29
 
28
30
  const Line = ({
@@ -32,6 +34,7 @@ const Line = ({
32
34
  labels,
33
35
  coordinates,
34
36
  color,
37
+ testID,
35
38
  }: LineProps) => {
36
39
  const { xStart, xEnd, yStart, yEnd } = coordinates;
37
40
  const theme = useTheme();
@@ -66,8 +69,8 @@ const Line = ({
66
69
 
67
70
  return pathData ? (
68
71
  <Path
69
- testID="line-path"
70
- accessibilityLabel={`chart-line-maxValue:${maxValue},minValue:${minValue},labelsLength:${labels.length}`}
72
+ testID={testID}
73
+ accessibilityLabel={`chart-line-maxValue:${maxValue},minValue:${minValue},labelsLength:${labels.length},color:${color}`}
71
74
  d={pathData}
72
75
  stroke={color || theme.colors.secondary}
73
76
  strokeWidth={DEFAULT_LINE_STROKE_WIDTH}
@@ -11,6 +11,7 @@ describe('Line', () => {
11
11
  minValue={1}
12
12
  labels={['1', '2', '3']}
13
13
  coordinates={{ xStart: 50, xEnd: 300, yStart: 50, yEnd: 250 }}
14
+ testID="line-path"
14
15
  />
15
16
  );
16
17
 
@@ -18,7 +19,7 @@ describe('Line', () => {
18
19
  const path = getByTestId('line-path');
19
20
  expect(path).toBeTruthy();
20
21
  expect(path.props.accessibilityLabel).toBe(
21
- 'chart-line-maxValue:3,minValue:1,labelsLength:3'
22
+ 'chart-line-maxValue:3,minValue:1,labelsLength:3,color:undefined'
22
23
  );
23
24
  expect(path.props.d).toBe(
24
25
  'M96.875,250L109.896,233.333C122.917,216.667,148.958,183.333,175,150C201.042,116.667,227.083,83.333,240.104,66.667L253.125,50'
@@ -33,13 +34,14 @@ describe('Line', () => {
33
34
  minValue={-3}
34
35
  labels={['1', '2', '3']}
35
36
  coordinates={{ xStart: 50, xEnd: 300, yStart: 50, yEnd: 250 }}
37
+ testID="line-path"
36
38
  />
37
39
  );
38
40
 
39
41
  const path = getByTestId('line-path');
40
42
  expect(path).toBeTruthy();
41
43
  expect(path.props.accessibilityLabel).toBe(
42
- 'chart-line-maxValue:-1,minValue:-3,labelsLength:3'
44
+ 'chart-line-maxValue:-1,minValue:-3,labelsLength:3,color:undefined'
43
45
  );
44
46
  expect(path.props.d).toBe(
45
47
  'M96.875,50L109.896,66.667C122.917,83.333,148.958,116.667,175,150C201.042,183.333,227.083,216.667,240.104,233.333L253.125,250'
@@ -54,13 +56,14 @@ describe('Line', () => {
54
56
  minValue={-1}
55
57
  labels={['1', '2', '3']}
56
58
  coordinates={{ xStart: 50, xEnd: 300, yStart: 50, yEnd: 250 }}
59
+ testID="line-path"
57
60
  />
58
61
  );
59
62
 
60
63
  const path = getByTestId('line-path');
61
64
  expect(path).toBeTruthy();
62
65
  expect(path.props.accessibilityLabel).toBe(
63
- 'chart-line-maxValue:1,minValue:-1,labelsLength:3'
66
+ 'chart-line-maxValue:1,minValue:-1,labelsLength:3,color:undefined'
64
67
  );
65
68
  expect(path.props.d).toBe(
66
69
  'M96.875,250L109.896,233.333C122.917,216.667,148.958,183.333,175,150C201.042,116.667,227.083,83.333,240.104,66.667L253.125,50'
@@ -75,13 +78,14 @@ describe('Line', () => {
75
78
  minValue={1.5}
76
79
  labels={['1', '2', '3']}
77
80
  coordinates={{ xStart: 50, xEnd: 300, yStart: 50, yEnd: 250 }}
81
+ testID="line-path"
78
82
  />
79
83
  );
80
84
 
81
85
  const path = getByTestId('line-path');
82
86
  expect(path).toBeTruthy();
83
87
  expect(path.props.accessibilityLabel).toBe(
84
- 'chart-line-maxValue:3.5,minValue:1.5,labelsLength:3'
88
+ 'chart-line-maxValue:3.5,minValue:1.5,labelsLength:3,color:undefined'
85
89
  );
86
90
  expect(path.props.d).toBe(
87
91
  'M96.875,250L109.896,233.333C122.917,216.667,148.958,183.333,175,150C201.042,116.667,227.083,83.333,240.104,66.667L253.125,50'
@@ -96,13 +100,14 @@ describe('Line', () => {
96
100
  minValue={1}
97
101
  labels={['1', '2', '3']}
98
102
  coordinates={{ xStart: 100, xEnd: 400, yStart: 100, yEnd: 300 }}
103
+ testID="line-path"
99
104
  />
100
105
  );
101
106
 
102
107
  const path = getByTestId('line-path');
103
108
  expect(path).toBeTruthy();
104
109
  expect(path.props.accessibilityLabel).toBe(
105
- 'chart-line-maxValue:3,minValue:1,labelsLength:3'
110
+ 'chart-line-maxValue:3,minValue:1,labelsLength:3,color:undefined'
106
111
  );
107
112
  expect(path.props.d).toBe(
108
113
  'M156.25,300L171.875,283.333C187.5,266.667,218.75,233.333,250,200C281.25,166.667,312.5,133.333,328.125,116.667L343.75,100'
@@ -117,6 +122,7 @@ describe('Line', () => {
117
122
  minValue={0}
118
123
  labels={[]}
119
124
  coordinates={{ xStart: 50, xEnd: 300, yStart: 50, yEnd: 250 }}
125
+ testID="line-path"
120
126
  />
121
127
  );
122
128
 
@@ -132,13 +138,14 @@ describe('Line', () => {
132
138
  labels={['1', '2', '3']}
133
139
  coordinates={{ xStart: 50, xEnd: 300, yStart: 50, yEnd: 250 }}
134
140
  color="#FF0000"
141
+ testID="line-path"
135
142
  />
136
143
  );
137
144
 
138
145
  const path = getByTestId('line-path');
139
146
  expect(path).toBeTruthy();
140
147
  expect(path.props.accessibilityLabel).toBe(
141
- 'chart-line-maxValue:3,minValue:1,labelsLength:3'
148
+ 'chart-line-maxValue:3,minValue:1,labelsLength:3,color:#FF0000'
142
149
  );
143
150
  expect(path.props.d).toBe(
144
151
  'M96.875,250L109.896,233.333C122.917,216.667,148.958,183.333,175,150C201.042,116.667,227.083,83.333,240.104,66.667L253.125,50'
@@ -9,7 +9,7 @@ exports[`Line should render with basic data 1`] = `
9
9
  }
10
10
  >
11
11
  <RNSVGPath
12
- accessibilityLabel="chart-line-maxValue:3,minValue:1,labelsLength:3"
12
+ accessibilityLabel="chart-line-maxValue:3,minValue:1,labelsLength:3,color:undefined"
13
13
  d="M96.875,250L109.896,233.333C122.917,216.667,148.958,183.333,175,150C201.042,116.667,227.083,83.333,240.104,66.667L253.125,50"
14
14
  fill={null}
15
15
  propList={