@fe6/water-pro
Version:
An enterprise-class UI design language and Vue-based implementation
324 lines (277 loc) • 13.2 kB
JavaScript
import { resolveComponent as _resolveComponent, withDirectives as _withDirectives, vShow as _vShow, resolveDirective as _resolveDirective, createVNode as _createVNode } from "vue";
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
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 _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; }
/** @format */
import { defineComponent, ref, reactive, toRefs, nextTick, watch, watchEffect, unref } from 'vue';
import { PlusOutlined, DashOutlined, LoadingOutlined, CloseOutlined } from '@ant-design/icons-vue';
import { getStrLength, hasOwn } from '@fe6/shared';
import ATag from '../../tag';
import ATooltip from '../../tooltip';
import APopover from '../../popover';
import AInput from '../../input';
import PropTypes from '../../_util/vue-types';
import { useRuleFormItem } from '../../_util/hooks/use-form-item';
import useConfigInject from '../../_util/hooks/useConfigInject';
import { getSlot } from '../../_util/props-util';
export default defineComponent({
name: 'ATagGroup',
components: {
PlusOutlined: PlusOutlined,
DashOutlined: DashOutlined,
LoadingOutlined: LoadingOutlined,
CloseOutlined: CloseOutlined
},
props: {
value: {
type: Array,
default: function _default() {
return [];
}
},
maxTagTextLength: PropTypes.number.def(4),
maxTagCount: PropTypes.number.def(4),
closable: PropTypes.bool.def(false),
createPlaceholder: PropTypes.string,
createable: PropTypes.bool,
createInputable: PropTypes.bool.def(true),
createLoading: PropTypes.bool,
removeLoading: PropTypes.bool,
closeEmitAble: PropTypes.bool.def(true),
nameLabel: PropTypes.string.def('name'),
color: PropTypes.string.def('blue'),
prefixCls: PropTypes.string,
tagStyle: PropTypes.style,
className: PropTypes.any,
showSelected: PropTypes.bool.def(true),
createBordered: PropTypes.bool.def(true),
createIcon: PropTypes.bool.def(true),
disabled: PropTypes.bool,
overlayClassName: PropTypes.any.def('')
},
emits: ['change', 'create-click', 'close-click'],
setup: function setup(props, _ref) {
var emit = _ref.emit;
var _useConfigInject = useConfigInject('tag-group', props),
prefixClsNew = _useConfigInject.prefixCls,
configProvider = _useConfigInject.configProvider;
var _useRuleFormItem = useRuleFormItem(props),
_useRuleFormItem2 = _slicedToArray(_useRuleFormItem, 1),
state = _useRuleFormItem2[0];
var options = ref(props.value);
var inputRef = ref();
var datas = reactive({
inputVisible: false,
inputValue: ''
});
var stateTruer = ref([]);
var removeIdx = ref(-1);
var handleClose = function handleClose(removedTag, id) {
if (!props.disabled) {
removeIdx.value = id;
if (props.closeEmitAble) {
emit('change', removedTag.name, 'remove');
}
emit('close-click', removedTag);
}
};
var showInput = function showInput(ev) {
if (!props.disabled) {
if (props.createInputable) {
datas.inputVisible = true;
nextTick(function () {
inputRef.value.focus();
});
}
emit('create-click', ev);
}
};
var handleInputConfirm = function handleInputConfirm() {
if (!props.disabled) {
if (datas.inputValue) {
emit('change', datas.inputValue, 'add');
_extends(datas, {
inputVisible: false,
inputValue: ''
});
}
}
};
var handleInputEnterConfirm = function handleInputEnterConfirm(ev) {
if (!props.disabled) {
if (datas.inputValue && ev.keyCode === 13) {
emit('change', datas.inputValue, 'add');
_extends(datas, {
inputVisible: false,
inputValue: ''
});
}
}
};
var handleInputChange = function handleInputChange(ev) {
if (!props.disabled) {
datas.inputValue = ev.target.value;
}
};
watch(function () {
return props.value;
}, function (newValue) {
options.value = newValue;
}); // 修复 客户标签弹框删除不了标签
watchEffect(function () {
stateTruer.value = unref(state).filter(function (sItem) {
return sItem.type !== 'delete';
}); // fix: tag-modal-list 中 select type 的时候,点击叉子一直 loading 状态
removeIdx.value = -1;
});
return _extends(_extends({}, toRefs(datas)), {
state: state,
stateTruer: stateTruer,
options: options,
removeIdx: removeIdx,
handleClose: handleClose,
showInput: showInput,
handleInputConfirm: handleInputConfirm,
handleInputEnterConfirm: handleInputEnterConfirm,
handleInputChange: handleInputChange,
inputRef: inputRef,
prefixClsNew: prefixClsNew,
getStrLength: getStrLength,
configProvider: configProvider
});
},
render: function render() {
var _this = this;
var _a;
var tagList = this.maxTagCount > 0 && this.stateTruer.length > this.maxTagCount ? this.stateTruer.slice(0, this.maxTagCount) : this.stateTruer;
var tagNode = [];
var getTagInnerNode = function getTagInnerNode(tagItem) {
var tagInnerNode = tagItem[_this.nameLabel];
if (_this.maxTagTextLength > 0 && getStrLength(tagItem[_this.nameLabel]) >= _this.maxTagTextLength) {
tagInnerNode = _createVNode(ATooltip, {
"title": tagItem[_this.nameLabel]
}, {
default: function _default() {
return [tagItem[_this.nameLabel]];
}
});
}
return tagInnerNode;
};
var getCloseIcon = function getCloseIcon(tagItem) {
var closeIconNode = _createVNode(CloseOutlined, null, null);
if (_this.removeIdx === tagItem.id) {
closeIconNode = _createVNode(LoadingOutlined, null, null);
}
return closeIconNode;
};
tagList.forEach(function (tagItem) {
var _ref2;
var theClosable = hasOwn(tagItem, 'canRemove') ? tagItem.canRemove : _this.closable && tagItem.id !== '0';
tagNode.push(_createVNode(ATag, {
"class": ["".concat(_this.prefixClsNew, "-inner"), (_ref2 = {}, _defineProperty(_ref2, "".concat(_this.prefixClsNew, "-inner-small"), _this.maxTagTextLength > 0 && getStrLength(tagItem[_this.nameLabel]) >= _this.maxTagTextLength), _defineProperty(_ref2, "".concat(_this.prefixClsNew, "-inner-big"), theClosable), _defineProperty(_ref2, "".concat(_this.prefixClsNew, "-inner-disabled"), _this.disabled), _ref2)],
"closable": theClosable,
"visible": true,
"color": _this.disabled ? '#f0f0f0' : _this.color,
"style": _this.tagStyle,
"onClose": function onClose() {
return _this.handleClose(tagItem, tagItem.id);
}
}, {
default: function _default() {
return [getTagInnerNode(tagItem)];
},
closeIcon: function closeIcon() {
return getCloseIcon(tagItem);
}
}));
});
var createNode = null;
if (this.createable) {
if (this.inputVisible) {
createNode = _createVNode(AInput, {
"ref": "inputRef",
"value": this.inputValue,
"type": "text",
"size": "small",
"class": ["".concat(this.prefixClsNew, "-create-input")],
"onBlur": this.handleInputConfirm,
"onChange": this.handleInputChange,
"onKeyup": this.handleInputEnterConfirm
}, null);
} else {
var _ref3;
createNode = _createVNode(ATag, {
"color": this.color,
"class": ["".concat(this.prefixClsNew, "-create"), (_ref3 = {}, _defineProperty(_ref3, "".concat(this.prefixClsNew, "-create-border"), this.createBordered), _defineProperty(_ref3, "".concat(this.prefixClsNew, "-create-disabled"), this.disabled), _defineProperty(_ref3, "".concat(this.prefixClsNew, "-create-border-disabled"), this.disabled && this.createBordered), _ref3)],
"loading": this.createLoading,
"onClick": this.showInput
}, {
default: function _default() {
return [_withDirectives(_createVNode(LoadingOutlined, null, null), [[_vShow, _this.createLoading]]), _withDirectives(_createVNode(_resolveComponent("plus-outlined"), null, null), [[_vShow, _this.createIcon && !_this.createLoading]]), _this.createPlaceholder || ((_a = _this.configProvider.locale) === null || _a === void 0 ? void 0 : _a.TagGroup.createPlaceholder)];
}
});
}
}
var popoverNode = null;
if (this.maxTagCount > 0 && this.stateTruer.length > this.maxTagCount) {
var popoverMoreNode = getSlot(this, 'more');
if (!popoverMoreNode.length) {
popoverMoreNode = [_createVNode(DashOutlined, null, null)];
}
var popoverTagNodes = [];
this.stateTruer.forEach(function (tagItem) {
var _ref4;
var theClosable = hasOwn(tagItem, 'canRemove') ? tagItem.canRemove : _this.closable && tagItem.id !== '0';
var sPopoverInner = tagItem[_this.nameLabel];
if (_this.maxTagTextLength > 0 && tagItem[_this.nameLabel].length > _this.maxTagTextLength) {
sPopoverInner = _createVNode(ATooltip, {
"title": tagItem[_this.nameLabel]
}, {
default: function _default() {
return [tagItem[_this.nameLabel]];
}
});
}
popoverTagNodes.push(_createVNode(ATag, {
"class": (_ref4 = {}, _defineProperty(_ref4, "".concat(_this.prefixClsNew, "-small"), _this.maxTagTextLength > 0 && tagItem[_this.nameLabel].length >= _this.maxTagTextLength), _defineProperty(_ref4, "".concat(_this.prefixClsNew, "-big"), theClosable), _defineProperty(_ref4, "".concat(_this.prefixClsNew, "-preive"), true), _defineProperty(_ref4, "".concat(_this.prefixClsNew, "-disabled"), _this.disabled), _ref4),
"closable": !_this.disabled && theClosable,
"color": _this.disabled ? '#f0f0f0' : _this.color,
"onClose": function onClose() {
return _this.handleClose(tagItem, tagItem.id);
}
}, {
default: function _default() {
return [sPopoverInner];
}
}));
});
var popoverInnerNode = _createVNode("div", {
"class": ["".concat(this.prefixClsNew, "-popover"), this.overlayClassName]
}, [popoverTagNodes]);
popoverNode = _createVNode(APopover, null, {
default: function _default() {
return [_createVNode(ATag, {
"color": _this.disabled ? '#f0f0f0' : _this.color,
"class": ["".concat(_this.prefixClsNew, "-more"), _defineProperty({}, "".concat(_this.prefixClsNew, "-more-disabled"), _this.disabled)]
}, {
default: function _default() {
return [popoverMoreNode];
}
})];
},
content: function content() {
return popoverInnerNode;
}
});
}
return _createVNode("div", {
"class": [this.prefixClsNew, this.className ? this.className : '']
}, [this.showSelected && tagNode, this.showSelected && popoverNode, createNode]);
}
});