This repository has been archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(EventListener): use hooks (#1755)
* chore(EventListener): use hooks * export hook * add changelog entry * Update CHANGELOG.md Co-Authored-By: Pavel Lučivňák <[email protected]> * fix format * fix UTs * add comment
- Loading branch information
1 parent
7e1443f
commit f9e53a4
Showing
18 changed files
with
108 additions
and
358 deletions.
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
55 changes: 22 additions & 33 deletions
55
packages/react-component-event-listener/src/EventListener.ts
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,40 +1,29 @@ | ||
import * as React from 'react' | ||
import * as PropTypes from 'prop-types' | ||
|
||
import addEventListener from './lib/addEventListener' | ||
import removeEventListener from './lib/removeEventListener' | ||
import shouldUpdateListener from './lib/shouldUpdateListener' | ||
import { EventListenerProps } from './types' | ||
import { listenerPropTypes } from './types.internal' | ||
import useEventListener from './useEventListener' | ||
import { EventListenerOptions, EventTypes, TargetRef } from './types' | ||
|
||
class EventListener extends React.Component<EventListenerProps> { | ||
static displayName = 'EventListener' | ||
static propTypes = listenerPropTypes | ||
static defaultProps = { | ||
capture: false, | ||
} | ||
function EventListener<T extends EventTypes>(props: EventListenerOptions<T>) { | ||
useEventListener(props) | ||
|
||
shouldComponentUpdate(nextProps: EventListenerProps) { | ||
return shouldUpdateListener(this.props, nextProps) | ||
} | ||
|
||
componentDidMount() { | ||
addEventListener(this.handleEvent, this.props as Required<EventListenerProps>) | ||
} | ||
|
||
componentDidUpdate(prevProps: EventListenerProps) { | ||
removeEventListener(this.handleEvent, prevProps as Required<EventListenerProps>) | ||
addEventListener(this.handleEvent, this.props as Required<EventListenerProps>) | ||
} | ||
|
||
componentWillUnmount() { | ||
removeEventListener(this.handleEvent, this.props as Required<EventListenerProps>) | ||
} | ||
|
||
handleEvent = (e: Event) => this.props.listener(e) | ||
return null | ||
} | ||
|
||
render() { | ||
return null | ||
} | ||
EventListener.displayName = 'EventListener' | ||
// TODO: use Babel plugin for this | ||
EventListener.propTypes = | ||
process.env.NODE_ENV !== 'production' | ||
? { | ||
capture: PropTypes.bool, | ||
listener: PropTypes.func.isRequired, | ||
targetRef: PropTypes.shape({ | ||
current: PropTypes.object, | ||
}).isRequired as PropTypes.Validator<TargetRef>, | ||
type: PropTypes.string.isRequired as PropTypes.Validator<EventTypes>, | ||
} | ||
: {} | ||
EventListener.defaultProps = { | ||
capture: false, | ||
} | ||
|
||
export default EventListener |
50 changes: 0 additions & 50 deletions
50
packages/react-component-event-listener/src/StackableEventListener.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
packages/react-component-event-listener/src/hooks/useEventListener.ts
This file was deleted.
Oops, something went wrong.
30 changes: 0 additions & 30 deletions
30
packages/react-component-event-listener/src/hooks/useStackableEventListener.ts
This file was deleted.
Oops, something went wrong.
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
21 changes: 0 additions & 21 deletions
21
packages/react-component-event-listener/src/lib/addEventListener.ts
This file was deleted.
Oops, something went wrong.
34 changes: 0 additions & 34 deletions
34
packages/react-component-event-listener/src/lib/listenerRegistries.ts
This file was deleted.
Oops, something went wrong.
24 changes: 0 additions & 24 deletions
24
packages/react-component-event-listener/src/lib/removeEventListener.ts
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
packages/react-component-event-listener/src/lib/shouldUpdateListener.ts
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
packages/react-component-event-listener/src/types.internal.ts
This file was deleted.
Oops, something went wrong.
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
41 changes: 41 additions & 0 deletions
41
packages/react-component-event-listener/src/useEventListener.ts
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 * as React from 'react' | ||
|
||
import { EventHandler, EventListenerOptions, EventTypes, TargetRef } from './types' | ||
|
||
const isActionSupported = ( | ||
targetRef: TargetRef, | ||
method: 'addEventListener' | 'removeEventListener', | ||
) => targetRef && !!targetRef.current && !!targetRef.current[method] | ||
|
||
const useEventListener = <T extends EventTypes>(options: EventListenerOptions<T>): void => { | ||
const { capture, listener, type, targetRef } = options | ||
|
||
const latestListener = React.useRef<EventHandler<T>>(listener) | ||
latestListener.current = listener | ||
|
||
const eventHandler = React.useCallback((event: DocumentEventMap[T]) => { | ||
return latestListener.current(event) | ||
}, []) | ||
|
||
React.useEffect(() => { | ||
if (isActionSupported(targetRef, 'addEventListener')) { | ||
;(targetRef.current as NonNullable<Node>).addEventListener(type, eventHandler, capture) | ||
} else if (process.env.NODE_ENV !== 'production') { | ||
throw new Error( | ||
'@stardust-ui/react-component-event-listener: Passed `targetRef` is not valid or does not support `addEventListener()` method.', | ||
) | ||
} | ||
|
||
return () => { | ||
if (isActionSupported(targetRef, 'removeEventListener')) { | ||
;(targetRef.current as NonNullable<Node>).removeEventListener(type, eventHandler, capture) | ||
} else if (process.env.NODE_ENV !== 'production') { | ||
throw new Error( | ||
'@stardust-ui/react-component-event-listener: Passed `targetRef` is not valid or does not support `removeEventListener()` method.', | ||
) | ||
} | ||
} | ||
}, [capture, targetRef, type]) | ||
} | ||
|
||
export default useEventListener |
Oops, something went wrong.