Skip to content

Commit

Permalink
Update with Collapsible
Browse files Browse the repository at this point in the history
  • Loading branch information
RulaKhaled committed May 28, 2024
1 parent 189b7ea commit a7fdd9f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/ui/ExpandableSection/ExpandableSection.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'

import ExpandableSection from 'ui/ExpandableSection'
import { ExpandableSection } from 'ui/ExpandableSection'

describe('ExpandableSection', () => {
it('renders the title correctly', () => {
Expand Down Expand Up @@ -41,6 +41,7 @@ describe('ExpandableSection', () => {
expect(contentElement).toBeInTheDocument()

await userEvent.click(button)
expect(contentElement).not.toBeInTheDocument()
const contentElementAfterCollapse = screen.queryByText('Test Content')
expect(contentElementAfterCollapse).not.toBeInTheDocument()
})
})
2 changes: 1 addition & 1 deletion src/ui/ExpandableSection/ExpandableSection.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Meta, StoryObj } from '@storybook/react'

import ExpandableSection from './ExpandableSection'
import { ExpandableSection } from './ExpandableSection'

type ExpandableSectionStory = {
title: string
Expand Down
32 changes: 15 additions & 17 deletions src/ui/ExpandableSection/ExpandableSection.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { ReactNode, useState } from 'react'
import * as Collapsible from '@radix-ui/react-collapsible'
import React, { ReactNode } from 'react'

import Icon from 'ui/Icon'

Expand All @@ -7,28 +8,25 @@ interface ExpandableSectionProps {
children: ReactNode
}

const ExpandableSection: React.FC<ExpandableSectionProps> = ({
export const ExpandableSection: React.FC<ExpandableSectionProps> = ({
title,
children,
}) => {
const [isExpanded, setIsExpanded] = useState(false)
const [isExpanded, setIsExpanded] = React.useState(false)

return (
<div className="my-2 border border-gray-200">
<button
onClick={() => {
setIsExpanded(!isExpanded)
}}
className="flex w-full items-center justify-between p-4 text-left font-semibold hover:bg-gray-100"
>
<span>{title}</span>
<Icon name={isExpanded ? 'chevronUp' : 'chevronDown'} size="sm" />
</button>
{isExpanded && (
<div className="border-t border-gray-200 p-4">{children}</div>
)}
<Collapsible.Root open={isExpanded} onOpenChange={setIsExpanded}>
<Collapsible.Trigger asChild>
<button className="flex w-full items-center justify-between p-4 text-left font-semibold hover:bg-gray-100">
<span>{title}</span>
<Icon name={isExpanded ? 'chevronUp' : 'chevronDown'} size="sm" />
</button>
</Collapsible.Trigger>
<Collapsible.Content className="border-t border-gray-200 p-4">
{children}
</Collapsible.Content>
</Collapsible.Root>
</div>
)
}

export default ExpandableSection
2 changes: 1 addition & 1 deletion src/ui/ExpandableSection/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default } from './ExpandableSection'
export { ExpandableSection } from './ExpandableSection'

0 comments on commit a7fdd9f

Please sign in to comment.