@ccs-design/rc-pro-component
Version:
ccs design basic react component
230 lines (189 loc) • 10.2 kB
JavaScript
import "antd/es/input/style";
import _Input from "antd/es/input";
var _excluded = ["size", "style", "value", "width", "height", "disabled", "children", "showField", "className", "placeholder", "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 './index.less';
/**
* 适用于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,
placeholder = props.placeholder,
onChange = props.onChange,
restProps = _objectWithoutProperties(props, _excluded);
var inputRef = useRef(null);
var ref = useRef(null);
var closeRef = useRef(null);
var downRef = useRef(null);
var alignRef = useRef({
points: ['tl', 'bl'],
offset: [0, 3]
});
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 value;
}; // 显示下拉图标
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 _ref$current$getBound = (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.getBoundingClientRect(),
left = _ref$current$getBound.left,
bottom = _ref$current$getBound.bottom;
var _document$body = document.body,
offsetWidth = _document$body.offsetWidth,
offsetHeight = _document$body.offsetHeight;
var point0 = 'tl'; // 弹出框对齐点
var point1 = 'bl'; // 目标对齐点
var offsetX = 0; // x偏移量
var offsetY = 3; // y偏移量
if (width && height) {
var rightWidth = offsetWidth - left; // 右侧宽度
var bottomHeight = offsetHeight - bottom; // 下侧高度
// 弹出框四个方位展开
if (rightWidth > width && bottomHeight > height) {// 右下空间足够、默认右下展开
} else if (rightWidth < width && bottomHeight > height) {
// 右边宽度不够、高度足够、左下侧展开
point1 = 'br';
point0 = 'tr';
} else if (rightWidth > width && bottomHeight < height) {
// 右边宽度足够、高度不够、右上侧展开
point1 = 'tl';
point0 = 'bl';
offsetY = -3;
} else {
// 右边宽度不够、高度不够、左上侧展开
point1 = 'tr';
point0 = 'br';
offsetY = -3;
}
}
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; // 监听窗口变化
useEventListener('resize', run, {
target: window
});
useEffect(function () {
getAlignMode();
}, []);
return /*#__PURE__*/React.createElement(Trigger, {
action: ['click'],
popup: /*#__PURE__*/React.createElement("div", {
className: "ccs-trigger-container",
style: {
width: width || 'auto',
minWidth: (_ref$current2 = ref.current) === null || _ref$current2 === void 0 ? void 0 : _ref$current2.clientWidth,
maxHeight: height,
overflowY: 'auto'
}
}, children && /*#__PURE__*/cloneElement(children, _objectSpread({
value: value,
onChange: onChange,
onVisible: setVisible,
width: width,
height: height
}, restProps))),
popupVisible: visible,
onPopupVisibleChange: function onPopupVisibleChange(e) {
if (!disabled) setVisible(e);
},
showAction: ['click'],
hideAction: ['click'],
popupAlign: alignRef.current,
popupStyle: {
position: 'absolute'
} // getPopupContainer={() => ref.current as any}
,
zIndex: 2000
}, /*#__PURE__*/React.createElement("div", {
ref: ref,
className: className,
style: style,
onMouseEnter: onMouseEnter,
onMouseLeave: onMouseLeave
}, /*#__PURE__*/React.createElement(_Input, {
readOnly: true,
disabled: disabled,
ref: inputRef,
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);
onMouseLeave();
}
},
className: "ccs-trigger-icon ccs-trigger-hidden"
}), /*#__PURE__*/React.createElement(DownOutlined, {
className: "ccs-trigger-icon ccs-trigger-show",
ref: downRef
})) : null
})));
};
export default CcsTrigger;