zp-bee
Version:
zp-bee,是一款基于 Dumi,由 React + TypeScript 开发的组件库 🎉。
106 lines (89 loc) • 3.55 kB
JavaScript
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
import isVisible from 'rc-util/es/Dom/isVisible';
import Select from './select';
import React, { useRef, useEffect, useCallback, forwardRef, useImperativeHandle } from 'react';
import { ConfigContext } from '../bee-config';
var Option = Select.Option; // span占位符样式
var spanPlaceholderStyle = {
position: 'relative',
display: 'inline-block',
zIndex: -1000,
height: 0
};
var BeeSelect = /*#__PURE__*/forwardRef(function (props, ref) {
var label = props.label,
_props$required = props.required,
required = _props$required === void 0 ? false : _props$required,
others = __rest(props, ["label", "required"]);
var _React$useContext = React.useContext(ConfigContext),
getPrefixCls = _React$useContext.getPrefixCls;
/** 初始化计算标志位 */
var isInited = useRef(false);
var prefixCls = getPrefixCls('select');
var container = useRef(null);
var injectRef = useRef(null);
var spanRef = useRef(null);
useImperativeHandle(ref, function () {
var _a, _b;
return {
onFocus: (_a = injectRef.current) === null || _a === void 0 ? void 0 : _a.focus,
onBlur: (_b = injectRef.current) === null || _b === void 0 ? void 0 : _b.blur
};
});
var insertLabel = useCallback(function () {
var _a;
if (!isInited.current && isVisible(spanRef.current)) {
var parent = (_a = container.current) === null || _a === void 0 ? void 0 : _a.previousSibling;
var dom = parent.querySelector(".".concat(prefixCls, "-selector"));
var span = document.createElement('span');
span.innerHTML = label;
span.classList.add("".concat(prefixCls, "-label")); // 是否必填
if (required) {
var must = document.createElement('span');
must.innerHTML = '*';
span.prepend(must);
}
dom.prepend(span);
dom.style.paddingLeft = "".concat(span.getBoundingClientRect().width + 16, "px"); // 仅单选
if (!others.mode) {
// 输入框的样式同样left
var search = parent.querySelector(".".concat(prefixCls, "-selection-search"));
if (search) {
search.style.left = "".concat(span.getBoundingClientRect().width + 16, "px");
}
var searchplaceholder = parent.querySelector(".".concat(prefixCls, "-selection-placeholder"));
if (searchplaceholder) {
searchplaceholder.style.left = "".concat(span.getBoundingClientRect().width + 16, "px");
}
}
isInited.current = true;
}
}, []);
useEffect(function () {
if (isInited.current) {
isInited.current = false; // label变化重新计算
insertLabel();
}
}, [label]);
useEffect(insertLabel);
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Select, Object.assign({
ref: injectRef,
placeholder: '请选择'
}, others)), /*#__PURE__*/React.createElement("noscript", {
ref: container
}), /*#__PURE__*/React.createElement("span", {
ref: spanRef,
style: spanPlaceholderStyle
}));
});
BeeSelect.Option = Option;
export default BeeSelect;