UNPKG

@alifd/meet-react

Version:

Fusion Mobile React UI System Component

152 lines 7.27 kB
import { rpx2vw4style as __rpx2vw__ } from "@alifd/babel-runtime-jsx-style-transform"; 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 classNames from 'classnames'; import React, { createContext, createElement, forwardRef, useEffect, useRef, useState, Children, cloneElement } from "react"; import { ScrollView } from "@alifd/meet-react-component-one"; import { useGuid } from '../utils/hooks'; import View from '../view'; import SideBarItem from './item'; export var SideBarContext = /*#__PURE__*/createContext({ activeKey: -1, align: 'left', change: function change() {} }); var getInitActiveKey = function getInitActiveKey(props) { if ('activeKey' in props && props.activeKey) { return props.activeKey; } else if (props.defaultActiveKey || props.defaultActiveKey === 0) { return props.defaultActiveKey; } else { return undefined; } }; var SideBar = function SideBar(props, ref) { var _props$prefix = props.prefix, prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix, _props$align = props.align, align = _props$align === void 0 ? 'left' : _props$align, activeKey = props.activeKey, _props$defaultActiveK = props.defaultActiveKey, defaultActiveKey = _props$defaultActiveK === void 0 ? -1 : _props$defaultActiveK, _props$onChange = props.onChange, onChange = _props$onChange === void 0 ? function () {} : _props$onChange, _props$dataSource = props.dataSource, dataSource = _props$dataSource === void 0 ? [] : _props$dataSource, height = props.height, width = props.width, className = props.className, _props$renderContent = props.renderContent, renderContent = _props$renderContent === void 0 ? null : _props$renderContent, contentClassName = props.contentClassName, contentStyle = props.contentStyle, renderSideBar = props.renderSideBar, children = props.children, others = __rest(props, ["prefix", "align", "activeKey", "defaultActiveKey", "onChange", "dataSource", "height", "width", "className", "renderContent", "contentClassName", "contentStyle", "renderSideBar", "children"]); var _useState = useState(getInitActiveKey(props)), _useState2 = _slicedToArray(_useState, 2), curActiveKey = _useState2[0], setActiveKey = _useState2[1]; var curSideBarScrollId = useGuid('side-bar-scroll-'); var curSideBarScrollContainerId = useGuid('side-bar-ctn-'); var activeBarRef = useRef(); var isControlled = ('activeKey' in props); var clsPrefix = "".concat(prefix, "side-bar"); var clses = { container: classNames(["".concat(clsPrefix, "-container")]), bar: classNames([clsPrefix, className]), scrollContainer: "".concat(clsPrefix, "-scroll-container"), scrollContent: "".concat(clsPrefix, "-scroll-content"), content: classNames("".concat(clsPrefix, "-content"), contentClassName) }; useEffect(function () { if (isControlled && curActiveKey && !props.activeKey) { setActiveKey(getInitActiveKey(props)); return; } if (isControlled) { setActiveKey(props.activeKey); } }); var content; if (dataSource && dataSource.length > 0) { content = dataSource.map(function (item, index) { return /*#__PURE__*/React.createElement(SideBarItem, _extends({ key: item.key || index, itemKey: item.key, renderContent: renderSideBar, ref: item.key === curActiveKey ? activeBarRef : null, prefix: prefix }, item)); }); } else { content = Children.map(children, function (child, index) { return cloneElement(child, { key: "mt_side_bar_".concat(index), itemKey: child.key || index, ref: "".concat(child.key) === "".concat(curActiveKey) ? activeBarRef : null, renderContent: child.renderContent || renderSideBar, prefix: prefix }); }); } var handleChange = function handleChange(v) { if (!isControlled) { setActiveKey(v); } onChange(v); }; var getChildren = function getChildren(activeBarKey) { var ret = null; Children.forEach(children, function (child) { if (ret) { return; } var _key = child.key; if ("".concat(_key) === "".concat(activeBarKey)) { ret = child.props.children; } }); return ret; }; return /*#__PURE__*/React.createElement(SideBarContext.Provider, { value: { activeKey: curActiveKey, align: align, change: handleChange } }, /*#__PURE__*/React.createElement(View, { className: clses.container }, /*#__PURE__*/React.createElement(View, _extends({}, others, { className: clses.bar, style: __rpx2vw__({ width: width, maxWidth: width }), ref: ref }), /*#__PURE__*/React.createElement(ScrollView, { id: curSideBarScrollId, className: clses.scrollContainer, showsVerticalScrollIndicator: false, showsHorizontalScrollIndicator: false, style: __rpx2vw__({ height: height, width: width, maxWidth: width }) }, /*#__PURE__*/React.createElement(View, { id: curSideBarScrollContainerId, className: clses.scrollContent }, content))), /*#__PURE__*/React.createElement(View, { className: clses.content, style: __rpx2vw__(contentStyle) }, renderContent || getChildren(curActiveKey)))); }; SideBar.displayName = 'SideBar'; export default /*#__PURE__*/forwardRef(SideBar);