@bos-alpha/data
Version:
数据管理
96 lines (95 loc) • 3.69 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import { jsx as _jsx } from "react/jsx-runtime";
import { useEffect, useState, useRef } from 'react';
import { Dropdown, Menu, Input } from 'antd';
export var SearchInput = function (_a) {
var value = _a.value, options = _a.options, placeholder = _a.placeholder, onChange = _a.onChange;
// 输入框dom
var inputRef = useRef(null);
// 输入框显示值
var _b = useState(''), iptVal = _b[0], setIptVal = _b[1];
// 下拉展开 可控
var _c = useState(false), visible = _c[0], setVisible = _c[1];
// 选中的值
var _d = useState([]), selectedKeys = _d[0], setSelectedKeys = _d[1];
// 可选值
var _e = useState(options), filterList = _e[0], setFilterList = _e[1];
useEffect(function () {
setSelectedKeys(value ? [value] : []);
if (value === null) {
onInput('');
}
}, [value]); // eslint-disable-line
useEffect(function () {
setSelectedKeys([]);
setIptVal('');
setFilterList(options);
}, [options]); // eslint-disable-line
var onInput = function (val) {
setIptVal(val);
var list = options;
// C开头 都模糊提示
if (/^C$|^CO$|^CON$|^CONC$|^CONCA$|^CONCAT$/.test(val)) {
list = [
{
value: "CONCAT(",
label: "CONCAT(property1,property2,...)"
},
{
value: "CONCAT_SEPARATOR('-',",
label: "CONCAT_SEPARATOR(separator,property1,property2,...)"
}
];
}
else if (/^CONCAT\($|^CONCAT\((\w+,)+$|^CONCAT_SEPARATOR\('.*',$|^CONCAT_SEPARATOR\('.*',(\w+,)+$/.test(val)) {
list = options.map(function (item) { return ({
value: "".concat(val).concat(item.value, ","),
label: "".concat(val).concat(item.value, ",")
}); });
}
else if (val) {
list = options.filter(function (_a) {
var label = _a.label;
return (label || '').indexOf(val) >= 0;
});
}
else {
list = options;
}
setFilterList(list);
onChange && onChange(val);
};
var onMenuSelect = function (key) {
onChange && onChange(key);
setIptVal(key);
if (/^CONCAT\(|^CONCAT_SEPARATOR\(/.test(key)) {
inputRef.current.focus({
cursor: 'end'
});
onInput(key);
setVisible(true);
}
else {
setVisible(false);
}
};
return (_jsx(Dropdown, __assign({ visible: visible, onVisibleChange: setVisible, trigger: ['click'], overlayStyle: {
maxHeight: '420px',
overflowY: 'auto'
}, overlay: _jsx(Menu, __assign({ onClick: function (e) {
setSelectedKeys([e.key]);
onMenuSelect(e.key);
}, selectedKeys: selectedKeys }, { children: filterList.map(function (item) { return (_jsx(Menu.Item, { children: item.label }, item.value)); }) }), void 0) }, { children: _jsx(Input, { ref: inputRef, value: iptVal, onInput: function (e) {
return onInput(e.currentTarget.value);
}, placeholder: placeholder }, void 0) }), void 0));
};