Skip to content

Commit

Permalink
Allow passing through custom attributes (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
Martijn Hols authored and danez committed Jun 11, 2016
1 parent 0c82a19 commit a387f9d
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"presets": [
"es2015",
"react"
"react",
"stage-1"
]
}
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"extends": "airbnb",
"parser": "babel-eslint",
"env": {
"browser": true,
"node": true
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@
"devDependencies": {
"babel-cli": "^6.9.0",
"babel-core": "^6.9.1",
"babel-eslint": "^6.0.4",
"babel-jest": "^12.1.0",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-1": "^6.5.0",
"cross-env": "^1.0.8",
"enzyme": "^2.3.0",
"eslint": "^2.11.1",
Expand Down
21 changes: 12 additions & 9 deletions src/components/Tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,27 @@ module.exports = React.createClass({
},

render() {
const { selected, disabled, panelId, className, children, id, ...attributes } = this.props;

return (
<li
{...attributes}
className={cx(
'ReactTabs__Tab',
this.props.className,
className,
{
'ReactTabs__Tab--selected': this.props.selected,
'ReactTabs__Tab--disabled': this.props.disabled,
'ReactTabs__Tab--selected': selected,
'ReactTabs__Tab--disabled': disabled,
}
)}
role="tab"
id={this.props.id}
aria-selected={this.props.selected ? 'true' : 'false'}
aria-expanded={this.props.selected ? 'true' : 'false'}
aria-disabled={this.props.disabled ? 'true' : 'false'}
aria-controls={this.props.panelId}
id={id}
aria-selected={selected ? 'true' : 'false'}
aria-expanded={selected ? 'true' : 'false'}
aria-disabled={disabled ? 'true' : 'false'}
aria-controls={panelId}
>
{this.props.children}
{children}
</li>
);
},
Expand Down
7 changes: 5 additions & 2 deletions src/components/TabList.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ module.exports = React.createClass({
},

render() {
const { className, children, ...attributes } = this.props;

return (
<ul
{...attributes}
className={cx(
'ReactTabs__TabList',
this.props.className
className
)}
role="tablist"
>
{this.props.children}
{children}
</ul>
);
},
Expand Down
17 changes: 8 additions & 9 deletions src/components/TabPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,24 @@ module.exports = React.createClass({
},

render() {
const children = (this.context.forceRenderTabPanel || this.props.selected) ?
this.props.children :
null;
const { className, children, selected, id, tabId, ...attributes } = this.props;

return (
<div
{...attributes}
className={cx(
'ReactTabs__TabPanel',
this.props.className,
className,
{
'ReactTabs__TabPanel--selected': this.props.selected,
'ReactTabs__TabPanel--selected': selected,
}
)}
role="tabpanel"
id={this.props.id}
aria-labelledby={this.props.tabId}
style={{ display: this.props.selected ? null : 'none' }}
id={id}
aria-labelledby={tabId}
style={{ display: selected ? null : 'none' }}
>
{children}
{(this.context.forceRenderTabPanel || selected) ? children : null}
</div>
);
},
Expand Down
14 changes: 13 additions & 1 deletion src/components/Tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,24 @@ module.exports = React.createClass({
}, 0);
}

const { className, ...attributes } = this.props;

// Delete all known props, so they don't get added to DOM
delete attributes.selectedIndex;
delete attributes.onSelect;
delete attributes.focus;
delete attributes.children;
delete attributes.forceRenderTabPanel;
delete attributes.onClick;
delete attributes.onKeyDown;

return (
<div
{...attributes}
className={cx(
'ReactTabs',
'react-tabs',
this.props.className
className
)}
onClick={this.handleClick}
onKeyDown={this.handleKeyDown}
Expand Down
13 changes: 13 additions & 0 deletions src/components/__tests__/Tab-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,17 @@ describe('<Tab />', () => {
expect(wrapper.hasClass('ReactTabs__Tab--disabled')).toBe(true);
expect(wrapper.prop('aria-disabled')).toBe('true');
});

it('should pass through custom properties', () => {
const wrapper = shallow(<Tab data-tooltip="Tooltip contents" />);

expect(wrapper.prop('data-tooltip')).toBe('Tooltip contents');
});

it('should not allow overriding all default properties', () => {
// eslint-disable-next-line jsx-a11y/aria-role
const wrapper = shallow(<Tab role="micro-tab" />);

expect(wrapper.prop('role')).toBe('tab');
});
});
13 changes: 13 additions & 0 deletions src/components/__tests__/TabList-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,17 @@ describe('<TabList />', () => {
expect(wrapper.hasClass('ReactTabs__TabList')).toBe(true);
expect(wrapper.hasClass('foobar')).toBe(true);
});

it('should pass through custom properties', () => {
const wrapper = shallow(<TabList data-tooltip="Tooltip contents" />);

expect(wrapper.prop('data-tooltip')).toBe('Tooltip contents');
});

it('should not allow overriding all default properties', () => {
// eslint-disable-next-line jsx-a11y/aria-role
const wrapper = shallow(<TabList role="micro-tab" />);

expect(wrapper.prop('role')).toBe('tablist');
});
});
13 changes: 13 additions & 0 deletions src/components/__tests__/TabPanel-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,18 @@ describe('Tab', () => {
expect(wrapper.prop('style')).not.toBe(null);
expect(wrapper.prop('style').display).toBe(null);
});

it('should pass through custom properties', () => {
const wrapper = shallow(<TabPanel data-tooltip="Tooltip contents" />);

expect(wrapper.prop('data-tooltip')).toBe('Tooltip contents');
});

it('should not allow overriding all default properties', () => {
// eslint-disable-next-line jsx-a11y/aria-role
const wrapper = shallow(<TabPanel role="micro-tab" />);

expect(wrapper.prop('role')).toBe('tabpanel');
});
});

6 changes: 6 additions & 0 deletions src/components/__tests__/Tabs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,10 @@ describe('react-tabs', () => {
assertTabSelected(innerTabs, 1);
});
});

it('should pass through custom properties', () => {
const wrapper = shallow(<Tabs data-tooltip="Tooltip contents" />);

expect(wrapper.prop('data-tooltip')).toBe('Tooltip contents');
});
});

0 comments on commit a387f9d

Please sign in to comment.