mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-05-31 01:26:16 +00:00
Created helper function to convert mutable refs from useRef into element refs for the ref prop
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
|||||||
faCircleNotch as loadingStatusIcon,
|
faCircleNotch as loadingStatusIcon,
|
||||||
} from '@fortawesome/free-solid-svg-icons';
|
} from '@fortawesome/free-solid-svg-icons';
|
||||||
import { MediaMatcher } from '../../utils/types';
|
import { MediaMatcher } from '../../utils/types';
|
||||||
|
import { mutableRefToElementRef } from '../../utils/helpers/components';
|
||||||
import { DomainStatus } from '../data';
|
import { DomainStatus } from '../data';
|
||||||
|
|
||||||
interface DomainStatusIconProps {
|
interface DomainStatusIconProps {
|
||||||
@@ -34,11 +35,7 @@ export const DomainStatusIcon: FC<DomainStatusIconProps> = ({ status, matchMedia
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<span
|
<span ref={mutableRefToElementRef(ref)}>
|
||||||
ref={(el: HTMLSpanElement) => {
|
|
||||||
ref.current = el;
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{status === 'valid'
|
{status === 'valid'
|
||||||
? <FontAwesomeIcon fixedWidth icon={checkIcon} className="text-muted" />
|
? <FontAwesomeIcon fixedWidth icon={checkIcon} className="text-muted" />
|
||||||
: <FontAwesomeIcon fixedWidth icon={invalidIcon} className="text-danger" />}
|
: <FontAwesomeIcon fixedWidth icon={invalidIcon} className="text-danger" />}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { complement, pipe } from 'ramda';
|
|||||||
import { faFileUpload as importIcon } from '@fortawesome/free-solid-svg-icons';
|
import { faFileUpload as importIcon } from '@fortawesome/free-solid-svg-icons';
|
||||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||||
import { useToggle } from '../../utils/helpers/hooks';
|
import { useToggle } from '../../utils/helpers/hooks';
|
||||||
|
import { mutableRefToElementRef } from '../../utils/helpers/components';
|
||||||
import { ServersImporter } from '../services/ServersImporter';
|
import { ServersImporter } from '../services/ServersImporter';
|
||||||
import { ServerData, ServersMap } from '../data';
|
import { ServerData, ServersMap } from '../data';
|
||||||
import { DuplicatedServersModal } from './DuplicatedServersModal';
|
import { DuplicatedServersModal } from './DuplicatedServersModal';
|
||||||
@@ -78,9 +79,7 @@ export const ImportServersBtn = ({ importServersFromFile }: ServersImporter): FC
|
|||||||
type="file"
|
type="file"
|
||||||
accept="text/csv"
|
accept="text/csv"
|
||||||
className="import-servers-btn__csv-select"
|
className="import-servers-btn__csv-select"
|
||||||
ref={(el) => {
|
ref={mutableRefToElementRef(ref)}
|
||||||
ref.current = el ?? undefined;
|
|
||||||
}}
|
|
||||||
onChange={onFile}
|
onChange={onFile}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { ShortUrl } from '../data';
|
|||||||
import { SelectedServer } from '../../servers/data';
|
import { SelectedServer } from '../../servers/data';
|
||||||
import { ShortUrlDetailLink } from './ShortUrlDetailLink';
|
import { ShortUrlDetailLink } from './ShortUrlDetailLink';
|
||||||
import './ShortUrlVisitsCount.scss';
|
import './ShortUrlVisitsCount.scss';
|
||||||
|
import { mutableRefToElementRef } from '../../utils/helpers/components';
|
||||||
|
|
||||||
interface ShortUrlVisitsCountProps {
|
interface ShortUrlVisitsCountProps {
|
||||||
shortUrl?: ShortUrl | null;
|
shortUrl?: ShortUrl | null;
|
||||||
@@ -35,7 +36,7 @@ export const ShortUrlVisitsCount = (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const prettifiedMaxVisits = prettify(maxVisits);
|
const prettifiedMaxVisits = prettify(maxVisits);
|
||||||
const tooltipRef = useRef<HTMLElement | null>();
|
const tooltipRef = useRef<HTMLElement | undefined>();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -43,9 +44,7 @@ export const ShortUrlVisitsCount = (
|
|||||||
{visitsLink}
|
{visitsLink}
|
||||||
<small
|
<small
|
||||||
className="short-urls-visits-count__max-visits-control"
|
className="short-urls-visits-count__max-visits-control"
|
||||||
ref={(el) => {
|
ref={mutableRefToElementRef(tooltipRef)}
|
||||||
tooltipRef.current = el;
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
{' '}/ {prettifiedMaxVisits}{' '}
|
{' '}/ {prettifiedMaxVisits}{' '}
|
||||||
<sup>
|
<sup>
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { getServerId, SelectedServer } from '../servers/data';
|
|||||||
import { TagBullet } from './helpers/TagBullet';
|
import { TagBullet } from './helpers/TagBullet';
|
||||||
import { NormalizedTag, TagModalProps } from './data';
|
import { NormalizedTag, TagModalProps } from './data';
|
||||||
import './TagCard.scss';
|
import './TagCard.scss';
|
||||||
|
import { mutableRefToElementRef } from '../utils/helpers/components';
|
||||||
|
|
||||||
export interface TagCardProps {
|
export interface TagCardProps {
|
||||||
tag: NormalizedTag;
|
tag: NormalizedTag;
|
||||||
@@ -28,7 +29,7 @@ export const TagCard = (
|
|||||||
const [isDeleteModalOpen, toggleDelete] = useToggle();
|
const [isDeleteModalOpen, toggleDelete] = useToggle();
|
||||||
const [isEditModalOpen, toggleEdit] = useToggle();
|
const [isEditModalOpen, toggleEdit] = useToggle();
|
||||||
const [hasTitle,, displayTitle] = useToggle();
|
const [hasTitle,, displayTitle] = useToggle();
|
||||||
const titleRef = useRef<HTMLElement>();
|
const titleRef = useRef<HTMLHeadingElement | undefined>();
|
||||||
const serverId = getServerId(selectedServer);
|
const serverId = getServerId(selectedServer);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -55,9 +56,7 @@ export const TagCard = (
|
|||||||
<h5
|
<h5
|
||||||
className="tag-card__tag-title text-ellipsis"
|
className="tag-card__tag-title text-ellipsis"
|
||||||
title={hasTitle ? tag.tag : undefined}
|
title={hasTitle ? tag.tag : undefined}
|
||||||
ref={(el) => {
|
ref={mutableRefToElementRef(titleRef)}
|
||||||
titleRef.current = el ?? undefined;
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
<TagBullet tag={tag.tag} colorGenerator={colorGenerator} />
|
<TagBullet tag={tag.tag} colorGenerator={colorGenerator} />
|
||||||
<span className="tag-card__tag-name" onClick={toggle}>{tag.tag}</span>
|
<span className="tag-card__tag-name" onClick={toggle}>{tag.tag}</span>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|||||||
import { faInfoCircle as infoIcon } from '@fortawesome/free-solid-svg-icons';
|
import { faInfoCircle as infoIcon } from '@fortawesome/free-solid-svg-icons';
|
||||||
import { UncontrolledTooltip } from 'reactstrap';
|
import { UncontrolledTooltip } from 'reactstrap';
|
||||||
import { Placement } from '@popperjs/core';
|
import { Placement } from '@popperjs/core';
|
||||||
|
import { mutableRefToElementRef } from './helpers/components';
|
||||||
|
|
||||||
type InfoTooltipProps = PropsWithChildren<{
|
type InfoTooltipProps = PropsWithChildren<{
|
||||||
className?: string;
|
className?: string;
|
||||||
@@ -10,14 +11,11 @@ type InfoTooltipProps = PropsWithChildren<{
|
|||||||
}>;
|
}>;
|
||||||
|
|
||||||
export const InfoTooltip: FC<InfoTooltipProps> = ({ className = '', placement, children }) => {
|
export const InfoTooltip: FC<InfoTooltipProps> = ({ className = '', placement, children }) => {
|
||||||
const ref = useRef<HTMLSpanElement | null>();
|
const ref = useRef<HTMLSpanElement | undefined>();
|
||||||
const refCallback = (el: HTMLSpanElement) => {
|
|
||||||
ref.current = el;
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<span className={className} ref={refCallback}>
|
<span className={className} ref={mutableRefToElementRef(ref)}>
|
||||||
<FontAwesomeIcon icon={infoIcon} />
|
<FontAwesomeIcon icon={infoIcon} />
|
||||||
</span>
|
</span>
|
||||||
<UncontrolledTooltip target={(() => ref.current) as any} placement={placement}>{children}</UncontrolledTooltip>
|
<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