lucid-ui
Version:
A UI component library from AppNexus.
258 lines (213 loc) • 11.9 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
import _isUndefined from "lodash/isUndefined";
import _get from "lodash/get";
import _findLastIndex from "lodash/findLastIndex";
import _map from "lodash/map";
import _isNil from "lodash/isNil";
import _keys from "lodash/keys";
import _noop from "lodash/noop";
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _extends() { _extends = Object.assign || 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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import { getFirst, findTypes, omitProps } from '../../util/component-types';
import { buildModernHybridComponent } from '../../util/state-management';
import * as reducers from './Tabs.reducers';
import Badge from '../Badge/Badge';
var cx = lucidClassNames.bind('&-Tabs');
var bool = PropTypes.bool,
func = PropTypes.func,
node = PropTypes.node,
number = PropTypes.number,
string = PropTypes.string,
any = PropTypes.any;
var Title = function Title(props) {
return null;
};
Title.peek = {
description: "Titles can be provided as a child or prop to a Tab."
};
Title.displayName = 'Tabs.Title';
Title.propName = 'Title';
var Tab = function Tab(props) {
var isDisabled = props.isDisabled,
isLastTab = props.isLastTab,
isOpen = props.isOpen,
isProgressive = props.isProgressive,
isSelected = props.isSelected,
Title = props.Title,
className = props.className,
count = props.count,
isVariableCountWidth = props.isVariableCountWidth,
_props$index = props.index,
index = _props$index === void 0 ? 0 : _props$index,
_props$onSelect = props.onSelect,
onSelect = _props$onSelect === void 0 ? _noop : _props$onSelect,
passThroughs = _objectWithoutProperties(props, ["isDisabled", "isLastTab", "isOpen", "isProgressive", "isSelected", "Title", "className", "count", "isVariableCountWidth", "index", "onSelect"]);
var handleClick = function handleClick(event) {
if (!isDisabled) {
onSelect(index, props, event);
}
};
return /*#__PURE__*/React.createElement("li", _extends({
className: cx('&-Tab', {
'&-Tab-is-active': isSelected,
'&-Tab-is-disabled': isDisabled,
'&-Tab-is-active-and-open': isOpen && isSelected,
'&-Tab-is-progressive': isProgressive && !isLastTab
}, className),
onClick: handleClick
}, omitProps(passThroughs, undefined, _keys(Tab.propTypes))), /*#__PURE__*/React.createElement("span", {
className: cx('&-Tab-content')
}, Title, !_isNil(count) && /*#__PURE__*/React.createElement(Badge, {
style: {
marginLeft: '12px',
width: isVariableCountWidth ? undefined : '20px',
minWidth: '20px'
},
type: "stroke",
kind: isSelected ? 'primary' : undefined
}, count)), isProgressive && !isLastTab && /*#__PURE__*/React.createElement("span", {
className: cx('&-Tab-arrow')
}, /*#__PURE__*/React.createElement("svg", {
className: cx('&-Tab-arrow-svg'),
viewBox: '0 0 8 37',
preserveAspectRatio: "none"
}, /*#__PURE__*/React.createElement("polyline", {
className: cx('&-Tab-arrow-tab-line'),
fill: "#fff",
points: "0,0 1,1 0,1"
}), /*#__PURE__*/React.createElement("polyline", {
className: cx('&-Tab-arrow-line'),
fill: "none",
stroke: "#fff",
strokeWidth: "1",
points: '0,37 7.3,18.5 0,0'
}))));
};
Tab.peek = {
description: "\n\t\t\t\tContent that will be rendered in a tab. Be sure to nest a Title inside\n\t\t\t\teach Tab or provide it as a prop. Props other than `isDisabled`,\n\t\t\t\t`isSelected`, and `Title` can be inferred from the parent `Tabs`\n\t\t\t\tcomponent, but directly provided `props` will take precedence.\n\t\t\t"
};
Tab.displayName = 'Tabs.Tab';
Tab.propName = 'Tab';
Tab.propTypes = {
className: string,
index: number,
isDisabled: bool,
isLastTab: bool,
isOpen: bool,
isProgressive: bool,
isSelected: bool,
onSelect: func,
Title: node,
count: number,
isVariableCountWidth: bool
};
var Tabs = /*#__PURE__*/function (_React$Component) {
_inherits(Tabs, _React$Component);
var _super = _createSuper(Tabs);
function Tabs() {
var _this;
_classCallCheck(this, Tabs);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "handleClicked", function (index, tabProps, event) {
var onSelect = _this.props.onSelect;
onSelect(index, {
event: event,
props: tabProps
});
});
return _this;
}
_createClass(Tabs, [{
key: "render",
value: function render() {
var _this2 = this;
var _this$props = this.props,
className = _this$props.className,
hasMultilineTitle = _this$props.hasMultilineTitle,
isOpen = _this$props.isOpen,
isProgressive = _this$props.isProgressive,
selectedIndex = _this$props.selectedIndex,
hasFullWidthTabs = _this$props.hasFullWidthTabs,
isFloating = _this$props.isFloating,
passThroughs = _objectWithoutProperties(_this$props, ["className", "hasMultilineTitle", "isOpen", "isProgressive", "selectedIndex", "hasFullWidthTabs", "isFloating"]); // Grab props array from each Tab
var tabChildProps = _map(findTypes(this.props, Tab), 'props');
var selectedIndexFromChildren = _findLastIndex(tabChildProps, {
isSelected: true
});
var actualSelectedIndex = selectedIndexFromChildren !== -1 ? selectedIndexFromChildren : selectedIndex;
return /*#__PURE__*/React.createElement("div", _extends({}, omitProps(passThroughs, undefined, _keys(Tabs.propTypes)), {
className: cx('&', className)
}), /*#__PURE__*/React.createElement("ul", {
className: cx('&-bar', {
'&-bar-is-multiline': hasMultilineTitle,
'&-variable-width': !hasFullWidthTabs,
'&-floating': isFloating
})
}, _map(tabChildProps, function (tabProps, index) {
return /*#__PURE__*/React.createElement(Tab, _extends({
key: index,
index: index,
isLastTab: index === tabChildProps.length - 1,
isOpen: isOpen,
isProgressive: isProgressive,
isSelected: index === actualSelectedIndex,
onSelect: _this2.handleClicked,
Title: _get(getFirst(tabProps, Tabs.Title), 'props.children', '')
}, tabProps));
})), /*#__PURE__*/React.createElement("div", {
className: cx('&-content')
}, _get(tabChildProps, [_isUndefined(actualSelectedIndex) ? '' : actualSelectedIndex, 'children'])));
}
}]);
return Tabs;
}(React.Component);
_defineProperty(Tabs, "displayName", 'Tabs');
_defineProperty(Tabs, "Title", Title);
_defineProperty(Tabs, "Tab", Tab);
_defineProperty(Tabs, "reducers", reducers);
_defineProperty(Tabs, "peek", {
description: "\n\t\t\t\t`Tabs` provides tabbed navigation. It has a flexible interface that\n\t\t\t\tallows tab content to be passed as regular React children or through\n\t\t\t\tprops.\n\t\t\t",
categories: ['navigation']
});
_defineProperty(Tabs, "propTypes", {
className: string,
selectedIndex: number,
onSelect: func,
isOpen: bool,
isProgressive: bool,
isFloating: bool,
hasMultilineTitle: bool,
hasFullWidthTabs: bool,
Tab: any
});
_defineProperty(Tabs, "defaultProps", {
selectedIndex: 0,
onSelect: _noop,
isOpen: true,
isProgressive: false,
isFloating: false,
hasMultilineTitle: false,
hasFullWidthTabs: false
});
export default buildModernHybridComponent(Tabs, {
reducers: reducers
});
export { Tabs as TabsDumb };