Skip to content

Commit

Permalink
Hide dropdown content when clicking away (#1989)
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerbarna authored May 24, 2024
1 parent 799f515 commit 8a7ad20
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
9 changes: 7 additions & 2 deletions app/routes/circulars._archive._index/DateSelectorMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '@trussworks/react-uswds'
import classNames from 'classnames'
import { type ChangeEvent, useRef, useState } from 'react'
import { useOnClickOutside } from 'usehooks-ts'

import DetailsDropdownContent from '~/components/DetailsDropdownContent'

Expand Down Expand Up @@ -77,7 +78,11 @@ export function DateSelector({
defaultStartDate?: string
defaultEndDate?: string
}) {
const ref = useRef<HTMLDivElement>(null)
const [showContent, setShowContent] = useState(false)
useOnClickOutside(ref, () => {
setShowContent(false)
})
const defaultShowDateRange = Boolean(
(defaultStartDate && !dateSelectorLabels[defaultStartDate]) ||
defaultEndDate
Expand Down Expand Up @@ -105,7 +110,7 @@ export function DateSelector({
}

return (
<>
<div ref={ref}>
<input
type="hidden"
name="startDate"
Expand Down Expand Up @@ -218,6 +223,6 @@ export function DateSelector({
</CardFooter>
)}
</DetailsDropdownContent>
</>
</div>
)
}
14 changes: 9 additions & 5 deletions app/routes/circulars._archive._index/SortSelectorButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
} from '@trussworks/react-uswds'
import classNames from 'classnames'
import type { ChangeEvent } from 'react'
import { useState } from 'react'
import { useRef, useState } from 'react'
import { useOnClickOutside } from 'usehooks-ts'

import DetailsDropdownContent from '~/components/DetailsDropdownContent'

Expand Down Expand Up @@ -58,9 +59,12 @@ export function SortSelector({
form?: string
defaultValue?: string
}) {
const [showContent, setShowContent] = useState(false)

const submit = useSubmit()
const ref = useRef<HTMLDivElement>(null)
const [showContent, setShowContent] = useState(false)
useOnClickOutside(ref, () => {
setShowContent(false)
})

function radioOnChange({ target }: ChangeEvent<HTMLInputElement>) {
setShowContent(false)
Expand Down Expand Up @@ -88,7 +92,7 @@ export function SortSelector({
)

return (
<>
<div ref={ref}>
<SortButton
sort={sanitizedValue}
expanded={showContent}
Expand All @@ -108,6 +112,6 @@ export function SortSelector({
</Grid>
</CardBody>
</DetailsDropdownContent>
</>
</div>
)
}

0 comments on commit 8a7ad20

Please sign in to comment.