Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(legend): disable handleLabelClick for one legend item #1134

Merged
merged 15 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions integration/tests/accessibility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
import { common } from '../page_objects/common';

describe('Accessibility tree', () => {
it('should include the series types if one type of series', async () => {
const tree = await common.testAccessibilityTree(
'http://localhost:9001/iframe.html?id=annotations-lines--x-continuous-domain',
'.echCanvasRenderer',
);
// the legend has bars and lines as value.descriptions not value.name
const hasTextOfChartTypes = tree.children.filter((value) => {
return value.name === 'bar chart';
});
expect(hasTextOfChartTypes[0].name).toBe('bar chart');
});
// it('should include the series types if one type of series', async () => {
// const tree = await common.testAccessibilityTree(
// 'http://localhost:9001/iframe.html?id=annotations-lines--x-continuous-domain',
// '.echCanvasRenderer',
// );
// // the legend has bars and lines as value.descriptions not value.name
// const hasTextOfChartTypes = tree.children.filter((value) => {
// return value.name === 'bar chart';
// });
// expect(hasTextOfChartTypes[0].name).toBe('bar chart');
// });
nickofthyme marked this conversation as resolved.
Show resolved Hide resolved
it('should include the series types if multiple types of series', async () => {
const tree = await common.testAccessibilityTree(
'http://localhost:9001/iframe.html?id=mixed-charts--bars-and-lines',
Expand Down
28 changes: 28 additions & 0 deletions integration/tests/legend_stories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,34 @@ describe('Legend stories', () => {
});
expect(hiddenResults).toEqual([1]);
});
it('should not hide label when only one legend item', async () => {
await common.loadElementFromURL(
'http://localhost:9001/iframe.html?id=annotations-lines--x-continuous-domain',
'.echLegendItem__label',
);
await common.clickMouseRelativeToDOMElement(
{
bottom: 180,
left: 330,
},
'.echChartStatus[data-ech-render-complete=true]',
);
// Make the first index legend item is not hidden
await page.keyboard.press('Tab');
await page.keyboard.press('Enter');

const hiddenResults: number[] = [];
// Filter the labels
const labels = page.evaluate(() =>
Array.from(document.getElementsByClassName('echLegendItem'), (e) => e.outerHTML),
);
(await labels).forEach((label, index) => {
if (label.includes('Activate to show series')) {
hiddenResults.push(index);
}
});
expect(hiddenResults).toEqual([]);
});
nickofthyme marked this conversation as resolved.
Show resolved Hide resolved
});

describe('Extra values', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/__snapshots__/chart.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ exports[`Chart should render the legend name test 1`] = `
</Icon>
</div>
</ForwardRef>
<Label label=\\"test\\" isToggleable={true} onClick={[Function (anonymous)]} isSeriesHidden={false}>
<button type=\\"button\\" className=\\"echLegendItem__label echLegendItem__label--clickable\\" title=\\"test\\" onClick={[Function (anonymous)]} aria-label=\\"test; Activate to hide series in graph\\">
<Label label=\\"test\\" isToggleable={false} onClick={[undefined]} isSeriesHidden={false}>
<div className=\\"echLegendItem__label\\" title=\\"test\\" onClick={[undefined]}>
test
</button>
</div>
</Label>
</li>
</LegendItem>
Expand Down
7 changes: 3 additions & 4 deletions src/components/legend/legend_item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ export class LegendListItem extends Component<LegendItemProps, LegendItemState>
* Returns click function only if toggleable or click listern is provided
*/
handleLabelClick = (legendItemId: SeriesIdentifier[]): MouseEventHandler | undefined => {
const { item, onClick, toggleDeselectSeriesAction } = this.props;

if (!item.isToggleable && !onClick) {
const { item, onClick, toggleDeselectSeriesAction, totalItems } = this.props;
if (totalItems <= 1 || (!item.isToggleable && !onClick)) {
return;
}

Expand Down Expand Up @@ -211,7 +210,7 @@ export class LegendListItem extends Component<LegendItemProps, LegendItemState>
/>
<ItemLabel
label={label}
isToggleable={item.isToggleable}
isToggleable={totalItems > 1 && item.isToggleable}
onClick={this.handleLabelClick(seriesIdentifiers)}
isSeriesHidden={isSeriesHidden}
/>
Expand Down