Skip to content

Commit

Permalink
Made the tab system rely on URL hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
KittyGiraudel committed Dec 22, 2015
1 parent 2a0109b commit d03c866
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 33 deletions.
4 changes: 2 additions & 2 deletions components/panel-list/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ const TabList = React.createClass({
<div>
{sections.map((section, index) =>
<Panel
key={`panel-${index}`}
key={`panel-${section.id || index}`}
id={section.id || index}
visible={JS ? selectedIndex === index : true}>
isVisible={JS ? selectedIndex === index : true}>
{!JS
? <span>section.title</span>
: null}
Expand Down
8 changes: 4 additions & 4 deletions components/panel/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ const Panel = React.createClass({
React.PropTypes.string,
React.PropTypes.number
]).isRequired,
visible: React.PropTypes.bool
isVisible: React.PropTypes.bool
},

getDefaultProps () {
return {
visible: true
isVisible: true
};
},

render () {
const { id, visible, children } = this.props;
const { id, isVisible, children } = this.props;

return (
<div
role='tabpanel'
id={`panel-${id}`}
aria-labelledby={`tab-${id}`}
aria-hidden={!visible}>
aria-hidden={!isVisible}>
{children}
</div>
);
Expand Down
9 changes: 3 additions & 6 deletions components/tab-list/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Tab from '../tab';
const TabList = React.createClass({
propTypes: {
sections: React.PropTypes.array.isRequired,
handleClick: React.PropTypes.func.isRequired,
selectedIndex: React.PropTypes.number
},

Expand All @@ -15,17 +14,15 @@ const TabList = React.createClass({
},

render () {
const { sections, handleClick, selectedIndex } = this.props;
const { sections, selectedIndex } = this.props;

return (
<ul role='tablist'>
{sections.map((section, index) =>
<Tab
key={`tab-${index}`}
id={section.id || index}
key={`tab-${section.id || index}`}
selected={selectedIndex === index}
handleClick={handleClick}
index={index}>
id={section.id || index}>
{section.title}
</Tab>
)}
Expand Down
17 changes: 7 additions & 10 deletions components/tab/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,31 @@ import React from 'react';
const Tab = React.createClass({
propTypes: {
children: React.PropTypes.node.isRequired,
handleClick: React.PropTypes.func.isRequired,
index: React.PropTypes.number.isRequired,
id: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number
]).isRequired,
selected: React.PropTypes.bool
isSelected: React.PropTypes.bool
},

getDefaultProps () {
return {
selected: false
isSelected: false
};
},

render () {
const { id, selected, index, handleClick, children } = this.props;
const { isSelected, id, children } = this.props;

return (
<li
key={`tab-${id}`}
id={`tab-${id}`}
role='tab'
aria-controls={`panel-${id}`}
aria-selected={selected}
onClick={() => handleClick(index)}
tabIndex={0}>
{children}
aria-selected={isSelected}>
<a href={`#panel-${id}`}>
{children}
</a>
</li>
);
}
Expand Down
6 changes: 6 additions & 0 deletions components/tab/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ li[role='tab'] {
li[role='tab'][aria-selected='true'] {
font-weight: bold;
}

li[role='tab'] > a {
text-decoration: none;
display: block;
color: inherit;
}
38 changes: 34 additions & 4 deletions components/tabs/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,54 @@ import TabList from '../tab-list';
const Tabs = React.createClass({
propTypes: {
sections: React.PropTypes.array.isRequired,
defaultIndex: React.PropTypes.number
initialIndex: React.PropTypes.number
},

getDefaultProps () {
return {
defaultIndex: 0
initialIndex: 0
};
},

targets: new Map(),
indexes: new Map(),

getInitialState () {
return {
JS: false,
selectedIndex: this.props.defaultIndex
selectedIndex: this.props.initialIndex
};
},

componentDidMount () {
this.setState({ JS: true });

// Set up initial hash/index mapping
this.props.sections.forEach((section, index) => {
this.targets.set(`#panel-${section.id || index}`, index);
this.indexes.set(index, `#panel-${section.id || index}`);
});

// Handle hash change
window.addEventListener('hashchange', this.handleHash, false);

// Handle initial index (if no hash already)
if (this.props.initialIndex && !window.location.hash) {
window.location.hash = this.indexes.get(this.props.initialIndex);
}

// Initial hash handling
this.handleHash();
},

componentWillUnmount () {
window.removeEventListener('hashchange', this.handleHash, false);
},

handleHash () {
if (!window) return;

this.showSection(this.targets.get(window.location.hash) || 0);
},

showSection (index) {
Expand All @@ -33,7 +63,7 @@ const Tabs = React.createClass({
return (
<div>
{this.state.JS
? <TabList {...this.props} {...this.state} handleClick={this.showSection} />
? <TabList {...this.props} {...this.state} />
: null}
<PanelList {...this.props} {...this.state} />
</div>
Expand Down
1 change: 1 addition & 0 deletions example/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const App = React.createClass({
content: <ExampleContent image='images/1.jpeg' text='This is in tab 1. It is a whole other component! Entire components can be passed down into each tab.' />
},
{
id: 'foobar',
title: 'Tab 2',
content: 'Tab 2 content'
},
Expand Down
12 changes: 6 additions & 6 deletions example/bundle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/css/index.css

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

0 comments on commit d03c866

Please sign in to comment.