Skip to content

Commit e32a0cb

Browse files
authored
Use forwardRef rather than custom forwardedRef prop (#503)
1 parent 6e1663b commit e32a0cb

File tree

6 files changed

+20
-27
lines changed

6 files changed

+20
-27
lines changed

index.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// <reference types="react" />
2-
import { CSSProperties, SVGAttributes } from 'react'
2+
import { CSSProperties, SVGAttributes, RefAttributes } from 'react'
33
import {
44
Transform,
55
IconProp,
@@ -20,8 +20,7 @@ export type Props = FontAwesomeIconProps
2020
// This is identical to the version of Omit in Typescript 3.5. It is included for compatibility with older versions of Typescript.
2121
type BackwardCompatibleOmit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
2222

23-
export interface FontAwesomeIconProps extends BackwardCompatibleOmit<SVGAttributes<SVGSVGElement>, 'children' | 'mask' | 'transform'> {
24-
forwardedRef?: ((e: any) => void) | React.MutableRefObject<any> | null
23+
export interface FontAwesomeIconProps extends BackwardCompatibleOmit<SVGAttributes<SVGSVGElement>, 'children' | 'mask' | 'transform'>, RefAttributes<SVGSVGElement> {
2524
icon: IconProp
2625
mask?: IconProp
2726
maskId?: string

index.es.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ function camelize(string) {
195195
return string.substr(0, 1).toLowerCase() + string.substr(1);
196196
}
197197

198-
var _excluded$1 = ["style"];
198+
var _excluded = ["style"];
199199

200200
function capitalize(val) {
201201
return val.charAt(0).toUpperCase() + val.slice(1);
@@ -256,7 +256,7 @@ function convert(createElement, element) {
256256

257257
var _extraProps$style = extraProps.style,
258258
existingStyle = _extraProps$style === void 0 ? {} : _extraProps$style,
259-
remaining = _objectWithoutProperties(extraProps, _excluded$1);
259+
remaining = _objectWithoutProperties(extraProps, _excluded);
260260

261261
mixins.attrs['style'] = _objectSpread2(_objectSpread2({}, mixins.attrs['style']), existingStyle);
262262
/* eslint-enable */
@@ -328,11 +328,7 @@ function objectWithKey(key, value) {
328328
return Array.isArray(value) && value.length > 0 || !Array.isArray(value) && value ? _defineProperty({}, key, value) : {};
329329
}
330330

331-
var _excluded = ["forwardedRef"];
332-
function FontAwesomeIcon(_ref) {
333-
var forwardedRef = _ref.forwardedRef,
334-
props = _objectWithoutProperties(_ref, _excluded);
335-
331+
var FontAwesomeIcon = /*#__PURE__*/React.forwardRef(function (props, ref) {
336332
var iconArgs = props.icon,
337333
maskArgs = props.mask,
338334
symbol = props.symbol,
@@ -358,7 +354,7 @@ function FontAwesomeIcon(_ref) {
358354

359355
var abstract = renderedIcon.abstract;
360356
var extraProps = {
361-
ref: forwardedRef
357+
ref: ref
362358
};
363359
Object.keys(props).forEach(function (key) {
364360
// eslint-disable-next-line no-prototype-builtins
@@ -367,7 +363,7 @@ function FontAwesomeIcon(_ref) {
367363
}
368364
});
369365
return convertCurry(abstract[0], extraProps);
370-
}
366+
});
371367
FontAwesomeIcon.displayName = 'FontAwesomeIcon';
372368
FontAwesomeIcon.propTypes = {
373369
beat: PropTypes.bool,

index.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
return string.substr(0, 1).toLowerCase() + string.substr(1);
203203
}
204204

205-
var _excluded$1 = ["style"];
205+
var _excluded = ["style"];
206206

207207
function capitalize(val) {
208208
return val.charAt(0).toUpperCase() + val.slice(1);
@@ -263,7 +263,7 @@
263263

264264
var _extraProps$style = extraProps.style,
265265
existingStyle = _extraProps$style === void 0 ? {} : _extraProps$style,
266-
remaining = _objectWithoutProperties(extraProps, _excluded$1);
266+
remaining = _objectWithoutProperties(extraProps, _excluded);
267267

268268
mixins.attrs['style'] = _objectSpread2(_objectSpread2({}, mixins.attrs['style']), existingStyle);
269269
/* eslint-enable */
@@ -335,11 +335,7 @@
335335
return Array.isArray(value) && value.length > 0 || !Array.isArray(value) && value ? _defineProperty({}, key, value) : {};
336336
}
337337

338-
var _excluded = ["forwardedRef"];
339-
function FontAwesomeIcon(_ref) {
340-
var forwardedRef = _ref.forwardedRef,
341-
props = _objectWithoutProperties(_ref, _excluded);
342-
338+
var FontAwesomeIcon = /*#__PURE__*/React__default["default"].forwardRef(function (props, ref) {
343339
var iconArgs = props.icon,
344340
maskArgs = props.mask,
345341
symbol = props.symbol,
@@ -365,7 +361,7 @@
365361

366362
var abstract = renderedIcon.abstract;
367363
var extraProps = {
368-
ref: forwardedRef
364+
ref: ref
369365
};
370366
Object.keys(props).forEach(function (key) {
371367
// eslint-disable-next-line no-prototype-builtins
@@ -374,7 +370,7 @@
374370
}
375371
});
376372
return convertCurry(abstract[0], extraProps);
377-
}
373+
});
378374
FontAwesomeIcon.displayName = 'FontAwesomeIcon';
379375
FontAwesomeIcon.propTypes = {
380376
beat: PropTypes__default["default"].bool,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
},
4949
"peerDependencies": {
5050
"@fortawesome/fontawesome-svg-core": "~1 || ~6",
51-
"react": ">=16.x"
51+
"react": ">=16.3"
5252
},
5353
"devDependencies": {
5454
"@babel/core": "^7.16.7",

src/components/FontAwesomeIcon.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import objectWithKey from '../utils/object-with-key'
77
import PropTypes from 'prop-types'
88
import React from 'react'
99

10-
export default function FontAwesomeIcon({ forwardedRef, ...props }) {
10+
const FontAwesomeIcon = React.forwardRef((props, ref) => {
1111
const {
1212
icon: iconArgs,
1313
mask: maskArgs,
@@ -48,7 +48,7 @@ export default function FontAwesomeIcon({ forwardedRef, ...props }) {
4848
}
4949

5050
const { abstract } = renderedIcon
51-
const extraProps = { ref: forwardedRef }
51+
const extraProps = { ref }
5252

5353
Object.keys(props).forEach(key => {
5454
// eslint-disable-next-line no-prototype-builtins
@@ -58,7 +58,7 @@ export default function FontAwesomeIcon({ forwardedRef, ...props }) {
5858
})
5959

6060
return convertCurry(abstract[0], extraProps)
61-
}
61+
})
6262

6363
FontAwesomeIcon.displayName = 'FontAwesomeIcon'
6464

@@ -172,4 +172,6 @@ FontAwesomeIcon.defaultProps = {
172172
swapOpacity: false
173173
}
174174

175+
export default FontAwesomeIcon
176+
175177
const convertCurry = convert.bind(null, React.createElement)

src/components/__tests__/FontAwesomeIcon.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ describe('using ref', () => {
471471
const spy = jest.fn(element => element)
472472

473473
mount(
474-
{ icon: faCoffee, forwardedRef: spy },
474+
{ icon: faCoffee, ref: spy },
475475
{
476476
createNodeMock: () => node
477477
}
@@ -486,7 +486,7 @@ describe('using ref', () => {
486486
const setForwardedRef = element => (forwardedRef = element)
487487

488488
mount(
489-
{ icon: faCoffee, forwardedRef: setForwardedRef },
489+
{ icon: faCoffee, ref: setForwardedRef },
490490
{
491491
createNodeMock: () => node
492492
}

0 commit comments

Comments
 (0)