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 forward ref to React SVG Component #5457

Merged
merged 7 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions packages/react-scripts/config/jest/fileTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ module.exports = {
return `module.exports = {
__esModule: true,
default: ${assetFilename},
ReactComponent: (props) => ({
ReactComponent: ({ svgRef, ...props }) => ({
$$typeof: Symbol.for('react.element'),
type: 'svg',
ref: null,
ref: svgRef || null,
key: null,
props: Object.assign({}, props, {
children: ${assetFilename}
Expand Down
3 changes: 2 additions & 1 deletion packages/react-scripts/config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ module.exports = {
{
loaderMap: {
svg: {
ReactComponent: '@svgr/webpack?-prettier,-svgo![path]',
ReactComponent:
'@svgr/webpack?-prettier,+ref,-svgo![path]',
},
},
},
Expand Down
6 changes: 4 additions & 2 deletions packages/react-scripts/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ module.exports = {
{
loaderMap: {
svg: {
ReactComponent: '@svgr/webpack?-prettier,-svgo![path]',
ReactComponent:
'@svgr/webpack?-prettier,+ref,-svgo![path]',
},
},
},
Expand Down Expand Up @@ -476,7 +477,8 @@ module.exports = {
}),
// Inlines the webpack runtime script. This script is too small to warrant
// a network request.
shouldInlineRuntimeChunk && new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/runtime~.+[.]js/]),
shouldInlineRuntimeChunk &&
new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/runtime~.+[.]js/]),
// Makes some environment variables available in index.html.
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@
import React from 'react';
import { ReactComponent as Logo } from './assets/logo.svg';

export default () => <Logo id="feature-svg-component" />;
export default () => {
return <Logo id="feature-svg-component" />;
};

export const SvgComponentWithRef = ({ svgRef }) => (
<Logo id="feature-svg-component-with-ref" svgRef={svgRef} />
);
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@

import React from 'react';
import ReactDOM from 'react-dom';
import SvgComponent from './SvgComponent';
import SvgComponent, { SvgComponentWithRef } from './SvgComponent';

describe('svg component', () => {
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<SvgComponent />, div);
expect(div.textContent).toBe('logo.svg');
});

it('svg root element ref is passed svgRef', () => {
const div = document.createElement('div');
const someRef = React.createRef();
ReactDOM.render(<SvgComponentWithRef svgRef={someRef} />, div);
const svgElement = div.getElementsByTagName('svg');
expect(svgElement).toHaveLength(1);
expect(svgElement[0]).toBe(someRef.current);
});
});