UNPKG

baseui

Version:

A React Component library implementing the Base design language

62 lines (60 loc) 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StatefulTabs = StatefulTabs; var React = _interopRequireWildcard(require("react")); var _tabs = require("./tabs"); 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); } /* 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. */ const getInitialState = (children, initialState) => { if (initialState && initialState.activeKey) { return initialState; } else { // @ts-ignore const firstKey = React.Children.map(children, // @ts-expect-error todo(flow->ts) child might be not a ReactElement, theoretically including null (child, index) => child.key || String(index))[0]; return { activeKey: firstKey }; } }; const defaultStateReducer = (state, action) => { if (action.type === _constants.STATE_CHANGE_TYPE.change) { return { activeKey: action.payload }; } return state; }; function StatefulTabs(props) { const { children, initialState, stateReducer = defaultStateReducer, onChange, ...restProps } = props; const [state, dispatch] = React.useReducer(stateReducer, getInitialState(children, initialState)); const handleChange = React.useCallback(params => { const { activeKey } = params; dispatch({ type: _constants.STATE_CHANGE_TYPE.change, payload: activeKey }); if (typeof onChange === 'function') onChange(params); }, []); return /*#__PURE__*/React.createElement(_tabs.Tabs, _extends({}, restProps, { activeKey: state.activeKey, onChange: handleChange }), children); }