lucid-ui
Version:
A UI component library from AppNexus.
186 lines (185 loc) • 7.95 kB
JavaScript
import _ from 'lodash';
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';
const cx = lucidClassNames.bind('&-Tabs');
const { bool, func, node, number, string, any } = PropTypes;
const Title = (props) => null;
Title.peek = {
description: `Titles can be provided as a child or prop to a Tab.`,
};
Title.displayName = 'Tabs.Title';
Title.propName = 'Title';
const Tab = (props) => {
const { isDisabled, isLastTab, isOpen, isProgressive, isSelected, Title, className, count, isVariableCountWidth, index = 0, // this defaults should not be invoked unless someone is using `Tab` outside of `Tabs`
onSelect = _.noop, // this defaults should not be invoked unless someone is using `Tab` outside of `Tabs`
...passThroughs } = props;
const handleClick = (event) => {
if (!isDisabled) {
onSelect(index, props, event);
}
};
return (React.createElement("li", Object.assign({ 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))),
React.createElement("span", { className: cx('&-Tab-content') },
Title,
!_.isNil(count) && (React.createElement(Badge, { style: {
marginLeft: '12px',
width: isVariableCountWidth ? undefined : '20px',
minWidth: '20px',
}, type: 'stroke', kind: isSelected ? 'primary' : undefined }, count))),
isProgressive && !isLastTab && (React.createElement("span", { className: cx('&-Tab-arrow') },
React.createElement("svg", { className: cx('&-Tab-arrow-svg'), viewBox: '0 0 8 37', preserveAspectRatio: 'none' },
React.createElement("polyline", { className: cx('&-Tab-arrow-tab-line'), fill: '#fff', points: '0,0 1,1 0,1' }),
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: `
Content that will be rendered in a tab. Be sure to nest a Title inside
each Tab or provide it as a prop. Props other than \`isDisabled\`,
\`isSelected\`, and \`Title\` can be inferred from the parent \`Tabs\`
component, but directly provided \`props\` will take precedence.
`,
};
Tab.displayName = 'Tabs.Tab';
Tab.propName = 'Tab';
Tab.propTypes = {
className: string `
Class names that are appended to the defaults.
`,
index: number `
The index of this \`Tab\` within the list of \`Tabs\`.
`,
isDisabled: bool `
Styles a \`Tab\` as disabled. This is typically used with
\`isProgressive\` to disable steps that have not been completed and
should not be selected until the current step has been completed.
`,
isLastTab: bool `
If \`true\`, this \`Tab\` is the last \`Tab\` in the list of \`Tabs\`.
`,
isOpen: bool `
If \`true\` then the active \`Tab\` will appear open on the bottom.
`,
isProgressive: bool `
If \`true\`, the \`Tab\` will appear as a \`Progressive\` tab.
`,
isSelected: bool `
If \`true\`, the \`Tab\` will appear selected.
`,
onSelect: func `
Callback for when the user clicks a \`Tab\`. Called with the index of the
\`Tab\` that was clicked.
`,
Title: node `
The content to be rendered as the \`Title\` of the \`Tab\`.
`,
count: number `
Optional prop that will show a count number next to the tab's title.
`,
isVariableCountWidth: bool `
Defaults to false.
Allows the count bubble to grow large. Useful if working with huge numbers.
`,
};
class Tabs extends React.Component {
constructor() {
super(...arguments);
this.handleClicked = (index, tabProps, event) => {
const { onSelect } = this.props;
onSelect(index, { event, props: tabProps });
};
}
render() {
const { className, hasMultilineTitle, isOpen, isProgressive, selectedIndex, hasFullWidthTabs, isFloating, ...passThroughs } = this.props;
// Grab props array from each Tab
const tabChildProps = _.map(findTypes(this.props, Tab), 'props');
const selectedIndexFromChildren = _.findLastIndex(tabChildProps, {
isSelected: true,
});
const actualSelectedIndex = selectedIndexFromChildren !== -1
? selectedIndexFromChildren
: selectedIndex;
return (React.createElement("div", Object.assign({}, omitProps(passThroughs, undefined, _.keys(Tabs.propTypes)), { className: cx('&', className) }),
React.createElement("ul", { className: cx('&-bar', {
'&-bar-is-multiline': hasMultilineTitle,
'&-variable-width': !hasFullWidthTabs,
'&-floating': isFloating,
}) }, _.map(tabChildProps, (tabProps, index) => (React.createElement(Tab, Object.assign({ key: index, index: index, isLastTab: index === tabChildProps.length - 1, isOpen: isOpen, isProgressive: isProgressive, isSelected: index === actualSelectedIndex, onSelect: this.handleClicked, Title: _.get(getFirst(tabProps, Tabs.Title), 'props.children', '') }, tabProps))))),
React.createElement("div", { className: cx('&-content') }, _.get(tabChildProps, [
_.isUndefined(actualSelectedIndex) ? '' : actualSelectedIndex,
'children',
]))));
}
}
Tabs.displayName = 'Tabs';
Tabs.Title = Title;
Tabs.Tab = Tab;
Tabs.reducers = reducers;
Tabs.peek = {
description: `
\`Tabs\` provides tabbed navigation. It has a flexible interface that
allows tab content to be passed as regular React children or through
props.
`,
categories: ['navigation'],
};
Tabs.propTypes = {
className: string `
Class names that are appended to the defaults.
`,
selectedIndex: number `
Indicates which of the \`Tabs.Tab\` children is currently selected. The
index of the last \`Tabs.Tab\` child with \`isSelected\` equal to
\`true\` takes precedence over this prop.
`,
onSelect: func `
Callback for when the user clicks a tab. Called with the index of the tab
that was clicked.
`,
isOpen: bool `
If \`true\` then the active tab will appear open on the bottom.
`,
isProgressive: bool `
Style the tabs as a progression. This is typically used for a work flow
where the user needs to move forward and backward through a series of
steps.
`,
isFloating: bool `
Provides a small bottom border that offers a barrier between the tab
group and the rest of the page.
Useful if the tabs are not anchored to anything.
`,
hasMultilineTitle: bool `
Set the multiline className. This is typically used for styling the
Tab.Title bar for improved readability when there are multiple React
elements in the tab headers.
`,
hasFullWidthTabs: bool `
If \`true\` the width will be evenly distributed to all tabs. \`False\`
typically used in conjunction with \`Tab.width\`
`,
Tab: any `
*Child Element* Can be used to define one or more individual \`Tab\`s in
the sequence of \`Tabs\`.
`,
};
Tabs.defaultProps = {
selectedIndex: 0,
onSelect: _.noop,
isOpen: true,
isProgressive: false,
isFloating: false,
hasMultilineTitle: false,
hasFullWidthTabs: false,
};
export default buildModernHybridComponent(Tabs, { reducers });
export { Tabs as TabsDumb };