All files / src/tagsearchbox Tag.tsx

32% Statements 16/50
46.88% Branches 15/32
18.18% Functions 4/22
31.91% Lines 15/47

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 233 234 235 236 237 238 239 240 241 242                                                                                                                                                      1x                       4x                                                                                                                         4x         4x         4x                                   4x                     4x   4x 4x 3x   6x   4x   4x                         8x                                           8x                  
/* eslint-disable */
import React from "react";
import { TagInput } from "./TagInput";
import { AttributeValue } from "./AttributeSelect";
import { FocusPosType } from "./TagSearchBox";
import { withTranslation, WithTranslationProps } from "../i18n";
import { Bubble } from "../bubble";
import { Icon } from "../icon";
import { withConfig, WithConfigProps } from "../_util/config-context";
import { Combine } from "../_type";
 
export interface TagValue {
  /**
   * 标签标识
   */
  _key: string;
 
  /**
   * 当前是否在编辑状态
   */
  _edit?: boolean;
 
  /**
   * 标签属性
   */
  attr?: AttributeValue;
 
  /**
   * 标签属性值
   */
  values?: any[];
}
 
export interface TagProps
  extends Combine<WithConfigProps, WithTranslationProps> {
  /**
   * 标签属性
   */
  attr?: AttributeValue;
 
  /**
   * 标签属性值
   */
  values?: any[];
 
  /**
   * 触发标签相关事件
   */
  dispatchTagEvent?: (type: string, payload?: any) => void;
 
  /**
   * 所有属性集合
   */
  attributes: AttributeValue[];
 
  /**
   * 当前聚焦状态
   */
  focused: FocusPosType;
 
  /**
   * 最大长度
   */
  maxWidth?: number;
 
  /**
   * 搜索框是否处于展开状态
   */
  active: boolean;
}
 
export interface TagState {
  inEditing: boolean;
}
 
const keys = {
  "8": "backspace",
  "13": "enter",
  "37": "left",
  "38": "up",
  "39": "right",
  "40": "down",
};
 
@withConfig
@withTranslation
export class Tag extends React.Component<TagProps, TagState> {
  state: TagState = {
    inEditing: false,
  };
 
  focusTag(): void {
    (this["input-inside"] as any).focusInput();
  }
 
  focusInput(): void {
    (this["input"] as any).focusInput();
  }
 
  resetInput() {
    (this["input-inside"] as any).resetInput();
  }
 
  setInputValue(value: string, callback?: Function): void {
    (this["input"] as any).setInputValue(value, callback);
  }
 
  getInputValue(): string {
    return this["input"].getInputValue();
  }
 
  addTagByInputValue(): boolean {
    return this["input"].addTagByInputValue();
  }
 
  addTagByEditInputValue(): boolean {
    if (!this["input-inside"]) return;
    return this["input-inside"].addTagByInputValue();
  }
 
  setInfo(info: any, callback?: Function): void {
    return this["input"].setInfo(info, callback);
  }
 
  moveToEnd(): void {
    return this["input"].moveToEnd();
  }
 
  getInfo(): any {
    const { attr, values } = this.props;
    return { attr, values };
  }
 
  edit(pos: string): void {
    this.setState({ inEditing: true });
    const input = this["input-inside"];
    input.setInfo(this.getInfo(), () => {
      if (pos === "attr") {
        return input.selectAttr();
      }
      return input.selectValue();
    });
  }
 
  editDone(): void {
    this.setState({ inEditing: false });
  }
 
  handleTagClick = (e, pos?: string): void => {
    this.props.dispatchTagEvent("click", pos);
    e.stopPropagation();
  };
 
  handleDelete = e => {
    e.stopPropagation();
    this.props.dispatchTagEvent("del");
  };
 
  handleKeyDown = (e): void => {
    if (!keys[e.keyCode]) return;
 
    e.preventDefault();
 
    switch (keys[e.keyCode]) {
      case "tab":
      case "enter":
        this.props.dispatchTagEvent("click", "value");
        break;
 
      case "backspace":
        this.props.dispatchTagEvent("del", "keyboard");
        break;
    }
  };
 
  render() {
    const { inEditing } = this.state;
    const {
      t,
      config: { classPrefix },
      active,
      attr,
      values,
      dispatchTagEvent,
      attributes,
      focused,
      maxWidth,
    } = this.props;
 
    let attrStr = attr ? attr.name : "";
    if (attr && attr.name) {
      attrStr += ": ";
    }
    const valueStr = values.map(item => item.name).join(" | ");
 
    const removeable = attr && "removeable" in attr ? attr.removeable : true;
 
    return (
      <div
        style={{
          display: "inline-block",
          minHeight: 20,
          width: inEditing && !active ? 0 : "auto",
          position: "relative",
          verticalAlign: "inherit",
        }}
      >
        <Bubble content={active ? t.tagSearchBoxEditingTips : null}>
          <div
            className={`${classPrefix}-tag`}
            ref={div => (this["content"] = div)}
            style={{
              display: inEditing ? "none" : undefined,
              paddingRight: removeable ? undefined : 8,
            }}
            onClick={this.handleTagClick}
          >
            <span onClick={e => this.handleTagClick(e, "attr")}>{attrStr}</span>
            <span onClick={e => this.handleTagClick(e, "value")}>
              {valueStr}
            </span>
            {active && removeable && (
              <Icon type="dismiss" onClick={this.handleDelete} />
            )}
          </div>
        </Bubble>
        <TagInput
          type="edit"
          hidden={!inEditing}
          maxWidth={maxWidth}
          handleKeyDown={this.handleKeyDown}
          active={active}
          ref={input => (this["input-inside"] = input)}
          attributes={attributes}
          dispatchTagEvent={dispatchTagEvent}
          isFocused={focused === FocusPosType.INPUT_EDIT}
        />
      </div>
    );
  }
}