Skip to content

Commit

Permalink
feat: use forwardRef on React Native
Browse files Browse the repository at this point in the history
See #184
  • Loading branch information
gregberge committed Oct 1, 2018
1 parent 7535693 commit 4bdd989
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
6 changes: 5 additions & 1 deletion packages/cli/src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ const SvgFile = ({ svgRef, ...props }) => (
</Svg>
)
export default SvgFile
const ForwardRef = React.forwardRef((props, ref) => (
<SvgFile svgRef={ref} {...props} />
))
export default ForwardRef
"
`;
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/__snapshots__/convert.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ const SvgComponent = ({ svgRef, ...props }) => (
</Svg>
)
export default SvgComponent
const ForwardRef = React.forwardRef((props, ref) => (
<SvgComponent svgRef={ref} {...props} />
))
export default ForwardRef
"
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ exports[`reactNativeTemplate should wrap code into a component 3`] = `
import Svg from 'react-native-svg';
const Test = ({ svgRef }) => <Svg />
export default Test"
const ForwardRef = React.forwardRef((props, ref) => <Test svgRef={ref} {...props} />)
export default ForwardRef"
`;

exports[`reactNativeTemplate should wrap code into a component 4`] = `
Expand All @@ -55,7 +57,9 @@ exports[`reactNativeTemplate should wrap code into a component 5`] = `
import Svg from 'react-native-svg';
const Test = ({ svgRef, title }) => <Svg />
export default Test"
const ForwardRef = React.forwardRef((props, ref) => <Test svgRef={ref} {...props} />)
export default ForwardRef"
`;

exports[`reactNativeTemplate should wrap code into a component 6`] = `
Expand All @@ -71,5 +75,7 @@ exports[`reactNativeTemplate should wrap code into a component 7`] = `
import Svg from 'react-native-svg';
const Test = ({ svgRef, ...props }) => <Svg />
export default Test"
const ForwardRef = React.forwardRef((props, ref) => <Test svgRef={ref} {...props} />)
export default ForwardRef"
`;
5 changes: 3 additions & 2 deletions packages/core/src/templates/reactNativeTemplate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getProps } from './util'
import { getProps, getForwardRef, getExportName } from './util'

const getComponents = components => {
if (!components.size) return ''
Expand Down Expand Up @@ -30,7 +30,8 @@ const reactNativeTemplate = (code, config, state) => {
result += `import Svg${components} from 'react-native-svg';\n`
result += warnLog
result += `const ${state.componentName} = ${props} => ${code}\n\n`
result += `export default ${state.componentName}`
result += getForwardRef(config, state)
result += `export default ${getExportName(config, state)}`

return result
}
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/templates/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@ export const getProps = config => {
return `({ ${props.join(', ')} })`
}

export const getExportName = (config, state) =>
config.ref ? 'ForwardRef' : state.componentName

export const getExport = (config, state) => {
const component = config.ref ? 'ForwardRef' : state.componentName
const exportName = getExportName(config, state)
if (state.webpack && state.webpack.previousExport) {
let result = ''
result += `export default ${state.webpack.previousExport}\n`
result += `export { ${component} as ReactComponent }`
result += `export { ${exportName} as ReactComponent }`
return result
}
if (state.rollup && state.rollup.previousExport) {
let result = ''
result += `${state.rollup.previousExport}\n`
result += `export { ${component} as ReactComponent }`
result += `export { ${exportName} as ReactComponent }`
return result
}
return `export default ${component}`
return `export default ${exportName}`
}

export const getForwardRef = (config, state) => {
Expand Down

0 comments on commit 4bdd989

Please sign in to comment.