Skip to content

Commit 319cf9b

Browse files
GasimGasimzadaiansu
authored andcommitted
Add forward ref to SVG Component (#5457)
* 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
1 parent 67e8e2c commit 319cf9b

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

packages/react-scripts/config/jest/fileTransform.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
// @remove-on-eject-begin
2-
/**
3-
* Copyright (c) 2014-present, Facebook, Inc.
4-
*
5-
* This source code is licensed under the MIT license found in the
6-
* LICENSE file in the root directory of this source tree.
7-
*/
8-
// @remove-on-eject-end
91
'use strict';
102

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

2012
if (filename.match(/\.svg$/)) {
21-
return `module.exports = {
13+
return `const React = require('react');
14+
module.exports = {
2215
__esModule: true,
2316
default: ${assetFilename},
24-
ReactComponent: (props) => ({
17+
ReactComponent: React.forwardRef((props, ref) => ({
2518
$$typeof: Symbol.for('react.element'),
2619
type: 'svg',
27-
ref: null,
20+
ref: ref,
2821
key: null,
2922
props: Object.assign({}, props, {
3023
children: ${assetFilename}
3124
})
32-
}),
25+
})),
3326
};`;
3427
}
3528

packages/react-scripts/config/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ module.exports = function(webpackEnv) {
377377
{
378378
loaderMap: {
379379
svg: {
380-
ReactComponent: '@svgr/webpack?-svgo![path]',
380+
ReactComponent: '@svgr/webpack?-svgo,+ref![path]',
381381
},
382382
},
383383
},

packages/react-scripts/fixtures/kitchensink/src/features/webpack/SvgComponent.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@
88
import React from 'react';
99
import { ReactComponent as Logo } from './assets/logo.svg';
1010

11-
export default () => <Logo id="feature-svg-component" />;
11+
export default () => {
12+
return <Logo id="feature-svg-component" />;
13+
};
14+
15+
export const SvgComponentWithRef = React.forwardRef((props, ref) => (
16+
<Logo id="feature-svg-component-with-ref" ref={ref} />
17+
));

packages/react-scripts/fixtures/kitchensink/src/features/webpack/SvgComponent.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@
77

88
import React from 'react';
99
import ReactDOM from 'react-dom';
10-
import SvgComponent from './SvgComponent';
10+
import SvgComponent, { SvgComponentWithRef } from './SvgComponent';
1111

1212
describe('svg component', () => {
1313
it('renders without crashing', () => {
1414
const div = document.createElement('div');
1515
ReactDOM.render(<SvgComponent />, div);
1616
expect(div.textContent).toBe('logo.svg');
1717
});
18+
19+
it('svg root element equals the passed ref', () => {
20+
const div = document.createElement('div');
21+
const someRef = React.createRef();
22+
ReactDOM.render(<SvgComponentWithRef ref={someRef} />, div);
23+
const svgElement = div.getElementsByTagName('svg');
24+
expect(svgElement).toHaveLength(1);
25+
expect(svgElement[0]).toBe(someRef.current);
26+
});
1827
});

0 commit comments

Comments
 (0)