Skip to content

Commit

Permalink
fix(collapsible): code review updates, tests and types
Browse files Browse the repository at this point in the history
  • Loading branch information
Powerplex committed Jun 7, 2024
1 parent caec9de commit 6c351bd
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 16 deletions.
9 changes: 2 additions & 7 deletions packages/components/collapsible/src/Collapsible.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ describe('Collapsible', () => {

render(
<Collapsible>
<Collapsible.Trigger asChild>
<button>Expand</button>
</Collapsible.Trigger>
<Collapsible.Trigger>Expand</Collapsible.Trigger>

<Collapsible.Content>
<p>Collapsible content</p>
Expand Down Expand Up @@ -48,7 +46,6 @@ describe('Collapsible', () => {

const trigger = screen.getByRole('button', { name: 'Expand' })

expect(trigger).toBeInTheDocument()
expect(screen.getByText('Collapsible content')).not.toBeVisible()

await user.click(trigger)
Expand Down Expand Up @@ -88,14 +85,14 @@ describe('Collapsible', () => {

const trigger = screen.getByRole('button', { name: 'Expand' })

expect(trigger).toBeInTheDocument()
expect(screen.getByText('Collapsible content')).not.toBeVisible()
expect(onChange).not.toHaveBeenCalled()

await user.click(trigger)

expect(screen.getByText('Collapsible content')).toBeVisible()
expect(onChange).toHaveBeenCalledWith(true)
expect(onChange).toHaveBeenCalledTimes(1)
})

it('should remain open when open is forced to `true`', async () => {
Expand All @@ -115,7 +112,6 @@ describe('Collapsible', () => {

const trigger = screen.getByRole('button', { name: 'Expand' })

expect(trigger).toBeInTheDocument()
expect(screen.getByText('Collapsible content')).toBeVisible()

await user.click(trigger)
Expand All @@ -140,7 +136,6 @@ describe('Collapsible', () => {

const trigger = screen.getByRole('button', { name: 'Expand' })

expect(trigger).toBeInTheDocument()
expect(screen.getByText('Collapsible content')).not.toBeVisible()

await user.click(trigger)
Expand Down
4 changes: 0 additions & 4 deletions packages/components/collapsible/src/Collapsible.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
type ComponentPropsWithoutRef,
createContext,
forwardRef,
type ReactNode,
useContext,
useId,
useMemo,
Expand All @@ -16,8 +15,6 @@ export interface CollapsibleProps extends ComponentPropsWithoutRef<'div'> {
* Change the default rendered element for the one passed as a child, merging their props and behavior.
*/
asChild?: boolean
children: ReactNode
className?: string
/**
* The open state of the collapsible when it is initially rendered. Use when you do not need to control its open state.
*/
Expand All @@ -26,7 +23,6 @@ export interface CollapsibleProps extends ComponentPropsWithoutRef<'div'> {
* When `true`, prevents the user from interacting with the collapsible.
*/
disabled?: boolean
id?: string
/**
* Event handler called when the open state of the collapsible changes.
*/
Expand Down
6 changes: 2 additions & 4 deletions packages/components/collapsible/src/CollapsibleContent.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Slot } from '@spark-ui/slot'
import { mergeProps } from '@zag-js/react'
import { cx } from 'class-variance-authority'
import { type ComponentPropsWithoutRef, forwardRef, type ReactNode } from 'react'
import { type ComponentPropsWithoutRef, forwardRef } from 'react'

import { useCollapsibleContext } from './Collapsible'

interface CollapsibleContentProps extends ComponentPropsWithoutRef<'div'> {
export interface CollapsibleContentProps extends ComponentPropsWithoutRef<'div'> {
asChild?: boolean
children: ReactNode
className?: string
}

export const Content = forwardRef<HTMLDivElement, CollapsibleContentProps>(
Expand Down
2 changes: 1 addition & 1 deletion packages/components/collapsible/src/CollapsibleTrigger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { type ComponentPropsWithoutRef, forwardRef, type ReactNode } from 'react

import { useCollapsibleContext } from './Collapsible'

interface CollapsibleTriggerProps extends ComponentPropsWithoutRef<'button'> {
export interface CollapsibleTriggerProps extends ComponentPropsWithoutRef<'button'> {
asChild?: boolean
children: ReactNode
className?: string
Expand Down
4 changes: 4 additions & 0 deletions packages/components/collapsible/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ export const Collapsible: FC<CollapsibleProps> & {
Collapsible.displayName = 'Collapsible'
Trigger.displayName = 'Collapsible.Trigger'
Content.displayName = 'Collapsible.Content'

export { type CollapsibleProps } from './Collapsible'
export { type CollapsibleContentProps } from './CollapsibleContent'
export { type CollapsibleTriggerProps } from './CollapsibleTrigger'

0 comments on commit 6c351bd

Please sign in to comment.