Skip to content

Add defaultOpen prop to Collapse for initial open state control. #474

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions src/Collapse/Collapse.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,20 @@ export const CheckboxEvents: Story<CollapseProps> = (args) => {
</div>
)
}

export const DefaultOpen: Story<CollapseProps> = (args) => {
return (
<Collapse {...args}>
<Collapse.Title className="text-xl font-medium">
Hi, 👋🏾 I am open by default
</Collapse.Title>
<Collapse.Content>
This content is visible by default because defaultOpen is set to true
</Collapse.Content>
</Collapse>
)
}
DefaultOpen.args = {
className: 'border border-base-300 bg-base-200',
defaultOpen: true
}
18 changes: 18 additions & 0 deletions src/Collapse/Collapse.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,22 @@ describe('Collapse', () => {
const contentElement = screen.getByText('Test Content')
expect(contentElement).toBeInTheDocument()
})

test('Should be closed by default without defaultOpen prop', () => {
render(<Collapse checkbox data-testid="collapse" />)
const checkboxInput = screen.getByRole('checkbox')
expect(checkboxInput).not.toBeChecked()
})

test('Should be open by default with defaultOpen prop', () => {
render(<Collapse checkbox defaultOpen data-testid="collapse" />)
const checkboxInput = screen.getByRole('checkbox')
expect(checkboxInput).toBeChecked()
})

test('Should respect open prop over defaultOpen', () => {
render(<Collapse checkbox defaultOpen open={false} data-testid="collapse" />)
const checkboxInput = screen.getByRole('checkbox')
expect(checkboxInput).not.toBeChecked()
})
})
9 changes: 7 additions & 2 deletions src/Collapse/Collapse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export type CollapseProps<T extends HTMLElement = HTMLDivElement> =
checkbox?: boolean
icon?: 'arrow' | 'plus'
open?: boolean

// The default open state ( From the issue suggestion )
defaultOpen?: boolean
onOpen?: () => void
onClose?: () => void
onToggle?: () => void
Expand Down Expand Up @@ -42,6 +45,7 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>(
checkbox,
icon,
open,
defaultOpen = false,
dataTheme,
className,
onOpen,
Expand All @@ -51,7 +55,7 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>(
},
ref
): JSX.Element => {
const [isChecked, setIsChecked] = useState(open)
const [isChecked, setIsChecked] = useState(open ?? defaultOpen)
const checkboxRef = useRef<HTMLInputElement>(null)

// Handle events for checkbox changes
Expand All @@ -65,7 +69,7 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>(
onClose()
}

setIsChecked(checkboxRef.current?.checked)
setIsChecked(checkboxRef.current?.checked ?? false)
}

// Handle blur events, specifically handling open/close for non checkbox types
Expand Down Expand Up @@ -100,6 +104,7 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>(
className="peer"
ref={checkboxRef}
onChange={handleCheckboxChange}
defaultChecked={defaultOpen}
/>
)}
{children}
Expand Down