Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I don't understand how to integrate useHref and Breadcrumbs.Item (unstable component) #1945

Open
ogonkov opened this issue Nov 13, 2024 · 0 comments

Comments

@ogonkov
Copy link
Contributor

ogonkov commented Nov 13, 2024

I'm trying to wrap Breadcrumb.Item to use it with useHref, but it seems impossible, because Breadcrumbs seems copy my props, and recreate default Breadcrumbs.Item component, w/out my logic:

<BreadcrumbItem
{...child.props}
key={key}
current={isCurrent}
disabled={props.disabled}
onAction={handleAction}
navigate={navigate}
>
{child.props.children}
</BreadcrumbItem>

What is my code looks like:

RouterBreadcrumbs.tsx

import type {FC} from 'react';
import {
    unstable_Breadcrumbs as Breadcrumbs,
    type unstable_BreadcrumbsItemProps as BreadcrumbsItemProps,
    type unstable_BreadcrumbsProps as BreadcrumbsProps,
} from '@gravity-ui/uikit/unstable';
import {
    type GoToRoutePayload,
    type LinkProps,
    useHref,
    useNavigate,
} from '@foobar/router';

interface RouterBreadcrumbsProps extends Omit<BreadcrumbsProps, 'navigate'> {}

const RouterBreadcrumbsComponent: FC<RouterBreadcrumbsProps> = (props) => {
    const navigate = useNavigate();

    return <Breadcrumbs {...props} navigate={navigate} />;
};

RouterBreadcrumbsComponent.displayName = 'Router(Breadcrumbs)';

interface RouterBreadcrumbsItemProps
    extends Omit<BreadcrumbsItemProps, 'href'>,
        Pick<LinkProps, 'to'> {}

const RouterBreadcrumbsItem: FC<RouterBreadcrumbsItemProps> = ({to, target, ...props}) => {
    const routeDescriptor = (to || '') as GoToRoutePayload;
    const href = useHref(routeDescriptor);

    return <Breadcrumbs.Item {...props} href={href} />;
};

RouterBreadcrumbsItem.displayName = 'Router(Breadcrumbs.Item)';

export const RouterBreadcrumbs = Object.assign(RouterBreadcrumbsComponent, {
    Item: RouterBreadcrumbsItem,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant