Skip to content

Commit 258ceeb

Browse files
ljanioliviertassinari
authored andcommitted
[Tabs] Fix calculating tab indicator position (#11825)
* [Tabs] Add failing test for #8379 * [Tabs] Fix calculating tab indicator position Fixes #8379
1 parent 98168a2 commit 258ceeb

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

packages/material-ui/src/Tabs/Tabs.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class Tabs extends React.Component {
143143
const children = this.tabs.children[0].children;
144144

145145
if (children.length > 0) {
146-
const tab = children[this.valueToIndex[value]];
146+
const tab = children[this.valueToIndex.get(value)];
147147
warning(tab, `Material-UI: the value provided \`${value}\` is invalid`);
148148
tabMeta = tab ? tab.getBoundingClientRect() : null;
149149
}
@@ -152,7 +152,7 @@ class Tabs extends React.Component {
152152
};
153153

154154
tabs = undefined;
155-
valueToIndex = {};
155+
valueToIndex = new Map();
156156

157157
handleResize = debounce(() => {
158158
this.updateIndicatorState(this.props);
@@ -313,15 +313,15 @@ class Tabs extends React.Component {
313313
/>
314314
);
315315

316-
this.valueToIndex = {};
316+
this.valueToIndex = new Map();
317317
let childIndex = 0;
318318
const children = React.Children.map(childrenProp, child => {
319319
if (!React.isValidElement(child)) {
320320
return null;
321321
}
322322

323323
const childValue = child.props.value === undefined ? childIndex : child.props.value;
324-
this.valueToIndex[childValue] = childIndex;
324+
this.valueToIndex.set(childValue, childIndex);
325325
const selected = childValue === value;
326326

327327
childIndex += 1;

packages/material-ui/src/Tabs/Tabs.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,19 @@ describe('<Tabs />', () => {
246246
);
247247
});
248248

249+
it('should accept any value as selected tab value', () => {
250+
const tab0 = {};
251+
const tab1 = {};
252+
assert.notStrictEqual(tab0, tab1);
253+
const wrapper2 = shallow(
254+
<Tabs width="md" onChange={noop} value={tab0}>
255+
<Tab value={tab0} />
256+
<Tab value={tab1} />
257+
</Tabs>,
258+
);
259+
assert.strictEqual(wrapper2.instance().valueToIndex.size, 2);
260+
});
261+
249262
it('should render the indicator', () => {
250263
const wrapper2 = mount(
251264
<Tabs width="md" onChange={noop} value={1}>

0 commit comments

Comments
 (0)