|
1 | | -import {Component} from 'react'; |
2 | | -import PropTypes from 'prop-types'; |
3 | | -import ReactDOM from 'react-dom'; |
| 1 | +// @flow |
| 2 | +import { Component } from 'react' |
| 3 | +import PropTypes from 'prop-types' |
| 4 | +import ReactDOM from 'react-dom' |
4 | 5 | const events = [ |
5 | | - 'KeyDown', |
6 | | - 'KeyPress', |
7 | | - 'KeyUp', |
8 | | - 'Click', |
9 | | - 'ContextMenu', |
10 | | - 'DoubleClick', |
11 | | - 'Drag', |
12 | | - 'DragEnd', |
13 | | - 'DragEnter', |
14 | | - 'DragExit', |
15 | | - 'DragLeave', |
16 | | - 'DragOver', |
17 | | - 'DragStart', |
18 | | - 'Drop', |
19 | | - 'Focus', |
20 | | - 'MouseDown', |
21 | | - 'MouseEnter', |
22 | | - 'MouseLeave', |
23 | | - 'MouseMove', |
24 | | - 'MouseOut', |
25 | | - 'MouseOver', |
26 | | - 'MouseUp' |
27 | | -]; |
| 6 | + 'KeyDown', |
| 7 | + 'KeyPress', |
| 8 | + 'KeyUp', |
| 9 | + 'Click', |
| 10 | + 'ContextMenu', |
| 11 | + 'DoubleClick', |
| 12 | + 'Drag', |
| 13 | + 'DragEnd', |
| 14 | + 'DragEnter', |
| 15 | + 'DragExit', |
| 16 | + 'DragLeave', |
| 17 | + 'DragOver', |
| 18 | + 'DragStart', |
| 19 | + 'Drop', |
| 20 | + 'Focus', |
| 21 | + 'MouseDown', |
| 22 | + 'MouseEnter', |
| 23 | + 'MouseLeave', |
| 24 | + 'MouseMove', |
| 25 | + 'MouseOut', |
| 26 | + 'MouseOver', |
| 27 | + 'MouseUp' |
| 28 | +] |
28 | 29 |
|
29 | 30 | const aliases = { |
30 | | - 'DoubleClick': 'dblclick' |
31 | | -}; |
| 31 | + DoubleClick: 'dblclick' |
| 32 | +} |
32 | 33 |
|
33 | | -const toEventName = event => |
34 | | - (aliases[event] || event).toLowerCase(); |
| 34 | +const toEventName = (event: string): string => |
| 35 | + (aliases[event] || event).toLowerCase() |
35 | 36 |
|
36 | 37 | export default class NativeListener extends Component { |
37 | | - static displayName = 'NativeListener'; |
38 | | - static propTypes = { |
39 | | - children: (props, propName) => { |
40 | | - if (props[propName].length) { |
41 | | - return new Error('NativeListener can only wrap one element'); |
42 | | - } |
43 | | - }, |
44 | | - ...events.reduce((accumulator, event) => ({ |
45 | | - ...accumulator, |
46 | | - [`on${event}`]: PropTypes.func, |
47 | | - [`on${event}Capture`]: PropTypes.func, |
48 | | - [`stop${event}`]: PropTypes.bool |
49 | | - }), {}) |
50 | | - }; |
| 38 | + static displayName = 'NativeListener' |
| 39 | + static propTypes = { |
| 40 | + children: (props, propName) => { |
| 41 | + if (props[propName].length) { |
| 42 | + return new Error('NativeListener can only wrap one element') |
| 43 | + } |
| 44 | + }, |
| 45 | + ...events.reduce( |
| 46 | + (accumulator, event) => ({ |
| 47 | + ...accumulator, |
| 48 | + [`on${event}`]: PropTypes.func, |
| 49 | + [`on${event}Capture`]: PropTypes.func, |
| 50 | + [`stop${event}`]: PropTypes.bool |
| 51 | + }), |
| 52 | + {} |
| 53 | + ) |
| 54 | + } |
51 | 55 |
|
52 | | - componentDidMount() { |
53 | | - const props = this.props; |
54 | | - const element = ReactDOM.findDOMNode(this); |
55 | | - events.forEach(event => { |
56 | | - const capture = props['on' + event + 'Capture']; |
57 | | - const bubble = props['on' + event]; |
58 | | - const stop = props['stop' + event]; |
59 | | - if (capture && typeof capture === 'function') { |
60 | | - element.addEventListener(toEventName(event), capture, true); |
61 | | - } |
62 | | - if (bubble && typeof bubble === 'function') { |
63 | | - element.addEventListener(toEventName(event), bubble, false); |
64 | | - } |
65 | | - if (stop === true) { |
66 | | - element.addEventListener(toEventName(event), nativeEvent => nativeEvent.stopPropagation(), false); |
67 | | - } |
68 | | - }); |
69 | | - } |
| 56 | + componentDidMount() { |
| 57 | + const props = this.props |
| 58 | + const element = ReactDOM.findDOMNode(this) |
| 59 | + if (element) { |
| 60 | + events.forEach(event => { |
| 61 | + const capture = props['on' + event + 'Capture'] |
| 62 | + const bubble = props['on' + event] |
| 63 | + const stop = props['stop' + event] |
| 64 | + if (capture && typeof capture === 'function') { |
| 65 | + element.addEventListener(toEventName(event), capture, true) |
| 66 | + } |
| 67 | + if (bubble && typeof bubble === 'function') { |
| 68 | + element.addEventListener(toEventName(event), bubble, false) |
| 69 | + } |
| 70 | + if (stop === true) { |
| 71 | + element.addEventListener( |
| 72 | + toEventName(event), |
| 73 | + nativeEvent => nativeEvent.stopPropagation(), |
| 74 | + false |
| 75 | + ) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | + } |
70 | 80 |
|
71 | | - render() { |
72 | | - return this.props.children; |
73 | | - } |
| 81 | + render() { |
| 82 | + return this.props.children |
| 83 | + } |
74 | 84 | } |
0 commit comments