Skip to content

Commit

Permalink
fix: fix typescript types (ref, title) (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmvp authored Mar 26, 2020
1 parent 868e5dc commit 6e7e6b2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ function SvgComponent({
return <svg><g /></svg>;
}
const ForwardRef = React.forwardRef((props, ref) => <SvgComponent svgRef={ref} {...props} />);
const ForwardRef = React.forwardRef((props, ref: React.Ref<SVGSVGElement>) => <SvgComponent svgRef={ref} {...props} />);
export default ForwardRef;"
`;
Expand All @@ -241,15 +241,15 @@ function SvgComponent({
return <svg><g /></svg>;
}
const ForwardRef = React.forwardRef((props, ref) => <SvgComponent svgRef={ref} {...props} />);
const ForwardRef = React.forwardRef((props, ref: React.Ref<SVGSVGElement>) => <SvgComponent svgRef={ref} {...props} />);
export default ForwardRef;"
`;
exports[`plugin typescript with "titleProp" adds "titleProp" and "titleId" prop 1`] = `
"import * as React from \\"react\\";
interface SVGRProps {
title?: String,
titleId?: String,
title?: string,
titleId?: string,
}
function SvgComponent({
Expand All @@ -275,6 +275,6 @@ function SvgComponent({
}
const MemoSvgComponent = React.memo(SvgComponent);
const ForwardRef = React.forwardRef((props, ref) => <MemoSvgComponent svgRef={ref} {...props} />);
const ForwardRef = React.forwardRef((props, ref: React.Ref<SVGSVGElement>) => <MemoSvgComponent svgRef={ref} {...props} />);
export default ForwardRef;"
`;
21 changes: 16 additions & 5 deletions packages/babel-plugin-transform-svg-component/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ export const getInterface = ({ types: t }, opts) => {
}
if (opts.titleProp) {
properties.push(
objectTypeProperty(t.identifier('title'), t.identifier('String'), true),
objectTypeProperty(t.identifier('title'), t.identifier('string'), true),
)
properties.push(
objectTypeProperty(t.identifier('titleId'), t.identifier('String'), true),
objectTypeProperty(t.identifier('titleId'), t.identifier('string'), true),
)
}
if (properties.length === 0) return null
Expand Down Expand Up @@ -192,6 +192,11 @@ export const getImport = ({ types: t }, opts) => {
export const getExport = ({ template }, opts) => {
let result = ''
let exportName = opts.state.componentName
const plugins = ['jsx']

if (opts.typescript) {
plugins.push('typescript')
}

if (opts.memo) {
const nextExportName = `Memo${exportName}`
Expand All @@ -201,20 +206,26 @@ export const getExport = ({ template }, opts) => {

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

if (opts.typescript) {
refType = ': React.Ref<SVGSVGElement>'
}

result += `const ${nextExportName} = React.forwardRef((props, ref${refType}) => <${exportName} svgRef={ref} {...props} />)\n\n`
exportName = nextExportName
}

if (opts.state.caller && opts.state.caller.previousExport) {
result += `${opts.state.caller.previousExport}\n`
result += `export { ${exportName} as ReactComponent }`
return template.ast(result, {
plugins: ['jsx'],
plugins,
})
}

result += `export default ${exportName}`
return template.ast(result, {
plugins: ['jsx'],
plugins,
})
}
38 changes: 37 additions & 1 deletion packages/cli/src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,42 @@ export default SvgFile
"
`;

exports[`cli should support various args: --typescript --ref --title-prop 1`] = `
"import * as React from 'react'
interface SVGRProps {
svgRef?: React.Ref<SVGSVGElement>;
title?: string;
titleId?: string;
}
function SvgFile({
svgRef,
title,
titleId,
...props
}: React.SVGProps<SVGSVGElement> & SVGRProps) {
return (
<svg
width={48}
height={1}
ref={svgRef}
aria-labelledby={titleId}
{...props}
>
{title ? <title id={titleId}>{title}</title> : null}
<path d=\\"M0 0h48v1H0z\\" fill=\\"#063855\\" fillRule=\\"evenodd\\" />
</svg>
)
}
const ForwardRef = React.forwardRef((props, ref: React.Ref<SVGSVGElement>) => (
<SvgFile svgRef={ref} {...props} />
))
export default ForwardRef
"
`;
exports[`cli should support various args: --typescript --ref 1`] = `
"import * as React from 'react'
interface SVGRProps {
Expand All @@ -421,7 +457,7 @@ function SvgFile({
)
}
const ForwardRef = React.forwardRef((props, ref) => (
const ForwardRef = React.forwardRef((props, ref: React.Ref<SVGSVGElement>) => (
<SvgFile svgRef={ref} {...props} />
))
export default ForwardRef
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ describe('cli', () => {
['--title-prop'],
['--typescript'],
['--typescript --ref'],
['--typescript --ref --title-prop'],
])(
'should support various args',
async args => {
Expand Down

1 comment on commit 6e7e6b2

@vercel
Copy link

@vercel vercel bot commented on 6e7e6b2 Mar 26, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.