|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + ScrollView, |
| 4 | + Slider, |
| 5 | + Switch, |
| 6 | + TextInput, |
| 7 | + ToolbarAndroid, |
| 8 | + ViewPagerAndroid, |
| 9 | + DrawerLayoutAndroid, |
| 10 | + WebView, |
| 11 | + FlatList, |
| 12 | + TouchableWithoutFeedback, |
| 13 | + findNodeHandle, |
| 14 | +} from 'react-native'; |
| 15 | +import gestureHandlerRootHOC from './gestureHandlerRootHOC'; |
| 16 | +import Directions from './Directions'; |
| 17 | +import State from './State'; |
| 18 | + |
| 19 | +// Factory for Handler components |
| 20 | +function createHandler(name, attachNativeHandler) { |
| 21 | + return class Handler extends React.Component { |
| 22 | + static displayName = name; |
| 23 | + |
| 24 | + container = React.createRef(); |
| 25 | + detachNativeHandler = null; |
| 26 | + |
| 27 | + componentDidMount() { |
| 28 | + // Get current DOM node |
| 29 | + const node = findNodeHandle(this.container.current); |
| 30 | + |
| 31 | + // Attach handler to DOM node |
| 32 | + const { enabled } = this.props; |
| 33 | + if (node && attachNativeHandler && enabled !== false) { |
| 34 | + this.detachNativeHandler = attachNativeHandler(node, this.props); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + componentDidUpdate() { |
| 39 | + // Get current DOM node |
| 40 | + const node = findNodeHandle(this.container.current); |
| 41 | + |
| 42 | + if (node && attachNativeHandler) { |
| 43 | + // Detach existing handlers |
| 44 | + if (this.detachNativeHandler) { |
| 45 | + this.detachNativeHandler(node); |
| 46 | + } |
| 47 | + |
| 48 | + // Attach handler to DOM node again |
| 49 | + const { enabled } = this.props; |
| 50 | + if (enabled !== false) { |
| 51 | + attachNativeHandler(node, this.props); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + setNativeProps() { |
| 57 | + // No implementation so far but is needed to avoid null calls |
| 58 | + } |
| 59 | + |
| 60 | + render() { |
| 61 | + const { children, ...rest } = this.props; |
| 62 | + |
| 63 | + // We don't want to create another layer, so instead we clone it only but keep the reference |
| 64 | + const child = React.Children.only(children); |
| 65 | + return React.cloneElement(child, { |
| 66 | + ref: this.container, |
| 67 | + ...rest, |
| 68 | + }); |
| 69 | + } |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +// Create all Handler components with their respective handler functions |
| 74 | +// (at the moment only TapGestureHandler is properly supported) |
| 75 | +const NativeViewGestureHandler = createHandler('NativeViewGestureHandler'); |
| 76 | +const TapGestureHandler = createHandler('TapGestureHandler', (node, props) => { |
| 77 | + const { onHandlerStateChange = () => {} } = props; |
| 78 | + const clickHandler = ({ x }) => |
| 79 | + onHandlerStateChange({ |
| 80 | + nativeEvent: { |
| 81 | + oldState: State.ACTIVE, |
| 82 | + state: State.UNDETERMINED, |
| 83 | + x, |
| 84 | + }, |
| 85 | + }); |
| 86 | + node.addEventListener('click', clickHandler); |
| 87 | + |
| 88 | + // Detach event listeners |
| 89 | + return () => { |
| 90 | + node.removeEventListener('click', clickHandler); |
| 91 | + }; |
| 92 | +}); |
| 93 | +const FlingGestureHandler = createHandler('FlingGestureHandler'); |
| 94 | +const ForceTouchGestureHandler = createHandler('ForceTouchGestureHandler'); |
| 95 | +const LongPressGestureHandler = createHandler('LongPressGestureHandler'); |
| 96 | +const PanGestureHandler = createHandler('PanGestureHandler'); |
| 97 | +const PinchGestureHandler = createHandler('PinchGestureHandler'); |
| 98 | +const RotationGestureHandler = createHandler('RotationGestureHandler'); |
| 99 | + |
| 100 | +// Factory for Button component |
| 101 | +// (at the moment this is a plain TouchableWithoutFeedback) |
| 102 | +function createButton(name) { |
| 103 | + return class Button extends React.Component { |
| 104 | + static displayName = name; |
| 105 | + |
| 106 | + render() { |
| 107 | + return <TouchableWithoutFeedback {...this.props} />; |
| 108 | + } |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +const RawButton = createButton('RawButton'); |
| 113 | +const BaseButton = createButton('BaseButton'); |
| 114 | +const RectButton = createButton('RectButton'); |
| 115 | +const BorderlessButton = createButton('BorderlessButton'); |
| 116 | + |
| 117 | +// Export same components as in GestureHandler.js |
| 118 | +export { |
| 119 | + ScrollView, |
| 120 | + Slider, |
| 121 | + Switch, |
| 122 | + TextInput, |
| 123 | + ToolbarAndroid, |
| 124 | + ViewPagerAndroid, |
| 125 | + DrawerLayoutAndroid, |
| 126 | + WebView, |
| 127 | + NativeViewGestureHandler, |
| 128 | + TapGestureHandler, |
| 129 | + FlingGestureHandler, |
| 130 | + ForceTouchGestureHandler, |
| 131 | + LongPressGestureHandler, |
| 132 | + PanGestureHandler, |
| 133 | + PinchGestureHandler, |
| 134 | + RotationGestureHandler, |
| 135 | + State, |
| 136 | + /* Buttons */ |
| 137 | + RawButton, |
| 138 | + BaseButton, |
| 139 | + RectButton, |
| 140 | + BorderlessButton, |
| 141 | + /* Other */ |
| 142 | + FlatList, |
| 143 | + gestureHandlerRootHOC, |
| 144 | + Directions, |
| 145 | +}; |
0 commit comments