Created
December 7, 2018 19:30
-
-
Save geraldfullam/83c54427f598aca49aee7301fb547672 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
export function isComponentWithName (el, name) { | |
if (typeof name === 'string') { | |
return React.isValidElement(el) && name === getDisplayName(el) | |
} | |
const validRegEx = name instanceof RegExp ? name : new RegExp(name) | |
return React.isValidElement(el) && validRegEx.test(getDisplayName(el)) | |
} | |
export function isComponentWithType (el, type) { | |
return React.isValidElement(el) && el.type === type | |
} | |
export function getDisplayName (el) { | |
if (React.isValidElement(el)) { | |
return el.type.displayName || el.type.name || 'Component' | |
} | |
return '' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment