1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and 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.  * @flow
    
  7.  */
    
  8. 
    
  9. import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
    
  10. import type {Props} from '../client/ReactFiberConfigDOM';
    
  11. 
    
  12. import {getFiberCurrentPropsFromNode} from '../client/ReactDOMComponentTree';
    
  13. 
    
  14. function isInteractive(tag: string): boolean {
    
  15.   return (
    
  16.     tag === 'button' ||
    
  17.     tag === 'input' ||
    
  18.     tag === 'select' ||
    
  19.     tag === 'textarea'
    
  20.   );
    
  21. }
    
  22. 
    
  23. function shouldPreventMouseEvent(
    
  24.   name: string,
    
  25.   type: string,
    
  26.   props: Props,
    
  27. ): boolean {
    
  28.   switch (name) {
    
  29.     case 'onClick':
    
  30.     case 'onClickCapture':
    
  31.     case 'onDoubleClick':
    
  32.     case 'onDoubleClickCapture':
    
  33.     case 'onMouseDown':
    
  34.     case 'onMouseDownCapture':
    
  35.     case 'onMouseMove':
    
  36.     case 'onMouseMoveCapture':
    
  37.     case 'onMouseUp':
    
  38.     case 'onMouseUpCapture':
    
  39.     case 'onMouseEnter':
    
  40.       return !!(props.disabled && isInteractive(type));
    
  41.     default:
    
  42.       return false;
    
  43.   }
    
  44. }
    
  45. 
    
  46. /**
    
  47.  * @param {object} inst The instance, which is the source of events.
    
  48.  * @param {string} registrationName Name of listener (e.g. `onClick`).
    
  49.  * @return {?function} The stored callback.
    
  50.  */
    
  51. export default function getListener(
    
  52.   inst: Fiber,
    
  53.   registrationName: string,
    
  54. ): Function | null {
    
  55.   const stateNode = inst.stateNode;
    
  56.   if (stateNode === null) {
    
  57.     // Work in progress (ex: onload events in incremental mode).
    
  58.     return null;
    
  59.   }
    
  60.   const props = getFiberCurrentPropsFromNode(stateNode);
    
  61.   if (props === null) {
    
  62.     // Work in progress.
    
  63.     return null;
    
  64.   }
    
  65.   const listener = props[registrationName];
    
  66.   if (shouldPreventMouseEvent(registrationName, inst.type, props)) {
    
  67.     return null;
    
  68.   }
    
  69. 
    
  70.   if (listener && typeof listener !== 'function') {
    
  71.     throw new Error(
    
  72.       `Expected \`${registrationName}\` listener to be a function, instead got a value of \`${typeof listener}\` type.`,
    
  73.     );
    
  74.   }
    
  75. 
    
  76.   return listener;
    
  77. }