Skip to content

Commit

Permalink
fix(accordion): selectedKeys added to the values memo function (#1639)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrgarciadev authored Sep 16, 2023
1 parent bbbdcbd commit bb8ed58
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-eagles-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/accordion": patch
---

Fix #1402 controlled accordion
1 change: 1 addition & 0 deletions packages/components/accordion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@react-types/shared": "^3.20.0"
},
"devDependencies": {
"@nextui-org/button": "workspace:*",
"@nextui-org/avatar": "workspace:*",
"@nextui-org/input": "workspace:*",
"@nextui-org/test-utils": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions packages/components/accordion/src/use-accordion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export function useAccordion<T extends object>(props: UseAccordionProps<T>) {
isCompact,
isDisabled,
hideIndicator,
selectedKeys,
disableAnimation,
keepContentMounted,
state?.expandedKeys.values,
Expand Down
49 changes: 38 additions & 11 deletions packages/components/accordion/stories/accordion.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from "@nextui-org/shared-icons";
import {Avatar} from "@nextui-org/avatar";
import {Input} from "@nextui-org/input";
import {Button} from "@nextui-org/button";

import {Accordion, AccordionProps, AccordionItem} from "../src";
import {AccordionItemProps} from "../src";
Expand Down Expand Up @@ -231,17 +232,43 @@ const ControlledTemplate = (args: AccordionProps) => {
console.log(selectedKeys);

return (
<Accordion {...args} selectedKeys={selectedKeys} onSelectionChange={setSelectedKeys}>
<AccordionItem key="1" aria-label="Accordion 1" title="Accordion 1">
{defaultContent}
</AccordionItem>
<AccordionItem key="2" aria-label="Accordion 2" title="Accordion 2">
{defaultContent}
</AccordionItem>
<AccordionItem key="3" aria-label="Accordion 3" title="Accordion 3">
{defaultContent}
</AccordionItem>
</Accordion>
<div className="flex flex-col gap-4">
<Accordion {...args} selectedKeys={selectedKeys}>
<AccordionItem key="1" aria-label="Accordion 1" title="Accordion 1">
{defaultContent}
</AccordionItem>
<AccordionItem key="2" aria-label="Accordion 2" title="Accordion 2">
{defaultContent}
</AccordionItem>
<AccordionItem key="3" aria-label="Accordion 3" title="Accordion 3">
{defaultContent}
</AccordionItem>
</Accordion>

<div className="flex gap-2">
<Button
onPress={() => {
setSelectedKeys(new Set(["1"]));
}}
>
Open 1
</Button>
<Button
onPress={() => {
setSelectedKeys(new Set(["2"]));
}}
>
Open 2
</Button>
<Button
onPress={() => {
setSelectedKeys(new Set(["3"]));
}}
>
Open 3
</Button>
</div>
</div>
);
};

Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 comments on commit bb8ed58

@vercel
Copy link

@vercel vercel bot commented on bb8ed58 Sep 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on bb8ed58 Sep 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.