Skip to content

Commit

Permalink
Add forward ref to SVG Component (#5457)
Browse files Browse the repository at this point in the history
* Add forward ref to SVG component

* Write proper component for the ref test

* Add ref to jest svg transform and fix tests

* Fix SVG file location

* Use proper `ref` instead of svgRef in tests

* Add ref to svgr loader
  • Loading branch information
GasimGasimzada authored and iansu committed Feb 14, 2019
1 parent 67e8e2c commit 319cf9b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
17 changes: 5 additions & 12 deletions packages/react-scripts/config/jest/fileTransform.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
// @remove-on-eject-begin
/**
* Copyright (c) 2014-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// @remove-on-eject-end
'use strict';

const path = require('path');
Expand All @@ -18,18 +10,19 @@ module.exports = {
const assetFilename = JSON.stringify(path.basename(filename));

if (filename.match(/\.svg$/)) {
return `module.exports = {
return `const React = require('react');
module.exports = {
__esModule: true,
default: ${assetFilename},
ReactComponent: (props) => ({
ReactComponent: React.forwardRef((props, ref) => ({
$$typeof: Symbol.for('react.element'),
type: 'svg',
ref: null,
ref: ref,
key: null,
props: Object.assign({}, props, {
children: ${assetFilename}
})
}),
})),
};`;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/react-scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ module.exports = function(webpackEnv) {
{
loaderMap: {
svg: {
ReactComponent: '@svgr/webpack?-svgo![path]',
ReactComponent: '@svgr/webpack?-svgo,+ref![path]',
},
},
},
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 = React.forwardRef((props, ref) => (
<Logo id="feature-svg-component-with-ref" ref={ref} />
));
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 equals the passed ref', () => {
const div = document.createElement('div');
const someRef = React.createRef();
ReactDOM.render(<SvgComponentWithRef ref={someRef} />, div);
const svgElement = div.getElementsByTagName('svg');
expect(svgElement).toHaveLength(1);
expect(svgElement[0]).toBe(someRef.current);
});
});

0 comments on commit 319cf9b

Please sign in to comment.