Skip to content

Commit ce2cf7c

Browse files
authored
Merge pull request #597 from adobe/clamoureux/legendTitleLimit
feat: add titleLimit to legend
2 parents e0cf606 + adf9c10 commit ce2cf7c

File tree

7 files changed

+58
-6
lines changed

7 files changed

+58
-6
lines changed

packages/react-spectrum-charts/src/components/Legend/Legend.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const Legend: FC<LegendProps> = ({
2323
highlight = false,
2424
isToggleable = false,
2525
legendLabels,
26+
titleLimit,
2627
lineType,
2728
lineWidth,
2829
onClick,

packages/react-spectrum-charts/src/rscToSbAdapter/legendAdapter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { childrenToOptions } from './childrenAdapter';
1616

1717
export const getLegendOptions = ({
1818
children,
19+
titleLimit,
1920
onClick,
2021
onMouseOut,
2122
onMouseOver,
@@ -25,6 +26,7 @@ export const getLegendOptions = ({
2526

2627
return {
2728
...legendProps,
29+
...(titleLimit !== undefined ? { titleLimit } : {}),
2830
hasOnClick: Boolean(onClick),
2931
hasMouseInteraction: Boolean(onMouseOut || onMouseOver),
3032
chartPopovers,

packages/react-spectrum-charts/src/stories/components/Legend/Legend.story.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ Labels.args = { legendLabels, highlight: true, ...defaultProps };
5353
const LabelLimit = LegendBarStory.bind({});
5454
LabelLimit.args = { legendLabels: truncatedLegendLabels, ...defaultProps };
5555

56+
const TitleLimit = LegendBarStory.bind({});
57+
TitleLimit.args = {
58+
title: 'Very long legend title that should be truncated',
59+
titleLimit: 250,
60+
...defaultProps,
61+
};
62+
5663
const OnClick = LegendBarStory.bind({});
5764
OnClick.args = {};
5865

@@ -77,4 +84,16 @@ Supreme.args = {
7784
title: 'Operating system',
7885
};
7986

80-
export { Basic, Descriptions, Disconnected, Labels, LabelLimit, OnClick, Popover, Position, Title, Supreme };
87+
export {
88+
Basic,
89+
Descriptions,
90+
Disconnected,
91+
Labels,
92+
LabelLimit,
93+
TitleLimit,
94+
OnClick,
95+
Popover,
96+
Position,
97+
Title,
98+
Supreme,
99+
};

packages/react-spectrum-charts/src/stories/components/Legend/Legend.test.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
waitFor,
2929
} from '../../../test-utils';
3030
import '../../../test-utils/__mocks__/matchMedia.mock.js';
31-
import { Basic, Descriptions, LabelLimit, OnClick, Popover, Position, Supreme, Title } from './Legend.story';
31+
import { Basic, Descriptions, LabelLimit, OnClick, Popover, Position, Supreme, Title, TitleLimit } from './Legend.story';
3232

3333
/**
3434
* Wait for the the duration of the legend tooltip hover delay.
@@ -286,6 +286,15 @@ describe('Legend', () => {
286286
).toBeInTheDocument();
287287
});
288288

289+
test('renders with titleLimit', async () => {
290+
render(<TitleLimit {...TitleLimit.args} />);
291+
const view = await screen.findByRole('graphics-document');
292+
expect(view).toBeInTheDocument();
293+
// Check that the legend title is present. Note that JSDOM causes this to not match the UI rendered storybook.
294+
// We're just testing that the chart renders with the titleLimit prop.
295+
expect(screen.getByText('Very long legend title that should be truncated')).toBeInTheDocument();
296+
});
297+
289298
// Legend is not a real React component. This is test just provides test coverage for sonarqube
290299
test('Legend pseudo element', async () => {
291300
render(<Legend />);

packages/vega-spec-builder/src/legend/legendSpecBuilder.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,17 @@ describe('addLegend()', () => {
278278
expect(legendSpec.scales).toEqual([...(defaultSpec.scales || []), defaultLegendEntriesScale]);
279279
});
280280

281+
test('should add titleLimit if provided', () => {
282+
const legendSpec = addLegend(defaultSpec, {
283+
descriptions: [{ seriesName: 'test', description: 'test' }],
284+
title: 'My title',
285+
titleLimit: 123,
286+
});
287+
const legend = legendSpec.legends?.[0];
288+
expect(legend?.titleLimit).toBe(123);
289+
expect(legend?.title).toBe('My title');
290+
});
291+
281292
test('should add fields to scales if they have not been added', () => {
282293
const legendSpec = addLegend(
283294
{ ...defaultSpec, scales: [{ name: COLOR_SCALE, type: 'ordinal' }] },

packages/vega-spec-builder/src/legend/legendSpecBuilder.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ export const formatFacetRefsWithPresets = (
197197
* @returns
198198
*/
199199
const getCategoricalLegend = (facets: Facet[], options: LegendSpecOptions): Legend => {
200-
const { name, position, title, labelLimit } = options;
201-
return {
200+
const { name, position, title, labelLimit, titleLimit } = options;
201+
const legend: Legend = {
202202
fill: `${name}Entries`,
203203
direction: ['top', 'bottom'].includes(position) ? 'horizontal' : 'vertical',
204204
orient: position,
@@ -207,6 +207,8 @@ const getCategoricalLegend = (facets: Facet[], options: LegendSpecOptions): Lege
207207
columns: getColumns(position),
208208
labelLimit,
209209
};
210+
if (titleLimit !== undefined) legend.titleLimit = titleLimit;
211+
return legend;
210212
};
211213

212214
/**
@@ -233,8 +235,14 @@ export const getContinuousLegend = (facet: Facet, options: LegendSpecOptions): L
233235
};
234236
};
235237

236-
const getLegendLayout = ({ position, title }: LegendSpecOptions): Partial<Legend> => {
237-
return { direction: ['top', 'bottom'].includes(position) ? 'horizontal' : 'vertical', orient: position, title };
238+
const getLegendLayout = ({ position, title, titleLimit }: LegendSpecOptions): Partial<Legend> => {
239+
const layout: Partial<Legend> = {
240+
direction: ['top', 'bottom'].includes(position) ? 'horizontal' : 'vertical',
241+
orient: position,
242+
title,
243+
};
244+
if (titleLimit !== undefined) layout.titleLimit = titleLimit;
245+
return layout;
238246
};
239247

240248
/**

packages/vega-spec-builder/src/types/legendSpec.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export interface LegendOptions {
6262
symbolShape?: SymbolShapeFacet;
6363
/** legend title */
6464
title?: string;
65+
/** The maximum allowed length in pixels of the legend title. */
66+
titleLimit?: number;
6567

6668
// children
6769
chartPopovers?: ChartPopoverOptions[];

0 commit comments

Comments
 (0)