@lvxiaowu/antd4
Version:
antd4-components
142 lines (121 loc) • 6.36 kB
JavaScript
var _excluded = ["label", "children"],
_excluded2 = ["wholeTip", "isTip"];
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; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : 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 _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); }
import React from "react";
/**
* 字段描述,用于列表中描述某些字段
*/
import { Space, Tooltip } from 'antd';
import cls from 'classnames';
import './style.less';
import { createContext, useContext } from 'react';
function getTip(tip, node) {
if (tip === true) {
return node;
}
if (tip === null || tip === void 0 ? void 0 : tip.title) {
return tip === null || tip === void 0 ? void 0 : tip.title;
}
return tip;
}
var Context = /*#__PURE__*/createContext({
ellipsis: true,
tooltip: true,
split: ':'
});
function FieldDesc(_ref) {
var children = _ref.children,
style = _ref.style,
className = _ref.className,
labelStyle = _ref.labelStyle,
contentStyle = _ref.contentStyle,
_ref$split = _ref.split,
split = _ref$split === void 0 ? ':' : _ref$split,
_ref$ellipsis = _ref.ellipsis,
ellipsis = _ref$ellipsis === void 0 ? true : _ref$ellipsis,
_ref$tooltip = _ref.tooltip,
tooltip = _ref$tooltip === void 0 ? true : _ref$tooltip,
_ref$space = _ref.space,
space = _ref$space === void 0 ? 0 : _ref$space;
var Wrap = function Wrap(p) {
return /*#__PURE__*/React.createElement(Space, _extends({
direction: "vertical",
size: space,
className: cls('sd-desc', className),
style: style
}, p));
};
var wholeTip = tooltip && (tooltip === null || tooltip === void 0 ? void 0 : tooltip.whole) !== false;
var ctxValue = {
ellipsis: ellipsis,
split: split,
labelStyle: labelStyle,
contentStyle: contentStyle,
wholeTip: wholeTip,
tooltip: tooltip
};
var content = /*#__PURE__*/React.createElement(Context.Provider, {
value: ctxValue
}, /*#__PURE__*/React.createElement(Wrap, null, children));
var tip = wholeTip && getTip(tooltip, /*#__PURE__*/React.createElement(Context.Provider, {
value: _objectSpread(_objectSpread({}, ctxValue), {}, {
isTip: true,
labelStyle: _objectSpread(_objectSpread({}, labelStyle), {}, {
color: '#fff'
}),
contentStyle: _objectSpread(_objectSpread({}, contentStyle), {}, {
color: '#fff'
})
})
}, /*#__PURE__*/React.createElement(Wrap, {
style: false
}, children)));
return tip ? /*#__PURE__*/React.createElement(Tooltip, _extends({
title: tip,
placement: "topLeft"
}, wholeTip), /*#__PURE__*/React.createElement("div", {
className: "sd-desc-wrap"
}, content)) : content;
}
function Item(_ref2) {
var label = _ref2.label,
children = _ref2.children,
restProps = _objectWithoutProperties(_ref2, _excluded);
var _useContext = useContext(Context),
wholeTip = _useContext.wholeTip,
isTip = _useContext.isTip,
ctxValue = _objectWithoutProperties(_useContext, _excluded2);
var _ctxValue$restProps = _objectSpread(_objectSpread({}, ctxValue), restProps),
ellipsis = _ctxValue$restProps.ellipsis,
split = _ctxValue$restProps.split,
labelStyle = _ctxValue$restProps.labelStyle,
contentStyle = _ctxValue$restProps.contentStyle,
_ctxValue$restProps$t = _ctxValue$restProps.tooltip,
tooltip = _ctxValue$restProps$t === void 0 ? true : _ctxValue$restProps$t;
var _ref3 = ellipsis || {},
_ref3$rows = _ref3.rows,
rows = _ref3$rows === void 0 ? 1 : _ref3$rows;
return /*#__PURE__*/React.createElement("div", {
className: "sd-desc-item"
}, label !== false && /*#__PURE__*/React.createElement("div", {
className: "desc-label",
style: labelStyle
}, label), split !== false && /*#__PURE__*/React.createElement("span", {
className: "desc-split"
}, split), /*#__PURE__*/React.createElement("div", {
className: cls('desc-content', ellipsis && !isTip && 'desc-ellipsis'),
style: _objectSpread(_objectSpread({}, contentStyle), ellipsis ? {
WebkitLineClamp: rows
} : {})
}, tooltip && !wholeTip ? /*#__PURE__*/React.createElement(Tooltip, _extends({
placement: "topLeft",
title: getTip(tooltip, children)
}, tooltip), children) : children));
}
FieldDesc.Item = Item;
export default FieldDesc;