Skip to content

Instantly share code, notes, and snippets.

@diomededavid
Last active December 24, 2022 23:13
Show Gist options
  • Select an option

  • Save diomededavid/37a0e41fa4c160b1b62d6e93d25ea221 to your computer and use it in GitHub Desktop.

Select an option

Save diomededavid/37a0e41fa4c160b1b62d6e93d25ea221 to your computer and use it in GitHub Desktop.
Typescript array for dropdown navigation menu component in React.js and daisy UI
/*
To create a dropdown navigation menu component in React using TypeScript, you can start by defining an interface for the menu items that will be displayed in the dropdown. This interface might include properties such as the label for the menu item, the destination URL, and any subitems that should be displayed in a submenu within the dropdown.
Next, you can create a component that accepts an array of menu items as a prop and uses this array to render the dropdown menu. For example:
*/
import * as React from 'react';
interface MenuItem {
label: string;
url: string;
subitems?: MenuItem[];
}
interface Props {
items: MenuItem[];
}
const DropdownMenu = (props: MenuItem & Props) => {
return (
<ul>
{props.items.map((item) => (
<li>
<a href={item.url}>{item.label}</a>
{item.subitems && (
<ul>
{item.subitems.map((subitem) => (
<li>
<a href={subitem.url}>{subitem.label}</a>
</li>
))}
</ul>
)}
</li>
))}
</ul>
);
};
export default DropdownMenu;
/* This component will render an unordered list with a list item for each menu item in the items array prop. If a menu item has subitems, it will also render a nested unordered list with list items for each subitem.
To use this component in your application, you can pass it an array of menu items like this:
*/
const menuItems: MenuItem[] = [
{ label: 'Home', url: '/' },
{ label: 'About', url: '/about' },
{
label: 'Products',
url: '/products',
subitems: [
{ label: 'Product 1', url: '/products/1' },
{ label: 'Product 2', url: '/products/2' },
{ label: 'Product 3', url: '/products/3' },
],
},
];
<DropdownMenu items={menuItems} />
// Generated from Chat GPT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment