Skip to content

Commit 076406e

Browse files
authored
fix(metric): use a correct React key for each grid item (elastic#1789)
This PR fixes the problem caused by a wrongly generated React key on each grid items.
1 parent 554f370 commit 076406e

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

packages/charts/src/chart_types/metric/renderer/dom/index.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ class Component extends React.Component<StateProps & DispatchProps> {
108108
});
109109
if (!datum) {
110110
return (
111-
<li key={`empty-${columnIndex}`} role="presentation">
111+
<li key={`${columnIndex}-${rowIndex}`} role="presentation">
112112
<div className={emptyMetricClassName}></div>
113113
</li>
114114
);
115115
}
116116
return (
117-
<li key={`${datum.title}${datum.subtitle}${datum.color}${columnIndex}`}>
117+
<li key={`${columnIndex}-${rowIndex}`}>
118118
<MetricComponent
119119
chartId={chartId}
120120
datum={datum}
@@ -132,13 +132,14 @@ class Component extends React.Component<StateProps & DispatchProps> {
132132
);
133133
}),
134134
// fill the grid row with empty panels
135-
...Array.from({ length: totalColumns - columns.length }, (_, columIndex) => {
135+
...Array.from({ length: totalColumns - columns.length }, (_, zeroBasedColumnIndex) => {
136+
const columnIndex = zeroBasedColumnIndex + columns.length;
136137
const emptyMetricClassName = classNames('echMetric', {
137-
'echMetric--rightBorder': columns.length + columIndex < totalColumns - 1,
138+
'echMetric--rightBorder': columns.length + columnIndex < totalColumns - 1,
138139
'echMetric--bottomBorder': rowIndex < totalRows - 1,
139140
});
140141
return (
141-
<li key={`missing-${columIndex}`} role="presentation">
142+
<li key={`missing-${columnIndex}-${rowIndex}`} role="presentation">
142143
<div className={emptyMetricClassName}></div>
143144
</li>
144145
);

storybook/stories/metric/2_grid.story.tsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
import { EuiIcon } from '@elastic/eui';
1010
import { action } from '@storybook/addon-actions';
11-
import { select, number, boolean } from '@storybook/addon-knobs';
12-
import React from 'react';
11+
import { select, number, boolean, button } from '@storybook/addon-knobs';
12+
import React, { useState } from 'react';
1313

1414
import {
1515
Chart,
@@ -142,6 +142,22 @@ export const Example = () => {
142142
const layout = select('layout', ['grid', 'vertical', 'horizontal'], 'grid');
143143
const configuredData =
144144
layout === 'grid' ? split(data, 4) : layout === 'horizontal' ? [data.slice(0, 4)] : split(data.slice(0, 4), 1);
145+
const [chartData, setChartData] = useState(configuredData);
146+
button('randomize data', () => {
147+
setChartData(
148+
split(
149+
data
150+
.slice()
151+
.map((d) => {
152+
return Math.random() > 0.8 ? undefined : d;
153+
})
154+
.slice(0, Math.ceil(Math.random() * data.length)),
155+
Math.ceil((Math.random() * data.length) / 2),
156+
),
157+
);
158+
});
159+
const debugRandomizedData = boolean('debug randomized data', false);
160+
145161
const onEventClickAction = action('click');
146162
const onEventOverAction = action('over');
147163
const onEventOutAction = action('out');
@@ -155,6 +171,11 @@ export const Example = () => {
155171
width: layout === 'vertical' ? '180px' : '720px',
156172
}}
157173
>
174+
{debugRandomizedData &&
175+
chartData
176+
.flat()
177+
.map((d) => `[${d?.value}]`)
178+
.join(' ')}
158179
<Chart>
159180
<Settings
160181
baseTheme={useBaseTheme()}
@@ -180,7 +201,7 @@ export const Example = () => {
180201
}}
181202
onElementOut={() => onEventOutAction('out')}
182203
/>
183-
<Metric id="metric" data={configuredData} />
204+
<Metric id="metric" data={chartData} />
184205
</Chart>
185206
</div>
186207
);

0 commit comments

Comments
 (0)