UNPKG

@alifd/meet-react

Version:

Fusion Mobile React UI System Component

82 lines 5.45 kB
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); } 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 _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(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); } import { __rest } from "tslib"; import React, { createElement, forwardRef, Fragment, useContext, isValidElement } from "react"; import classNames from 'classnames'; import { Text } from "@alifd/meet-react-component-one"; import View from '../view'; import Icon from '../icon'; import { TabContext } from './tab'; import { useGuid } from '../utils/hooks'; import { isString } from '../utils'; var TabItem = function TabItem(props, ref) { var _props$prefix = props.prefix, prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix, className = props.className, size = props.size, itemKey = props.itemKey, _props$title = props.title, title = _props$title === void 0 ? '' : _props$title, _props$icon = props.icon, icon = _props$icon === void 0 ? '' : _props$icon, children = props.children, _props$onClick = props.onClick, onClick = _props$onClick === void 0 ? function () {} : _props$onClick, _props$renderContent = props.renderContent, renderContent = _props$renderContent === void 0 ? null : _props$renderContent, others = __rest(props, ["prefix", "className", "size", "itemKey", "title", "icon", "children", "onClick", "renderContent"]); var _useContext = useContext(TabContext), activeKey = _useContext.activeKey, change = _useContext.change, fullWidth = _useContext.fullWidth, shape = _useContext.shape, activeLineType = _useContext.activeLineType; var active = "".concat(activeKey) === "".concat(itemKey); var clsPrefix = "".concat(prefix, "tab-item"); var curItemId = useGuid('tab-item-'); var clses = { tab: classNames(clsPrefix, "".concat(clsPrefix, "--").concat(size), "".concat(clsPrefix, "--").concat(shape), _defineProperty(_defineProperty(_defineProperty({}, "".concat(clsPrefix, "--active"), active), "".concat(clsPrefix, "--active-").concat(shape), active), "".concat(clsPrefix, "--fullwidth"), fullWidth), className), activeLine: classNames("".concat(clsPrefix, "-active-line"), "".concat(clsPrefix, "-active-line--").concat(activeLineType), "".concat(clsPrefix, "-active-line--").concat(activeLineType, "-").concat(size)), tabIcon: classNames("".concat(clsPrefix, "-icon"), "".concat(clsPrefix, "-icon--").concat(size), "".concat(clsPrefix, "-icon--").concat(shape), _defineProperty(_defineProperty({}, "".concat(clsPrefix, "-icon--active"), active), "".concat(clsPrefix, "-icon--active-").concat(shape), active)), tabText: classNames("".concat(clsPrefix, "-text"), "".concat(clsPrefix, "-text--").concat(size), "".concat(clsPrefix, "-text--").concat(shape), _defineProperty(_defineProperty(_defineProperty({}, "".concat(clsPrefix, "-text--active"), active), "".concat(clsPrefix, "-text--active-").concat(size), active && shape !== 'capsule'), "".concat(clsPrefix, "-text--active-").concat(shape), active)) }; var renderTitle = function renderTitle() { if (isValidElement(title)) { return /*#__PURE__*/React.createElement(View, null, title); } else if (isString(title)) { return /*#__PURE__*/React.createElement(Text, { numberOfLines: 1, className: clses.tabText }, title); } return null; }; var handleClick = function handleClick(e) { if (onClick) { onClick(Object.assign(Object.assign({}, e), { itemKey: itemKey })); } change(itemKey); }; return /*#__PURE__*/React.createElement(View, _extends({}, others, { id: curItemId, ref: ref, className: clses.tab, onClick: handleClick }), renderContent ? renderContent({ title: title, icon: icon, active: active }) : /*#__PURE__*/React.createElement(Fragment, null, icon ? /*#__PURE__*/React.createElement(Icon, { className: clses.tabIcon, name: icon }) : null, renderTitle()), shape !== 'capsule' && /*#__PURE__*/React.createElement(View, { className: classNames(clses.activeLine, _defineProperty(_defineProperty({}, "".concat(clsPrefix, "-active-line--enter"), active), "".concat(clsPrefix, "-active-line--exit"), !active)) })); }; TabItem.displayName = 'Tab.Item'; export default /*#__PURE__*/forwardRef(TabItem);