Skip to content

Commit d8de050

Browse files
committed
fix(Tab): max-call-exceeded
1 parent 7878504 commit d8de050

File tree

6 files changed

+20
-16
lines changed

6 files changed

+20
-16
lines changed

docs/tab/index.en-us.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Disable animation with `animation={false}`
2121

2222
| Param | Descripiton | Type | Default Value |
2323
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | -------- |
24-
| activeKey | Key of the current active tab | Number/String | - |
24+
| activeKey | Key of the current active tab<br> This prop will make tab a 'controlled component', end user will be unable to change tabs | Number/String | - |
2525
| size | Size of tab<br><br>**option**:<br>'small', 'medium' | Enum | 'medium' |
2626
| shape | Shape of tab<br><br>**option**:<br>'pure', 'wrapped', 'text', 'capsule' | Enum | 'pure' |
2727
| defaultActiveKey | Key of default active tab | Number/String | - |

docs/tab/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Fusion 提供了三级选项卡,分别用于不同的场景。
3333

3434
| 参数 | 说明 | 类型 | 默认值 |
3535
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | -------- |
36-
| activeKey | 被激活的选项卡的 key | Number/String | - |
36+
| activeKey | 被激活的选项卡的 key, 赋值则tab为受控组件, 用户无法切换 | Number/String | - |
3737
| size | 尺寸<br><br>**可选值**:<br>'small', 'medium' | Enum | 'medium' |
3838
| shape | 外观形态<br><br>**可选值**:<br>'pure', 'wrapped', 'text', 'capsule' | Enum | 'pure' |
3939
| defaultActiveKey | 初始化时被激活的选项卡的 key | Number/String | - |

src/tab/tab.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default class Tab extends Component {
1515
static propTypes = {
1616
prefix: PropTypes.string,
1717
/**
18-
* 被激活的选项卡的 key
18+
* 被激活的选项卡的 key, 赋值则tab为受控组件, 用户无法切换
1919
*/
2020
activeKey: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
2121
/**

src/tab/tabs/nav.jsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,20 @@ class Nav extends React.Component {
6868
events.off(window, 'resize', this.onWindowResized);
6969
}
7070

71+
/**
72+
* The key method about move
73+
* @param {number} target position to slide to
74+
* @param {bool} checkSlideBtn need to check the slide button status or not
75+
* @param {bool} setActive need to check the active status or not
76+
*/
7177
setOffset(target, checkSlideBtn = true, setActive = true) {
7278
const { tabPosition } = this.props;
7379
const navWH = getOffsetWH(this.nav, tabPosition);
7480
const wrapperWH = getOffsetWH(this.wrapper);
7581

82+
// target should not be great than 0, i.e. should not over slide to left-most
7683
target = target >= 0 ? 0 : target;
84+
// when need to slide, should not slide to exceed right-most
7785
target = target <= wrapperWH - navWH && wrapperWH - navWH < 0 ? wrapperWH - navWH : target;
7886

7987
const relativeOffset = target - this.offset;
@@ -140,7 +148,7 @@ class Nav extends React.Component {
140148
if (minOffset >= 0 || navWH <= navbarWH) {
141149
next = false;
142150
prev = false;
143-
this.setOffset(0);
151+
this.setOffset(0, false); // no need to check slide again since this call is invoked from inside setSlideBtn
144152
} else if (this.offset < 0 && this.offset <= minOffset) {
145153
prev = true;
146154
next = false;
@@ -186,7 +194,7 @@ class Nav extends React.Component {
186194
}
187195

188196
removeTab = (key, e) => {
189-
e && e.stopPropagation(); // 不再传递事件,防止触发父级事件处理器
197+
e && e.stopPropagation(); // stop bubble, so that it won't trigger upstream listener
190198
this.props.onClose(key);
191199
}
192200

src/tab/tabs/utils.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export const triggerEvents = {
66
};
77

88
export function getOffsetWH(node, tabPosition) {
9-
let prop = 'offsetWidth';
9+
let prop = 'width';
1010
if (tabPosition === 'left' || tabPosition === 'right') {
11-
prop = 'offsetHeight';
11+
prop = 'height';
1212
}
13-
return node ? node[prop] : 0;
13+
return node ? node.getBoundingClientRect()[prop] : 0;
1414
}
1515

1616
export function getOffsetLT(node, tabPosition) {

test/tab/index-spec.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ describe('Tab', () => {
179179

180180
describe('slide mode', () => {
181181
let wrapper, target;
182-
const panes = [1, 2, 3, 4, 5, 6, 7, 9, 10].map((item, index) => <Tab.Item title={`tab item ${item}`} key={index}></Tab.Item>);
182+
const panes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map((item, index) => <Tab.Item title={`tab item ${item}`} key={index}></Tab.Item>);
183183
const boxStyle = { width: '200px' };
184184

185185
beforeEach(() => {
@@ -218,14 +218,10 @@ describe('Tab', () => {
218218
assert(wrapper.find('.next-menu-item').at(0).hasClass('next-selected'));
219219
});
220220

221-
// TODO: not work
222221
it('should scrollToActiveTab', () => {
223-
wrapper = mount(<div style={boxStyle}><Tab activeKey="9">{panes}</Tab></div>, { attachTo: target });
224-
// console.log(wrapper.find('.next-tabs').instance());
225-
wrapper.setProps({ activeKey: '3' });
226-
// wrapper.find('.next-tabs-tab').at(1).simulate('click');
227-
222+
wrapper = mount(<div style={boxStyle}><Tab defaultActiveKey="9">{panes}</Tab></div>, { attachTo: target });
223+
wrapper.find('.next-tabs-tab').at(3).simulate('click');
224+
assert(wrapper.find('.next-tabs-tab').at(3).hasClass('active'))
228225
});
229-
230226
});
231227
});

0 commit comments

Comments
 (0)