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

Advanced Title support and proper handling of children #27

Open
wants to merge 6 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
57 changes: 30 additions & 27 deletions dist/react-simpletabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,41 @@
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/

/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/

/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/

/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/

/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/

/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/

/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/


/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/

/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/

/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/

/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
Expand Down Expand Up @@ -84,10 +84,7 @@ return /******/ (function(modules) { // webpackBootstrap
onMount: React.PropTypes.func,
onBeforeChange: React.PropTypes.func,
onAfterChange: React.PropTypes.func,
children: React.PropTypes.oneOfType([
React.PropTypes.array,
React.PropTypes.element
]).isRequired
children: React.PropTypes.node.isRequired
},
getDefaultProps:function () {
return { tabActive: 1 };
Expand Down Expand Up @@ -142,16 +139,15 @@ return /******/ (function(modules) { // webpackBootstrap
throw new Error('Tabs must contain at least one Tabs.Panel');
}

if (!Array.isArray(this.props.children)) {
this.props.children = [this.props.children];
}
var $menuItems = React.Children
.map(this.props.children, function($panel, index) {
if (typeof $panel === 'function') {
$panel = $panel()
}

var $menuItems = this.props.children
.map(function($panel) {return typeof $panel === 'function' ? $panel() : $panel;})
.filter(function($panel) {return $panel;})
.map(function($panel, index) {
var ref = ("tab-menu-" + (index + 1));
var title = $panel.props.title;

var classes = classNames(
'tabs-menu-item',
this.state.tabActive === (index + 1) && 'is-active'
Expand All @@ -174,7 +170,13 @@ return /******/ (function(modules) { // webpackBootstrap
},
_getSelectedPanel:function () {
var index = this.state.tabActive - 1;
var $panel = this.props.children[index];
var $panel
React.Children.forEach(this.props.children, function ($item, i) {
if (index === i) {
$panel = $item;
return;
}
})

return (
React.createElement("article", {ref: "tab-panel", className: "tab-panel"},
Expand All @@ -187,11 +189,11 @@ return /******/ (function(modules) { // webpackBootstrap
Tabs.Panel = React.createClass({
displayName: 'Panel',
propTypes: {
title: React.PropTypes.string.isRequired,
children: React.PropTypes.oneOfType([
React.PropTypes.array,
title: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.element
]).isRequired
]).isRequired,
children: React.PropTypes.node.isRequired
},
render:function () {
return React.createElement("div", null, this.props.children);
Expand Down Expand Up @@ -252,3 +254,4 @@ return /******/ (function(modules) { // webpackBootstrap
/***/ }
/******/ ])
});
;
2 changes: 1 addition & 1 deletion dist/react-simpletabs.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions lib/__tests__/test-reactsimpletabs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ describe('Tabs', function() {
expect(panel.getDOMNode().children[0].innerHTML).toEqual('content1');
expect(menuItem.getDOMNode().children[0].innerHTML).toEqual('item1');
});
it('handles component titles', function() {
var instance = TU.renderIntoDocument(
<Tabs>
<Tabs.Panel title={<span><i>1</i>item</span>}>content1</Tabs.Panel>
<Tabs.Panel title={<span><i>2</i>item</span>}>content2</Tabs.Panel>
</Tabs>
);

var menuItem = TU.findRenderedDOMComponentWithClass(instance, 'tabs-menu-item is-active');
var panel = TU.findRenderedDOMComponentWithClass(instance, 'tab-panel');
var title = menuItem.getDOMNode().children[0].children[0];

expect(title.children[0].innerHTML).toEqual('1');
expect(title.children[1].innerHTML).toEqual('item');
});
});

describe('onBeforeChange', function(){
Expand Down
34 changes: 18 additions & 16 deletions lib/react-simpletabs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ var Tabs = React.createClass({
onMount: React.PropTypes.func,
onBeforeChange: React.PropTypes.func,
onAfterChange: React.PropTypes.func,
children: React.PropTypes.oneOfType([
React.PropTypes.array,
React.PropTypes.element
]).isRequired
children: React.PropTypes.node.isRequired
},
getDefaultProps () {
return { tabActive: 1 };
Expand Down Expand Up @@ -77,16 +74,15 @@ var Tabs = React.createClass({
throw new Error('Tabs must contain at least one Tabs.Panel');
}

if (!Array.isArray(this.props.children)) {
this.props.children = [this.props.children];
}
var $menuItems = React.Children
.map(this.props.children, ($panel, index) => {
if (typeof $panel === 'function') {
$panel = $panel()
}

var $menuItems = this.props.children
.map($panel => typeof $panel === 'function' ? $panel() : $panel)
.filter($panel => $panel)
.map(($panel, index) => {
var ref = `tab-menu-${index + 1}`;
var title = $panel.props.title;

var classes = classNames(
'tabs-menu-item',
this.state.tabActive === (index + 1) && 'is-active'
Expand All @@ -109,7 +105,13 @@ var Tabs = React.createClass({
},
_getSelectedPanel () {
var index = this.state.tabActive - 1;
var $panel = this.props.children[index];
var $panel
React.Children.forEach(this.props.children, function ($item, i) {
if (index === i) {
$panel = $item;
return;
}
})

return (
<article ref='tab-panel' className='tab-panel'>
Expand All @@ -122,11 +124,11 @@ var Tabs = React.createClass({
Tabs.Panel = React.createClass({
displayName: 'Panel',
propTypes: {
title: React.PropTypes.string.isRequired,
children: React.PropTypes.oneOfType([
React.PropTypes.array,
title: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.element
]).isRequired
]).isRequired,
children: React.PropTypes.node.isRequired
},
render () {
return <div>{this.props.children}</div>;
Expand Down