Skip to content

Commit

Permalink
fix: drawer trigger "as component" props (#2087)
Browse files Browse the repository at this point in the history
* fix: drawer trigger as props

* fix: modal.trigger missing state prop

* fix: dropdown.trigger missing state prop
  • Loading branch information
mleralec committed Jun 15, 2023
1 parent 2f67b72 commit 60a7898
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
9 changes: 4 additions & 5 deletions packages/Drawer/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Dialog,
DialogBackdropProps,
DialogDisclosure,
DialogDisclosureProps,
DialogInitialState,
DialogOptions,
DialogProps,
Expand Down Expand Up @@ -162,11 +161,11 @@ export const Footer: React.FC<BoxProps> = props => {
)
}

type TriggerProps = { state: DialogDisclosureProps; children: React.ReactNode; as?: As }
type TriggerProps = { state: DialogStateReturn; children: React.ReactNode; as?: As }

export const Trigger: React.FC<TriggerProps> = ({ as, state, ...rest }) => {
return <DialogDisclosure as={as} {...state} {...rest} />
}
export const Trigger = forwardRef<'button', TriggerProps>(({ as, state, ...rest }, ref) => {
return <DialogDisclosure as={as} ref={ref} {...state} {...rest} />
})

export const Drawer = Object.assign(DrawerComponent, {
Trigger,
Expand Down
22 changes: 22 additions & 0 deletions packages/Drawer/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,26 @@ describe('<Drawer>', () => {
fireEvent.click(getByText('open'))
expect(queryByRole('dialog')).toHaveStyleRule('height', '50%')
})

it('should render "as" correctly', () => {
const Test = () => {
const drawerState = useDrawerState()

return (
<>
<Drawer.Trigger as="button" onClick={() => null} state={drawerState}>
open
</Drawer.Trigger>
<Drawer aria-label="drawer" state={drawerState}>
test
</Drawer>
</>
)
}

const { getByText, queryByRole } = render(<Test />)
expect(queryByRole('dialog')).toBeNull()
fireEvent.click(getByText('open'))
expect(queryByRole('dialog')).toHaveTextContent('test')
})
})
10 changes: 8 additions & 2 deletions packages/DropdownMenu/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
useMenuState,
} from 'reakit'
import { useNextFrame } from '@welcome-ui/utils'
import { CreateWuiProps, forwardRef, OmitReakitState, WuiProps } from '@welcome-ui/system'
import { As, CreateWuiProps, forwardRef, OmitReakitState, WuiProps } from '@welcome-ui/system'

import { Arrow } from './Arrow'
import { Item } from './Item'
Expand Down Expand Up @@ -99,8 +99,14 @@ export function useDropdownMenuState(options?: DropdownMenuInitialState): Dropdo
return { open: dropdownMenuState.visible, ...dropdownMenuState }
}

type TriggerProps = { state: MenuStateReturn; children: React.ReactNode; as?: As }

export const Trigger = forwardRef<'button', TriggerProps>(({ as, state, ...rest }, ref) => {
return <MenuButton as={as} ref={ref} {...state} {...rest} />
})

export const DropdownMenu = Object.assign(DropdownMenuComponent, {
Trigger: MenuButton,
Trigger,
Item,
Separator,
Arrow,
Expand Down
10 changes: 8 additions & 2 deletions packages/Modal/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
useDialogState,
} from 'reakit'
import { BoxProps } from '@welcome-ui/box'
import { CreateWuiProps, forwardRef, OmitReakitState } from '@welcome-ui/system'
import { As, CreateWuiProps, forwardRef, OmitReakitState } from '@welcome-ui/system'
import { useTheme } from '@xstyled/styled-components'
import { Shape, ShapeProps } from '@welcome-ui/shape'

Expand Down Expand Up @@ -113,9 +113,15 @@ const Cover: React.FC<ShapeProps> = props => {
)
}

type TriggerProps = { state: DialogStateReturn; children: React.ReactNode; as?: As }

export const Trigger = forwardRef<'button', TriggerProps>(({ as, state, ...rest }, ref) => {
return <DialogDisclosure as={as} ref={ref} {...state} {...rest} />
})

// Nested exports
export const Modal = Object.assign(ModalComponent, {
Trigger: DialogDisclosure,
Trigger,
Content,
Header,
Body,
Expand Down
4 changes: 3 additions & 1 deletion packages/Modal/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ describe('<Modal>', () => {

return (
<>
<Modal.Trigger state={modalState}>open</Modal.Trigger>
<Modal.Trigger as="button" state={modalState} type="button">
open
</Modal.Trigger>
<Modal ariaLabel="modal" state={modalState}>
<Modal.Content>
{shouldRender && <Modal.Body>Modal.Body exist?</Modal.Body>}
Expand Down

0 comments on commit 60a7898

Please sign in to comment.