UNPKG

@alifd/meet-react

Version:

Fusion Mobile React UI System Component

80 lines 4.48 kB
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); } 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(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(arr) { if (Array.isArray(arr)) return arr; } import { __rest } from "tslib"; import React, { createElement, forwardRef, useState, cloneElement } from "react"; import classNames from 'classnames'; import { Image } from "@alifd/meet-react-component-one"; import View from '../view'; import Icon from '../icon'; import { isStrictMiniApp, isString } from '../utils'; var Avatar = function Avatar(props, ref) { var _props$prefix = props.prefix, prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix, _props$size = props.size, size = _props$size === void 0 ? 'medium' : _props$size, _props$shape = props.shape, shape = _props$shape === void 0 ? 'circle' : _props$shape, src = props.src, alt = props.alt, srcSet = props.srcSet, className = props.className, _props$icon = props.icon, icon = _props$icon === void 0 ? null : _props$icon, _props$onError = props.onError, onError = _props$onError === void 0 ? function () {} : _props$onError, _props$onClick = props.onClick, onClick = _props$onClick === void 0 ? function () {} : _props$onClick, others = __rest(props, ["prefix", "size", "shape", "src", "alt", "srcSet", "className", "icon", "onError", "onClick"]); var children = props.children; var _useState = useState(true), _useState2 = _slicedToArray(_useState, 2), isImgExist = _useState2[0], setImgExist = _useState2[1]; var iconCls = "".concat(prefix, "avatar-icon--").concat(size); var handleError = function handleError(err) { if (!isStrictMiniApp) { onError(err); setImgExist(false); } }; if (src) { if (isImgExist) { var source = { uri: src }; children = /*#__PURE__*/React.createElement(Image, { src: src, source: source, srcSet: srcSet, alt: alt, onError: handleError }); } else if (alt) { children = isString(alt) ? alt.substr(0, 2) : children; } } else if (isString(icon)) { children = /*#__PURE__*/React.createElement(Icon, { className: iconCls, name: icon }); } else if (icon) { children = cloneElement(icon, { className: iconCls }); } else { children = isString(children) ? children.substr(0, 2) : children; } return /*#__PURE__*/React.createElement(View, _extends({ ref: ref }, others, { className: classNames(className, "".concat(prefix, "avatar"), "".concat(prefix, "avatar--").concat(size), "".concat(prefix, "avatar--").concat(shape)), onClick: onClick }), children); }; Avatar.displayName = 'Avatar'; export default /*#__PURE__*/forwardRef(Avatar);