Skip to content

Commit e5cc2e1

Browse files
feat(accordion)!: add sections prop (#572)
1 parent 8a30d4b commit e5cc2e1

File tree

12 files changed

+284
-291
lines changed

12 files changed

+284
-291
lines changed

packages/accordion/.size-snapshot.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@
1919
}
2020
},
2121
"index.cjs.js": {
22-
"bundled": 4786,
23-
"minified": 2554,
24-
"gzipped": 1070
22+
"bundled": 4402,
23+
"minified": 2227,
24+
"gzipped": 961
2525
},
2626
"index.esm.js": {
27-
"bundled": 4662,
28-
"minified": 2434,
29-
"gzipped": 1052,
27+
"bundled": 4261,
28+
"minified": 2087,
29+
"gzipped": 952,
3030
"treeshaked": {
3131
"rollup": {
32-
"code": 161,
33-
"import_statements": 101
32+
"code": 162,
33+
"import_statements": 83
3434
},
3535
"webpack": {
36-
"code": 2507
36+
"code": 2168
3737
}
3838
}
3939
}

packages/accordion/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ attributes for a group of sections.
2727
```jsx
2828
import { useAccordion } from '@zendeskgarden/container-accordion';
2929

30-
const Accordion = ({ expandable = true, collapsible = true } = {}) => {
30+
const Accordion = ({ sections = [0, 1, 2], expandable = true, collapsible = true } = {}) => {
3131
const { getHeaderProps, getTriggerProps, getPanelProps, expandedSections, disabledSections } =
3232
useAccordion({
33+
sections,
3334
expandedSections: [0],
3435
expandable,
3536
collapsible
@@ -82,8 +83,8 @@ return <Accordion expandable={true} collapsible={true} />;
8283
```jsx
8384
import { AccordionContainer } from '@zendeskgarden/container-accordion';
8485

85-
const Accordion = ({ expandable = true, collapsible = true } = {}) => (
86-
<AccordionContainer expandable={expandable} collapsible={collapsible}>
86+
const Accordion = ({ sections = [0, 1, 2], expandable = true, collapsible = true } = {}) => (
87+
<AccordionContainer sections={sections} expandable={expandable} collapsible={collapsible}>
8788
{({ getHeaderProps, getTriggerProps, getPanelProps, expandedSections, disabledSections }) => (
8889
<>
8990
{sections.map((section, index) => {

packages/accordion/demo/accordion.stories.mdx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ import { Meta, ArgsTable, Canvas, Story, Markdown } from '@storybook/addon-docs'
22
import { useArgs } from '@storybook/client-api';
33
import { AccordionContainer } from '@zendeskgarden/container-accordion';
44
import { AccordionStory } from './stories/AccordionStory';
5+
import { SECTIONS } from './stories/data.ts';
56
import README from '../README.md';
67

78
<Meta
89
title="Packages/Accordion"
910
component={AccordionContainer}
10-
args={{ as: 'hook', sections: 5 }}
11+
args={{
12+
as: 'hook',
13+
sections: SECTIONS
14+
}}
1115
argTypes={{
12-
as: { options: ['container', 'hook'], control: 'radio', table: { category: 'Story' } },
13-
sections: { control: { type: 'range', min: 1, max: 9 }, table: { category: 'Story' } }
16+
as: { options: ['container', 'hook'], control: 'radio', table: { category: 'Story' } }
1417
}}
1518
/>
1619

@@ -39,7 +42,7 @@ import README from '../README.md';
3942
<Canvas>
4043
<Story
4144
name="Controlled"
42-
args={{ expandedSections: [0] }}
45+
args={{ expandedSections: SECTIONS.slice(0, 1) }}
4346
argTypes={{
4447
defaultExpandedSections: { control: false },
4548
collapsible: { control: false },
@@ -48,10 +51,10 @@ import README from '../README.md';
4851
>
4952
{args => {
5053
const updateArgs = useArgs()[1];
51-
const handleChange = index => {
52-
const expandedSections = args.expandedSections.includes(index)
53-
? args.expandedSections.filter(section => section !== index)
54-
: [...args.expandedSections, index];
54+
const handleChange = value => {
55+
const expandedSections = args.expandedSections.includes(value)
56+
? args.expandedSections.filter(section => section !== value)
57+
: [...args.expandedSections, value];
5558
updateArgs({ expandedSections });
5659
};
5760
return <AccordionStory {...args} onChange={handleChange} />;

packages/accordion/demo/stories/AccordionStory.tsx

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ import {
1515
useAccordion
1616
} from '@zendeskgarden/container-accordion';
1717

18-
interface IComponentProps extends IUseAccordionReturnValue {
19-
sections: number[];
18+
type ISectionValue = number;
19+
20+
interface IComponentProps<T = ISectionValue> extends IUseAccordionReturnValue<T> {
21+
sections: IUseAccordionProps<T>['sections'];
2022
}
2123

2224
const Component = ({
@@ -28,18 +30,17 @@ const Component = ({
2830
getPanelProps
2931
}: IComponentProps) => (
3032
<div style={{ width: 300 }}>
31-
{sections.map((section, index) => {
32-
const disabled = disabledSections.indexOf(index) !== -1;
33-
const hidden = expandedSections.indexOf(index) === -1;
33+
{sections.map((value, index) => {
34+
const disabled = disabledSections.indexOf(value) !== -1;
35+
const hidden = expandedSections.indexOf(value) === -1;
3436

3537
return (
36-
<div key={section}>
37-
<div {...getHeaderProps({ ariaLevel: 2 })}>
38+
<div key={value}>
39+
<div {...getHeaderProps({ 'aria-level': 2 })}>
3840
<button
3941
{...getTriggerProps({
40-
index,
42+
value,
4143
role: null,
42-
tabIndex: null,
4344
disabled
4445
})}
4546
className="text-left w-full"
@@ -50,7 +51,7 @@ const Component = ({
5051
</div>
5152
<section
5253
{...getPanelProps({
53-
index,
54+
value,
5455
role: null,
5556
hidden
5657
})}
@@ -65,38 +66,33 @@ const Component = ({
6566
</div>
6667
);
6768

68-
interface IProps extends IUseAccordionProps {
69-
sections: number[];
70-
}
71-
72-
const Container = ({ sections, ...props }: IProps) => (
69+
const Container = (props: IAccordionContainerProps<ISectionValue>) => (
7370
<AccordionContainer {...props}>
74-
{containerProps => <Component sections={sections} {...containerProps} />}
71+
{/* eslint-disable-next-line react/destructuring-assignment */}
72+
{containerProps => <Component sections={props.sections} {...containerProps} />}
7573
</AccordionContainer>
7674
);
7775

78-
const Hook = ({ sections, ...props }: IProps) => {
76+
const Hook = (props: IUseAccordionProps<ISectionValue>) => {
7977
const hookProps = useAccordion(props);
8078

81-
return <Component sections={sections} {...hookProps} />;
79+
// eslint-disable-next-line react/destructuring-assignment
80+
return <Component sections={props.sections} {...hookProps} />;
8281
};
8382

84-
interface IArgs extends IAccordionContainerProps {
83+
interface IArgs extends IAccordionContainerProps<ISectionValue> {
8584
as: 'hook' | 'container';
86-
sections: number;
8785
}
8886

89-
export const AccordionStory: Story<IArgs> = ({ as, sections, ...props }: IArgs) => {
87+
export const AccordionStory: Story<IArgs> = ({ as, ...props }: IArgs) => {
9088
const Accordion = () => {
91-
const _sections = Array.from({ length: sections }, (_, index) => index);
92-
9389
switch (as) {
9490
case 'container':
95-
return <Container sections={_sections} {...props} />;
91+
return <Container {...props} />;
9692

9793
case 'hook':
9894
default:
99-
return <Hook sections={_sections} {...props} />;
95+
return <Hook {...props} />;
10096
}
10197
};
10298

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright Zendesk, Inc.
3+
*
4+
* Use of this source code is governed under the Apache License, Version 2.0
5+
* found at http://www.apache.org/licenses/LICENSE-2.0.
6+
*/
7+
8+
export const SECTIONS = ['section-1', 'section-2', 'section-3', 'section-4', 'section-5'];

packages/accordion/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
"types": "dist/typings/index.d.ts",
2222
"dependencies": {
2323
"@babel/runtime": "^7.8.4",
24-
"@zendeskgarden/container-utilities": "^1.0.8",
25-
"react-uid": "^2.2.0"
24+
"@zendeskgarden/container-utilities": "^1.0.8"
2625
},
2726
"peerDependencies": {
2827
"prop-types": "^15.6.1",

0 commit comments

Comments
 (0)