From ae8a58fc887565f94cfd35fc7e955431b75cc00c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20de=20Jong?= Date: Wed, 10 Feb 2016 11:39:18 +0100 Subject: [PATCH 1/3] Add a cancel option to tab change event handler --- lib/components/Tabs.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/components/Tabs.js b/lib/components/Tabs.js index ad99d53fbe..52a22d6fc4 100644 --- a/lib/components/Tabs.js +++ b/lib/components/Tabs.js @@ -108,22 +108,27 @@ module.exports = React.createClass({ } }, - setSelected(index, focus) { + setSelected: function setSelected(index, focus) { // Don't do anything if nothing has changed if (index === this.state.selectedIndex) return; // Check index boundary if (index < 0 || index >= this.getTabsCount()) return; // Keep reference to last index for event handler - const last = this.state.selectedIndex; - - // Update selected index - this.setState({ selectedIndex: index, focus: focus === true }); + var last = this.state.selectedIndex; + // Check wether change event handler cancels the tab change + var cancel = false; + // Call change event handler if (typeof this.props.onSelect === 'function') { - this.props.onSelect(index, last); + cancel = (this.props.onSelect(index, last) === false); } + + if (!cancel) { + // Update selected index + this.setState({ selectedIndex: index, focus: focus === true }); + } }, getNextTab(index) { From f944daa6378486db7975f296bb159aaf150a3401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20de=20Jong?= Date: Wed, 10 Feb 2016 11:48:46 +0100 Subject: [PATCH 2/3] Add a cancel option to tab change event handler --- lib/components/Tabs.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/components/Tabs.js b/lib/components/Tabs.js index 52a22d6fc4..30f6fd2e57 100644 --- a/lib/components/Tabs.js +++ b/lib/components/Tabs.js @@ -108,21 +108,21 @@ module.exports = React.createClass({ } }, - setSelected: function setSelected(index, focus) { + setSelected(index, focus) { // Don't do anything if nothing has changed if (index === this.state.selectedIndex) return; // Check index boundary if (index < 0 || index >= this.getTabsCount()) return; // Keep reference to last index for event handler - var last = this.state.selectedIndex; + const last = this.state.selectedIndex; // Check wether change event handler cancels the tab change - var cancel = false; - + let cancel = false; + // Call change event handler if (typeof this.props.onSelect === 'function') { - cancel = (this.props.onSelect(index, last) === false); + cancel = this.props.onSelect(index, last); } if (!cancel) { From bf7ddb996eca81bf1cc03e72c728c18545fc0d3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20de=20Jong?= Date: Wed, 10 Feb 2016 14:02:18 +0100 Subject: [PATCH 3/3] Bug fix --- lib/components/Tabs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/components/Tabs.js b/lib/components/Tabs.js index 30f6fd2e57..9c83726865 100644 --- a/lib/components/Tabs.js +++ b/lib/components/Tabs.js @@ -122,7 +122,7 @@ module.exports = React.createClass({ // Call change event handler if (typeof this.props.onSelect === 'function') { - cancel = this.props.onSelect(index, last); + cancel = (this.props.onSelect(index, last) === false); } if (!cancel) {