Jelajahi Sumber

Added dynamic link to be able to use it as anchor tab or router link based on to prop

jnfrati 4 tahun lalu
induk
melakukan
f9453981ea
1 mengubah file dengan 24 tambahan dan 0 penghapusan
  1. 24 0
      dashboard/src/components/DynamicLink.tsx

+ 24 - 0
dashboard/src/components/DynamicLink.tsx

@@ -0,0 +1,24 @@
+import React from "react";
+import { Link, LinkProps } from "react-router-dom";
+
+const DynamicLink: React.FC<LinkProps> = ({ to, children, ...props }) => {
+  // It is a simple element with nothing to link to
+  if (!to) return <span {...props}>{children}</span>;
+
+  // It is intended to be an external link
+  if (typeof to === "string" && /^https?:\/\//.test(to))
+    return (
+      <a href={to} {...props}>
+        {children}
+      </a>
+    );
+
+  // Finally, it is an internal link
+  return (
+    <Link to={to} {...props}>
+      {children}
+    </Link>
+  );
+};
+
+export default DynamicLink;