|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +import invariant from 'invariant'; |
| 14 | +import ReactNative from '../../Renderer/shims/ReactNative.js'; |
| 15 | +import type { |
| 16 | + BlurEvent, |
| 17 | + FocusEvent, |
| 18 | + PressEvent, |
| 19 | +} from '../../Types/CoreEventTypes'; |
| 20 | +import {Platform, TVEventHandler} from 'react-native'; |
| 21 | + |
| 22 | +type TVTouchableConfig = $ReadOnly<{| |
| 23 | + getDisabled: () => boolean, |
| 24 | + onBlur: (event: BlurEvent) => mixed, |
| 25 | + onFocus: (event: FocusEvent) => mixed, |
| 26 | + onPress: (event: PressEvent) => mixed, |
| 27 | +|}>; |
| 28 | + |
| 29 | +export default class TVTouchable { |
| 30 | + _tvEventHandler: TVEventHandler; |
| 31 | + |
| 32 | + constructor(component: any, config: TVTouchableConfig) { |
| 33 | + invariant(Platform.isTV, 'TVTouchable: Requires `Platform.isTV`.'); |
| 34 | + this._tvEventHandler = new TVEventHandler(); |
| 35 | + this._tvEventHandler.enable(component, (_, tvData) => { |
| 36 | + tvData.dispatchConfig = {}; |
| 37 | + if (ReactNative.findNodeHandle(component) === tvData.tag) { |
| 38 | + if (tvData.eventType === 'focus') { |
| 39 | + config.onFocus(tvData); |
| 40 | + } else if (tvData.eventType === 'blur') { |
| 41 | + config.onBlur(tvData); |
| 42 | + } else if (tvData.eventType === 'select') { |
| 43 | + if (!config.getDisabled()) { |
| 44 | + config.onPress(tvData); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + destroy(): void { |
| 52 | + this._tvEventHandler.disable(); |
| 53 | + } |
| 54 | +} |
0 commit comments