tntd
Version:
tntd是基于 TNT Design 设计体系的 React UI 组件库,主要用于研发企业级中后台产品。
173 lines (172 loc) • 11.1 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _useMergedState = _interopRequireDefault(require("rc-util/lib/hooks/useMergedState"));
var _avatar = _interopRequireDefault(require("../avatar"));
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _actions = _interopRequireDefault(require("../actions"));
var _group = _interopRequireWildcard(require("./group"));
require("./index.less");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
var __rest = void 0 && (void 0).__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;
};
var CheckCard = function CheckCard(props) {
var _a;
var _useMountMergeState = (0, _useMergedState["default"])(props.defaultChecked || false, {
value: props.checked,
onChange: props.onChange
}),
_useMountMergeState2 = _slicedToArray(_useMountMergeState, 2),
stateChecked = _useMountMergeState2[0],
setStateChecked = _useMountMergeState2[1];
var checkCardGroup = (0, _react.useContext)(_group.CheckCardGroupConnext);
var handleClick = function handleClick(e) {
var _a, _b;
(_a = props === null || props === void 0 ? void 0 : props.onClick) === null || _a === void 0 ? void 0 : _a.call(props, e);
var newChecked = !stateChecked;
(_b = checkCardGroup === null || checkCardGroup === void 0 ? void 0 : checkCardGroup.toggleOption) === null || _b === void 0 ? void 0 : _b.call(checkCardGroup, {
value: props.value
});
setStateChecked === null || setStateChecked === void 0 ? void 0 : setStateChecked(newChecked, e);
};
// small => sm large => lg
var getSizeCls = function getSizeCls(size) {
if (size === 'large') return 'lg';
if (size === 'small') return 'sm';
return '';
};
(0, _react.useEffect)(function () {
var _a;
(_a = checkCardGroup === null || checkCardGroup === void 0 ? void 0 : checkCardGroup.registerValue) === null || _a === void 0 ? void 0 : _a.call(checkCardGroup, props.value);
return function () {
var _a;
return (_a = checkCardGroup === null || checkCardGroup === void 0 ? void 0 : checkCardGroup.cancelValue) === null || _a === void 0 ? void 0 : _a.call(checkCardGroup, props.value);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.value]);
/**
* 头像自定义
*
* @param prefixCls
* @param cover
* @returns
*/
var renderCover = function renderCover(prefixCls, cover) {
return _react["default"].createElement("div", {
className: "".concat(prefixCls, "-cover")
}, typeof cover === 'string' ? _react["default"].createElement("img", {
src: cover,
alt: "tntd-checkcard"
}) : cover);
};
var customizePrefixCls = props.prefixCls,
className = props.className,
avatar = props.avatar,
title = props.title,
description = props.description,
cover = props.cover,
extra = props.extra,
_props$style = props.style,
style = _props$style === void 0 ? {} : _props$style,
others = __rest(props, ["prefixCls", "className", "avatar", "title", "description", "cover", "extra", "style"]);
var checkCardProps = Object.assign({}, others);
var prefixCls = 'tntd-checkcard';
checkCardProps.checked = stateChecked;
var multiple = false;
if (checkCardGroup) {
// 受组控制模式
checkCardProps.disabled = props.disabled || checkCardGroup.disabled;
checkCardProps.loading = props.loading || checkCardGroup.loading;
checkCardProps.bordered = props.bordered || checkCardGroup.bordered;
multiple = checkCardGroup.multiple;
var isChecked = checkCardGroup.multiple ? (_a = checkCardGroup.value) === null || _a === void 0 ? void 0 : _a.includes(props.value) : checkCardGroup.value === props.value;
// loading时check为false
checkCardProps.checked = checkCardProps.loading ? false : isChecked;
checkCardProps.size = props.size || checkCardGroup.size;
}
var _checkCardProps$disab = checkCardProps.disabled,
disabled = _checkCardProps$disab === void 0 ? false : _checkCardProps$disab,
size = checkCardProps.size,
cardLoading = checkCardProps.loading,
_checkCardProps$borde = checkCardProps.bordered,
bordered = _checkCardProps$borde === void 0 ? true : _checkCardProps$borde,
checked = checkCardProps.checked;
var sizeCls = getSizeCls(size);
var classString = (0, _classnames["default"])(prefixCls, className, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefixCls, "-loading"), cardLoading), "".concat(prefixCls, "-").concat(sizeCls), sizeCls), "".concat(prefixCls, "-checked"), checked), "".concat(prefixCls, "-multiple"), multiple), "".concat(prefixCls, "-disabled"), disabled), "".concat(prefixCls, "-bordered"), bordered), "".concat(prefixCls, "-ghost"), props.ghost));
var metaDom = (0, _react.useMemo)(function () {
if (cardLoading) {
return _react["default"].createElement(_group.CardLoading, {
prefixCls: prefixCls || ''
});
}
if (cover) {
return renderCover(prefixCls || '', cover);
}
var avatarDom = avatar ? _react["default"].createElement("div", {
className: "".concat(prefixCls, "-avatar")
}, typeof avatar === 'string' ? _react["default"].createElement(_avatar["default"], {
size: 48,
shape: "square",
src: avatar
}) : avatar) : null;
var headerDom = (title !== null && title !== void 0 ? title : extra) != null && _react["default"].createElement("div", {
className: "".concat(prefixCls, "-header")
}, _react["default"].createElement("div", {
className: "".concat(prefixCls, "-header-left")
}, _react["default"].createElement("div", {
className: "".concat(prefixCls, "-title")
}, title), props.subTitle ? _react["default"].createElement("div", {
className: "".concat(prefixCls, "-subTitle")
}, props.subTitle) : null), extra && _react["default"].createElement("div", {
className: "".concat(prefixCls, "-extra")
}, extra));
var descriptionDom = description ? _react["default"].createElement("div", {
className: "".concat(prefixCls, "-description")
}, description) : null;
var metaClass = (0, _classnames["default"])("".concat(prefixCls, "-content"), _defineProperty({}, "".concat(prefixCls, "-avatar-header"), avatarDom && headerDom && !descriptionDom));
return _react["default"].createElement("div", {
className: metaClass
}, avatarDom, headerDom || descriptionDom ? _react["default"].createElement("div", {
className: "".concat(prefixCls, "-detail")
}, headerDom, descriptionDom) : null);
}, [avatar, cardLoading, cover, description, extra, prefixCls, props.subTitle, title]);
return _react["default"].createElement("div", {
className: classString,
style: style,
onClick: function onClick(e) {
if (!cardLoading && !disabled) {
handleClick(e);
}
},
onMouseEnter: props.onMouseEnter
}, metaDom, props.children ? _react["default"].createElement("div", {
className: (0, _classnames["default"])("".concat(prefixCls, "-body")),
style: props.bodyStyle
}, props.children) : null, props.actions ? _react["default"].createElement(_actions["default"], {
actions: props.actions,
prefixCls: prefixCls
}) : null);
};
CheckCard.Group = _group["default"];
var _default = exports["default"] = CheckCard;