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 3 commits
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
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"dependencies": {
"@hookform/resolvers": "^2.8.5",
"@radix-ui/react-accordion": "^1.1.2",
"@radix-ui/react-collapsible": "^1.0.3",
"@radix-ui/react-label": "^2.0.2",
"@radix-ui/react-popover": "^1.0.6",
"@radix-ui/react-radio-group": "^1.1.3",
Expand Down
74 changes: 74 additions & 0 deletions src/ui/ExpandableSection/ExpandableSection.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import React, { ReactNode, useState } from 'react'

import { ExpandableSection } from 'ui/ExpandableSection'

interface TestExpandableSectionProps {
title: string
children: ReactNode
}

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

return (
<ExpandableSection>
<ExpandableSection.Trigger
isExpanded={isExpanded}
onClick={() => setIsExpanded(!isExpanded)}
>
{title}
</ExpandableSection.Trigger>
<ExpandableSection.Content>{children}</ExpandableSection.Content>
</ExpandableSection>
)
}

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

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

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(
<TestExpandableSection title="Test Title">
Test Content
</TestExpandableSection>
)

const button = screen.getByRole('button')

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

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

import { ExpandableSection } from './ExpandableSection'

const meta: Meta<typeof ExpandableSection> = {
title: 'Components/ExpandableSection',
component: ExpandableSection,
}
export default meta

type Story = StoryObj<typeof ExpandableSection>

const DefaultStory: React.FC = () => {
const [isExpanded, setIsExpanded] = useState(false)
return (
<ExpandableSection>
<ExpandableSection.Trigger
isExpanded={isExpanded}
onClick={() => setIsExpanded(!isExpanded)}
>
Expandable Section
</ExpandableSection.Trigger>
<ExpandableSection.Content>
This is the content of the expandable section.
</ExpandableSection.Content>
</ExpandableSection>
)
}

const WithHtmlContentStory: React.FC = () => {
const [isExpanded, setIsExpanded] = useState(false)
return (
<ExpandableSection>
<ExpandableSection.Trigger
isExpanded={isExpanded}
onClick={() => setIsExpanded(!isExpanded)}
>
Expandable Section with HTML
</ExpandableSection.Trigger>
<ExpandableSection.Content>
<div>
Copy link
Contributor

Choose a reason for hiding this comment

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

I think ExpandableSection.Content should functionally be a div, so could probably remove this div here. Doesn't really matter though

<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>
</ExpandableSection.Content>
</ExpandableSection>
)
}

export const Default: Story = {
render: () => <DefaultStory />,
}

export const WithHtmlContent: Story = {
render: () => <WithHtmlContentStory />,
}
67 changes: 67 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,67 @@
import * as Collapsible from '@radix-ui/react-collapsible'
Copy link
Contributor

Choose a reason for hiding this comment

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

You'll probably need to install this as a dependency as with Radix you install the different primitives individually, as they're packaged individually.

import cs from 'classnames'
Copy link
Contributor

Choose a reason for hiding this comment

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

Should use new cn wrapper instead of cs

Copy link
Contributor

Choose a reason for hiding this comment

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

It's a drop in replacement

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated. Why did we replace cs though?

Copy link
Contributor

Choose a reason for hiding this comment

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

cn is a mix of classnames, and another utility called tailwind-merge, tailwind-merge will remove duplicate and potentially conflicting classnames for us, which has caused some issues beforehand.

import React, { forwardRef, ReactNode } from 'react'

import Icon from 'ui/Icon'

const ExpandableSectionRoot = forwardRef<
React.ElementRef<typeof Collapsible.Root>,
React.ComponentPropsWithoutRef<typeof Collapsible.Root>
>(({ children, className, ...props }, ref) => (
<Collapsible.Root
className={cs('my-2 border border-gray-200', className)}
{...props}
ref={ref}
>
{children}
</Collapsible.Root>
))

ExpandableSectionRoot.displayName = 'ExpandableSectionRoot'

interface ExpandableSectionTriggerProps
extends React.ComponentPropsWithoutRef<typeof Collapsible.Trigger> {
isExpanded: boolean
children: ReactNode
}

const ExpandableSectionTrigger = forwardRef<
React.ElementRef<typeof Collapsible.Trigger>,
ExpandableSectionTriggerProps
>(({ isExpanded, className, children, ...props }, ref) => (
<Collapsible.Trigger asChild>
<button
className={cs(
'flex w-full items-center justify-between p-4 text-left font-semibold hover:bg-gray-100',
className
)}
{...props}
ref={ref}
>
<span>{children}</span>
<Icon name={isExpanded ? 'chevronUp' : 'chevronDown'} size="sm" />
</button>
</Collapsible.Trigger>
))

ExpandableSectionTrigger.displayName = 'ExpandableSectionTrigger'

const ExpandableSectionContent = forwardRef<
React.ElementRef<typeof Collapsible.Content>,
React.ComponentPropsWithoutRef<typeof Collapsible.Content>
>(({ children, className, ...props }, ref) => (
<Collapsible.Content
className={cs('border-t border-gray-200 p-4', className)}
{...props}
ref={ref}
>
{children}
</Collapsible.Content>
))

ExpandableSectionContent.displayName = 'ExpandableSectionContent'

export const ExpandableSection = Object.assign(ExpandableSectionRoot, {
Trigger: ExpandableSectionTrigger,
Content: ExpandableSectionContent,
})
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 { ExpandableSection } from './ExpandableSection'
Loading