-
Notifications
You must be signed in to change notification settings - Fork 20
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
feat(react): update to support react 17 #1563
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e706abf
feat(react): update to oficially support react 17
scurker c600901
add prop-types dependency
scurker 5b401e0
fix combobox test
scurker 72e6098
Merge branch 'develop' into react-17
scurker 3793977
Merge branch 'develop' into react-17
scurker fb84506
Merge branch 'develop' into react-17
scurker c0d49a0
update clickoutside listener
scurker 3c65bd0
Merge branch 'develop' into react-17
scurker a706f39
Merge branch 'develop' into react-17
scurker 74d0208
Merge branch 'develop' into react-17
scurker 7020a8f
Merge branch 'develop' into react-17
scurker 91dfe05
Merge branch 'develop' into react-17
anastasialanz 7f25ed5
Merge branch 'develop' into react-17
anastasialanz e4d12af
Merge branch 'develop' into react-17
anastasialanz ff1b1e5
test: switch to user-event for clicks in OptionsMenu test
anastasialanz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
109 changes: 42 additions & 67 deletions
109
packages/react/src/components/ClickOutsideListener/index.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 |
---|---|---|
@@ -1,97 +1,72 @@ | ||
import React from 'react'; | ||
import React, { useRef, useEffect } from 'react'; | ||
import setRef from '../../utils/setRef'; | ||
|
||
export interface ClickOutsideListenerProps< | ||
T extends HTMLElement = HTMLElement | ||
> { | ||
children?: React.ReactNode; | ||
children?: React.ReactElement; | ||
onClickOutside: (e: MouseEvent | TouchEvent) => void; | ||
mouseEvent?: 'mousedown' | 'click' | 'mouseup' | false; | ||
touchEvent?: 'touchstart' | 'touchend' | false; | ||
target?: T; | ||
} | ||
|
||
export default class ClickOutsideListener extends React.Component<ClickOutsideListenerProps> { | ||
static displayName = 'ClickOutsideListener'; | ||
|
||
static defaultProps = { | ||
mouseEvent: 'click', | ||
touchEvent: 'touchend' | ||
}; | ||
|
||
private nodeRef: HTMLElement | null; | ||
|
||
handleEvent = (event: MouseEvent | TouchEvent) => { | ||
const { nodeRef, props } = this; | ||
const { onClickOutside, target } = props; | ||
function ClickOutsideListener( | ||
{ | ||
children, | ||
mouseEvent = 'mouseup', | ||
touchEvent = 'touchend', | ||
target, | ||
onClickOutside = () => null | ||
}: ClickOutsideListenerProps, | ||
ref: React.ForwardedRef<HTMLElement> | ||
): JSX.Element | null { | ||
const childElementRef = useRef<HTMLElement>(); | ||
|
||
const handleEvent = (event: MouseEvent | TouchEvent) => { | ||
if (event.defaultPrevented) { | ||
return; | ||
} | ||
|
||
const eventTarget = event.target as HTMLElement; | ||
if ( | ||
(target && !target.contains(eventTarget)) || | ||
(nodeRef && !nodeRef.contains(eventTarget)) | ||
(!!target && !target.contains(eventTarget)) || | ||
(childElementRef.current && | ||
!childElementRef.current.contains(eventTarget)) | ||
) { | ||
onClickOutside(event); | ||
} | ||
}; | ||
|
||
componentDidMount() { | ||
this.attachEventListeners(); | ||
} | ||
|
||
componentDidUpdate(prevProps: ClickOutsideListenerProps) { | ||
const { mouseEvent, touchEvent } = this.props; | ||
if ( | ||
prevProps.mouseEvent !== mouseEvent || | ||
prevProps.touchEvent !== touchEvent | ||
) { | ||
this.removeEventListeners(prevProps.mouseEvent, prevProps.touchEvent); | ||
this.attachEventListeners(); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
const { mouseEvent, touchEvent } = this.props; | ||
this.removeEventListeners(mouseEvent, touchEvent); | ||
} | ||
|
||
private attachEventListeners = () => { | ||
const { mouseEvent, touchEvent } = this.props; | ||
typeof mouseEvent === 'string' && | ||
document.addEventListener(mouseEvent, this.handleEvent); | ||
typeof touchEvent === 'string' && | ||
document.addEventListener(touchEvent, this.handleEvent); | ||
const resolveRef = (node: HTMLElement) => { | ||
childElementRef.current = node; | ||
// Ref for this component should pass-through to the child node | ||
setRef(ref, node); | ||
// If child has its own ref, we want to update | ||
// its ref with the newly cloned node | ||
const { ref: childRef } = children as any; | ||
setRef(childRef, node); | ||
}; | ||
|
||
private removeEventListeners = ( | ||
mouseEvent: ClickOutsideListenerProps['mouseEvent'], | ||
touchEvent: ClickOutsideListenerProps['touchEvent'] | ||
) => { | ||
useEffect(() => { | ||
typeof mouseEvent === 'string' && | ||
document.removeEventListener(mouseEvent, this.handleEvent); | ||
document.addEventListener(mouseEvent, handleEvent); | ||
typeof touchEvent === 'string' && | ||
document.removeEventListener(touchEvent, this.handleEvent); | ||
}; | ||
document.addEventListener(touchEvent, handleEvent); | ||
|
||
resolveRef = (node: HTMLElement) => { | ||
this.nodeRef = node; | ||
return () => { | ||
typeof mouseEvent === 'string' && | ||
document.removeEventListener(mouseEvent, handleEvent); | ||
typeof touchEvent === 'string' && | ||
document.removeEventListener(touchEvent, handleEvent); | ||
}; | ||
}, [mouseEvent, touchEvent]); | ||
|
||
setRef; | ||
// If child has its own ref, we want to update | ||
// its ref with the newly cloned node | ||
const { ref } = this.props.children as any; | ||
setRef(ref, node); | ||
}; | ||
|
||
render() { | ||
const { props, resolveRef } = this; | ||
return !props.children | ||
? null | ||
: React.cloneElement(props.children as any, { | ||
ref: resolveRef | ||
}); | ||
} | ||
return !children | ||
? null | ||
: React.cloneElement(children as React.ReactElement, { ref: resolveRef }); | ||
} | ||
|
||
ClickOutsideListener.displayName = 'ClickOutsideListener'; | ||
|
||
export default React.forwardRef(ClickOutsideListener); |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes here I suspect are related to testing-library/user-event#592, but we're not using
user-event
but are callingfireEvent
due to specific limitations with testing library.