All files / src/tagsearchbox/valueselect SingleValueSelect.tsx

1.64% Statements 1/61
0% Branches 0/28
0% Functions 0/14
1.82% Lines 1/55

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                                            1x                                                                                                                                                                                                                                                                                            
/* eslint-disable */
import React from "react";
import { withTranslation, WithTranslationProps } from "../../i18n";
import { List } from "../../list";
import { DropdownBox } from "../../dropdown";
import { WithConfigProps, withConfig } from "../../_util/config-context";
import { Combine } from "../../_type";
 
export interface SingleValueSelectProps
  extends Combine<WithTranslationProps, WithConfigProps> {
  values: any[];
  inputValue: string;
  onChange?: (value: any[]) => void;
  onSelect?: (value: any[]) => void;
  offset: number;
  maxHeight: number;
}
 
export interface SingleValueSelectState {
  select: number;
}
 
const keys = {
  "8": "backspace",
  "9": "tab",
  "13": "enter",
  "37": "left",
  "38": "up",
  "39": "right",
  "40": "down",
};
 
@withConfig
@withTranslation
export class SingleValueSelect extends React.Component<
  SingleValueSelectProps,
  SingleValueSelectState
> {
  constructor(props) {
    super(props);
    const { values, inputValue, onSelect } = this.props;
 
    let select = -1;
    values.forEach((item, index) => {
      if (item.name === inputValue) {
        select = index;
      }
    });
 
    this.state = {
      select,
    };
  }
 
  componentDidMount() {
    const { select } = this.state;
    if (select < 0 && this.props.onSelect) {
      this.props.onSelect(this.getValue(select));
    }
  }
 
  componentWillReceiveProps(nextProps: SingleValueSelectProps) {
    const { values, inputValue } = nextProps;
    const list = values.map(item => item.name);
    const select = list.indexOf(inputValue);
    this.setState({ select });
  }
 
  // 父组件调用
  handleKeyDown = (keyCode: number): boolean => {
    if (!keys[keyCode]) return;
    const { onSelect } = this.props;
    const { select } = this.state;
 
    switch (keys[keyCode]) {
      case "enter":
      case "tab":
        if (onSelect) {
          onSelect(this.getValue(select));
        }
        return false;
 
      case "up":
        this.move(-1);
        break;
 
      case "down":
        this.move(1);
        break;
    }
  };
 
  getValue(select: number): any[] {
    if (select < 0) return [];
    const { values, inputValue } = this.props;
    const list = values;
    if (select < list.length) {
      return [list[select]];
    } else {
      const select = list.map(item => item.name).indexOf(inputValue);
      this.setState({ select });
      if (select < 0) return [];
      return [list[select]];
    }
  }
 
  move = (step: number): void => {
    const { select } = this.state;
    const { values } = this.props;
    const list = values;
    if (list.length <= 0) return;
    this.setState({ select: (select + step + list.length) % list.length });
  };
 
  handleClick = (e: React.MouseEvent, index: number): void => {
    e.stopPropagation();
    if (this.props.onSelect) {
      this.props.onSelect(this.getValue(index));
    }
  };
 
  render() {
    const { select } = this.state;
    const {
      t,
      config: { classPrefix },
      values,
      offset,
      maxHeight,
    } = this.props;
 
    const list = values.map((item, index) => {
      return (
        <List.Item
          key={index}
          selected={select === index}
          onClick={e => this.handleClick(e, index)}
        >
          <span title={item.name} style={item.style || {}}>
            {item.name}
          </span>
        </List.Item>
      );
    });
 
    if (list.length === 0) {
      list.push(
        <li role="presentation" key={0} className="is-disabled">
          <div className={`${classPrefix}-option-list__item`} role="menuitem">
            {t.emptyText}
          </div>
        </li>
      );
    }
 
    return (
      <DropdownBox style={{ left: offset }} onClick={e => e.stopPropagation()}>
        <List type="option" style={{ maxHeight }}>
          {list}
        </List>
      </DropdownBox>
    );
  }
}