All files index.jsx

92.59% Statements 50/54
81.25% Branches 39/48
90.91% Functions 10/11
92.59% Lines 50/54
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                                                                                    15x 15x 15x       15x 15x 15x 15x 15x       15x           6x 6x 3x     6x 2x 1x 1x           2x 2x 2x   2x 1x         1x 1x 1x         4x 4x 4x 4x 1x 3x 3x 3x   3x 3x       3x 3x           2x 2x 2x 2x   2x 2x 2x 2x 2x             37x 37x 37x         29x     1x                  
import React, { Component } from 'react';
import PropTypes from 'prop-types';
 
import './style.scss';
import CloseIcon from './icons.jsx';
 
export default class TaggleInput extends Component {
  static propTypes = {
    tags: PropTypes.array,
    maxTags: PropTypes.number,
    allowDuplicates: PropTypes.bool,
    duplicateTagClass: PropTypes.string,
    placeholder: PropTypes.string,
    onBeforeTagAdd: PropTypes.func,
    onAfterTagAdd: PropTypes.func,
    onBeforeTagRemove: PropTypes.func,
    onAfterTagRemove: PropTypes.func,
    onInputBlur: PropTypes.func,
    onInputChange: PropTypes.func,
    keyCodesToAddTag: PropTypes.array,
    saveOnBlur: PropTypes.bool,
    readOnly: PropTypes.bool
  }
 
  static defaultProps = {
    tags: [],
    maxTags: 0,
    allowDuplicates: false,
    duplicateTagClass: 'bounce',
    placeholder: 'Enter the tags',
    onBeforeTagAdd: undefined,
    onAfterTagAdd: undefined,
    onBeforeTagRemove: undefined,
    onAfterTagRemove: undefined,
    onInputBlur: undefined,
    onInputChange: undefined,
    keyCodesToAddTag: [13],
    saveOnBlur: false,
    readOnly: false
  }
 
  constructor(props) {
    super(props);
    const { tags } = props;
    this.state = {
      tags,
      duplicateIndex: ''
    };
    this.onKeyDown = this.onKeyDown.bind(this);
    this.onBlur = this.onBlur.bind(this);
    this.onChange = this.onChange.bind(this);
    this.addTag = this.addTag.bind(this);
    this.removeTag = this.removeTag.bind(this);
  }
 
  componentDidMount() {
    Iif (this.input) {
      this.input.focus();
    }
  }
 
  onKeyDown(event) {
    const { keyCodesToAddTag } = this.props;
    if (keyCodesToAddTag.includes(event.keyCode) && event.target && event.target.value) {
      this.addTag(event.target.value);
    }
 
    if (event.keyCode === 8) {
      if (!(event.target && event.target.value)) {
        const { tags } = this.state;
        this.removeTag(tags.length - 1);
      }
    }
  }
 
  onBlur(event) {
    const { onInputBlur, saveOnBlur } = this.props;
    Eif (typeof onInputBlur === 'function') {
      onInputBlur(event);
    }
    if (saveOnBlur && event.target && event.target.value) {
      this.addTag(event.target.value);
    }
  }
 
  onChange(event) {
    const { onInputChange } = this.props;
    Eif (typeof onInputChange === 'function') {
      onInputChange(event);
    }
  }
 
  addTag(tag) {
    const tagRef = tag.trim();
    const { allowDuplicates, onBeforeTagAdd, onAfterTagAdd } = this.props;
    const { tags } = this.state;
    if (!allowDuplicates && tags.indexOf(tagRef) !== -1) {
      this.setState({ duplicateIndex: tags.indexOf(tagRef) });
    } else Eif (tagRef) {
      Eif (typeof onBeforeTagAdd === 'function') {
        onBeforeTagAdd(tagRef);
      }
      this.setState({ tags: [...tags, tagRef], duplicateIndex: '' });
      Iif (this.input) {
        this.input.value = '';
        this.input.focus();
      }
      Eif (typeof onAfterTagAdd === 'function') {
        onAfterTagAdd(tagRef);
      }
    }
  }
 
  removeTag(index) {
    const { onAfterTagRemove, onBeforeTagRemove } = this.props;
    const { tags } = this.state;
    Eif (typeof onBeforeTagRemove === 'function') {
      onBeforeTagRemove(tags[index]);
    }
    const updatedTags = [...tags];
    updatedTags.splice(index, 1);
    this.setState({ tags: updatedTags });
    Eif (typeof onAfterTagRemove === 'function') {
      onAfterTagRemove(tags[index]);
    }
  }
 
  render() {
    const {
      maxTags, placeholder, duplicateTagClass, readOnly
    } = this.props;
    const { tags, duplicateIndex } = this.state;
    return (
      <div className="taggle_wrapper">
        <ul className="taggle_list">
          {tags.length > 0
            && tags.map((tag, index) => (
              <li key={Math.random()} className={`taggle ${duplicateIndex === index ? duplicateTagClass : ''}`}>
                <span className="taggle_text">
                  {tag}
                  {!readOnly && <button className="close" type="button" onClick={() => this.removeTag(index)}><CloseIcon /></button>}
                </span>
              </li>))}
          {(!readOnly && (!maxTags || tags.length < maxTags)) && <li><input ref={(ele) => { this.input = ele; }} type="text" placeholder={placeholder} onKeyDown={this.onKeyDown} onBlur={this.onBlur} onChange={this.onChange} /></li>}
        </ul>
      </div>
    );
  }
}