#Link + Pagination + Search Prop + Shadcn/UI

22 messages · Page 1 of 1 (latest)

stuck schooner
#

Hey there,

I'm trying to build the pagination for my app and I struggle more than I should. 😅

I use the pagination components from Shadcn/UI and I replaced the a element from PaginationLink with Link from @tanstack/react-router.

It works as expected if I concatenate stuff, but if I try to use the search prop, I get a type error even though it works.

Can anyone help me, please? Thanks!

granite trail
stuck schooner
granite trail
#

can you provide a complete minimal example by forking an existing router example on stackblitz?

void timber
#

Hey! I've stumbled upon this problem myself and I've managed to fix it thanks to the custom link component thingy as per the docs, but I still got a problem when it comes to passing Link props type to <PaginationPrevious> and <PaginationNext> components. Honestly, I'm not sure if it's even viable to do so. Would love to get some help/clarification on this topic.

Here's the complete minimal reproduction of the issue, see line 65: https://stackblitz.com/edit/tanstack-router-qyn239bg?file=src%2Froutes%2Fposts.tsx

@granite trail I'd appreciate if you could take a look at this 🙏

Forked example of Tanstack Router with broken typesafety of shadcn/ui pagination components

void timber
# granite trail here is an example how that is used https://github.com/EskiMojo14/retrospecs-sta...

Not really sure how to apply this in my example. I've did some more digging before you replied and I found that LinkComponentProps fixes the types but I'm not sure how valid this approach is.

I've replaced:

type PaginationLinkProps = {
  isActive?: boolean
} & Pick<ButtonProps, "size"> &
  React.ComponentProps<"a">

with:

type PaginationLinkProps = {
  isActive?: boolean
} & Pick<ButtonProps, 'size'> &
  LinkComponentProps

How valid is this type?

granite trail
#

cc @covert patrol

covert patrol
#

Afk but i think this is possible with ValidateLinkOptions

type PaginationLinkProps<TOptions> = ValidateLinkOptions<TOptions> & Pick<ButtonProps, 'size'>

void timber
covert patrol
#

Can you show an example of how you are using it?

void timber
#

But this casting props as any is just awful

#

What exactly is the LinkComponentProps though? With that type, there's no need for casting props as any but I'm not comfortable using something that I'm not fully understanding so would appreciate some input here

covert patrol
#

LinkComponentProps is an internal type used for props of Link. Without type parameters there will be no narrowing to a specific route for params or search

#

ValidateLinkOptions should get you the correct type safety with only one type parameter.

It focuses more on type safety of a public interface, custom.Link for example but given generic functions/components it can be hard to eliminate all required type assertions

#

The type assertions should only be required internally

#

And as never can usually work if you dont like any

void timber
#

Makes sense. Couldn't get it to work with never nor unknown so I just stuck with casting props to PaginationLinkProps<TOptions> which makes sense type wise as I really dislike any. Thanks for the help!

#

Here's the complete fix for anyone who will find this thread in the future: