-
Notifications
You must be signed in to change notification settings - Fork 23
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
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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() | ||
}) | ||
}) |
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,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} />, | ||
} |
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,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 |
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. 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. |
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 { default } from './ExpandableSection' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.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.
There's also a couple of components that Spencer has also done up already in the newer style that you can also reference.
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.
Card
,RadioTileGroup
,CopyClipboard
, andCodeSnippet
are the ones I've done/redone, for reference! :)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.
The
SummaryDropdown
is using theAccordion
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 theCollapsible
.