-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Accordion Component #23
Changes from 11 commits
481a949
b248647
04acbd3
25f58b7
5ca5c75
4af6f59
11585cf
406a587
465d503
6136601
069864f
a13478f
e6dbfc0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
import { Accordion } from './Accordion'; | ||
|
||
const meta: Meta<typeof Accordion> = { | ||
title: 'Components/Accordion', | ||
component: Accordion, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof Accordion>; | ||
|
||
const defaultProps = { | ||
items: [ | ||
{ title: 'Section 1', content: 'Content for section 1' }, | ||
{ title: 'Section 2', content: 'Content for section 2' }, | ||
], | ||
}; | ||
|
||
export const Default: Story = { | ||
args: { | ||
...defaultProps, | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const AccordionContainer = styled.div` | ||
width: 100%; | ||
`; | ||
|
||
export const AccordionItem = styled.div` | ||
border: 1px solid ${({ theme }) => theme.colors.border || '#ccc'}; | ||
margin-bottom: ${({ theme }) => theme.spacing.xs || '5px'}; | ||
`; | ||
|
||
export const AccordionTitle = styled.div` | ||
background-color: ${({ theme }) => theme.colors.backgroundAlt || '#f1f1f1'}; | ||
padding: ${({ theme }) => theme.spacing.sm || '10px'}; | ||
cursor: pointer; | ||
`; | ||
|
||
export const AccordionContent = styled.div` | ||
padding: ${({ theme }) => theme.spacing.sm || '10px'}; | ||
display: none; | ||
|
||
&.open { | ||
display: block; | ||
} | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { screen, fireEvent } from '@testing-library/react'; | ||
import { renderWithDeps } from '../../../jest.utils'; | ||
import { Accordion } from './Accordion'; | ||
|
||
describe('<Accordion />', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add an accessibility test here. You can find an example of how to do this in the updated testing template. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the accessibility test in Accordion.test.tsx |
||
it('renders', () => { | ||
renderWithDeps(<Accordion items={[{ title: 'Title', content: 'Content' }]} />); | ||
|
||
const titleElement = screen.getByText('Title'); | ||
|
||
expect(titleElement).toBeInTheDocument(); | ||
}); | ||
|
||
it('toggles content on click', () => { | ||
renderWithDeps(<Accordion items={[{ title: 'Title', content: 'Content' }]} />); | ||
|
||
const titleElement = screen.getByText('Title'); | ||
fireEvent.click(titleElement); | ||
|
||
const contentElement = screen.getByText('Content'); | ||
expect(contentElement).toBeVisible(); | ||
|
||
fireEvent.click(titleElement); | ||
expect(contentElement).not.toBeVisible(); | ||
}); | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React, { useState, ReactNode } from 'react'; | ||
import { | ||
AccordionContainer, | ||
AccordionItem, | ||
AccordionTitle, | ||
AccordionContent, | ||
} from './Accordion.style'; | ||
|
||
type AccordionProps = { | ||
items: { title: string; content: ReactNode }[]; // Change content to ReactNode | ||
}; | ||
|
||
export const Accordion: React.FC<AccordionProps> = ({ items }) => { | ||
const [openIndex, setOpenIndex] = useState<number | null>(null); | ||
|
||
const handleToggle = (index: number) => { | ||
setOpenIndex(index === openIndex ? null : index); | ||
}; | ||
|
||
return ( | ||
<AccordionContainer> | ||
{items.map((item, index) => ( | ||
<AccordionItem key={index}> | ||
<AccordionTitle onClick={() => handleToggle(index)}> | ||
{item.title} | ||
</AccordionTitle> | ||
<AccordionContent className={openIndex === index ? 'open' : ''}> | ||
{item.content} | ||
</AccordionContent> | ||
</AccordionItem> | ||
))} | ||
</AccordionContainer> | ||
); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really nice work on the styled components structure here!