@ccs-design/rc-pro
Version:
ccs design basic react component
322 lines (273 loc) • 13.1 kB
JavaScript
import "antd/es/config-provider/style";
import _ConfigProvider from "antd/es/config-provider";
import "antd/es/spin/style";
import _Spin from "antd/es/spin";
import "antd/es/input/style";
import _Input from "antd/es/input";
var _excluded = ["size", "style", "value", "width", "height", "disabled", "children", "showField", "className", "forceRender", "placeholder", "popupContainer", "onChange"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
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 _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, { cloneElement, useEffect, useRef, useState } from 'react';
import Trigger from 'rc-trigger';
import { CloseCircleFilled, DownOutlined } from '@ant-design/icons';
import _isPlainObject from 'lodash/isPlainObject';
import _isNil from 'lodash/isNil';
import { useDebounceFn, useEventListener, useUpdate } from 'ahooks';
import zhCN from "antd/es/locale-provider/zh_CN";
import './index.less';
var zIndex = 2000;
export var classPrefix = 'ccs-trigger';
/**
* 适用于Form.Item中的 trigger组件,其内容自行定义,遵循ant form 自定义组件规范
* @param CcsTriggerPropsType
* @returns
*/
var CcsTrigger = function CcsTrigger(props) {
var _ref$current2;
var size = props.size,
style = props.style,
value = props.value,
width = props.width,
height = props.height,
disabled = props.disabled,
children = props.children,
showField = props.showField,
className = props.className,
forceRender = props.forceRender,
placeholder = props.placeholder,
popupContainer = props.popupContainer,
onChange = props.onChange,
restProps = _objectWithoutProperties(props, _excluded);
var inputRef = useRef(null);
var ref = useRef(null);
var containerRef = useRef(null);
var closeRef = useRef(null);
var downRef = useRef(null);
var alignRef = useRef({
points: ['tl', 'bl'],
offset: [0, 3],
size: {
width: width,
height: height
},
maxSize: {
width: width,
height: height
}
});
var _useState = useState(null),
_useState2 = _slicedToArray(_useState, 2),
visible = _useState2[0],
setVisible = _useState2[1];
var update = useUpdate();
var renderValue = function renderValue() {
if (showField) {
return _isPlainObject(value) ? value[showField] : undefined;
}
return inputRef.current;
};
var _useState3 = useState(false),
_useState4 = _slicedToArray(_useState3, 2),
loading = _useState4[0],
setLoading = _useState4[1]; // 显示下拉图标
var onMouseLeave = function onMouseLeave() {
var _closeRef$current, _downRef$current;
(_closeRef$current = closeRef.current) === null || _closeRef$current === void 0 ? void 0 : _closeRef$current.classList.replace('ccs-trigger-show', 'ccs-trigger-hidden');
(_downRef$current = downRef.current) === null || _downRef$current === void 0 ? void 0 : _downRef$current.classList.replace('ccs-trigger-hidden', 'ccs-trigger-show');
}; // 显示清除数据图标
var onMouseEnter = function onMouseEnter() {
if (!_isNil(value)) {
var _downRef$current2, _closeRef$current2;
(_downRef$current2 = downRef.current) === null || _downRef$current2 === void 0 ? void 0 : _downRef$current2.classList.replace('ccs-trigger-show', 'ccs-trigger-hidden');
(_closeRef$current2 = closeRef.current) === null || _closeRef$current2 === void 0 ? void 0 : _closeRef$current2.classList.replace('ccs-trigger-hidden', 'ccs-trigger-show');
}
}; // 获取对齐方式、默认右下侧展开
var getAlignMode = function getAlignMode() {
var _ref$current;
var rect = (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.getBoundingClientRect();
if (!rect) return;
var top = rect.top,
left = rect.left,
right = rect.right,
bottom = rect.bottom;
var _document$body = document.body,
offsetWidth = _document$body.offsetWidth,
offsetHeight = _document$body.offsetHeight;
var rightWidth = offsetWidth - left; // 右侧宽度
var bottomHeight = offsetHeight - bottom; // 下侧高度
// 元素 上右下左弹出框空间 top rightWidth bottomHeight right
var x = 'right';
var y = 'bottom';
if (width) {
if (rightWidth >= width) {// 右侧宽度足够
} else if (right >= width) {
// 左侧宽度足够
x = 'left';
} else {
// 两侧宽度都不够,取最大
x = rightWidth > right ? 'right' : 'left'; // 弹出框最大宽度
alignRef.current.size.width = Math.max(right, rightWidth) - 10;
}
} else {
// 没有传入宽度、取最大值
x = rightWidth > right ? 'right' : 'left';
alignRef.current.maxSize.width = Math.max(right, rightWidth) - 10;
}
if (height) {
if (bottomHeight >= height) {// 底部高度足够
} else if (top >= height) {
// 顶部高度足够
y = 'top';
} else {
// 上下高度都不够取最大
y = bottomHeight > top ? 'bottom' : 'top'; // 弹出框最大高度
alignRef.current.size.height = Math.max(top, bottomHeight) - 10;
}
} else {
// 没有传入高度、取最大值
y = bottomHeight > top ? 'bottom' : 'top';
alignRef.current.maxSize.height = Math.max(top, bottomHeight) - 10;
}
var point0 = 'tl'; // 弹出框对齐点
var point1 = 'bl'; // 目标对齐点
var offsetX = 0; // x偏移量
var offsetY = 3; // y偏移量
// 弹出框四个方位展开
if (x === 'right' && y === 'bottom') {// 右下空间足够、默认右下展开
} else if (x === 'left' && y === 'bottom') {
// 右边宽度不够、高度足够、左下侧展开
point1 = 'br';
point0 = 'tr';
} else if (x === 'right' && y === 'top') {
// 右边宽度足够、高度不够、右上侧展开
point1 = 'tl';
point0 = 'bl';
offsetY = -3;
} else {
// 右边宽度不够、高度不够、左上侧展开
point1 = 'tr';
point0 = 'br';
offsetY = -3;
}
alignRef.current = _objectSpread(_objectSpread({}, alignRef.current), {}, {
points: [point0, point1],
offset: [offsetX, offsetY]
});
}; // resize 防抖
var _useDebounceFn = useDebounceFn(function (e) {
if (!alignRef.current.timeStamp && visible) {
// 防抖开始、关闭已打开的弹出框
alignRef.current.timeStamp = e.timeStamp;
setVisible(false);
} else {
// 防抖结束
getAlignMode();
update();
alignRef.current.timeStamp = undefined;
}
}, {
wait: 500,
leading: true
}),
run = _useDebounceFn.run;
var onChangeText = function onChangeText(text) {
inputRef.current = text;
update();
}; // 监听窗口变化
useEventListener('resize', run, {
target: window
});
var _useState5 = useState(zIndex),
_useState6 = _slicedToArray(_useState5, 1),
triggerIndex = _useState6[0];
useEffect(function () {
setTimeout(function () {
getAlignMode();
}, 10);
zIndex += 1;
}, []);
var _alignRef$current$siz = alignRef.current.size,
ppWidth = _alignRef$current$siz.width,
ppHeight = _alignRef$current$siz.height;
var _alignRef$current$max = alignRef.current.maxSize,
maxWidth = _alignRef$current$max.width,
maxHeight = _alignRef$current$max.height;
return /*#__PURE__*/React.createElement(_ConfigProvider, {
locale: zhCN
}, /*#__PURE__*/React.createElement(Trigger, {
mask: true,
action: ['click'],
forceRender: forceRender,
prefixCls: classPrefix,
popup: /*#__PURE__*/React.createElement("div", {
className: "".concat(classPrefix, "-container"),
ref: containerRef,
style: {
width: ppWidth || 'auto',
minWidth: (_ref$current2 = ref.current) === null || _ref$current2 === void 0 ? void 0 : _ref$current2.clientWidth,
maxHeight: ppHeight || maxHeight,
maxWidth: maxWidth
}
}, children && /*#__PURE__*/cloneElement(children, _objectSpread({
value: value,
onChange: onChange,
onChangeText: onChangeText,
onVisible: setVisible,
onLoading: setLoading,
width: ppWidth,
height: ppHeight
}, restProps))),
popupVisible: visible,
onPopupVisibleChange: function onPopupVisibleChange(e) {
if (!disabled) setVisible(e);
},
showAction: ['click'],
hideAction: ['click'],
popupAlign: alignRef.current,
popupStyle: {
position: 'absolute'
},
getPopupContainer: popupContainer ? function () {
return ref.current;
} : undefined,
zIndex: triggerIndex
}, /*#__PURE__*/React.createElement("div", {
ref: ref,
className: className,
style: style,
onMouseEnter: onMouseEnter,
onMouseLeave: onMouseLeave
}, /*#__PURE__*/React.createElement(_Spin, {
spinning: loading
}, /*#__PURE__*/React.createElement(_Input, {
readOnly: true,
disabled: disabled,
value: renderValue(),
placeholder: placeholder || '请选择',
size: size || 'middle',
suffix: !disabled ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(CloseCircleFilled, {
ref: closeRef,
onClick: function onClick() {
if (onChange) {
onChange(undefined);
onChangeText(undefined);
onMouseLeave();
}
},
className: "".concat(classPrefix, "-icon ").concat(classPrefix, "-hidden")
}), /*#__PURE__*/React.createElement(DownOutlined, {
className: "".concat(classPrefix, "-icon ").concat(classPrefix, "-show"),
ref: downRef
})) : null
})))));
};
export default CcsTrigger;