@nutui/nutui-react-taro
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
108 lines (107 loc) • 6.5 kB
JavaScript
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import React, { useContext, useEffect, useRef, useState } from "react";
import { Image, View } from "@tarojs/components";
import classNames from "classnames";
import { User } from "@nutui/icons-react-taro";
import { AvatarContext } from "../avatargroup/context";
import { ComponentDefaults } from "../../utils/typings";
import { harmony, td } from "../../utils/taro/platform";
import AvatarGroup from "../avatargroup/index";
import { pxTransform } from "../../utils/taro/px-transform";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
size: harmony() || td() ? '40' : '',
shape: 'round',
icon: '',
background: '#eee',
color: '#666',
fit: 'cover',
src: '',
alt: '',
avatarIndex: 0
});
var classPrefix = "nut-avatar";
export var Avatar = function(props) {
var _$_object_spread = _object_spread({}, defaultProps, props), children = _$_object_spread.children, size = _$_object_spread.size, shape = _$_object_spread.shape, background = _$_object_spread.background, color = _$_object_spread.color, src = _$_object_spread.src, icon = _$_object_spread.icon, fit = _$_object_spread.fit, avatarIndex = _$_object_spread.avatarIndex, className = _$_object_spread.className, style = _$_object_spread.style, onClick = _$_object_spread.onClick, onError = _$_object_spread.onError;
var _useState // 是否显示的最大头像个数
= _sliced_to_array(useState(false), 2), showMax = _useState[0], setShowMax = _useState[1];
var avatarRef = useRef(null);
var parent = useContext(AvatarContext);
var sizeValue = [
'large',
'normal',
'small'
];
var propAvatarGroup = parent.propAvatarGroup;
var groupSize = propAvatarGroup === null || propAvatarGroup === void 0 ? void 0 : propAvatarGroup.size;
var groupShape = propAvatarGroup === null || propAvatarGroup === void 0 ? void 0 : propAvatarGroup.shape;
var groupCount = propAvatarGroup === null || propAvatarGroup === void 0 ? void 0 : propAvatarGroup.avatarCount;
var groupMax = propAvatarGroup === null || propAvatarGroup === void 0 ? void 0 : propAvatarGroup.max;
var _obj;
var classes = classNames((_obj = {}, _define_property(_obj, "nut-avatar-".concat(groupSize || size || 'normal'), true), _define_property(_obj, "nut-avatar-".concat(groupShape || shape), true), _define_property(_obj, "nut-avatar-".concat(groupSize || size || 'normal', "-round"), shape === 'round' && true), _obj));
var nativeClasses = classNames(_define_property({}, "nut-avatar-first-child", avatarIndex === 1));
var cls = classNames(classPrefix, classes, className, nativeClasses);
var _obj1;
var styles = _object_spread((_obj1 = {
width: sizeValue.indexOf(size) > -1 ? '' : pxTransform(parseInt(size)),
height: sizeValue.indexOf(size) > -1 ? '' : pxTransform(parseInt(size)),
backgroundColor: "".concat(background),
color: color
}, _define_property(_obj1, harmony() ? 'marginRight' : 'marginLeft', avatarIndex !== 1 && (propAvatarGroup === null || propAvatarGroup === void 0 ? void 0 : propAvatarGroup.gap) ? "".concat(propAvatarGroup === null || propAvatarGroup === void 0 ? void 0 : propAvatarGroup.gap, "px") : ''), _define_property(_obj1, "zIndex", (propAvatarGroup === null || propAvatarGroup === void 0 ? void 0 : propAvatarGroup.level) === 'right' ? Math.abs(groupCount - avatarIndex) : ''), _obj1), style);
var maxStyles = {
backgroundColor: "".concat(propAvatarGroup === null || propAvatarGroup === void 0 ? void 0 : propAvatarGroup.maxBackground),
color: "".concat(propAvatarGroup === null || propAvatarGroup === void 0 ? void 0 : propAvatarGroup.maxColor)
};
useEffect(function() {
var maxCount = groupMax || groupCount;
if (avatarIndex === groupCount && avatarIndex !== maxCount && groupCount > maxCount) {
setShowMax(true);
}
}, [
avatarIndex,
groupCount
]);
var errorEvent = function() {
onError && onError();
};
var clickAvatar = function(e) {
onClick && onClick(e);
};
return /*#__PURE__*/ React.createElement(React.Fragment, null, (showMax || !groupMax || avatarIndex <= groupMax) && /*#__PURE__*/ React.createElement(View, {
className: cls,
style: !showMax ? styles : maxStyles,
onClick: clickAvatar,
ref: avatarRef
}, (!groupMax || avatarIndex <= groupMax) && /*#__PURE__*/ React.createElement(React.Fragment, null, src && /*#__PURE__*/ React.createElement(Image, {
className: "nut-avatar-img nut-avatar-".concat(groupSize || size || 'normal', "-img"),
src: src,
style: {
objectFit: fit
},
onError: errorEvent
}), /*#__PURE__*/ React.isValidElement(icon) ? /*#__PURE__*/ React.createElement(View, {
className: "nut-avatar-text"
}, /*#__PURE__*/ React.cloneElement(icon, _object_spread_props(_object_spread({}, icon.props), {
color: color,
className: "".concat(icon.props.className || '', " nut-avatar-icon nut-avatar-").concat(groupSize || size || 'normal', "-icon")
}))) : null, children && /*#__PURE__*/ React.createElement(View, {
style: {
color: color
},
className: "nut-avatar-text nut-avatar-".concat(groupSize || size || 'normal', "-text")
}, children), !src && !icon && !children && /*#__PURE__*/ React.createElement(View, {
className: "nut-avatar-text"
}, /*#__PURE__*/ React.createElement(User, {
style: {
color: color
},
className: "nut-avatar-icon nut-avatar-".concat(groupSize || size || 'normal', "-icon")
}))), showMax && /*#__PURE__*/ React.createElement(View, {
className: "nut-avatar-text nut-avatar-".concat(groupSize || 'normal', "-text"),
style: _object_spread({}, maxStyles)
}, (propAvatarGroup === null || propAvatarGroup === void 0 ? void 0 : propAvatarGroup.maxContent) ? propAvatarGroup === null || propAvatarGroup === void 0 ? void 0 : propAvatarGroup.maxContent : "+ ".concat(avatarIndex - Number(groupMax || 0)))));
};
Avatar.displayName = 'NutAvatar';
Avatar.Group = AvatarGroup;