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

Support for prop ref #341

Merged
merged 4 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type Props = FontAwesomeIconProps
type BackwardCompatibleOmit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;

export interface FontAwesomeIconProps extends BackwardCompatibleOmit<SVGAttributes<SVGSVGElement>, 'children' | 'mask' | 'transform'> {
forwardedRef?: ((e: any) => void) | React.RefObject<any>
icon: IconProp
mask?: IconProp
className?: string
Expand Down
9 changes: 7 additions & 2 deletions index.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,10 @@ function objectWithKey(key, value) {
return Array.isArray(value) && value.length > 0 || !Array.isArray(value) && value ? _defineProperty({}, key, value) : {};
}

function FontAwesomeIcon(props) {
function FontAwesomeIcon(_ref2) {
var forwardedRef = _ref2.forwardedRef,
props = _objectWithoutProperties(_ref2, ["forwardedRef"]);

var iconArgs = props.icon,
maskArgs = props.mask,
symbol = props.symbol,
Expand All @@ -321,7 +324,9 @@ function FontAwesomeIcon(props) {
}

var abstract = renderedIcon.abstract;
var extraProps = {};
var extraProps = {
ref: forwardedRef
};
Object.keys(props).forEach(function (key) {
// eslint-disable-next-line no-prototype-builtins
if (!FontAwesomeIcon.defaultProps.hasOwnProperty(key)) {
Expand Down
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,10 @@
return Array.isArray(value) && value.length > 0 || !Array.isArray(value) && value ? _defineProperty({}, key, value) : {};
}

function FontAwesomeIcon(props) {
function FontAwesomeIcon(_ref2) {
var forwardedRef = _ref2.forwardedRef,
props = _objectWithoutProperties(_ref2, ["forwardedRef"]);

var iconArgs = props.icon,
maskArgs = props.mask,
symbol = props.symbol,
Expand All @@ -326,7 +329,9 @@
}

var abstract = renderedIcon.abstract;
var extraProps = {};
var extraProps = {
ref: forwardedRef
};
Object.keys(props).forEach(function (key) {
// eslint-disable-next-line no-prototype-builtins
if (!FontAwesomeIcon.defaultProps.hasOwnProperty(key)) {
Expand Down
4 changes: 2 additions & 2 deletions src/components/FontAwesomeIcon.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import objectWithKey from '../utils/object-with-key'
import PropTypes from 'prop-types'
import React from 'react'

export default function FontAwesomeIcon(props) {
export default function FontAwesomeIcon({ forwardedRef, ...props }) {
const { icon: iconArgs, mask: maskArgs, symbol, className, title } = props

const iconLookup = normalizeIconArgs(iconArgs)
Expand Down Expand Up @@ -37,7 +37,7 @@ export default function FontAwesomeIcon(props) {
}

const { abstract } = renderedIcon
const extraProps = {}
const extraProps = { ref: forwardedRef }

Object.keys(props).forEach(key => {
// eslint-disable-next-line no-prototype-builtins
Expand Down
38 changes: 36 additions & 2 deletions src/components/__tests__/FontAwesomeIcon.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ const faCircle = {

fontawesome.library.add(faCoffee, faCircle)

function mount(props = {}) {
const component = renderer.create(<FontAwesomeIcon {...props} />)
function mount(props = {}, { createNodeMock } = {}) {
const component = renderer.create(<FontAwesomeIcon {...props} />, {
createNodeMock
})

return component.toJSON()
}
Expand Down Expand Up @@ -289,3 +291,35 @@ describe('swap opacity', () => {
expect(vm.props.className.includes('fa-swap-opacity')).toBeFalsy()
})
})

describe('using ref', () => {
const node = {}

test('function', () => {
const spy = jest.fn(element => element)

mount(
{ icon: faCoffee, forwardedRef: spy },
{
createNodeMock: () => node
}
)

expect(spy.mock.calls.length).toBe(1)
expect(spy.mock.results[0].value).toBe(node)
})

test('callback ref', () => {
let forwardedRef = null
const setForwardedRef = element => (forwardedRef = element)

mount(
{ icon: faCoffee, forwardedRef: setForwardedRef },
{
createNodeMock: () => node
}
)

expect(forwardedRef).toBe(node)
})
})