ProNextJS / declarative-routing

NextJS Typesafe Routing System
MIT License
233 stars 18 forks source link

[Proposal] Add Navigate feature #41

Open bstivenprz opened 23 hours ago

bstivenprz commented 23 hours ago

Hey there!

I propose to add Navigate component from react-router-dom to makeRoute, something like this:

export default function AuthenticationLayout() {
  if (!authenticated)
    return <LoginRoute.Navigate />;

  return <HomeRoute.Navigate />;
}

I made this changes for my own:

export type RouteBuilder<
  Params extends z.ZodSchema,
  Search extends z.ZodSchema
> = {
  Navigate: React.FC<
    Omit<NavigateProps, "to"> & z.input<Params> & { search?: z.input<Search> }
  >;
};

routeBuilder.Navigate = function RouteLink({
  search: linkSearch,
  ...props
}: Omit<NavigateProps, "to"> & z.input<Params> & { search?: z.input<Search> }) {
  const params = info.params.parse(props);
  const extraProps = { ...props };
  for (const key of Object.keys(params)) {
    delete extraProps[key];
  }
  return (
    <Navigate
      {...props}
      to={routeBuilder(info.params.parse(props), linkSearch)}
    />
  );
};

This would work great for this cases. Works for me.

What do you think?

hgd

jherr commented 16 hours ago

Is this just for the React-router?

jherr commented 16 hours ago

This would probably be better as a PR.