Skip to content

Commit d5dc45d

Browse files
authored
fix(virtualize): onSwitching for Virtualize should called with correct index (#647)
1 parent a12dd90 commit d5dc45d

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

packages/react-swipeable-views-utils/src/virtualize.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ export default function virtualize(MyComponent) {
116116
}
117117
};
118118

119+
handleSwitching = (indexContainer, type) => {
120+
const { slideCount, onSwitching } = this.props;
121+
const { indexStart } = this.state;
122+
123+
let index = indexContainer + indexStart;
124+
125+
if (slideCount) {
126+
index = mod(index, slideCount);
127+
}
128+
129+
if (onSwitching) {
130+
onSwitching(index, type);
131+
}
132+
};
133+
119134
handleTransitionEnd = () => {
120135
// Delay the update of the window to fix an issue with react-motion.
121136
this.timer = setTimeout(() => {
@@ -132,6 +147,7 @@ export default function virtualize(MyComponent) {
132147
children,
133148
index: indexProp,
134149
onChangeIndex,
150+
onSwitching,
135151
onTransitionEnd,
136152
overscanSlideAfter,
137153
overscanSlideBefore,
@@ -157,6 +173,7 @@ export default function virtualize(MyComponent) {
157173
<MyComponent
158174
index={indexContainer}
159175
onChangeIndex={this.handleChangeIndex}
176+
onSwitching={this.handleSwitching}
160177
onTransitionEnd={this.handleTransitionEnd}
161178
{...other}
162179
>

packages/react-swipeable-views-utils/src/virtualize.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,20 @@ describe('virtualize', () => {
284284
assert.strictEqual(wrapper.state().index, 10, 'should not update the state index');
285285
});
286286
});
287+
288+
describe('prop: onSwitching', () => {
289+
it('should be called with the right arguments', () => {
290+
const handleSwitching = spy();
291+
const wrapper = shallow(
292+
<VirtualizeSwipeableViews
293+
index={10}
294+
slideRenderer={slideRenderer}
295+
onSwitching={handleSwitching}
296+
/>,
297+
);
298+
299+
wrapper.find(Empty).simulate('switching', 3.05, 'move');
300+
assert.deepEqual(handleSwitching.args, [[10.05, 'move']]);
301+
});
302+
});
287303
});

0 commit comments

Comments
 (0)