Skip to content

Commit

Permalink
fix: remove modal exception for outside click
Browse files Browse the repository at this point in the history
  • Loading branch information
lisalupi committed Oct 24, 2024
1 parent 26778e0 commit e83de16
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 68 deletions.
85 changes: 22 additions & 63 deletions packages/ui/src/components/SelectInputV2/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,23 +220,14 @@ const handleClickOutside = (
refSelect: RefObject<HTMLDivElement>,
onSearch: Dispatch<SetStateAction<DataType>>,
options: DataType,
isInsideModal: boolean,
) => {
if (
ref.current &&
!ref.current.contains(event.target as Node) &&
!refSelect.current?.contains(event.target as Node)
) {
if (isInsideModal) {
// Timeout when the selectInput is inside a modal to not close both the modal and the dropdown if it scrolls
setTimeout(() => {
setIsDropdownVisibile(false) // hide dropdown when clicking outside of the dropdown
onSearch(options) // reset displayed options to default when dropdown is hidden
}, 100)
} else {
setIsDropdownVisibile(false)
onSearch(options)
}
setIsDropdownVisibile(false)
onSearch(options)
}
}

Expand Down Expand Up @@ -748,33 +739,16 @@ export const Dropdown = ({
setSearch('')
}

const modalElement = document.getElementById('backdrop-modal')

if (modalElement) {
modalElement.addEventListener('mousedown', event =>
handleClickOutside(
event,
ref,
setIsDropdownVisible,
refSelect,
onSearch,
options,
true,
),
)
} else {
document.addEventListener('mousedown', event =>
handleClickOutside(
event,
ref,
setIsDropdownVisible,
refSelect,
onSearch,
options,
false,
),
)
}
document.addEventListener('mousedown', event =>
handleClickOutside(
event,
ref,
setIsDropdownVisible,
refSelect,
onSearch,
options,
),
)

if (!searchable) {
document.addEventListener('keydown', event =>
Expand All @@ -791,31 +765,16 @@ export const Dropdown = ({
}

return () => {
if (!modalElement) {
document.removeEventListener('mousedown', event =>
handleClickOutside(
event,
ref,
setIsDropdownVisible,
refSelect,
onSearch,
options,
false,
),
)
} else {
modalElement.addEventListener('mousedown', event =>
handleClickOutside(
event,
ref,
setIsDropdownVisible,
refSelect,
onSearch,
options,
true,
),
)
}
document.removeEventListener('mousedown', event =>
handleClickOutside(
event,
ref,
setIsDropdownVisible,
refSelect,
onSearch,
options,
),
)

if (!searchable) {
document.removeEventListener('keydown', event =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export const EmptyState: StoryFn<typeof SelectInputV2> = args => (
{...args}
emptyState={
<EmptyStateComponent
bordered
description="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
image={<DynamicIllustration name="empty" width="120px" />}
learnMore={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export const Modal: StoryFn<typeof SelectInputV2> = args => (
Title
</Text>
<Text as="p" variant="body" sentiment="neutral">
This is an example of SelectInput inside a modal. Click outside the
select input to close the SelectInput and outside the modal to close
both the modal and the selectInput.
This is an example of SelectInput inside a modal.
</Text>
<SelectInputV2 {...args} label="Label" />
<Text as="p" variant="body" sentiment="neutral">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Searchable.parameters = {
docs: {
description: {
story:
'Add a search bar in the dropdown to search through the different options. If there are less than 6 options, the search bar will **not** appear.',
'Add a search bar in the dropdown to search through the different options. If there are less than 6 options, the search bar will **not** appear, even if the prop is set to `true`.',
},
},
}

0 comments on commit e83de16

Please sign in to comment.