-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: remove unused file * chore: add missing Icons * feat: add Accordion component * feat: add jest tests * chore: update disabled styling * chore: amends per review comments * Create warm-icons-provide.md Accordion Component SOV-567 * fix: arrow-up svg path
- Loading branch information
1 parent
8523a06
commit efcc9e4
Showing
10 changed files
with
271 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@sovryn/ui": patch | ||
--- | ||
|
||
Accordion Component SOV-567 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
.accordion { | ||
@apply flex flex-col my-3; | ||
} | ||
|
||
.label { | ||
@apply flex flex-row cursor-pointer items-stretch; | ||
|
||
&.disabled, | ||
&:disabled { | ||
@apply opacity-50 cursor-default; | ||
} | ||
} | ||
|
||
.arrow { | ||
@apply ml-2 flex flex-col items-center justify-center; | ||
} | ||
|
||
.icon { | ||
@apply transition-transform; | ||
&.isOpen { | ||
@apply transform rotate-180; | ||
} | ||
} | ||
|
||
.content { | ||
@apply mt-2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { useArgs } from '@storybook/client-api'; | ||
import { Story, Meta } from '@storybook/react'; | ||
|
||
import React, { ComponentProps, useCallback } from 'react'; | ||
|
||
import { | ||
Dropdown, | ||
DropdownMode, | ||
DropdownSize, | ||
Menu, | ||
MenuItem, | ||
} from '../../2_molecules'; | ||
import { Paragraph } from '../Paragraph/Paragraph'; | ||
import { Accordion } from './Accordion'; | ||
|
||
export default { | ||
title: 'Atoms/Accordion', | ||
component: Accordion, | ||
} as Meta; | ||
|
||
const Template: Story<ComponentProps<typeof Accordion>> = args => { | ||
const [, updateArgs] = useArgs(); | ||
const handleOnChange = useCallback( | ||
(toOpen: boolean) => updateArgs({ open: toOpen }), | ||
[updateArgs], | ||
); | ||
|
||
return <Accordion {...args} onClick={handleOnChange} />; | ||
}; | ||
|
||
export const Default = Template.bind({}); | ||
Default.args = { | ||
label: 'Test (click to toggle)', | ||
children: <div>Simple Test content</div>, | ||
disabled: false, | ||
dataLayoutId: 'accordion-simple', | ||
}; | ||
|
||
export const RichContent = Template.bind({}); | ||
RichContent.args = { | ||
label: 'Test (click to toggle)', | ||
children: ( | ||
<div className="w-75 bg-gray-30 p-3 rounded"> | ||
<Paragraph className="mb-2 text-gray-80">Content panel</Paragraph> | ||
<Dropdown | ||
text="Dropdown Button" | ||
size={DropdownSize.large} | ||
mode={DropdownMode.sameWidth} | ||
> | ||
<Menu> | ||
<MenuItem text="Dropdown Item 1" /> | ||
<MenuItem text="Dropdown Item 2" /> | ||
<MenuItem text="Dropdown Item 3" /> | ||
</Menu> | ||
</Dropdown> | ||
</div> | ||
), | ||
disabled: false, | ||
dataLayoutId: 'accordion-richcontent', | ||
}; | ||
|
||
export const OpenAndDisabled = Template.bind({}); | ||
OpenAndDisabled.args = { | ||
label: 'Test (disabled)', | ||
children: ( | ||
<div className="w-75 bg-gray-30 p-3 rounded"> | ||
<Paragraph className="mb-2 text-gray-80">Content panel</Paragraph> | ||
<Dropdown | ||
text="Dropdown Button" | ||
size={DropdownSize.large} | ||
mode={DropdownMode.sameWidth} | ||
> | ||
<Menu> | ||
<MenuItem text="Dropdown Item 1" /> | ||
<MenuItem text="Dropdown Item 2" /> | ||
<MenuItem text="Dropdown Item 3" /> | ||
</Menu> | ||
</Dropdown> | ||
</div> | ||
), | ||
disabled: true, | ||
open: true, | ||
dataLayoutId: 'accordion-opendisabled', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { render } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import React from 'react'; | ||
|
||
import { Accordion } from './Accordion'; | ||
|
||
describe('Accordion', () => { | ||
it('renders accordion', () => { | ||
const { getByText } = render( | ||
<Accordion label="Test" dataLayoutId="accordion-simple"> | ||
<div>Content</div> | ||
</Accordion>, | ||
); | ||
expect(getByText('Test')).toBeInTheDocument(); | ||
}); | ||
|
||
it('calls eventHandler on click', () => { | ||
const handleClick = jest.fn(); | ||
const { getByTestId } = render( | ||
<Accordion | ||
label="Test (click to toggle)" | ||
dataLayoutId="accordion-simple" | ||
onClick={handleClick} | ||
> | ||
<div>Content</div> | ||
</Accordion>, | ||
); | ||
userEvent.click(getByTestId('accordion-simple')); | ||
expect(handleClick).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('shows content on click', async () => { | ||
const handleClick = () => { | ||
render( | ||
<Accordion | ||
label="Test (click to toggle)" | ||
dataLayoutId="accordion-simple" | ||
open | ||
> | ||
<div data-layout-id="content-to-show">Content</div> | ||
</Accordion>, | ||
); | ||
}; | ||
const { getByTestId } = render( | ||
<Accordion | ||
label="Test (click to toggle)" | ||
dataLayoutId="accordion-simple" | ||
onClick={handleClick} | ||
> | ||
<div data-layout-id="original-content">Content</div> | ||
</Accordion>, | ||
); | ||
userEvent.click(getByTestId('accordion-simple')); | ||
expect(getByTestId('content-to-show')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders disabled accordion and prevents clicks on it', () => { | ||
const handleClick = jest.fn(); | ||
const { getByTestId } = render( | ||
<Accordion | ||
label="Test (click to toggle)" | ||
dataLayoutId="accordion-simple" | ||
onClick={handleClick} | ||
disabled | ||
> | ||
<div>Content</div> | ||
</Accordion>, | ||
); | ||
userEvent.click(getByTestId('accordion-simple')); | ||
expect(handleClick).toHaveBeenCalledTimes(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React, { ReactNode, FC, useCallback } from 'react'; | ||
|
||
import classNames from 'classnames'; | ||
|
||
import { Heading } from '../Heading/Heading'; | ||
import { HeadingType } from '../Heading/Heading.types'; | ||
import { Icon } from '../Icon/Icon'; | ||
import { IconNames } from '../Icon/Icon.types'; | ||
import styles from './Accordion.module.css'; | ||
|
||
export interface IAccordionProps { | ||
label: ReactNode; | ||
children: ReactNode; | ||
className?: string; | ||
disabled?: boolean; | ||
open?: boolean; | ||
onClick?: (toOpen: boolean) => void; | ||
dataLayoutId?: string; | ||
} | ||
|
||
export const Accordion: FC<IAccordionProps> = ({ | ||
label, | ||
children, | ||
className, | ||
disabled = false, | ||
open = false, | ||
onClick, | ||
dataLayoutId, | ||
}) => { | ||
const onClickCallback = useCallback( | ||
() => !disabled && onClick?.(!open), | ||
[disabled, open, onClick], | ||
); | ||
|
||
return ( | ||
<div className={classNames(styles.accordion, className)}> | ||
<button | ||
className={classNames(styles.label, { | ||
[styles.disabled]: disabled, | ||
})} | ||
onClick={onClickCallback} | ||
data-layout-id={dataLayoutId} | ||
> | ||
<> | ||
{typeof label === 'string' ? ( | ||
<Heading type={HeadingType.h3}>{label}</Heading> | ||
) : ( | ||
label | ||
)} | ||
</> | ||
<div className={styles.arrow}> | ||
<Icon | ||
icon={IconNames.ARROW_UP} | ||
size={8} | ||
className={classNames(styles.icon, { | ||
[styles.isOpen]: open, | ||
})} | ||
/> | ||
</div> | ||
</button> | ||
{open && ( | ||
<div | ||
className={styles.content} | ||
data-layout-id={`${dataLayoutId}-content`} | ||
> | ||
{children} | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Accordion'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters