nextui icon indicating copy to clipboard operation
nextui copied to clipboard

[BUG] - Typescript error with DropdownItem

Open matteo-pkxp opened this issue 3 years ago • 23 comments

NextUI Version

2.1.13

Describe the bug

Hello, I have a type issue when using the Dropdown component, this is the code triggering the ts error:

<DropdownMenu aria-label="Profile Actions" variant="flat">
    { menuItems.map((entry) => (
        <DropdownItem key={entry.link} showDivider={entry.divider}>
            {entry.name}
        </DropdownItem>
    ))}
    <DropdownItem key="custom">Test123</DropdownItem>
</DropdownMenu>

The error is: Type 'Element[]' is not assignable to type 'CollectionElement<object>'. The code runs fine and the result is as expected, that is a list of DropdownItems followed by the non-iterated "Test123" item, but the error (that I couldn't suppress) prevents me from building the app for production.

If I wrap it with a fragment <></>, which makes the TS error disappear, this time the error is at runtime and becomes TypeError: Cannot convert a Symbol value to a string.

Any idea on how to make it work? I have custom element at the bottom of this dropdown, that doesn't match the structure of the iterated elements.

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

I'm using Typescript 5.1.6 and NextJS 13.4.13

Expected behavior

There should be either no runtime or linter errors.

Screenshots or Videos

No response

Operating System Version

Windows with Linux Subsystem 2.0 (Debian LTS)

Browser

Chrome

matteo-pkxp avatar Sep 25 '23 09:09 matteo-pkxp

I found a not-so-elegant solution to ignore the TS error:

<DropdownMenu aria-label="Profile Actions" variant="flat">
{[
  // @ts-ignore
  menuItems.map((entry) => (
      <DropdownItem key={entry.link}>
          {entry.name}
      </DropdownItem>
  )),
  <DropdownItem key="custom">Test123</DropdownItem>,
]}
</DropdownMenu>

This suppresses the TS error, renders the Dropdown as expected and throws no runtime errors, but I'm sure there's a better solution.

matteo-pkxp avatar Sep 25 '23 09:09 matteo-pkxp

Option number 2, which is probably the best one:

 <DropdownMenu aria-label="Profile Actions" variant="flat">
   <DropdownSection>
      { menuItems.map((entry) => (
          <DropdownItem key={entry.link} showDivider={entry.divider}>
              {entry.name}
          </DropdownItem>
      ))}
    </DropdownSection>
    <DropdownItem key="custom">Test123</DropdownItem>
</DropdownMenu>

The only downside here is the aria-label warning, because It's a titleless section.

Not sure if I should close the issue though. I don't think the errors I encountered are expected.

matteo-pkxp avatar Sep 25 '23 10:09 matteo-pkxp

Hello~

I did some research to figure this out. so the underlying of menu is using react-aria-components. if you wish to render dynamic items, I think the proper way should go with this

 <DropdownMenu aria-label="Profile Actions" variant="flat" items={menuItems}>
   {(item) => <DropdownItem key={item.key} showDivider={item.divider} />}
</DropdownMenu>

you can find the reason why suggest to use items props instead of Array.map here

lalalazero avatar Sep 27 '23 02:09 lalalazero

Hi! Thanks for the suggestion, this is surely something that's good to know. Unfortunately, it doesn't fix the issue.

Type '(item: MenuEntry) => JSX.Element' is not assignable to type 'CollectionChildren<object>'.
Type '(item: MenuEntry) => JSX.Element' is not assignable to type '(item: object) => CollectionElement<object>'.
Types of parameters 'item' and 'item' are incompatible.
Property 'name' is missing in type '{}' but required in type 'MenuEntry'.ts(2322)

Looks like it's a Typescript incompatibility of some sort. The dropdown gets rendered as intended, it's just the compiler that goes crazy.

Also, if I do something like this below, only the "custom" element gets rendered:

 <DropdownMenu aria-label="Profile Actions" variant="flat" items={menuItems}>
   {(item) => <DropdownItem key={item.key} showDivider={item.divider} />}
   <DropdownItem key="custom">Test123</DropdownItem>
</DropdownMenu>

The only solution is to use the DropdownSection component, that supports the items prop.

 <DropdownMenu aria-label="Profile Actions" variant="flat">
   <DropdownSection  items={menuItems} aria-label="I'd rather not set this, it has no meaning to me in this case">
      {(item) => <DropdownItem key={item.key} showDivider={item.divider} />}
   </DropdownSection>
   <DropdownItem key="custom">Test123</DropdownItem>
</DropdownMenu>

matteo-pkxp avatar Sep 27 '23 08:09 matteo-pkxp

I'm having same issue

carlosriveroib avatar Oct 01 '23 22:10 carlosriveroib

Hey ! I see you closed this in 2.2.0 @jrgarciadev ?

I'm still having this issue with the following in 2.2.1:

<Dropdown placement="bottom-end">
  <DropdownMenu aria-label="Profile Actions" variant="flat">
    {session.user?.isAdmin && (
      <DropdownSection title="Admin" showDivider>
        <DropdownItem key="prisma">Whatever</DropdownItem>
      </DropdownSection>
    )}
  </DropdownMenu>
</Dropdown>

Same symptoms, ts-error, then runtime error if wrapping it inside a fragment 😕

xavier-villelegier avatar Nov 04 '23 17:11 xavier-villelegier

My code that is experiencing the same error in 2.2.8:

        {
          notificationsData.map((notification) => {
            return (
              <DropdownItem key={notification.id}>
                <Notification
                  body={notification.body}
                  date={notification.createdAt}
                  read={notification.read}
                />
              </DropdownItem>
            )
          })
        }

@xavier-villelegier Do you know of a work around?

aaronhawkey avatar Nov 09 '23 21:11 aaronhawkey

Hey guys I'm trying to figure out ways to fix this, the react-aria collection API only accepts specific components as children (Item, Section) to build the collection, that's why it fails when trying to pass custom components.

@aaronhawkey did you try using the Dropdown dynamic render?

import React from "react";
import {Dropdown, DropdownTrigger, DropdownMenu, DropdownItem, Button} from "@nextui-org/react";

export default function App() {

  const notificationsData = [
    {
      key: "1",
      read: false,
      body: "First message",
      createdAt: null
    },
    {
      key: "2",
      read: false,
      body: "Second Message",
      createdAt: null
    },
    {
      key: "3",
      read: false,
      body: "Third message",
      createdAt: null
    },
  ];

  return (
    <Dropdown>
      <DropdownTrigger>
        <Button 
          variant="bordered" 
        >
          Open Menu
        </Button>
      </DropdownTrigger>
      <DropdownMenu aria-label="Notifications" items={notificationsData}>
        {(notification) => (
          <DropdownItem  key={item.key} textValue={notification.body}>
             <Notification
                  body={notification.body}
                  date={notification.createdAt}
                  read={notification.read}
                />
          </DropdownItem>
        )}
      </DropdownMenu>
    </Dropdown>
  );
}

jrgarciadev avatar Nov 10 '23 11:11 jrgarciadev

Hey, I'm struggling with a similar issue, although in my case it's with the DropdownSection:

<DropdownSection showDivider key={"notifications"} items={notifications}>
  {(item: any) => (
    <DropdownItem
      key={item.id} 
      onPress={notificationPressHandler}
      className={styles.notification__wrapper}
    >
      <span className={styles.notification__topic}>{item.topic}</span>
      <p className={styles.notification__content}>{item.content}</p>
    </DropdownItem>
  )}
</DropdownSection>

This is only a TypeScript error, I'll be using ts-ignore for now to avoid it. Components render fine.

louisescher avatar Nov 19 '23 13:11 louisescher

Hey ! I see you closed this in 2.2.0 @jrgarciadev ?

I'm still having this issue with the following in 2.2.1:

<Dropdown placement="bottom-end">
  <DropdownMenu aria-label="Profile Actions" variant="flat">
    {session.user?.isAdmin && (
      <DropdownSection title="Admin" showDivider>
        <DropdownItem key="prisma">Whatever</DropdownItem>
      </DropdownSection>
    )}
  </DropdownMenu>
</Dropdown>

Same symptoms, ts-error, then runtime error if wrapping it inside a fragment 😕

i have same isue for this

begalinsaf avatar Dec 08 '23 15:12 begalinsaf

you can try this bad solution

 let dropdownItems = [
    {
      key: 'profile',
      label: 'Profile',
    },
    {
      key: 'signout',
      label: 'Signout',
    },
  ];

  if (auth?.user.role !== 'USER') {
    dropdownItems.push({
      key: 'dashboard',
      label: 'Dashboard',
    });
  }
  
  //return
  
             <DropdownMenu
                aria-label='dynamic'
                items={dropdownItems}
              >
                {(item) => (
                  <DropdownItem
                    key={item.key}
                    color={item.key === 'signout' ? 'danger' : 'default'}
                    className={item.key === 'signout' ? 'text-danger' : ''}
                  >
                    {item.label}
                  </DropdownItem>
                )}
              </DropdownMenu>

begalinsaf avatar Mar 03 '24 06:03 begalinsaf

const list = [ { key: '1', value: 'item_1'}, { key: '2', value: 'item_2'}, ]

<DropdownMenu aria-label="Some label" variant="flat">
  <DropdownSection items={list}>
    {list.map((el) => <DropdownItem key={el.key}>{el.value}</DropdownItem>)}
  </DropdownSection>
  
  <DropdownItem key="logout" color="danger" onClick={handleLogout}>
    Logout
  </DropdownItem>
</DropdownMenu>

It works for me

savchukartur avatar Mar 11 '24 10:03 savchukartur

Hey guys I'm trying to figure out ways to fix this, the react-aria collection API only accepts specific components as children (Item, Section) to build the collection, that's why it fails when trying to pass custom components.

@aaronhawkey did you try using the Dropdown dynamic render?

import React from "react";
import {Dropdown, DropdownTrigger, DropdownMenu, DropdownItem, Button} from "@nextui-org/react";

export default function App() {

  const notificationsData = [
    {
      key: "1",
      read: false,
      body: "First message",
      createdAt: null
    },
    {
      key: "2",
      read: false,
      body: "Second Message",
      createdAt: null
    },
    {
      key: "3",
      read: false,
      body: "Third message",
      createdAt: null
    },
  ];

  return (
    <Dropdown>
      <DropdownTrigger>
        <Button 
          variant="bordered" 
        >
          Open Menu
        </Button>
      </DropdownTrigger>
      <DropdownMenu aria-label="Notifications" items={notificationsData}>
        {(notification) => (
          <DropdownItem  key={item.key} textValue={notification.body}>
             <Notification
                  body={notification.body}
                  date={notification.createdAt}
                  read={notification.read}
                />
          </DropdownItem>
        )}
      </DropdownMenu>
    </Dropdown>
  );
}

Sorry for the late reply. I did and it worked for me.

aaronhawkey avatar Mar 11 '24 15:03 aaronhawkey

None of the above solutions work for me for conditional rendering. I managed to implement a slightly tricky solution by using Type Guard, like so:

function testT<T>(input: T | false | undefined | null): input is T {
  return Boolean(input)
}

function filter<T>(input: (T | false | undefined | null)[]): T[] {
  return input.filter(testT)
}

...


        <DropdownSection showDivider>
          {/* here comes the above 'filter 'function */}
          {filter([
            hasUnpublished && (
              <DropdownItem key="publish" className="text-success">
                Publish
              </DropdownItem>
            ),
            hasPublished && (
              <DropdownItem key="unpublish" className="text-warning">
                Unpublish
              </DropdownItem>
            ),
          ])}
        </DropdownSection>

rkrajewski avatar Apr 02 '24 17:04 rkrajewski

If you use a ternary if ? : you can return a empty DropdownItem with a className of "hidden". It will work. Or just hide the the DropdownItem with a "hidden" class. If it's a client component then you'll be sending the javascript of all anyways.

pablojsx avatar Apr 15 '24 07:04 pablojsx

This should work

<DropdownMenu aria-label="Profile Actions" variant="flat">
      {menuItems ? (
        menuItems.map((entry) => (
          <DropdownItem key={entry.link} showDivider={entry.divider}>
            {entry.name}
          </DropdownItem>
        ))
      ) : (
        <DropdownItem>handle this, if there is no data</DropdownItem>
      )}
      <DropdownItem key="custom">Test123</DropdownItem>
    </DropdownMenu>

edymg avatar Jul 07 '24 03:07 edymg

I think this is a big bug,DropdownMenu no longer seems to accept the prop of children. in vscode,eslint error:不能将类型“{ children: (item: { link: string; name: string; }) => Element; "aria-label": string; items: { link: string; name: string; }[]; }”分配给类型“IntrinsicAttributes & Props & { ref?: Ref | undefined; }”。 类型“IntrinsicAttributes & Props & { ref?: Ref | undefined; }”上不存在属性“children”。ts(2322) and see the web ,error too: image Error: Functions are not valid as a child of Client Components. This may happen if you return children instead of <children /> from render. Or maybe you meant to call this function rather than return it. <... aria-label=... items={[...]} children={function children}>

HM-Suiji avatar Jul 14 '24 02:07 HM-Suiji

update nextui

pablojsx avatar Jul 14 '24 15:07 pablojsx

August 2024 and the issue is still happening is there a fix in the way?

MFRamon avatar Aug 20 '24 01:08 MFRamon

@MFRamon can you share your code reproducing the issue? I saw some usages aren't really bugs but misuse of the collection-based components.

wingkwong avatar Aug 20 '24 02:08 wingkwong