Skip to content

Commit

Permalink
Merge pull request #65 from joemaddalone/sxy
Browse files Browse the repository at this point in the history
feat(shorthand): docs and tests for shorthands: exy, sxy, cxy, and rxy
  • Loading branch information
joemaddalone committed Dec 31, 2020
2 parents d17dd28 + fd42055 commit 68deb49
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 38 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ const App = () => {

Writing path commands via the library makes creating paths super simple and intuitive. However it can seem like overkill for really common patterns you may need. react-svg-path includes a number of components to allow for a declarative interface for generating the paths you need.

_Note: The following shorthand version of some props are available where applicable:_
* **sxy**: sx & sy = sxy
* **exy**: ex & ey = exy
* **cxy**: cx & cy = cxy
* **rxy**: rx & cy = rxy

The following components are available

## Shapes
Expand Down
8 changes: 6 additions & 2 deletions example/src/TestApp.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import React from 'react';
import TargetDemo from './components/demos/targets/TargetDemo';
import { Svg, Line, Circle } from 'react-svg-path';

import './App.css';

const App = () => {
return (
<div>
<TargetDemo />
<Svg width={500} height={500}>
<Line exy={10} sxy={490} stroke='#333'>
<Circle size={10} stroke='#333' />
</Line>
</Svg>
</div>
);
};
Expand Down
25 changes: 24 additions & 1 deletion example/src/components/demo-builders/BasicShapeDemo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import demoDocs from '../../docs/demos';
import './BasicShapeDemo.css';

export const BasicShapeDemo = ({ shape }) => {
const { Component, props } = docs[shape];
const { Component, props, shorthands } = docs[shape];
const demos = demoDocs.basicShapes[shape];
const initialState = demos.map((d, i) => {
return Object.keys(d)
Expand Down Expand Up @@ -41,6 +41,29 @@ export const BasicShapeDemo = ({ shape }) => {
/>
`.trim()}
</code>
{shorthands && (
<>
<p>This component has shorthand props available:</p>
<table className='ui table'>
<thead>
<tr>
<th>prop</th>
<th>description</th>
</tr>
</thead>
<tbody>
{shorthands.map((sh, shi) => {
return (
<tr key={shi}>
<td>{sh.k}</td>
<td>{sh.description}</td>
</tr>
);
})}
</tbody>
</table>
</>
)}
<p>{t(`${shape}.description`)}</p>
{demos &&
demos.map(({ svgDimensions, ...rest }, i) => {
Expand Down
25 changes: 24 additions & 1 deletion example/src/components/demo-builders/CurveDemo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Tabs } from './Tabs';
import { translate as t } from '../../i18n/i18n';

export const CurveDemo = ({ curve }) => {
const { Component, props } = docs[curve];
const { Component, props, shorthands } = docs[curve];
const demos = JSON.parse(JSON.stringify(demoDocs.curves[curve]));
const initialState = demos.map((d, i) => {
return Object.keys(d)
Expand Down Expand Up @@ -38,6 +38,29 @@ export const CurveDemo = ({ curve }) => {
/>
`.trim()}
</code>
{shorthands && (
<>
<p>This component has shorthand props available:</p>
<table className='ui table'>
<thead>
<tr>
<th>prop</th>
<th>description</th>
</tr>
</thead>
<tbody>
{shorthands.map((sh, shi) => {
return (
<tr key={shi}>
<td>{sh.k}</td>
<td>{sh.description}</td>
</tr>
);
})}
</tbody>
</table>
</>
)}
<p>{t(`${curve}.description`)}</p>
{demos &&
demos.map(({ svgDimensions, ...rest }, i) => {
Expand Down
2 changes: 1 addition & 1 deletion example/src/components/demos/targets/Gridset.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class Gridset {
r: rightCell.r,
b: bottomCell.b,
cx: (leftCell.l + w) / 2,
cx: (topCell.t + h) / 2,
cy: (topCell.t + h) / 2,
cells
};
}
Expand Down
86 changes: 70 additions & 16 deletions example/src/docs/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,45 @@ const cy = {
description: 'Center x coordinate of the shape.'
};

export const shorthands = {
sxy: {
...commonNumber,
isRequired: false,
default: null,
k: 'sxy',
p: ['sx', 'sy'],
description:
'If sx and sy are equal values you can use the shortcut "sxy" to set both at once.'
},
exy: {
...commonNumber,
isRequired: false,
default: null,
k: 'exy',
p: ['ex', 'ey'],
description:
'If ex and ey are equal values you can use the shortcut "exy" to set both at once.'
},
cxy: {
...commonNumber,
isRequired: false,
default: null,
k: 'cxy',
p: ['cx', 'cy'],
description:
'If cx and cy are equal values you can use the shortcut "cxy" to set both at once.'
},
rxy: {
...commonNumber,
isRequired: false,
default: null,
k: 'rxy',
p: ['rx', 'ry'],
description:
'If rx and ry are equal values you can use the shortcut "rxy" to set both at once.'
}
};

const centerEnd = {
default: true,
type: 'boolean',
Expand All @@ -63,7 +102,8 @@ const docs = {
cy,
centerEnd
},
nestingProps: centeredShapeNestingProps
nestingProps: centeredShapeNestingProps,
shorthands: [shorthands.cxy]
},
ellipse: {
category: 'basicShapes',
Expand All @@ -85,7 +125,8 @@ const docs = {
cy,
centerEnd
},
nestingProps: centeredShapeNestingProps
nestingProps: centeredShapeNestingProps,
shorthands: [shorthands.cxy]
},
polygon: {
category: 'basicShapes',
Expand Down Expand Up @@ -136,7 +177,8 @@ const docs = {
},
centerEnd
},
nestingProps: centeredShapeNestingProps
nestingProps: centeredShapeNestingProps,
shorthands: [shorthands.cxy]
},
polyline: {
category: 'basicShapes',
Expand Down Expand Up @@ -201,7 +243,8 @@ const docs = {
},
nestingProps: ({ sx, sy, ex, ey }) => {
return { ex: sx, ey: sy, sx: ex, sy: ey };
}
},
shorthands: [shorthands.exy, shorthands.sxy]
},
radialLines: {
category: 'basicShapes',
Expand All @@ -227,7 +270,8 @@ const docs = {
cy,
centerEnd
},
nestingProps: centeredShapeNestingProps
nestingProps: centeredShapeNestingProps,
shorthands: [shorthands.cxy]
},
rect: {
category: 'basicShapes',
Expand All @@ -249,7 +293,8 @@ const docs = {
cy,
centerEnd
},
nestingProps: centeredShapeNestingProps
nestingProps: centeredShapeNestingProps,
shorthands: [shorthands.cxy]
},
regPolygon: {
category: 'basicShapes',
Expand All @@ -271,7 +316,8 @@ const docs = {
cy,
centerEnd
},
nestingProps: centeredShapeNestingProps
nestingProps: centeredShapeNestingProps,
shorthands: [shorthands.cxy]
},
sector: {
category: 'basicShapes',
Expand All @@ -297,7 +343,8 @@ const docs = {
},
centerEnd
},
nestingProps: centeredShapeNestingProps
nestingProps: centeredShapeNestingProps,
shorthands: [shorthands.cxy]
},
segment: {
category: 'basicShapes',
Expand All @@ -323,7 +370,8 @@ const docs = {
},
centerEnd
},
nestingProps: centeredShapeNestingProps
nestingProps: centeredShapeNestingProps,
shorthands: [shorthands.cxy]
},
square: {
category: 'basicShapes',
Expand All @@ -341,7 +389,8 @@ const docs = {
cy,
centerEnd
},
nestingProps: centeredShapeNestingProps
nestingProps: centeredShapeNestingProps,
shorthands: [shorthands.cxy]
},
star: {
category: 'basicShapes',
Expand All @@ -367,7 +416,8 @@ const docs = {
cy,
centerEnd
},
nestingProps: centeredShapeNestingProps
nestingProps: centeredShapeNestingProps,
shorthands: [shorthands.cxy]
},
triangle: {
category: 'basicShapes',
Expand All @@ -385,7 +435,8 @@ const docs = {
cy,
centerEnd
},
nestingProps: centeredShapeNestingProps
nestingProps: centeredShapeNestingProps,
shorthands: [shorthands.cxy]
},
arc: {
category: 'curves',
Expand Down Expand Up @@ -453,7 +504,8 @@ const docs = {
},
nestingProps: ({ sx, sy, ex, ey }) => {
return { ex: sx, ey: sy, sx: ex, sy: ey };
}
},
shorthands: [shorthands.exy, shorthands.sxy, shorthands.rxy]
},
cubic: {
category: 'curves',
Expand Down Expand Up @@ -520,7 +572,7 @@ const docs = {
isRequired: false,
description:
'If set to true all points after sx & sy will become relative to sx & sy.'
}
},
},
nestingProps: ({ sx, sy, ex, ey, S }) => {
let endX = ex;
Expand All @@ -530,7 +582,8 @@ const docs = {
endY = S[S.length - 1][1];
}
return { ex: sx, ey: sy, sx: endX, sy: endY };
}
},
shorthands: [shorthands.exy, shorthands.sxy]
},
quad: {
category: 'curves',
Expand Down Expand Up @@ -598,7 +651,8 @@ const docs = {
endY = T[T.length - 1][1];
}
return { ex: sx, ey: sy, sx: endX, sy: endY };
}
},
shorthands: [shorthands.exy, shorthands.sxy, shorthands.cxy]
}
};

Expand Down
6 changes: 6 additions & 0 deletions scripts/makeReadMe.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ const App = () => {
Writing path commands via the library makes creating paths super simple and intuitive. However it can seem like overkill for really common patterns you may need. react-svg-path includes a number of components to allow for a declarative interface for generating the paths you need.
_Note: The following shorthand version of some props are available where applicable:_
* **sxy**: sx & sy = sxy
* **exy**: ex & ey = exy
* **cxy**: cx & cy = cxy
* **rxy**: rx & cy = rxy
The following components are available
`;
Expand Down
7 changes: 6 additions & 1 deletion src/components/BasicShapes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/* eslint-disable no-prototype-builtins */
import createSimpleComponent from '../utils/createSimpleComponent';
import shorthand from '../utils/shorthand';
import docs from '../docs/docs.mjs';

const shapes = Object.keys(docs).reduce((accum, cur) => {
const component = (props) => createSimpleComponent(docs[cur], props);
const component = (props) => {
const p = props ? shorthand(props) : props;
return createSimpleComponent(docs[cur], p);
};
component.displayName = docs[cur].Component;
accum[docs[cur].Component] = component;
return accum;
Expand Down

0 comments on commit 68deb49

Please sign in to comment.