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

new(vx-legend): add Line, legendLabelProps, more props in renderShape #749

Merged
merged 3 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions packages/vx-legend/src/legends/Legend/LegendLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import React from 'react';

export type LegendLabelProps = {
export type LegendLabelOwnProps = {
align?: string;
label?: React.ReactNode;
flex?: string | number;
margin?: string | number;
children?: React.ReactNode;
};

export type LegendLabelProps = LegendLabelOwnProps &
Omit<React.HTMLProps<HTMLDivElement>, keyof LegendLabelOwnProps>;

export default function LegendLabel({
flex = '1',
label,
margin = '5px 0',
align = 'left',
children,
...restProps
}: LegendLabelProps & Omit<React.HTMLProps<HTMLDivElement>, keyof LegendLabelProps>) {
}: LegendLabelProps) {
return (
<div
className="vx-legend-label"
Expand Down
6 changes: 6 additions & 0 deletions packages/vx-legend/src/legends/Legend/LegendShape.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { FormattedLabel, LegendShape as LegendShapeType } from '../../types';

export type LegendShapeProps<Data, Output> = {
label: FormattedLabel<Data, Output>;
item: Data;
itemIndex: number;
margin?: string | number;
shape?: LegendShapeType<Data, Output>;
fill?: (label: FormattedLabel<Data, Output>) => any;
Expand All @@ -20,6 +22,8 @@ export default function LegendShape<Data, Output>({
height,
margin,
label,
item,
itemIndex,
fill,
size,
shapeStyle,
Expand All @@ -36,6 +40,8 @@ export default function LegendShape<Data, Output>({
>
{renderShape<Data, Output>({
shape,
item,
itemIndex,
label,
width,
height,
Expand Down
8 changes: 7 additions & 1 deletion packages/vx-legend/src/legends/Legend/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import cx from 'classnames';
import LegendItem from './LegendItem';
import LegendLabel from './LegendLabel';
import LegendLabel, { LegendLabelProps } from './LegendLabel';
import LegendShape from './LegendShape';
import valueOrIdentity, { valueOrIdentityString } from '../../util/valueOrIdentity';
import labelTransformFactory from '../../util/labelTransformFactory';
Expand Down Expand Up @@ -55,6 +55,8 @@ export type LegendProps<Datum, Output, Scale = ScaleType<Datum, Output>> = {
labelFormat?: LabelFormatter<Datum>;
/** Given the legend scale and labelFormatter, returns a label with datum, index, value, and label. */
labelTransform?: LabelFormatterFactory<Datum, Output, Scale>;
/** Additional props to be set on LegendLabel. */
legendLabelProps?: Partial<LegendLabelProps>;
};

const defaultStyle = {
Expand All @@ -81,6 +83,7 @@ export default function Legend<Datum, Output, Scale = ScaleType<Datum, Output>>(
itemMargin = '0',
direction = 'column',
itemDirection = 'row',
legendLabelProps,
children,
...legendItemProps
}: LegendProps<Datum, Output, Scale>) {
Expand Down Expand Up @@ -112,6 +115,8 @@ export default function Legend<Datum, Output, Scale = ScaleType<Datum, Output>>(
height={shapeHeight}
width={shapeWidth}
margin={shapeMargin}
item={domain[i]}
itemIndex={i}
label={label}
fill={fill}
size={size}
Expand All @@ -122,6 +127,7 @@ export default function Legend<Datum, Output, Scale = ScaleType<Datum, Output>>(
flex={labelFlex}
margin={labelMargin}
align={labelAlign}
{...legendLabelProps}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously this couldn't be customized at all

/>
</LegendItem>
))}
Expand Down
2 changes: 1 addition & 1 deletion packages/vx-legend/src/shapes/Circle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function ShapeCircle({ fill, width, height, style }: ShapeCircleP
return (
<svg width={size} height={size}>
<Group top={radius} left={radius}>
<circle r={radius} fill={String(fill)} style={style} />
<circle r={radius} fill={fill} style={style} />
</Group>
</svg>
);
Expand Down
29 changes: 29 additions & 0 deletions packages/vx-legend/src/shapes/Line.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import { Group } from '@vx/group';

export type ShapeShapeLineProps = {
fill?: string;
width?: string | number;
height?: string | number;
style?: React.CSSProperties;
};

export default function ShapeLine({ fill, width, height, style }: ShapeShapeLineProps) {
const cleanHeight = typeof height === 'string' || typeof height === 'undefined' ? 0 : height;
const lineThickness = typeof style?.strokeWidth === 'number' ? style?.strokeWidth : 2;
return (
<svg width={width} height={height}>
<Group top={cleanHeight / 2 - lineThickness / 2}>
<line
x1={0}
x2={width}
y1={0}
y2={0}
stroke={fill}
strokeWidth={lineThickness}
style={style}
/>
</Group>
</svg>
);
}
6 changes: 4 additions & 2 deletions packages/vx-legend/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,18 @@ export type ItemTransformer<Datum, Output> = (
export type RenderShapeProvidedProps<Data, Output> = {
width?: string | number;
height?: string | number;
label?: FormattedLabel<Data, Output>;
label: FormattedLabel<Data, Output>;
item: Data;
itemIndex: number;
fill?: string;
size?: string | number;
// TODO: ideally this would support SVGProps, but this is invalid for Rect/Circle shapes
style?: React.CSSProperties;
};

export type LegendShape<Data, Output> =
| 'rect'
| 'circle'
| 'line'
| React.FC<RenderShapeProvidedProps<Data, Output>>
| React.ComponentClass<RenderShapeProvidedProps<Data, Output>>;

Expand Down
16 changes: 13 additions & 3 deletions packages/vx-legend/src/util/renderShape.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import RectShape from '../shapes/Rect';
import CircleShape from '../shapes/Circle';
import LineShape from '../shapes/Line';

import {
LegendShape,
Expand All @@ -14,6 +15,8 @@ import {
type RenderShapeArgs<Data, Output> = {
shape?: LegendShape<Data, Output>;
label: FormattedLabel<Data, Output>;
item: Data;
itemIndex: number;
fill?: FillAccessor<Data, Output>;
size?: SizeAccessor<Data, Output>;
shapeStyle?: ShapeStyleAccessor<Data, Output>;
Expand All @@ -30,22 +33,29 @@ export default function renderShape<Data, Output>({
width,
height,
label,
item,
itemIndex,
shapeStyle = NO_OP,
}: RenderShapeArgs<Data, Output>) {
const props: RenderShapeProvidedProps<Data, Output> = {
width,
height,
item,
itemIndex,
label,
fill: fill({ ...label }),
size: size({ ...label }),
style: shapeStyle({ ...label }),
};

if (typeof shape === 'string') {
if (shape === 'rect') {
return React.createElement(RectShape, props);
if (shape === 'circle') {
return React.createElement(CircleShape, props);
}
return React.createElement(CircleShape, props);
if (shape === 'line') {
return React.createElement(LineShape, props);
}
return React.createElement(RectShape, props);
}
if (React.isValidElement(shape)) {
return React.cloneElement(shape, props);
Expand Down
9 changes: 8 additions & 1 deletion packages/vx-legend/test/Legend.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { shallow } from 'enzyme';
import { scaleLinear } from '@vx/scale';

import { Legend } from '../src';
import { Legend, LegendLabel } from '../src';

const defaultProps = {
scale: scaleLinear<number>({
Expand Down Expand Up @@ -39,4 +39,11 @@ describe('<Legend />', () => {
flexDirection: 'row',
});
});

test('it should pass through legendLabelProps to legend labels', () => {
const style = { fontFamily: 'Comic Sans' };
const wrapper = shallow(<Legend {...defaultProps} legendLabelProps={{ style }} />);
const label = wrapper.find(LegendLabel).first();
expect(label.prop('style')).toEqual(style);
});
});