Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit df7ae11

Browse files
Refactored selection logic so that both ways of handlng Tabs work a little better
1 parent 3befc5e commit df7ae11

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

dash_core_components/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dash_core_components/metadata.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,6 +2783,13 @@
27832783
"description": "A Dash component that lets you render pages with tabs - the Tabs component's children\ncan be dcc.Tab components, which can hold a label that will be displayed as a tab, and can in turn hold\nchildren components that will be that tab's content.",
27842784
"displayName": "Tabs",
27852785
"methods": [
2786+
{
2787+
"name": "parseChildrenToArray",
2788+
"docblock": null,
2789+
"modifiers": [],
2790+
"params": [],
2791+
"returns": null
2792+
},
27862793
{
27872794
"name": "selectHandler",
27882795
"docblock": null,

src/components/Tabs.react.js

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const EnhancedTab = ({
1818
disabled_className,
1919
mobile_breakpoint,
2020
amountOfTabs,
21-
colors
21+
colors,
2222
}) => {
2323
let tabStyle = style;
2424
if (disabled) {
@@ -111,38 +111,50 @@ export default class Tabs extends Component {
111111
constructor(props) {
112112
super(props);
113113

114+
this.selectHandler = this.selectHandler.bind(this);
115+
this.parseChildrenToArray = this.parseChildrenToArray.bind(this);
116+
117+
this.parseChildrenToArray();
118+
114119
if (!this.props.value) {
115120
// if no value specified on Tabs component, set it to the first child's (which should be a Tab component) value
116-
const value = this.props.children[0].props.children.props.value;
121+
const value =
122+
this.props.children[0].props.children.props.value || 'tab-1';
117123
this.state = {
118-
selected: value || 'tab-1'
124+
selected: value,
119125
};
120-
if(this.props.setProps) {
126+
if (this.props.setProps) {
121127
// updating the prop in Dash is necessary so that callbacks work
122128
this.props.setProps({
123-
value: value
129+
value: value,
124130
});
125131
}
126132
} else {
127133
this.state = {
128-
selected: this.props.value
134+
selected: this.props.value,
129135
};
130136
}
131-
this.selectHandler = this.selectHandler.bind(this);
137+
}
138+
parseChildrenToArray() {
139+
if (!R.is(Array, this.props.children)) {
140+
// if dcc.Tabs.children contains just one single element, it gets passed as an object
141+
// instead of an array - so we put in in a array ourselves!
142+
this.props.children = [this.props.children];
143+
}
132144
}
133145
selectHandler(value) {
134146
this.setState({
135-
selected: value
147+
selected: value,
136148
});
137149
if (this.props.setProps) {
138150
this.props.setProps({value: value});
139151
}
140152
}
141153
componentWillReceiveProps(newProps) {
142154
const value = newProps.value;
143-
if (!value) {
155+
if (typeof value !== 'undefined') {
144156
this.setState({
145-
selected: value
157+
selected: value,
146158
});
147159
}
148160
}
@@ -151,13 +163,9 @@ export default class Tabs extends Component {
151163
let selectedTab;
152164
let selectedTabContent;
153165

154-
if (this.props.children) {
155-
if (!R.is(Array, this.props.children)) {
156-
// if dcc.Tabs.children contains just one single element, it gets passed as an object
157-
// instead of an array - so we put in in a array ourselves!
158-
this.props.children = [this.props.children];
159-
}
166+
this.parseChildrenToArray();
160167

168+
if (this.props.children) {
161169
const amountOfTabs = this.props.children.length;
162170

163171
EnhancedTabs = this.props.children.map((child, index) => {
@@ -202,8 +210,6 @@ export default class Tabs extends Component {
202210
selectedTab = this.props.children.filter(child => {
203211
return child.props.children.props.value === this.state.selected;
204212
});
205-
window.console.log('selected', this.state.selected);
206-
window.console.log('selectedTab[0]', selectedTab[0]);
207213
if ('props' in selectedTab[0]) {
208214
selectedTabContent = selectedTab[0].props.children;
209215
}
@@ -265,13 +271,11 @@ export default class Tabs extends Component {
265271
border-bottom: none;
266272
}
267273
:global(.tab-container--vert .tab:last-of-type) {
268-
border-bottom: 1px solid
269-
${this.props.colors.border} !important;
274+
border-bottom: 1px solid ${this.props.colors.border} !important;
270275
}
271276
:global(.tab-container--vert .tab--selected) {
272277
border: 1px solid ${this.props.colors.border};
273-
border-left: 2px solid
274-
${this.props.colors.primary};
278+
border-left: 2px solid ${this.props.colors.primary};
275279
border-right: none;
276280
}
277281
@@ -291,8 +295,8 @@ Tabs.defaultProps = {
291295
colors: {
292296
border: '#d6d6d6',
293297
primary: '#1975FA',
294-
background: '#f9f9f9'
295-
}
298+
background: '#f9f9f9',
299+
},
296300
};
297301

298302
Tabs.propTypes = {
@@ -364,6 +368,6 @@ Tabs.propTypes = {
364368
colors: PropTypes.shape({
365369
border: PropTypes.string,
366370
primary: PropTypes.string,
367-
background: PropTypes.string
368-
})
371+
background: PropTypes.string,
372+
}),
369373
};

0 commit comments

Comments
 (0)