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

64.58% Statements 31/48
46.93% Branches 23/49
58.33% Functions 7/12
64.58% Lines 31/48

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                                              6x 6x 6x 6x 6x 6x           1x 1x     1x                       12x 12x 12x       1x 1x       7x 7x   7x                 7x                                   3x       6x 6x 6x 6x 6x                                                                                               7x   7x 7x   7x 7x                                                                                                 3x 3x              
/* eslint-disable react/forbid-component-props */
 
/*** Libraries ***/
import React, { Component } from 'react';
import { SearchUI_defaultProps } from './props/defaultProps';
import { Search_propTypes, SearchUI_propTypes } from './props/propTypes';
 
/** * Components ** */
import TextBoxIcon from '@zohodesk/components/lib/TextBoxIcon/TextBoxIcon';
import { Container, Box } from '@zohodesk/components/lib/Layout';
import { mergeStyle } from '@zohodesk/utils';
import { Icon } from '@zohodesk/icons';
import ToggleDropDown from '../../../dropdown/ToggleDropDown/ToggleDropDown';
 
/** * Methods ** */
import { debounce } from '@zohodesk/components/lib/utils/debounce';
import { cancelBubblingEffect } from '@zohodesk/components/es/utils/Common';
 
/** * CSS ** */
import defaultStyle from './LookupSearch.module.css';
 
export default class Search extends Component {
  constructor(props) {
    super(props);
    this.handleTextBoxFocus = this.handleTextBoxFocus.bind(this);
    this.handleTextBoxBlur = this.handleTextBoxBlur.bind(this);
    this.handleGetTextBoxRef = this.handleGetTextBoxRef.bind(this);
    this.handleRenderChildren = this.handleRenderChildren.bind(this);
    this.state = {
      isFocus: false
    };
  }
 
  handleTextBoxFocus(e) {
    const { onFocus } = this.props;
    this.setState({
      isFocus: true
    });
    onFocus && onFocus(e);
  }
 
  handleTextBoxBlur(e) {
    const { onBlur } = this.props;
    this.setState({
      isFocus: false
    });
    onBlur && onBlur(e);
  }
 
  handleGetTextBoxRef(el) {
    const { getRef } = this.props;
    this.textBox = el;
    getRef && getRef(el);
  }
 
  handleRenderChildren({isActive, isFocus}) {
    const { value, selectedId, renderChildren } = this.props;
    return renderChildren({isActive, isFocus, value, selectedId});
  }
 
  render() {
    const { options, onSelect, value, selectedId, renderChildren } = this.props;
    const { isFocus } = this.state;
 
    const searchUIExtraProps = {
      isFocus: isFocus,
      onFocus: this.handleTextBoxFocus,
      onBlur: this.handleTextBoxBlur,
      getRef: this.handleGetTextBoxRef,
      value: value,
      renderChildren: typeof renderChildren == 'function' ? this.handleRenderChildren : undefined
    };
 
    return options ? (
      <ToggleDropDown
        value={value}
        options={options}
        onClick={onSelect}
        needTick
        isArrow={false}
        isToggleStateNeeded={true}
        selectedId={selectedId}
      >
        <SearchUI {...this.props} {...searchUIExtraProps} />
      </ToggleDropDown>
    ) : (
      <SearchUI {...this.props} {...searchUIExtraProps} />
    );
  }
}
 
Search.propTypes = Search_propTypes;
 
class SearchUI extends Component {
  constructor(props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
    this.handleClear = this.handleClear.bind(this);
    this.handleKeyDown = this.handleKeyDown.bind(this);
    this.handleSearch = debounce(this.handleSearch.bind(this), 500);
  }
 
  handleSearch() {
    const { onSearch } = this.props;
    onSearch && onSearch();
  }
 
  handleKeyDown(e) {
    const { keyCode } = e;
    const { onSearch, needOnTypeSearch, onKeyDown } = this.props;
    onKeyDown && onKeyDown(e);
    if (keyCode === 13) {
      !needOnTypeSearch && onSearch && onSearch();
    }
  }
 
  handleChange(value) {
    const { onChange, needOnTypeSearch } = this.props;
    onChange && onChange(value);
    needOnTypeSearch && this.handleSearch();
  }
 
  handleClear(e) {
    const { onChange } = this.props;
    cancelBubblingEffect(e);
    onChange && onChange('');
    this.handleSearch();
  }
 
  render() {
    const {
      dataId,
      searchStr,
      placeHolder,
      getRef,
      title,
      isBoxed,
      isSearchIconNeed,
      options,
      activeClass,
      isFocus,
      onFocus,
      onBlur,
      renderChildren,
      isActive,
      hasSeparator,
      customStyle
    } = this.props;
 
    const isActiveState = isFocus || isActive;
    const isFocusWidth = isActiveState || searchStr;
 
    const style = mergeStyle(defaultStyle, customStyle);
    return (
      <Container
        isCover={false}
        alignBox='row'
        align='vertical'
        className={`
          ${style.wrapper} ${isBoxed ? style.boxType : style.borderType} 
          ${isActiveState ? `${style.active} ${activeClass || ''}` : ''}
          ${isFocusWidth ? style.focusWidth : ''}
        `}
      >
        {typeof renderChildren == 'function' ? renderChildren({isActive, isFocus}) : options ? (
          <Container
            className={`${style.drpSearchBox} ${isActiveState ? style.lineActive : ''}`}
            isCover={false}
            align='vertical'
            alignBox='row'
            data-title={title}
          >
            <Icon name='ZD-search' size='11' />
            <Icon name='ZD-down' size='7' iconClass={style.iconArrow} />
          </Container>
        ) : isSearchIconNeed ? (
            <Icon name='ZD-search' size='11' iconClass={`${style.searchIcon} ${isBoxed ? style.searchIconBox : ''}`}/>
        ): null}
        {hasSeparator ? <div className={style.separator}></div> : null}
        <Box flexible onClick={cancelBubblingEffect}>
          <TextBoxIcon
            customProps={{ TextBoxProps: { 'data-a11y-autofocus': true } }}
            placeHolder={placeHolder}
            value={searchStr}
            onChange={this.handleChange}
            onFocus={onFocus}
            onBlur={onBlur}
            onKeyDown={this.handleKeyDown}
            onMouseDown={cancelBubblingEffect}
            dataId={dataId}
            size='small'
            inputRef={getRef}
            onClear={this.handleClear}
            onClearMouseDown={cancelBubblingEffect}
            needBorder={false}
          />
        </Box>
      </Container>
    );
  }
}
 
SearchUI.defaultProps = SearchUI_defaultProps;
SearchUI.propTypes = SearchUI_propTypes;
 
// if (__DOCS__) {
//   Search.docs = {
//     componentGroup: 'Lookup'
//   };
// }