Skip to content

Commit

Permalink
feat(core): add "memo" option
Browse files Browse the repository at this point in the history
Closes #230
  • Loading branch information
gregberge committed Dec 23, 2019
1 parent 9d7fb34 commit f0e3793
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 4 deletions.
38 changes: 38 additions & 0 deletions packages/babel-plugin-transform-svg-component/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,42 @@ describe('plugin', () => {
export default ForwardRef;"
`)
})

it('should work with memo', () => {
const { code } = testPlugin('<svg><div /></svg>', {
state: { componentName: 'SvgComponent' },
memo: true,
})
expect(code).toMatchInlineSnapshot(`
"import React from \\"react\\";
function SvgComponent() {
return <svg><div /></svg>;
}
const MemoSvgComponent = React.memo(SvgComponent);
export default MemoSvgComponent;"
`)
})

it('should work with memo + ref', () => {
const { code } = testPlugin('<svg><div /></svg>', {
state: { componentName: 'SvgComponent' },
memo: true,
ref: true,
})
expect(code).toMatchInlineSnapshot(`
"import React from \\"react\\";
function SvgComponent({
svgRef
}) {
return <svg><div /></svg>;
}
const MemoSvgComponent = React.memo(SvgComponent);
const ForwardRef = React.forwardRef((props, ref) => <MemoSvgComponent svgRef={ref} {...props} />);
export default ForwardRef;"
`)
})
})
13 changes: 9 additions & 4 deletions packages/babel-plugin-transform-svg-component/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,16 @@ export const getExport = ({ template }, opts) => {
let result = ''
let exportName = opts.state.componentName

if (opts.memo) {
const nextExportName = `Memo${exportName}`
result += `const ${nextExportName} = React.memo(${exportName})\n\n`
exportName = nextExportName
}

if (opts.ref) {
exportName = 'ForwardRef'
result += `const ForwardRef = React.forwardRef((props, ref) => <${
opts.state.componentName
} svgRef={ref} {...props} />)\n\n`
const nextExportName = `ForwardRef`
result += `const ${nextExportName} = React.forwardRef((props, ref) => <${exportName} svgRef={ref} {...props} />)\n\n`
exportName = nextExportName
}

if (opts.state.caller && opts.state.caller.previousExport) {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ program
)
.option('--icon', 'use "1em" as width and height')
.option('--native', 'add react-native support with react-native-svg')
.option('--memo', 'add React.memo into the result component')
.option('--ref', 'forward ref to SVG root element')
.option('--no-dimensions', 'remove width and height from root SVG tag')
.option(
Expand Down
24 changes: 24 additions & 0 deletions packages/core/src/__snapshots__/convert.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,30 @@ export default SvgComponent
"
`;

exports[`convert config should support options 17 1`] = `
"import React from 'react'
function SvgComponent(props) {
return (
<svg width={88} height={88} {...props}>
<g
stroke=\\"#063855\\"
strokeWidth={2}
fill=\\"none\\"
fillRule=\\"evenodd\\"
strokeLinecap=\\"square\\"
>
<path d=\\"M51 37L37 51M51 51L37 37\\" />
</g>
</svg>
)
}
const MemoSvgComponent = React.memo(SvgComponent)
export default MemoSvgComponent
"
`;

exports[`convert config titleProp: without title added 1`] = `
"import React from 'react'
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const DEFAULT_CONFIG = {
native: false,
prettier: true,
prettierConfig: null,
memo: false,
ref: false,
replaceAttrValues: null,
svgProps: null,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ describe('convert', () => {
template.ast`const noop = () => null; export default noop;`,
},
{ titleProp: true },
{ memo: true },
]

test.each(configs)('should support options %#', async config => {
Expand Down

0 comments on commit f0e3793

Please sign in to comment.