-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5188 from Sage/click-away-wrapper
feat(click-away-wrapper): add ClickAwayWrapper component to internal directory
- Loading branch information
Showing
8 changed files
with
475 additions
and
216 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/__internal__/click-away-wrapper/click-away-wrapper.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React, { useEffect } from "react"; | ||
import Events from "../utils/helpers/events"; | ||
|
||
export interface ClickAwayWrapperProps { | ||
children: React.ReactNode; | ||
handleClickAway: (ev: CustomEvent) => void; | ||
eventTypeId?: "mousedown" | "click"; | ||
targets: React.RefObject<HTMLElement>[]; | ||
} | ||
|
||
const ClickAwayWrapper = ({ | ||
children, | ||
handleClickAway, | ||
eventTypeId = "click", | ||
targets, | ||
}: ClickAwayWrapperProps) => { | ||
useEffect(() => { | ||
const fnClickAway = (ev: CustomEvent) => { | ||
const clickedElements = targets.filter( | ||
(ref: React.RefObject<HTMLElement>) => | ||
ref?.current && Events.composedPath(ev).includes(ref.current) | ||
); | ||
|
||
if (!clickedElements || !clickedElements.length) { | ||
handleClickAway(ev); | ||
} | ||
}; | ||
|
||
document.addEventListener(eventTypeId, fnClickAway as EventListener); | ||
|
||
return function cleanup() { | ||
document.removeEventListener(eventTypeId, fnClickAway as EventListener); | ||
}; | ||
}, [handleClickAway, targets, eventTypeId]); | ||
|
||
return <>{children}</>; | ||
}; | ||
|
||
ClickAwayWrapper.displayName = "ClickAwayWrapper"; | ||
|
||
export default ClickAwayWrapper; |
81 changes: 81 additions & 0 deletions
81
src/__internal__/click-away-wrapper/click-away-wrapper.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import React, { useRef } from "react"; | ||
import { mount } from "enzyme"; | ||
import ClickAwayWrapper, { | ||
ClickAwayWrapperProps, | ||
} from "./click-away-wrapper.component"; | ||
|
||
const MockComponent = ({ | ||
handleClickAway, | ||
eventTypeId, | ||
}: Omit<ClickAwayWrapperProps, "children" | "targets">) => { | ||
const ref = useRef<HTMLDivElement>(null); | ||
return ( | ||
<ClickAwayWrapper | ||
targets={[ref]} | ||
handleClickAway={handleClickAway} | ||
eventTypeId={eventTypeId} | ||
> | ||
<div ref={ref}>Child</div> | ||
</ClickAwayWrapper> | ||
); | ||
}; | ||
|
||
describe("ClickAwayWrapper", () => { | ||
it("adds the event listener on mount", () => { | ||
const addListenerSpy = jest.spyOn(document, "addEventListener"); | ||
mount(<MockComponent handleClickAway={jest.fn()} />); | ||
expect(addListenerSpy).toHaveBeenCalled(); | ||
}); | ||
|
||
it("removes the event listener on unmount", () => { | ||
const removeListenerSpy = jest.spyOn(document, "removeEventListener"); | ||
const wrapper = mount(<MockComponent handleClickAway={jest.fn()} />); | ||
wrapper.unmount(); | ||
expect(removeListenerSpy).toHaveBeenCalled(); | ||
}); | ||
|
||
it("calls handleClickAway when mousedown is outside of wrapper element", () => { | ||
const handleClickAway = jest.fn(); | ||
mount( | ||
<MockComponent | ||
handleClickAway={handleClickAway} | ||
eventTypeId="mousedown" | ||
/> | ||
); | ||
document.dispatchEvent(new MouseEvent("mousedown", { bubbles: true })); | ||
expect(handleClickAway).toHaveBeenCalled(); | ||
}); | ||
|
||
it("does not call handleClickAway when mousedown is inside of wrapper element", () => { | ||
const handleClickAway = jest.fn(); | ||
const wrapper = mount(<MockComponent handleClickAway={handleClickAway} />); | ||
document.dispatchEvent( | ||
new CustomEvent("mousedown", { | ||
detail: { | ||
enzymeTestingTarget: wrapper?.find("div").getDOMNode(), | ||
}, | ||
}) | ||
); | ||
expect(handleClickAway).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("calls handleClickAway when click is outside of wrapper element", () => { | ||
const handleClickAway = jest.fn(); | ||
mount(<MockComponent handleClickAway={handleClickAway} />); | ||
document.dispatchEvent(new MouseEvent("click", { bubbles: true })); | ||
expect(handleClickAway).toHaveBeenCalled(); | ||
}); | ||
|
||
it("does not call handleClickAway when click is inside of wrapper element", () => { | ||
const handleClickAway = jest.fn(); | ||
const wrapper = mount(<MockComponent handleClickAway={handleClickAway} />); | ||
document.dispatchEvent( | ||
new CustomEvent("click", { | ||
detail: { | ||
enzymeTestingTarget: wrapper?.find("div").getDOMNode(), | ||
}, | ||
}) | ||
); | ||
expect(handleClickAway).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from "./click-away-wrapper.component"; | ||
export type { ClickAwayWrapperProps } from "./click-away-wrapper.component"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.