Skip to content
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

feat: Create a reusable component for expandable section #2903

Merged
merged 4 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/ui/ExpandableSection/ExpandableSection.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'

import ExpandableSection from 'ui/ExpandableSection'

describe('ExpandableSection', () => {
it('renders the title correctly', () => {
const title = 'Test Title'
render(
<ExpandableSection title={title}>
<div>Content</div>
</ExpandableSection>
)
const titleElement = screen.getByText(title)
expect(titleElement).toBeInTheDocument()
})

it('renders the children correctly after expanding', async () => {
render(
<ExpandableSection title="Test Title">
<div>Test Content</div>
</ExpandableSection>
)

const button = screen.getByRole('button')
await userEvent.click(button)

const contentElement = screen.getByText('Test Content')
expect(contentElement).toBeInTheDocument()
})

it('collapses the children after clicking the button twice', async () => {
render(
<ExpandableSection title="Test Title">Test Content</ExpandableSection>
)

const button = screen.getByRole('button')

await userEvent.click(button)
const contentElement = screen.getByText('Test Content')
expect(contentElement).toBeInTheDocument()

await userEvent.click(button)
expect(contentElement).not.toBeInTheDocument()
})
})
50 changes: 50 additions & 0 deletions src/ui/ExpandableSection/ExpandableSection.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Meta, StoryObj } from '@storybook/react'

import ExpandableSection from './ExpandableSection'

type ExpandableSectionStory = {
title: string
children: React.ReactNode
}

const meta: Meta<ExpandableSectionStory> = {
title: 'Components/ExpandableSection',
component: ExpandableSection,
argTypes: {
title: {
description: 'Title of the expandable section',
control: 'text',
},
children: {
description: 'Content of the expandable section',
control: 'text',
},
},
}
export default meta

type Story = StoryObj<ExpandableSectionStory>

export const Default: Story = {
args: {
title: 'Expandable Section',
children: 'This is the content of the expandable section.',
},
render: (args) => <ExpandableSection {...args} />,
}

export const WithHtmlContent: Story = {
args: {
title: 'Expandable Section with HTML',
children: (
<div>
<p>This is the content of the expandable section.</p>
<p>
It can contain HTML elements like <strong>bold text</strong> and{' '}
<em>italic text</em>.
</p>
</div>
),
},
render: (args) => <ExpandableSection {...args} />,
}
34 changes: 34 additions & 0 deletions src/ui/ExpandableSection/ExpandableSection.tsx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems very similar to the SummaryDropdown. Are you alright refactoring this to follow that style, as well as using the primitives from Radix? Refactoring to this style will also increase the composability of the component so it's super nice to work with and style, bringing it up to the GV2 style of writing composable components, as well as making it fully accessible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also a couple of components that Spencer has also done up already in the newer style that you can also reference.

Copy link
Contributor

@spalmurray-codecov spalmurray-codecov May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Card, RadioTileGroup, CopyClipboard, and CodeSnippet are the ones I've done/redone, for reference! :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SummaryDropdown is using the Accordion which may not be best for what we're trying to accomplish unless you're looking to stack a couple of this drop downs together, an alternative if they're going to all be separate or only one would be the Collapsible.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React, { ReactNode, useState } from 'react'

import Icon from 'ui/Icon'

interface ExpandableSectionProps {
title: string
children: ReactNode
}

const ExpandableSection: React.FC<ExpandableSectionProps> = ({
title,
children,
}) => {
const [isExpanded, setIsExpanded] = 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>
)}
</div>
)
}

export default ExpandableSection
1 change: 1 addition & 0 deletions src/ui/ExpandableSection/index.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we swap this from a default export to a named export similar to this, as this is the kinda style we're switching to make things a little nicer on the import.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './ExpandableSection'
Loading