mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-04-20 21:46:20 +00:00
Created helper function to convert mutable refs from useRef into element refs for the ref prop
This commit is contained in:
@@ -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>
|
||||
|
||||
5
src/utils/helpers/components.ts
Normal file
5
src/utils/helpers/components.ts
Normal 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
|
||||
};
|
||||
Reference in New Issue
Block a user