All files / popover stateful-container.js

100% Statements 41/41
100% Branches 16/16
100% Functions 15/15
100% Lines 39/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178                                            28x                                                   1x       10x 2x   8x         1x       1x       1x       2x       2x       1x       11x     11x 1x         8x     8x 1x         19x 19x 1x 1x   18x                           7x 7x 1x   6x                               37x   37x                           37x 36x   37x 36x   37x 8x 8x 8x 8x   29x     37x          
/*
Copyright (c) 2018-2019 Uber Technologies, Inc.
 
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
import * as React from 'react';
import {
  ACCESSIBILITY_TYPE,
  PLACEMENT,
  STATE_CHANGE_TYPE,
  TRIGGER_TYPE,
} from './constants.js';
import type {
  PopoverPropsWithoutChildrenT,
  StateT,
  StatefulPopoverContainerPropsT,
  StateChangeTypeT,
  StateReducerT,
} from './types.js';
 
const defaultStateReducer: StateReducerT = (type, nextState) => nextState;
 
class StatefulContainer extends React.Component<
  StatefulPopoverContainerPropsT,
  StateT,
> {
  static defaultProps: $Shape<StatefulPopoverContainerPropsT> = {
    accessibilityType: ACCESSIBILITY_TYPE.menu,
    ignoreBoundary: false,
    overrides: {},
    onMouseEnterDelay: 200,
    onMouseLeaveDelay: 200,
    placement: PLACEMENT.auto,
    showArrow: false,
    triggerType: TRIGGER_TYPE.click,
    dismissOnClickOutside: true,
    dismissOnEsc: true,
    stateReducer: defaultStateReducer,
  };
 
  state = {
    isOpen: false,
    ...this.props.initialState,
  };
 
  onBlur = () => {
    this.close();
  };
 
  onClick = () => {
    if (this.state.isOpen) {
      this.close();
    } else {
      this.open();
    }
  };
 
  onClickOutside = () => {
    this.close();
  };
 
  onEsc = () => {
    this.close();
  };
 
  onFocus = () => {
    this.open();
  };
 
  onMouseEnter = () => {
    this.open();
  };
 
  onMouseLeave = () => {
    this.close();
  };
 
  onContentClose = () => {
    this.close();
  };
 
  open() {
    this.internalSetState(STATE_CHANGE_TYPE.open, {
      isOpen: true,
    });
    if (this.props.onOpen) {
      this.props.onOpen();
    }
  }
 
  close() {
    this.internalSetState(STATE_CHANGE_TYPE.close, {
      isOpen: false,
    });
    if (this.props.onClose) {
      this.props.onClose();
    }
  }
 
  internalSetState(type: StateChangeTypeT, changes: StateT) {
    const {stateReducer} = this.props;
    if (typeof stateReducer !== 'function') {
      this.setState(changes);
      return;
    }
    this.setState(prevState => stateReducer(type, changes, prevState));
  }
 
  /**
   * If user passed a content render prop, we want to give them a
   * callback to close the Popover. This is useful in any case where
   * a dev wants to manually close the popover based on some action.
   *
   * One simple example is a Popover with a "Dismiss" button in it:
   * <StatefulPopover content={({close}) => <button onClick={close}>Dismiss</button>}>
   *  Click me
   * </StatefulPopover>
   */
  renderContent = () => {
    const {content} = this.props;
    if (typeof content === 'function') {
      return content({close: this.onContentClose});
    }
    return content;
  };
 
  render() {
    const {
      accessibilityType,
      dismissOnClickOutside,
      dismissOnEsc,
      ignoreBoundary,
      overrides,
      onMouseEnterDelay,
      onMouseLeaveDelay,
      placement,
      showArrow,
      triggerType,
      mountNode,
    } = this.props;
 
    const popoverProps: PopoverPropsWithoutChildrenT = {
      accessibilityType,
      ignoreBoundary,
      isOpen: this.state.isOpen,
      overrides,
      content: this.renderContent,
      onMouseEnterDelay,
      onMouseLeaveDelay,
      placement,
      showArrow,
      triggerType,
      mountNode,
    };
 
    if (dismissOnClickOutside) {
      popoverProps.onClickOutside = this.onClickOutside;
    }
    if (dismissOnEsc) {
      popoverProps.onEsc = this.onEsc;
    }
    if (triggerType === TRIGGER_TYPE.hover) {
      popoverProps.onBlur = this.onBlur;
      popoverProps.onFocus = this.onFocus;
      popoverProps.onMouseEnter = this.onMouseEnter;
      popoverProps.onMouseLeave = this.onMouseLeave;
    } else {
      popoverProps.onClick = this.onClick;
    }
 
    return this.props.children(popoverProps);
  }
}
 
export default StatefulContainer;