All files / components/modal modal.jsx

76% Statements 19/25
70.18% Branches 40/57
69.23% Functions 9/13
82.61% Lines 19/23
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216            4x 4x     4x 4x       1x 1x   1x                   1x 1x     1x         9x         1x 1x                                   3x 3x                       9x                                                                                                             1x                                                                 1x                                                               1x                                          
import React from 'react';
import LoadStatus from '../utilities/load-status.jsx';
import PropTypes from 'prop-types';
 
class Modal extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      open: false
    };
    this.clickHandler = this.clickHandler.bind(this);
    this.closeModal = this.closeModal.bind(this);
  }
 
  clickHandler(e) {
    Eif (e.currentTarget.className.indexOf('btn-modal-cta') >= 0 && this.props.action) {
      this.props.action(() => { this.closeModal(true); });
    }
    Iif (e.currentTarget.className.indexOf('btn-modal-cta') < 0) {
      this.props.customButtons.forEach(button => {
        if (e.currentTarget.className.indexOf(button.name) >= 0 && button.action) {
          button.action(() => { this.closeModal(true); });
        }
      });
    }
  }
 
  closeModal(outsideJob = false) {
    this.setState({ open: false }, () => {
      Iif (outsideJob === true) {
        document.getElementById(`${this.props.id}_close-btn`).click();
      }
      this.props.closeAction();
    });
  }
 
  render() {
    const launcher = this.props.launcherIsLink ? (
      <a
        id={this.props.launcherId}
        className={`${this.props.launcherTheme}${this.props.hideLauncher ? ' hidden' : ''}`}
        onClick={() => {
          this.setState({ open: true });
          this.props.launchAction();
        }}
        data-toggle="modal"
        data-target={`#${this.props.id}`}
      >
        <i className={`fa ${this.props.icon}`} />
        <span>
          {this.props.icon && this.props.launcherText ? ' ' : ''}
          {this.props.launcherText}
        </span>
      </a>
    ) : (
      <button
        id={this.props.launcherId}
        type="button"
        className={`btn button-modal-launcher ${this.props.launcherTheme}${this.props.hideLauncher ? ' hidden' : ''}`}
        style={{ margin: '0 0.5em 0 0' }}
        onClick={() => {
          this.setState({ open: true });
          this.props.launchAction();
        }}
        data-toggle="modal"
        data-target={`#${this.props.id}`}
      >
        <i className={`fa ${this.props.icon}`} />
        <span>
          {this.props.icon && this.props.launcherText ? ' ' : ''}
          {this.props.launcherText}
        </span>
      </button>
    );
    return (
      <div className={this.props.inline ? 'd-inline' : null}>
        {launcher}
        <div id={this.props.id} className={`modal ${this.props.theme}${this.props.fades ? ' fade' : ''}`} role="dialog">
          <div className="modal-dialog">
            <div className="modal-content row">
              <div className="modal-header col-xs-12">
                <button id={`${this.props.id}_close-btn`} type="button" className="close" data-dismiss="modal" onClick={this.closeModal}>&times;</button>
                <h4 className="modal-title">
                  {this.props.modalHeadline}
                  <br />
                  <small>{this.props.modalSubheadline}</small>
                </h4>
              </div>
              <div className="modal-body col-xs-12">
                {this.state.open ? (
                  !this.props.loading ? (
                    <div>
                      {this.props.modalContent}
                      {this.props.children}
                    </div>
                  ) : (
                    <div
                      id={`${this.props.id}-load-status-container`}
                      className="text-center align-middle"
                      style={{
                        backgroundColor: 'rgba(150, 150, 150, 0.05)',
                        position: 'absolute',
                        borderRadius: '5px',
                        top: 0, right: '10px', bottom: 0, left: '10px',
                        display: 'flex',
                        alignItems: 'center',
                        justifyContent: 'center'
                      }}
                    >
                      <LoadStatus size="3em" color="#848484" spins />
                    </div>
                  )
                ) : ''}
              </div>
              <div className="modal-footer col-xs-12">
                { this.state.open && !this.props.loading ? (
                  <div>
                    {this.props.modalFooter}
                    {this.props.hideCloseButton ? '' : (
                      <button
                        className={`btn-modal-cancel btn ${this.props.closeButtonTheme}`}
                        data-dismiss="modal"
                        onClick={this.closeModal}
                        style={{ margin: '0 0.5em 0 0' }}
                      >
                        Cancel
                      </button>
                    )}
                    {this.props.customButtons.length > 0 ? this.props.customButtons.map((btn, b) => {
                      return (
                        <button
                          key={b}
                          type="button"
                          className={`btn ${btn.name || 'undefined-btn-modal-action'} ${btn.theme || 'btn-default'}`}
                          style={{ margin: '0 0.5em 0 0' }}
                          onClick={this.clickHandler}
                        >
                          {btn.icon ? (<i className={`fa ${btn.icon}`} />) : ''}
                          {btn.icon && btn.text ? ' ' : ''}
                          {btn.text || ''}
                        </button>
                      );
                    }) : ''}
                    {this.props.hideCTAButton ? '' : (
                      <button
                        className={`btn-modal-cta btn ${this.props.ctaButtonTheme}`}
                        style={{ margin: '0 0.5em 0 0' }}
                        onClick={this.clickHandler}
                      >
                        {this.props.ctaButtonText}
                      </button>
                    )}
                  </div>
                ) : '' }
              </div>
            </div>
          </div>
        </div>
      </div>
    );
  }
}
Modal.propTypes = {
  theme: PropTypes.string,
  fades: PropTypes.bool,
  inline: PropTypes.bool,
  hideLauncher: PropTypes.bool,
  loading: PropTypes.bool,
  launcherId: PropTypes.string,
  id: PropTypes.string.isRequired,
  launcherTheme: PropTypes.string,
  hideCloseButton: PropTypes.bool,
  closeButtonTheme: PropTypes.string,
  hideCTAButton: PropTypes.bool,
  ctaButtonTheme: PropTypes.string,
  icon: PropTypes.string,
  launcherText: PropTypes.string,
  launcherIsLink: PropTypes.bool,
  action: PropTypes.func,
  modalHeadline: PropTypes.string,
  modalSubheadline: PropTypes.string,
  ctaButtonText: PropTypes.string,
  launchAction: PropTypes.func,
  closeAction: PropTypes.func,
  customButtons: PropTypes.array,
  modalContent: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.element
  ]),
  modalFooter: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.element
  ])
};
Modal.defaultProps = {
  theme: 'modal-default',
  fades: false,
  inline: false,
  hideLauncher: false,
  loading: false,
  hideCTAButton: false,
  hideCloseButton: false,
  closeButtonTheme: 'btn-default',
  ctaButtonTheme: 'btn-default',
  launcherTheme: 'btn-default',
  launcherIsLink: false,
  launcherText: 'Launch',
  icon: '',
  ctaButtonText: 'Ok',
  customButtons: [],
  launchAction: () => {},
  closeAction: () => {}
};
 
export default Modal;