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

add diagonal right to left pattern #744

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
12 changes: 11 additions & 1 deletion packages/vx-demo/src/sandboxes/vx-pattern/Example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ const Patterns: React.FC<{ id: string }>[] = [
orientation={['diagonal']}
/>
),
({ id }) => (
<PatternLines
id={id}
height={6}
width={6}
stroke="black"
strokeWidth={1}
orientation={['diagonalRightToLeft']}
/>
),
({ id }) => (
<PatternLines
id={id}
Expand Down Expand Up @@ -100,7 +110,7 @@ const Patterns: React.FC<{ id: string }>[] = [
];

export default function Example({ width, height, margin = defaultMargin }: PatternProps) {
const numColumns = width > 600 ? 4 : 2;
const numColumns = 3;
const numRows = Patterns.length / numColumns;
const columnWidth = Math.max((width - margin.left - margin.right) / numColumns, 0);
const rowHeight = Math.max((height - margin.bottom - margin.top) / numRows, 0);
Expand Down
3 changes: 2 additions & 1 deletion packages/vx-pattern/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export type PatternOrientationType = 'horizontal' | 'vertical' | 'diagonal';
export type PatternOrientationType = 'horizontal' | 'vertical' | 'diagonal' | 'diagonalRightToLeft';

export const PatternOrientation: { [key in PatternOrientationType]: key } = {
horizontal: 'horizontal',
vertical: 'vertical',
diagonal: 'diagonal',
diagonalRightToLeft: 'diagonalRightToLeft',
};
6 changes: 5 additions & 1 deletion packages/vx-pattern/src/patterns/Lines.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import cx from 'classnames';
import Pattern from './Pattern';
import { PatternOrientation, PatternOrientationType } from '../constants';

function pathForOrientation({
export function pathForOrientation({
height,
orientation,
}: {
Expand All @@ -19,6 +19,10 @@ function pathForOrientation({
return `M 0,${height} l ${height},${-height} M ${-height / 4},${height / 4} l ${height /
2},${-height / 2}
M ${(3 / 4) * height},${(5 / 4) * height} l ${height / 2},${-height / 2}`;
case PatternOrientation.diagonalRightToLeft:
return `M 0,0 l ${height},${height}
M ${-height / 4},${(3 / 4) * height} l ${height / 2},${height / 2}
M ${(3 / 4) * height},${-height / 4} l ${height / 2},${height / 2}`;
default:
return `M ${height / 2}, 0 l 0, ${height}`;
}
Expand Down
15 changes: 15 additions & 0 deletions packages/vx-pattern/test/PatternLines.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React from 'react';
import { shallow } from 'enzyme';

import { PatternLines } from '../src';
import { PatternOrientationType } from '../src/constants';
import { pathForOrientation } from '../src/patterns/Lines';

describe('<PatternLines />', () => {
test('it should be defined', () => {
Expand Down Expand Up @@ -32,4 +34,17 @@ describe('<PatternLines />', () => {
const wrapper = shallow(<PatternLines id="test" height={4} width={4} />);
expect(wrapper.find('rect')).toHaveLength(0);
});

test('it should render only the specified pattern lines', () => {
const size = 4;
const orientation: PatternOrientationType[] = ['diagonal', 'diagonalRightToLeft'];
const renderedPaths = orientation.map(o =>
pathForOrientation({ orientation: o, height: size }),
);
const wrapper = shallow(
<PatternLines id="test" height={size} width={size} orientation={orientation} />,
);
expect(wrapper.find('path')).toHaveLength(2);
expect(wrapper.find('path').map(path => path.prop('d'))).toEqual(renderedPaths);
});
});