nextui icon indicating copy to clipboard operation
nextui copied to clipboard

[BUG] - `Accordion` doesn't handle `selectedKeys` changes

Open wolkyura opened this issue 3 years ago • 4 comments

NextUI Version

2.0.19

Describe the bug

It's only possible to update accordion selection through the onSelectionChange handler. I have attached a link to a codesandbox demonstrating a simple example where accordions are used as steps with Back/Next buttons. When trying to change selectedKeys - nothing happens.

  const [selectedKeys, setSelectedKeys] = React.useState<Selection>(new Set(["1"]));

  return (
    <Accordion
      selectedKeys={selectedKeys}
      onSelectionChange={setSelectedKeys}
    >
      <AccordionItem key="1" aria-label="Accordion 1" title="Accordion 1">
        <Button onClick={() => setSelectedKeys(new Set(["2"]))}>Next</Button>
      </AccordionItem>
    </Accordion>
)

Your Example Website or App

https://codesandbox.io/p/sandbox/reverent-maria-vr8229

Steps to Reproduce the Bug or Issue

  1. Click on the link to sandbox
  2. See accordions
  3. Click on the "Next" button

Expected behavior

I'd expect that it's possible to update selectedKeys from any handler by just returning new Set of values (new Set())

Screenshots or Videos

No response

Operating System Version

macOS 12.3.1

Browser

Chrome

wolkyura avatar Aug 14 '23 16:08 wolkyura

As given in the documentation in https://nextui.org/docs/components/accordion, the type for selectedKeys should be React.Key[] and not a Set. Then, the rest should work fine.

sohamsen15 avatar Aug 16 '23 17:08 sohamsen15

I tried to change Set() to string[], but it didn't work either (https://codesandbox.io/p/sandbox/busy-leftpad-5xy9p6).

The example of controlled Acoordion uses new Set() (https://nextui.org/docs/components/accordion#controlled): Screen Shot 2023-08-16 at 8 08 43 PM

Also, I think it's a bit confusing that state and setter have different types

Screen Shot 2023-08-16 at 8 04 43 PM

wolkyura avatar Aug 16 '23 18:08 wolkyura

Yeah, this is a bug. What I think is happening is that the onSelectionChange doesn't pass through the state change to the actual rendered component.

sohamsen15 avatar Aug 16 '23 18:08 sohamsen15

Hey guys, we're looking into this, but yes it is a bug. Thanks, @wolkyura for reporting it

jrgarciadev avatar Aug 16 '23 18:08 jrgarciadev