All files / src/version2/lookup/AlertLookup AlertLookup.js

100% Statements 16/16
53.19% Branches 25/47
100% Functions 2/2
100% Lines 16/16

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 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232                                  1x                                           2x 2x                                                                                                                                 2x 2x 2x 2x   2x 2x 2x 2x 2x   2x         2x                                                                                                                                                                                                       1x   1x                
/**** Libraries ****/
import React, { Component } from 'react';
import { defaultProps } from './props/defaultProps';
import { propTypes } from './props/propTypes';
 
/**** Components ****/
import Lookup from '../../../lookup/Lookup/Lookup';
import AlertHeader from '../AlertHeader/AlertHeader';
import Button from '@zohodesk/components/lib/Button/Button';
import Section from '../../../lookup/Section/Section';
import FormAction from '../../../FormAction/FormAction';
import { Container, Box } from '@zohodesk/components/lib/Layout';
import { getUniqueId } from '@zohodesk/components/lib/Provider/IdProvider';
 
/**** CSS ****/
import style from './AlertLookupNew.module.css';
 
let buttonPaletteObject = {
  success: {
    buttonPalette: 'successFilled'
  },
  error: {
    buttonPalette: 'dangerFilled'
  },
  danger: {
    buttonPalette: 'dangerFilled'
  },
  warning: {
    buttonPalette: 'dangerFilled'
  },
  info: {
    buttonPalette: 'primaryFilled'
  },
  notification: {
    buttonPalette: 'primaryFilled'
  }
};
export default class AlertLookup extends Component {
  constructor(props) {
    super(props);
    this.getAriaId = getUniqueId(this);
    // this.documentKeydownHandler = this.documentKeydownHandler.bind(this);
  }
 
  // componentDidMount() {
  //   let { isActive } = this.props;
  //   if (isActive) {
  //     document.addEventListener('keydown', this.documentKeydownHandler)
  //   }
  // }
 
  // componentDidUpdate(prevProps) {
  //   let { isActive } = this.props;
  //   if (prevProps.isActive !== isActive) {
  //     isActive
  //       ? document.addEventListener('keydown', this.documentKeydownHandler)
  //       : document.removeEventListener('keydown', this.documentKeydownHandler);
  //   }
  // }
  // componentWillUnmount() {
  //   document.removeEventListener('keydown', this.documentKeydownHandler);
  // }
 
  // documentKeydownHandler(e = {}) {
  //   let { onSubmitClick, onCancelClick, isActive, onEsc, onClose } = this.props;
  //   if (e.keyCode === 27 && isActive) {
  //     e && e.preventDefault && e.preventDefault();
  //     onEsc == 'close'
  //       ? onClose && onClose(e)
  //       : onCancelClick && onCancelClick(e);
  //   } else if (e.keyCode === 13 && isActive) {
  //     e && e.preventDefault && e.preventDefault();
  //     onSubmitClick && onSubmitClick(e);
  //   }
  // }
 
  render() {
    let {
      isActive,
      size,
      type = 'danger',
      title,
      message,
      confirmationMessage,
      submitText,
      cancelText,
      onSubmitClick,
      onCancelClick,
      children,
      forwardRef,
      onClose,
      headerChildren,
      wrapHeader,
      dataId,
      onLookupClick,
      customClass = {},
      needIcon,
      a11y,
      customProps,
      align,
      childAnimationName,
      isHtmlContent,
      scroll,
      isDefaultFocus,
      onEsc
    } = this.props;
    const { LookupProps = {}, AlertHeaderProps = {}, PrimaryButtonProps = {}, SecondaryButtonProps = {} } = customProps;
    let { containerClass = '', sectionClass = '' } = customClass;
    let { buttonPalette = 'dangerFilled' } = buttonPaletteObject[type] || {};
 
    let ariaMessageId = this.getAriaId();
    let ariaConfirmMsgId = this.getAriaId();
    let ariaTitleId = this.getAriaId();
    let ariaLeaveButtonId = this.getAriaId();
    let ariaStayButtonId = this.getAriaId();
 
    a11y = {
      ariaLabelledby: a11y && a11y.ariaLabelledby ? a11y.ariaLabelledby : ariaTitleId,
      ariaDescribedby: a11y && a11y.ariaDescribedby ? a11y.ariaDescribedby : ariaMessageId + ' ' + ariaConfirmMsgId
    };
 
    return (
      <Lookup
        onClose={onEsc == 'close' ? onClose : onCancelClick}
        needFocusScope
        isActive={isActive}
        size={size}
        dataId={dataId}
        forwardRef={forwardRef}
        onClick={onLookupClick}
        customClass={containerClass}
        a11y={a11y}
        childAnimationName={childAnimationName}
        {...LookupProps}
      >
        <Container alignBox='row' align={align} wrap='wrap'>
          <Section alignBox='row' className={style.section}>
            <Container alignBox='column'>
              <Box>
                <AlertHeader
                  htmlId={ariaTitleId}
                  title={title}
                  type={type}
                  needIcon={needIcon}
                  onClose={onClose}
                  breakChildren={wrapHeader}
                  {...AlertHeaderProps}
                >
                  {headerChildren ? headerChildren() : null}
                </AlertHeader>
              </Box>
              <Box flexible>
                <div
                  className={`${style.innerSection} ${
                    style[`${scroll}Scroll`] ? style[`${scroll}Scroll`] : ''
                  } ${sectionClass}`}
                >
                  {children ? (
                    children
                  ) : (
                    <>
                      {isHtmlContent && message ? (
                        <div
                          id={ariaMessageId}
                          dangerouslySetInnerHTML={{
                            __html: message
                          }}
                        />
                      ) : (
                        <div id={ariaMessageId}>{message}</div>
                      )}
                      {confirmationMessage && (
                        <div id={ariaConfirmMsgId} className={`${message ? style.confirmMsgText : ''}`}>
                          {confirmationMessage}
                        </div>
                      )}
                    </>
                  )}
                </div>
              </Box>
              {submitText || onSubmitClick || cancelText || onCancelClick ? (
                <Box>
                  <div className={style.footer}>
                    <FormAction size='small' paddingLeftSize='xmedium'>
                      <Button
                        text={submitText}
                        dataId={dataId === 'alertPopup' ? 'alertConfirmButton' : `${dataId}_confirmButton`}
                        onClick={onSubmitClick}
                        palette={buttonPalette}
                        customProps={{ 'data-a11y-autofocus': isDefaultFocus }}
                        {...PrimaryButtonProps}
                        a11y={{
                          'aria-labelledby': ariaTitleId + ' ' + ariaMessageId + ' ' + ariaLeaveButtonId
                        }}
                        id={ariaLeaveButtonId}
                      />
                      {cancelText && (
                        <Button
                          dataId={dataId === 'alertPopup' ? 'alertCancelButton' : `${dataId}_cancelButton`}
                          text={cancelText}
                          onClick={onCancelClick}
                          palette='secondary'
                          {...SecondaryButtonProps}
                          a11y={{
                            'aria-labelledby': ariaTitleId + ' ' + ariaMessageId + ' ' + ariaStayButtonId
                          }}
                          id={ariaStayButtonId}
                        />
                      )}
                    </FormAction>
                  </div>
                </Box>
              ) : null}
            </Container>
          </Section>
        </Container>
      </Lookup>
    );
  }
}
 
AlertLookup.propTypes = propTypes;
 
AlertLookup.defaultProps = defaultProps;
 
// if (__DOCS__) {
//   AlertLookup.docs = {
//     componentGroup: 'AlertLookup',
//     folderName: 'Alert'
//   };
// }