1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-22 13:04:19 +00:00
pds-2021-g2-nest/nest_frontend/utils/makeIcon.js

30 lines
988 B
JavaScript
Raw Normal View History

2021-05-22 02:17:02 +00:00
import React, { isValidElement } from "react"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
2021-05-23 14:20:53 +00:00
import { IconDefinition } from "@fortawesome/fontawesome-svg-core"
2021-05-22 02:17:02 +00:00
/**
* Try to create an icon element based on what is passed to the function:
* - If a {@link JSX.Element} is passed, a `<span>` element containing it will be created and returned.
* - If a {@link IconDefinition} is passed, a `<span>` element containing a {@link FontAwesomeIcon} will be created
* and returned.
* - If a falsy value is passed, `null` will be returned.
*
* @param icon - The icon value.
* @param props - Props to pass to the span element when it is created.
* @returns {JSX.Element|null}
*/
export default function makeIcon(icon, props) {
if(isValidElement(icon)) {
return <span {...props}>{icon}</span>
}
else if(icon) {
return (
<span {...props}><FontAwesomeIcon icon={icon}/></span>
)
}
else {
return null
}
}