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

Update example-react-modal.mdx #904

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 8 additions & 21 deletions docs/example-react-modal.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,18 @@ title: Modals
---

```jsx
import React, {useEffect} from 'react'
import ReactDOM from 'react-dom'
import {render, fireEvent} from '@testing-library/react'
import React from 'react'
import { render, fireEvent } from '@testing-library/react'

const modalRoot = document.createElement('div')
modalRoot.setAttribute('id', 'modal-root')
document.body.appendChild(modalRoot)

const Modal = ({onClose, children}) => {
const el = document.createElement('div')

useEffect(() => {
modalRoot.appendChild(el)

return () => modalRoot.removeChild(el)
})

return ReactDOM.createPortal(
const Modal = ({ onClose, children }) => {
return (
<div onClick={onClose}>
<div onClick={e => e.stopPropagation()}>
{children}
<hr />
<button onClick={onClose}>Close</button>
</div>
</div>,
el,
</div>
)
}

Expand All @@ -38,10 +24,10 @@ test('modal shows the children and a close button', () => {
const handleClose = jest.fn()

// Act
const {getByText} = render(
const { getByText } = render(
<Modal onClose={handleClose}>
<div>test</div>
</Modal>,
</Modal>
)
// Assert
expect(getByText('test')).toBeTruthy()
Expand All @@ -52,4 +38,5 @@ test('modal shows the children and a close button', () => {
// Assert
expect(handleClose).toHaveBeenCalledTimes(1)
})

```