All files / src/lookup/Lookup Lookup.js

64.7% Statements 11/17
63.63% Branches 14/22
40% Functions 2/5
64.7% Lines 11/17

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                                6x 6x 6x                                                                         6x               6x 6x             6x   6x                                               6x                                                                 3x 3x              
/*** Libraries ***/
import React, { Component } from 'react';
import { defaultProps } from './props/defaultProps';
import { propTypes } from './props/propTypes';
 
/*** Components ***/
import FreezeLayer from '../../FreezeLayer/FreezeLayer';
import { Container } from '@zohodesk/components/lib/Layout';
 
/*** CSS ***/
import style from './Lookup.module.css';
import FocusScope from '@zohodesk/a11y/es/FocusScope/FocusScope';
/*eslint-disable react/forbid-component-props */
 
export default class Lookup extends Component {
  constructor(props) {
    super(props);
    this.handleKeyDown = this.handleKeyDown.bind(this);
    this.createRef = React.createRef();
  }
 
  componentDidUpdate(prevProps) {
    const { isActive } = this.props;
    if (prevProps.isActive !== isActive) {
      isActive
        ? document.addEventListener('keydown', this.handleKeyDown)
        : document.removeEventListener('keydown', this.handleKeyDown);
    }
  }
  componentWillUnmount() {
    document.removeEventListener('keydown', this.handleKeyDown);
  }
 
  handleKeyDown(e) {
    const { onKeyDown } = this.props;
    onKeyDown && onKeyDown(e);
  }
 
  render() {
    const {
      isActive,
      children,
      size,
      dataId,
      forwardRef,
      onClick,
      customClass,
      htmlId,
      a11y,
      childAnimationName,
      onClose,
      needFocusScope,
      customProps,
      isMinHeight,
      lookupClass
    } = this.props;
    const {
      role = 'dialog',
      ariaLabelledby,
      ariaDescribedby,
      ariaLabel,
      ariaModal = isActive ? true : undefined,
      ...a11yAttributes
    } = a11y;
    const { focusScopeProps = {} } = customProps;
    const {
      needAutoFocus = true,
      needRestoreFocus = true,
      needListNavigation = false,
      needFocusLoop = true,
      needEnterAction = true
    } = focusScopeProps;
    const content = (
      <div
        ref={this.createRef}
        tabIndex='-1'
        data-a11y-need-focus-style='false'
        role={role}
        aria-labelledby={ariaLabelledby}
        aria-describedby={ariaDescribedby}
        aria-label={ariaLabel}
        aria-modal={ariaModal}
        id={htmlId}
        className={`${style.box} ${style[`${size}Size`]} ${lookupClass ? lookupClass : ''}`}
        data-id={dataId}
        data-test-id={dataId}
        {...a11yAttributes}
      >
        <Container
          data-drag-container='true'
          isCover={false}
          className={isMinHeight ? `${style.wrapper}` : `${style.coverwrap}`}
        >
          {children}
        </Container>
      </div>
    );
    return (
      <FreezeLayer
        align='horizontal'
        animationName='fade'
        childAnimationName={childAnimationName}
        isActive={isActive}
        palette='snow'
        forwardRef={forwardRef}
        onClick={onClick}
        customClass={customClass}
      >
        <Container alignBox='row' className={style.container} dataId='fldValue'>
          {needFocusScope ? (
            <FocusScope
              needFocusLoop={needFocusLoop}
              needListNavigation={needListNavigation}
              elementRef={this.createRef}
              needAutoFocus={needAutoFocus}
              needRestoreFocus={needRestoreFocus}
              onClose={onClose}
              needEnterAction={needEnterAction}
            >
              {content}
            </FocusScope>
          ) : (
            content
          )}
        </Container>
      </FreezeLayer>
    );
  }
}
 
Lookup.propTypes = propTypes;
Lookup.defaultProps = defaultProps;
 
// if (__DOCS__) {
//   Lookup.docs = {
//     componentGroup: 'Lookup'
//   };
// }