From 9dfc9b9baca6fd7a2a928cd13dd4f0c8d5f55fec Mon Sep 17 00:00:00 2001 From: Angelo Ashmore Date: Fri, 8 Oct 2021 16:12:52 -1000 Subject: [PATCH] refactor: reorganize PrismicLink --- src/PrismicLink.tsx | 61 +++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/PrismicLink.tsx b/src/PrismicLink.tsx index b2088a5..bce0de5 100644 --- a/src/PrismicLink.tsx +++ b/src/PrismicLink.tsx @@ -6,6 +6,12 @@ import { isInternalURL } from "./lib/isInternalURL"; import { usePrismicContext } from "./usePrismicContext"; +type ComponentProps = T extends React.ComponentType + ? U + : T extends keyof JSX.IntrinsicElements + ? React.ComponentProps + : unknown; + /** * Props provided to a component when rendered with ``. */ @@ -33,12 +39,6 @@ export interface LinkProps { children?: React.ReactNode; } -type ComponentProps = T extends React.ComponentType - ? U - : T extends keyof JSX.IntrinsicElements - ? React.ComponentProps - : unknown; - /** * Props for ``. */ @@ -49,6 +49,8 @@ export type PrismicLinkProps< ExternalComponent extends string | React.ComponentType = | string | React.ComponentType, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + LinkResolverFunction extends prismicH.LinkResolverFunction = prismicH.LinkResolverFunction, > = ComponentProps & ComponentProps & { /** @@ -59,7 +61,7 @@ export type PrismicLinkProps< * repository's content, a Link Resolver does not need to be provided. * @see Learn about Link Resolvers and Route Resolvers {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver} */ - linkResolver?: prismicH.LinkResolverFunction; + linkResolver?: LinkResolverFunction; /** * The component rendered for internal URLs. Defaults to ``. @@ -145,16 +147,22 @@ export const PrismicLink = < ExternalComponent extends | string | React.ComponentType = typeof defaultExternalComponent, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + LinkResolverFunction extends prismicH.LinkResolverFunction = prismicH.LinkResolverFunction, >( - props: PrismicLinkProps, + props: PrismicLinkProps< + InternalComponent, + ExternalComponent, + LinkResolverFunction + >, ): JSX.Element | null => { const context = usePrismicContext(); const linkResolver = props.linkResolver || context.linkResolver; - let href: string | null = null; + let href: string | null | undefined; if ("href" in props) { - href = props.href || null; + href = props.href; } else if ("document" in props && props.document) { href = prismicH.asLink(props.document, linkResolver); } else if ("field" in props && props.field) { @@ -186,28 +194,21 @@ export const PrismicLink = < const Component = isInternal ? InternalComponent : ExternalComponent; - const restProps = {} as ComponentProps & - ComponentProps; - for (const key in props) { - if ( - ![ - "linkResolver", - "internalComponent", - "externalComponent", - "field", - "document", - "href", - "rel", - "target", - ].includes(key) - ) { - restProps[key as keyof typeof restProps] = props[ - key as keyof typeof props - ] as typeof restProps[keyof typeof restProps]; - } + const passthroughProps: typeof props = Object.assign({}, props); + delete passthroughProps.linkResolver; + delete passthroughProps.internalComponent; + delete passthroughProps.externalComponent; + delete passthroughProps.rel; + delete passthroughProps.target; + if ("field" in passthroughProps) { + delete passthroughProps.field; + } else if ("document" in passthroughProps) { + delete passthroughProps.document; + } else if ("href" in passthroughProps) { + delete passthroughProps.href; } return href ? ( - + ) : null; };