All files / src/components/Popover Popover.react.js

63.64% Statements 7/11
33.33% Branches 2/6
50% Functions 2/4
63.64% Lines 7/11

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                                                                                                                          1x                                     2x 2x                                         2x   2x                                                           2x                                                               1x  
// @flow
 
import React, { Component } from 'react';
import { Popover as MuiPopover } from 'material-ui/Popover';
 
import FlatButton from 'material-ui/FlatButton';
import RaisedButton from 'material-ui/RaisedButton';
 
import lightBaseTheme from 'material-ui/styles/baseThemes/lightBaseTheme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
 
type Props = {
  /** This is the point on the anchor where the popover's targetOrigin will attach to.
   * Options: vertical: [top, center, bottom] horizontal: [left, middle, right].
   */
  anchorOrigin?: {
    vertical?: 'top' | 'center' | 'bottom',
    horizontal?: 'left' | 'middle' | 'right',
  },
  /** If true, the popover will apply transitions when it is added to the DOM. */
  animated?: boolean,
  /** If true, the popover will hide when the anchor is scrolled off the screen */
  autoCloseWhenOffScreen?: boolean,
  /** For Dash use - user can assign label to button */
  buttonLabel?: string,
  /** For Dash use - user can anchor the popover to flat or raised button */
  buttonType?: 'flat' | 'raised',
  /** If true, the popover (potentially) ignores targetOrigin and anchorOrigin to make itself fit
   * on screen, which is useful for mobile devices. */
  canAutoPosition?: boolean,
  /** The content of the popover */
  children?: Node,
  /** The CSS class name of the root element */
  className?: string,
  /** If true, the popover is visible. */
  open?: boolean,
  /** Represents the parent scrollable container. It can be an element or a string like window. */
  scrollableContainer?: Object | string,
  /** Override the inline-styles of the root element. */
  style?: Object,
  /** This is the point on the popover which will attach to the anchor's origin.
   * Options: vertical: [top, center, bottom] horizontal: [left, middle, right].
   */
  targetOrigin?: {
    vertical?: 'top' | 'center' | 'bottom',
    horizontal?: 'left' | 'middle' | 'right',
  },
  /** If true, the popover will render on top of an invisible layer, which will prevent clicks
   * to the underlying elements, and trigger an onRequestClose('clickAway') call.
   */
  useLayerForClickAway?: boolean,
  /** The zDepth of the popover. */
  zDepth?: propTypes.zDepth,
};
 
type State = {
  open: boolean,
  anchorEl: Object,
};
 
const defaultProps = {
  anchorOrigin: {vertical: 'bottom', horizontal: 'left'},
  animated: true,
  autoCloseWhenOffScreen: true,
  buttonLabel: '',
  buttonType: 'raised',
  canAutoPosition: true,
  children: null,
  className: '',
  open: false,
  scrollableContainer: 'window',
  style: {},
  targetOrigin: {vertical: 'top', horizontal: 'left'},
  useLayerForClickAway: true,
  zDepth: 1,
};
 
export default class Popover extends Component<Props, State> {
  constructor(props) {
    super(props);
    this.state = {open: props.open};
  }
 
  handleClick = (event) => {
    // This prevents ghost click.
    event.preventDefault();
 
    this.setState({
      open: true,
      anchorEl: event.currentTarget,
    });
  };
 
  handleRequestClose = () => {
    this.setState({
      open: false,
    });
  };
 
  render() {
    const { anchorOrigin, animated, autoCloseWhenOffScreen, canAutoPosition, className,
      scrollableContainer, style, targetOrigin, useLayerForClickAway, zDepth} = this.props;
 
    Iif (this.props.buttonType === 'flat') {
      return (
        <div>
          <MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
            <div>
              <FlatButton
                onClick={this.handleClick}
                label={this.props.buttonLabel !== '' ? this.props.buttonLabel : "Click Me!"}
              />);
              <MuiPopover
                anchorEl={this.state.anchorEl}
                anchorOrigin={anchorOrigin}
                animated={animated}
                autoCloseWhenOffScreen={autoCloseWhenOffScreen}
                canAutoPosition={canAutoPosition}
                className={className}
                onRequestClose={this.handleRequestClose}
                open={this.state.open}
                scrollableContainer={scrollableContainer}
                style={style}
                targetOrigin={targetOrigin}
                useLayerForClickAway={useLayerForClickAway}
                zDepth={zDepth}
              >
                {this.props.children}
              </MuiPopover>
            </div>
          </MuiThemeProvider>
        </div>);
    }
    return (
      <div>
        <MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
          <div>
            <RaisedButton
              onClick={this.handleClick}
              label={this.props.buttonLabel !== '' ? this.props.buttonLabel : "Click Me!"}
            />
            <MuiPopover
              anchorEl={this.state.anchorEl}
              anchorOrigin={anchorOrigin}
              animated={animated}
              autoCloseWhenOffScreen={autoCloseWhenOffScreen}
              canAutoPosition={canAutoPosition}
              className={className}
              onRequestClose={this.handleRequestClose}
              open={this.state.open}
              scrollableContainer={scrollableContainer}
              style={style}
              targetOrigin={targetOrigin}
              useLayerForClickAway={useLayerForClickAway}
              zDepth={zDepth}
            >
              {this.props.children}
            </MuiPopover>
          </div>
        </MuiThemeProvider>
      </div>
    );
  }
}
 
Popover.defaultProps = defaultProps;