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 | 1x 1x | /* eslint-disable */ import React from "react"; import { List } from "../list"; import { DropdownBox } from "../dropdown"; import { TagSearchBoxContext } from "./TagSearchBoxContext"; export interface AttributeValue { /** * 为资源属性需求值的类型 */ type: string | [string, { [key: string]: any }]; /** * 属性的唯一标识,会在结果中返回 */ key: string; /** * 资源属性值名称 */ name: string; /** * 资源属性可用值 */ values?: any[] | Function; /** * 该属性是否可重复选择 * @default false */ reusable?: boolean; /** * 该属性是否可移除 * @default true */ removeable?: boolean; } export interface AttributeSelectProps { attributes: AttributeValue[]; inputValue: string; onSelect?: (attribute: AttributeValue) => void; maxHeight: number; } export interface AttributeSelectState { select: number; } const keys = { "8": "backspace", "9": "tab", "13": "enter", "37": "left", "38": "up", "39": "right", "40": "down", }; export class AttributeSelect extends React.Component< AttributeSelectProps, AttributeSelectState > { static contextType = TagSearchBoxContext; state: AttributeSelectState = { select: -1, }; componentWillReceiveProps(nextProps: AttributeSelectProps) { if (this.props.inputValue !== nextProps.inputValue) { this.setState({ select: -1 }); } } // 父组件调用 handleKeyDown = (keyCode: number): boolean => { if (!keys[keyCode]) return; const { onSelect } = this.props; const { select } = this.state; switch (keys[keyCode]) { case "enter": case "tab": if (select < 0) break; if (onSelect) { onSelect(this.getAttribute(select)); } return false; case "up": this.move(-1); break; case "down": this.move(1); break; } }; getUseableList(): Array<AttributeValue> { const { attributes, inputValue } = this.props; // 获取冒号前字符串模糊查询 const fuzzyValue = /(.*?)(:|:).*/.test(inputValue) ? RegExp.$1 : inputValue; return attributes.filter( item => item.name.includes(inputValue) || item.name.includes(fuzzyValue) ); } getAttribute(select: number): AttributeValue { const list = this.getUseableList(); if (select < list.length) { return list[select]; } } move = (step: number): void => { const { select } = this.state; const list = this.getUseableList(); if (list.length <= 0) return; this.setState({ select: (select + step + list.length) % list.length }); }; handleClick = (e: React.MouseEvent, index: number): void => { e.stopPropagation(); e.nativeEvent.stopPropagation(); if (this.props.onSelect) { this.props.onSelect(this.getAttribute(index)); } }; render() { const { attributesSelectTips } = this.context; const { select } = this.state; const { maxHeight } = this.props; const list = this.getUseableList().map((item, index) => { return ( <List.Item key={index} selected={select === index} onClick={e => this.handleClick(e, index)} > {item.name} </List.Item> ); }); if (list.length === 0) return null; return ( <DropdownBox onClick={e => e.stopPropagation()}> <List type="option" style={{ maxHeight }}> {attributesSelectTips && ( <List.Item disabled>{attributesSelectTips}</List.Item> )} {list} </List> </DropdownBox> ); } } |