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

issue #134: use an UncontrolledPopover to fix popovers for Safari #141

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
48 changes: 36 additions & 12 deletions src/components/Form/FormHelpButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { render, fireEvent } from '@testing-library/react'
import { render, fireEvent, waitForElementToBeRemoved } from '@testing-library/react'
import FormHelpButton from './FormHelpButton'

describe('FormHelpButton', () => {
Expand All @@ -15,28 +15,52 @@ describe('FormHelpButton', () => {
expect(queryByText('def')).toBeNull()
})

it('opens', () => {
const { getByLabelText, queryByText } = render(<FormHelpButton identifier="abc" label="def" />)
it('opens', async () => {
const { getByLabelText, findByText, queryByText } = render(<FormHelpButton identifier="abc" label="def" />)

fireEvent.focus(getByLabelText('help'))
fireEvent.click(getByLabelText('help'))

await findByText('def')
expect(queryByText('def')).not.toBeNull()
})

it('displays help', () => {
const { getByLabelText, getByText } = render(<FormHelpButton identifier="abc" label="def" help="some help" />)
it('displays help', async () => {
const { getByLabelText, findByText, queryByText } = render(
<FormHelpButton identifier="abc" label="def" help="some help" />,
)

fireEvent.focus(getByLabelText('help'))
fireEvent.click(getByLabelText('help'))

expect(getByText('some help')).toBeTruthy()
await findByText('some help')
expect(queryByText('some help')).toBeTruthy()
})

it('closes', () => {
const { getByLabelText, queryByText } = render(<FormHelpButton identifier="abc" label="def" />)
it('closes inside', async () => {
const { getByLabelText, findByText, queryByText } = render(<FormHelpButton identifier="abc" label="def" />)
fireEvent.click(getByLabelText('help'))
await findByText('def')
expect(queryByText('def')).not.toBeNull()

fireEvent.click(getByLabelText('help'))

await waitForElementToBeRemoved(() => queryByText('def'))
expect(queryByText('def')).toBeNull()
})

it('closes outside', async () => {
const { getByLabelText, findByText, getByText, queryByText } = render(
<div>
<FormHelpButton identifier="abc" label="def" />
<span>click here</span>
</div>,
)
fireEvent.click(getByLabelText('help'))
await findByText('def')
expect(queryByText('def')).not.toBeNull()

fireEvent.focus(getByLabelText('help'))
fireEvent.blur(getByLabelText('help'))
fireEvent.click(getByText('click here'))

await waitForElementToBeRemoved(() => queryByText('def'))
expect(queryByText('def')).toBeNull()
})
})
14 changes: 5 additions & 9 deletions src/components/Form/FormHelpButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from 'react'
import React from 'react'

import { Button, Card, CardBody, CardHeader, Popover } from 'reactstrap'
import { Button, Card, CardBody, CardHeader, UncontrolledPopover } from 'reactstrap'

import { FaQuestion } from 'react-icons/fa'

Expand All @@ -17,30 +17,26 @@ export interface FormHelpButtonProps {
}

export default function FormHelpButton({ identifier, label, help }: FormHelpButtonProps) {
const [popoverOpen, setPopoverOpen] = useState(false)

return (
<>
<Button
id={safeId(identifier)}
className="help-button"
type="button"
aria-label="help"
onClick={e => {
e.preventDefault()
e.stopPropagation()
}}
onFocus={() => setPopoverOpen(true)}
onBlur={() => setPopoverOpen(false)}
aria-label="help"
>
<FaQuestion className="help-button-icon" />
</Button>
<Popover placement="right" target={safeId(identifier)} trigger="focus" hideArrow isOpen={popoverOpen}>
<UncontrolledPopover placement="right" target={safeId(identifier)} trigger="legacy" hideArrow>
<Card>
<CardHeader>{label}</CardHeader>
<CardBody>{help}</CardBody>
</Card>
</Popover>
</UncontrolledPopover>
</>
)
}