baseui
Version:
A React Component library implementing the Base design language
129 lines (127 loc) • 5.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var React = _interopRequireWildcard(require("react"));
var _overrides = require("../helpers/overrides");
var _styledComponents = require("./styled-components");
var _constants = require("./constants");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (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 && Object.prototype.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 _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); } /*
Copyright (c) Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
class Tabs extends React.Component {
onChange({
activeKey
}) {
const {
onChange
} = this.props;
typeof onChange === 'function' && onChange({
activeKey
});
}
getTabs() {
const {
activeKey,
disabled,
orientation,
children,
overrides = {}
} = this.props;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const tabs = React.Children.map(children, (child, index) => {
if (!child) return;
const key = child.key || String(index);
return /*#__PURE__*/React.cloneElement(child, {
key,
id: key,
// for aria-labelledby
active: key === activeKey,
disabled: disabled || child.props.disabled,
$orientation: orientation,
onSelect: () => this.onChange({
activeKey: key
}),
children: child.props.title,
overrides: (0, _overrides.mergeOverrides)(overrides, child.props.overrides || {})
});
});
return tabs;
}
getPanels() {
const {
activeKey,
disabled,
orientation,
children,
overrides = {},
renderAll
} = this.props;
const {
TabContent: TabContentOverride
} = overrides;
const [TabContent, tabContentProps] = (0, _overrides.getOverrides)(TabContentOverride, _styledComponents.TabContent);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const tabs = React.Children.map(children, (child, index) => {
if (!child) return;
const key = child.key || String(index);
const isActive = key === activeKey;
const props = {
key,
'aria-labelledby': key
};
const sharedProps = {
$active: isActive,
$disabled: disabled,
$orientation: orientation
};
return /*#__PURE__*/React.createElement(TabContent, _extends({
role: "tabpanel"
}, sharedProps, tabContentProps, props), renderAll ? child.props.children : null, isActive && !renderAll ? child.props.children : null);
});
return tabs;
}
getSharedProps() {
const {
disabled,
orientation
} = this.props;
return {
$disabled: disabled,
$orientation: orientation
};
}
render() {
const sharedProps = this.getSharedProps();
const {
overrides = {}
} = this.props;
const {
Root: RootOverride,
TabBar: TabBarOverride
} = overrides;
const [Root, rootProps] = (0, _overrides.getOverrides)(RootOverride, _styledComponents.Root);
const [TabBar, tabBarProps] = (0, _overrides.getOverrides)(TabBarOverride, _styledComponents.TabBar);
return /*#__PURE__*/React.createElement(Root, _extends({
"data-baseweb": "tabs"
}, sharedProps, rootProps), /*#__PURE__*/React.createElement(TabBar, _extends({
role: "tablist"
}, sharedProps, tabBarProps), this.getTabs()), this.getPanels());
}
}
exports.default = Tabs;
_defineProperty(Tabs, "defaultProps", {
disabled: false,
onChange: () => {},
overrides: {},
orientation: _constants.ORIENTATION.horizontal,
renderAll: false
});