loners-form
Version:
为工作而生,基于 antd 而生的可配置化表单
115 lines (91 loc) • 6.66 kB
JavaScript
import "antd/es/tooltip/style";
import _Tooltip from "antd/es/tooltip";
import "antd/es/select/style";
import _Select from "antd/es/select";
var _excluded = ["mode", "tooltip", "valueEnum", "options", "onChange"];
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useMemo, useState } from 'react';
var Option = _Select.Option;
var Multi = function Multi(props) {
var mode = props.mode,
_props$tooltip = props.tooltip,
tooltip = _props$tooltip === void 0 ? false : _props$tooltip,
valueEnum = props.valueEnum,
_props$options = props.options,
options = _props$options === void 0 ? [] : _props$options,
onChange = props.onChange,
rawProps = _objectWithoutProperties(props, _excluded);
var _useState = useState([]),
_useState2 = _slicedToArray(_useState, 2),
value = _useState2[0],
setValue = _useState2[1];
var dependency = JSON.stringify(options);
var opts = useMemo(function () {
return (options === null || options === void 0 ? void 0 : options.length) > 0 ? [{
label: '全选',
value: -99
}].concat(_toConsumableArray(options)) : [];
}, // eslint-disable-next-line react-hooks/exhaustive-deps
[dependency]);
var selectedAll = useMemo(function () {
return (options === null || options === void 0 ? void 0 : options.map(function (_ref) {
var val = _ref.value;
return val;
})) || [];
}, // eslint-disable-next-line react-hooks/exhaustive-deps
[dependency]);
var handleChange = function handleChange(val, option) {
// 取消全选
if ((val === null || val === void 0 ? void 0 : val.length) === opts.length) {
setValue([]);
onChange === null || onChange === void 0 ? void 0 : onChange([], option);
return;
} // 全选
if (val.includes(-99)) {
setValue(selectedAll);
onChange === null || onChange === void 0 ? void 0 : onChange(selectedAll, option);
return;
}
setValue(val);
onChange === null || onChange === void 0 ? void 0 : onChange(val, option);
};
return /*#__PURE__*/React.createElement(React.Fragment, null, tooltip ? /*#__PURE__*/React.createElement(_Select, _extends({
mode: "multiple",
filterOption: function filterOption(inputValue, option) {
var _option$children, _option$children$prop, _option$children$prop2;
return option === null || option === void 0 ? void 0 : (_option$children = option.children) === null || _option$children === void 0 ? void 0 : (_option$children$prop = _option$children.props) === null || _option$children$prop === void 0 ? void 0 : (_option$children$prop2 = _option$children$prop.title) === null || _option$children$prop2 === void 0 ? void 0 : _option$children$prop2.includes(inputValue);
},
value: value,
onChange: handleChange
}, rawProps), opts === null || opts === void 0 ? void 0 : opts.map(function (_ref2) {
var label = _ref2.label,
val = _ref2.value;
return /*#__PURE__*/React.createElement(Option, {
key: val,
value: val
}, /*#__PURE__*/React.createElement(_Tooltip, _extends({
title: label,
placement: "left"
}, tooltip), /*#__PURE__*/React.createElement("div", {
className: "text-ellipsis"
}, label)));
})) : /*#__PURE__*/React.createElement(_Select, _extends({
mode: "multiple",
value: value,
options: opts,
onChange: handleChange
}, rawProps)));
};
export default Multi;