flowbite-react icon indicating copy to clipboard operation
flowbite-react copied to clipboard

Breadcrumb component to allow a react-router <Link> instead of a bare <a>

Open rnicholus opened this issue 1 year ago • 3 comments

  • [x] I have searched the Issues to see if this bug has already been reported
  • [x] I have tested the latest version

Summary

Currently, <BreadcrumbItem> can be rendered as an anchor link or a span. But given that this is a React library, and the overwhelming majority of React apps use react-router, <BreadcrumbItem> should at least be able to render a <Link>.

This can probably be accomplished by refactoring <BreadcrumbItem> to accept as={Link}.

rnicholus avatar Jun 03 '24 14:06 rnicholus

i'm using next, also want this feature...

j-vitali avatar Jun 03 '24 14:06 j-vitali

I am working on this feature

akashMasih avatar Jun 11 '24 08:06 akashMasih

this changes based on button, but how to test it?

import type { ComponentPropsWithoutRef, ElementType, ForwardedRef, FC } from "react";
import { createElement, forwardRef } from "react";
import { HiOutlineChevronRight } from "react-icons/hi";
import { twMerge } from "tailwind-merge";
import { mergeDeep } from "../../helpers/merge-deep";
import { getTheme } from "../../theme-store";
import type { DeepPartial } from "../../types";
import type { FlowbiteBoolean } from "../Flowbite";

export interface FlowbiteBreadcrumbItemTheme {
  base: string;
  chevron: string;
  href: FlowbiteBoolean;
  icon: string;
}

export type BreadcrumbItemProps<T extends ElementType = "span"> = {
  as?: T;
  href?: string;
  icon?: FC<ComponentPropsWithoutRef<"svg">>;
  theme?: DeepPartial<FlowbiteBreadcrumbItemTheme>;
} & ComponentPropsWithoutRef<T>;

export const BreadcrumbItem = forwardRef(
  <T extends ElementType = "span">(
    { children, className, as: Component, href, icon: Icon, theme: customTheme = {}, ...props }: BreadcrumbItemProps<T>,
    ref: ForwardedRef<T>,
  ) => {
    const BaseComponent = Component || (href ? "a" : "span");

    const theme = mergeDeep(getTheme().breadcrumb.item, customTheme);

    return (
      <li className={twMerge(theme.base, className)} {...props}>
        <HiOutlineChevronRight aria-hidden className={theme.chevron} data-testid="flowbite-breadcrumb-separator" />
        {createElement(
          BaseComponent,
          {
            ref,
            href,
            className: theme.href[href ? "on" : "off"],
            "data-testid": "flowbite-breadcrumb-item",
            ...props,
          },
          <>
            {Icon && <Icon aria-hidden className={theme.icon} />}
            {children}
          </>,
        )}
      </li>
    );
  },
);

BreadcrumbItem.displayName = "Breadcrumb.Item";

JuniYadi avatar Nov 27 '24 05:11 JuniYadi