TVTouchable.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. 'use strict';
  11. import invariant from 'invariant';
  12. import ReactNative from '../../Renderer/shims/ReactNative';
  13. import type {
  14. BlurEvent,
  15. FocusEvent,
  16. PressEvent,
  17. } from '../../Types/CoreEventTypes';
  18. import Platform from '../../Utilities/Platform';
  19. import TVEventHandler from '../../Components/AppleTV/TVEventHandler';
  20. type TVTouchableConfig = $ReadOnly<{|
  21. getDisabled: () => boolean,
  22. onBlur: (event: BlurEvent) => mixed,
  23. onFocus: (event: FocusEvent) => mixed,
  24. onPress: (event: PressEvent) => mixed,
  25. |}>;
  26. export default class TVTouchable {
  27. _tvEventHandler: TVEventHandler;
  28. constructor(component: any, config: TVTouchableConfig) {
  29. invariant(Platform.isTV, 'TVTouchable: Requires `Platform.isTV`.');
  30. this._tvEventHandler = new TVEventHandler();
  31. this._tvEventHandler.enable(component, (_, tvData) => {
  32. tvData.dispatchConfig = {};
  33. if (ReactNative.findNodeHandle(component) === tvData.tag) {
  34. if (tvData.eventType === 'focus') {
  35. config.onFocus(tvData);
  36. } else if (tvData.eventType === 'blur') {
  37. config.onBlur(tvData);
  38. } else if (tvData.eventType === 'select') {
  39. if (!config.getDisabled()) {
  40. config.onPress(tvData);
  41. }
  42. }
  43. }
  44. });
  45. }
  46. destroy(): void {
  47. this._tvEventHandler.disable();
  48. }
  49. }