Created helper function to convert mutable refs from useRef into element refs for the ref prop

This commit is contained in:
Alejandro Celaya
2022-06-06 20:46:51 +02:00
parent b450e4093e
commit 7669254a0c
6 changed files with 18 additions and 21 deletions

View File

@@ -3,6 +3,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faInfoCircle as infoIcon } from '@fortawesome/free-solid-svg-icons';
import { UncontrolledTooltip } from 'reactstrap';
import { Placement } from '@popperjs/core';
import { mutableRefToElementRef } from './helpers/components';
type InfoTooltipProps = PropsWithChildren<{
className?: string;
@@ -10,14 +11,11 @@ type InfoTooltipProps = PropsWithChildren<{
}>;
export const InfoTooltip: FC<InfoTooltipProps> = ({ className = '', placement, children }) => {
const ref = useRef<HTMLSpanElement | null>();
const refCallback = (el: HTMLSpanElement) => {
ref.current = el;
};
const ref = useRef<HTMLSpanElement | undefined>();
return (
<>
<span className={className} ref={refCallback}>
<span className={className} ref={mutableRefToElementRef(ref)}>
<FontAwesomeIcon icon={infoIcon} />
</span>
<UncontrolledTooltip target={(() => ref.current) as any} placement={placement}>{children}</UncontrolledTooltip>

View File

@@ -0,0 +1,5 @@
import { MutableRefObject, Ref } from 'react';
export const mutableRefToElementRef = <T>(ref: MutableRefObject<T | undefined>): Ref<T> => (el) => {
ref.current = el ?? undefined; // eslint-disable-line no-param-reassign
};