Skip to content

Commit 37a9f15

Browse files
Make Accordion support render prop (#2502)
Co-authored-by: Robert Snow <[email protected]>
1 parent 56e1cf0 commit 37a9f15

File tree

2 files changed

+65
-35
lines changed

2 files changed

+65
-35
lines changed

packages/@react-spectrum/accordion/src/Accordion.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {classNames, useDOMRef, useStyleProps} from '@react-spectrum/utils';
1616
import {DOMRef, Node} from '@react-types/shared';
1717
import {filterDOMProps, mergeProps} from '@react-aria/utils';
1818
import {FocusRing} from '@react-aria/focus';
19-
import React, {useRef} from 'react';
19+
import React, {forwardRef, useRef} from 'react';
2020
import {SpectrumAccordionProps} from '@react-types/accordion';
2121
import styles from '@adobe/spectrum-css-temp/components/accordion/vars.css';
2222
import {TreeState, useTreeState} from '@react-stately/tree';
@@ -96,5 +96,5 @@ function AccordionItem<T>(props: AccordionItemProps<T>) {
9696
);
9797
}
9898

99-
const _Accordion = React.forwardRef(Accordion);
99+
const _Accordion = forwardRef(Accordion) as <T>(props: SpectrumAccordionProps<T> & {ref?: DOMRef<HTMLDivElement>}) => ReturnType<typeof Accordion>;
100100
export {_Accordion as Accordion};

packages/@react-spectrum/accordion/stories/Accordion.stories.tsx

Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,69 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13+
import {Story as _Story, Meta} from '@storybook/react';
1314
import {Accordion, Item} from '../src';
1415
import React from 'react';
15-
import {storiesOf} from '@storybook/react';
16-
17-
storiesOf('Accordion', module)
18-
.add(
19-
'default',
20-
() => render({})
21-
)
22-
.add(
23-
'disabledKeys: files, shared',
24-
() => render({disabledKeys: ['files', 'shared']})
25-
)
26-
.add('defaultExpandedKeys: files', () => render({
27-
defaultExpandedKeys: ['files']
28-
}))
29-
.add('disabledKeys: files, shared, defaultExpandedKeys: files', () => render({
30-
defaultExpandedKeys: ['files'],
31-
disabledKeys: ['files', 'shared']
32-
}));
33-
34-
function render(props = {}) {
35-
return (
36-
<Accordion {...props} >
37-
<Item key="files" title="Your files">
38-
files
39-
</Item>
40-
<Item key="shared" title="Shared with you">
41-
shared
42-
</Item>
43-
<Item key="last" title="Last item">
44-
last
45-
</Item>
46-
</Accordion>
47-
);
16+
import {SpectrumAccordionProps} from '@react-types/accordion';
17+
18+
type ItemType = {
19+
key: React.Key,
20+
title: string
21+
};
22+
23+
/**
24+
* Helper type so `bind` returns the actual Story type.
25+
*/
26+
interface Story<T> extends _Story<T> {
27+
bind: (this: ThisParameterType<typeof Function.bind>, thisArg: Parameters<typeof Function.bind>[0], ...argArray: Parameters<typeof Function.bind>[1][]) => _Story<T>
4828
}
29+
30+
export default {
31+
title: 'Accordion',
32+
component: Accordion
33+
} as Meta<SpectrumAccordionProps<ItemType>>;
34+
35+
const AccordionRenderPropsTemplate: Story<SpectrumAccordionProps<ItemType>> = (args) => (
36+
<Accordion {...args}>
37+
{item => <Item key={item.key} title={item.title}>{item.key}</Item>}
38+
</Accordion>
39+
);
40+
41+
export const Default = AccordionRenderPropsTemplate.bind({});
42+
Default.storyName = 'default';
43+
Default.args = {
44+
items: [
45+
{key: 'files', title: 'Your files'},
46+
{key: 'shared', title: 'Shared with you'},
47+
{key: 'last', title: 'Last item'}
48+
]
49+
};
50+
51+
const AccordionTemplate: Story<SpectrumAccordionProps<ItemType>> = (args) => (
52+
<Accordion {...args} >
53+
<Item key="files" title="Your files">
54+
files
55+
</Item>
56+
<Item key="shared" title="Shared with you">
57+
shared
58+
</Item>
59+
<Item key="last" title="Last item">
60+
last
61+
</Item>
62+
</Accordion>
63+
);
64+
65+
export const DefaultExpandedKeys = AccordionTemplate.bind({});
66+
DefaultExpandedKeys.storyName = 'defaultExpandedKeys: files';
67+
DefaultExpandedKeys.args = {defaultExpandedKeys: ['files']};
68+
69+
export const DisabledKeys = AccordionTemplate.bind({});
70+
DisabledKeys.storyName = 'disabledKeys: files, shared';
71+
DisabledKeys.args = {disabledKeys: ['files', 'shared']};
72+
73+
export const DisabledDefaultExpandedKeys = AccordionTemplate.bind({});
74+
DisabledDefaultExpandedKeys.storyName = 'defaultExpandedKeys: files, disabledKeys: files, shared';
75+
DisabledDefaultExpandedKeys.args = {
76+
defaultExpandedKeys: ['files'],
77+
disabledKeys: ['files', 'shared']
78+
};

0 commit comments

Comments
 (0)