All files / src/lookup/header/ViewDropDown ViewDropDown.js

57.89% Statements 11/19
7.4% Branches 2/27
33.33% Functions 2/6
57.89% Lines 11/19

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                                                    1x 1x                                                                                   1x 1x   1x 1x                                                                                                                                                                                           2x   2x   2x 2x 2x                    
/**** Libraries ****/
import React from 'react';
import { defaultProps } from './props/defaultProps';
import { propTypes } from './props/propTypes';
 
/**** Components ****/
import { SelectComponent } from '@zohodesk/components/lib/Select/Select';
import Popup from '@zohodesk/components/lib/Popup/Popup';
import ListItem from '@zohodesk/components/lib/ListItem/ListItem';
import DropDownSearch from '@zohodesk/components/lib/DropDown/DropDownSearch';
import { Card, CardHeader, CardContent } from '@zohodesk/components/lib/Card';
 
import ResponsiveDropBox from '@zohodesk/components/lib/ResponsiveDropBox/ResponsiveDropBox';
import { ResponsiveReceiver } from '@zohodesk/components/lib/Responsive/CustomResponsive';
 
/**** Icons ****/
import { Icon } from '@zohodesk/icons';
import { Container, Box } from '@zohodesk/components/lib/Layout';
import Loader from '@zohodesk/svg/lib/Loader/Loader';
import style from './ViewDropDown.module.css';
 
/* eslint-disable react/forbid-component-props */
/*eslint-disable  react/sort-prop-types*/
 
class DropDown extends SelectComponent {
  constructor(props) {
    super(props);
    this.onSearchKeyDown = this.onSearchKeyDown.bind(this);
  }
 
  onSearchKeyDown(e) {
    const { closePopupOnly } = this.props;
 
    if (e.keyCode === 27) {
      closePopupOnly && closePopupOnly(e);
    } else {
      this.handleKeyDown(e);
    }
  }
 
  responsiveFunc({ mediaQueryOR }) {
    return {
      tabletMode: mediaQueryOR([{ maxWidth: 700 }])
    };
  }
 
  render() {
    const {
      needSearch,
      dropBoxSize,
      emptyMessage,
      searchEmptyMessage,
      position,
      defaultDropBoxPosition,
      getTargetRef,
      getContainerRef,
      isPopupOpen,
      removeClose,
      isPopupReady,
      animationStyle,
      searchBoxSize,
      searchBoxVariant,
      searchBoxPlaceHolder,
      maxLength,
      dataId,
      isAbsolutePositioningNeeded,
      isRestrictScroll,
      positionsOffset,
      targetOffset
    } = this.props;
    const { hoverIndex, selected, searchStr, isFetchingOptions, selectedId } = this.state;
 
    const suggestions = this.handleFilterSuggestions();
    return (
      <div className={style.container} data-id={dataId} data-test-id={dataId}>
        <div onClick={this.togglePopup} ref={getTargetRef}>
          <div className={`${style.labelCnt} ${isPopupOpen ? style.focused : ''}`}>
            <span className={style.labelText} data-title={selected} data-id={`${dataId}_selectedValue`} data-test-id={`${dataId}_selectedValue`}>
              {selected}
            </span>
            <Icon name='ZD-down' size='7' iconClass={style.arrowIcon} dataId={`${dataId}_icon`} />
          </div>
        </div>
        {isPopupOpen ? (
          <ResponsiveReceiver query={this.responsiveFunc} responsiveId='Helmet'>
            {({ tabletMode }) => (
              <ResponsiveDropBox
                isAnimate
                isArrow={false}
                isActive={isPopupReady}
                boxPosition={position || `${defaultDropBoxPosition}Center`}
                getRef={getContainerRef}
                onClick={removeClose}
                animationStyle={animationStyle}
                size='large'
                alignBox='row'
                isResponsivePadding
                dataId={`${dataId}_dropbox`}
                isAbsolutePositioningNeeded={isAbsolutePositioningNeeded}
                isRestrictScroll={isRestrictScroll}
                positionsOffset={positionsOffset}
                targetOffset={targetOffset}
              >
                <Box flexible>
                  <Card>
                    {needSearch ? (
                      <CardHeader>
                        <DropDownSearch
                          value={searchStr}
                          onChange={this.handleSearch}
                          onKeyDown={this.onSearchKeyDown}
                          getRef={this.searchInputRef}
                          size={searchBoxSize}
                          variant={searchBoxVariant}
                          placeHolder={searchBoxPlaceHolder}
                          maxLength={maxLength}
                          dataId={`${dataId}_search`}
                        />
                      </CardHeader>
                    ) : null}
                    <CardContent
                      shrink
                      eleRef={this.suggestionContainerRef}
                      customClass={!tabletMode && dropBoxSize ? style[dropBoxSize] : ''}
                    >
                      {suggestions.length ? (
                        <div className={style.listItemContainer} data-id={`${dataId}_Options`}>
                          {suggestions.map((option = {}, index) => {
                            const { id, value } = option;
                            return (
                              <ListItem
                                active={id === selectedId}
                                getRef={this.suggestionItemRef}
                                highlight={hoverIndex === index ? true : false}
                                id={id}
                                key={`${id}dropDown`}
                                onClick={this.handleChange}
                                onMouseEnter={this.handleMouseEnter}
                                size='medium'
                                value={value}
                                index={index}
                                needTick
                                needBorder={false}
                              />
                            );
                          })}
                        </div>
                      ) : isFetchingOptions ? (
                        <Container align='both' className={style.loader}>
                          <Loader />
                        </Container>
                      ) : searchStr ? (
                        <div className={style.emptyState}>{searchEmptyMessage || emptyMessage}</div>
                      ) : (
                        <div className={style.emptyState}>{emptyMessage}</div>
                      )}
                    </CardContent>
                  </Card>
                </Box>
              </ResponsiveDropBox>
            )}
          </ResponsiveReceiver>
        ) : null}
      </div>
    );
  }
}
DropDown.propTypes = propTypes;
 
DropDown.defaultProps = defaultProps;
 
const ViewDropDown = Popup(DropDown);
ViewDropDown.defaultProps = DropDown.defaultProps;
ViewDropDown.propTypes = DropDown.propTypes;
 
// if (__DOCS__) {
//   DropDown.docs = {
//     componentGroup: 'Lookup'
//   };
//   // eslint-disable-next-line react/forbid-foreign-prop-types
//   ViewDropDown.propTypes = DropDown.propTypes;
// }
export default ViewDropDown;