Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tabSelectedClassName property for item #111

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function getTabs() {
/* Optional parameters */
key: index,
tabClassName: 'tab',
tabSelectedClassName: 'tab--selected',
panelClassName: 'panel',
}));
}
Expand All @@ -84,35 +85,37 @@ render(<App />, document.getElementById('root'));

All entities listed below should be used as props to the `Tabs` component.

| Prop | Type | Description | Default |
| ---------------- | ------------- | ---------------------------------------------------------------------------- | --------------- |
| items | Array | Tabs data | [Item](#Item)[] |
| beforeChange | Function | Fires right before a tab changes. Return `false` to prevent the tab change | undefined |
| onChange | Function | onChange callback | undefined |
| selectedTabKey | Number/String | Selected tab | undefined |
| showMore | Bool | Whether to show `Show more` or not | `true` |
| showMoreLabel | String/Node | `Show more` tab name | `...` |
| transform | Bool | Transform to accordion when the wrapper width is less than `transformWidth`. | `true` |
| transformWidth | Number | Transform width. | 800 |
| unmountOnExit | Bool | Whether to unmount inactive tabs from DOM tree or not | `true` |
| tabsWrapperClass | String | Wrapper class | undefined |
| tabClassName | String | Tab class | undefined |
| panelClassName | String | Tab panel class | undefined |
| allowRemove | Bool | Allows tabs removal. | `false` |
| removeActiveOnly | Bool | Only active tab has removal option | `false` |
| showInkBar | Bool | Add MaterialUI InkBar effect | `false` |
| uid | any | An optional external id. The component rerenders when it changes | undefined |
| Prop | Type | Description | Default |
| ---------------- | ------------- | ---------------------------------------------------------------------------- | --------------- |
| items | Array | Tabs data | [Item](#Item)[] |
| beforeChange | Function | Fires right before a tab changes. Return `false` to prevent the tab change | undefined |
| onChange | Function | onChange callback | undefined |
| selectedTabKey | Number/String | Selected tab | undefined |
| showMore | Bool | Whether to show `Show more` or not | `true` |
| showMoreLabel | String/Node | `Show more` tab name | `...` |
| transform | Bool | Transform to accordion when the wrapper width is less than `transformWidth`. | `true` |
| transformWidth | Number | Transform width. | 800 |
| unmountOnExit | Bool | Whether to unmount inactive tabs from DOM tree or not | `true` |
| tabsWrapperClass | String | Wrapper class | undefined |
| tabClassName | String | Tab class | undefined |
| tabSelectedClassName | String | Tab class for selected item | undefined |
| panelClassName | String | Tab panel class | undefined |
| allowRemove | Bool | Allows tabs removal. | `false` |
| removeActiveOnly | Bool | Only active tab has removal option | `false` |
| showInkBar | Bool | Add MaterialUI InkBar effect | `false` |
| uid | any | An optional external id. The component rerenders when it changes | undefined |

#### Item

| Prop | Type | Description |
| -------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| title | String | Tab title |
| getContent | Function | A function that returns data that will be rendered when tab become active |
| content | String | Use this prop insted of getContent. This is a sync version of `getContent`. The data will be always rendered in a hidden div. Sometimes it may be useful for SEO |
| key | Number | A uniq tab id |
| tabClassName | String | Tab class name |
| panelClassName | String | Panel class name |
| title | String | Tab title |
| getContent | Function | A function that returns data that will be rendered when tab become active |
| content | String | Use this prop insted of getContent. This is a sync version of `getContent`. The data will be always rendered in a hidden div. Sometimes it may be useful for SEO |
| key | Number | A uniq tab id |
| tabClassName | String | Tab class name |
| tabSelectedClassName | String | Tab class name for selected item |
| panelClassName | String | Panel class name |

### License

Expand Down
12 changes: 8 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export default class Tabs extends Component {

return items.reduce(
(result, item, index) => {
const { key = index, title, content, getContent, disabled, tabClassName, panelClassName } = item;
const { key = index, title, content, getContent, disabled, tabClassName, tabSelectedClassName, panelClassName } = item;

const selected = selectedTabKey === key;
const payload = { tabIndex, collapsed, selected, disabled, key };
Expand All @@ -182,6 +182,7 @@ export default class Tabs extends Component {
},
allowRemove: allowRemove && (!removeActiveOnly || selected),
className: tabClassName,
classNameSelected: tabSelectedClassName,
};

const panelPayload = {
Expand Down Expand Up @@ -224,7 +225,7 @@ export default class Tabs extends Component {
);
};

getTabProps = ({ title, key, selected, collapsed, tabIndex, disabled, className, onRemove, allowRemove }) => ({
getTabProps = ({ title, key, selected, collapsed, tabIndex, disabled, className, classNameSelected, onRemove, allowRemove }) => ({
selected,
allowRemove,
children: title,
Expand All @@ -243,6 +244,7 @@ export default class Tabs extends Component {
tabIndex,
disabled,
className,
classNameSelected
}),
});

Expand All @@ -263,13 +265,13 @@ export default class Tabs extends Component {
hasChildSelected: isSelectedTabHidden,
});

getClassNamesFor = (type, { selected, collapsed, tabIndex, disabled, className = '', isHidden }) => {
getClassNamesFor = (type, { selected, collapsed, tabIndex, disabled, className = '', classNameSelected = '', isHidden }) => {
const { tabClass, panelClass } = this.props;
switch (type) {
case 'tab':
return cs('RRT__tab', className, tabClass, {
'RRT__tab--first': !tabIndex,
'RRT__tab--selected': selected,
['RRT__tab--selected' + (classNameSelected.length ? ' ' + classNameSelected : '')]: selected,
'RRT__tab--disabled': disabled,
'RRT__tab--collapsed': collapsed,
});
Expand Down Expand Up @@ -425,6 +427,7 @@ Tabs.propTypes = {
containerClass: PropTypes.string,
tabsWrapperClass: PropTypes.string,
tabClass: PropTypes.string,
tabClassSelected: PropTypes.string,
panelClass: PropTypes.string,
// optional external id. Force rerender when it changes
// eslint-disable-next-line react/forbid-prop-types
Expand All @@ -447,6 +450,7 @@ Tabs.defaultProps = {
containerClass: undefined,
tabsWrapperClass: undefined,
tabClass: undefined,
tabClassSelected: undefined,
panelClass: undefined,
showMoreLabel: '...',
unmountOnExit: true,
Expand Down