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

Commit 25cb8d6

Browse files
committed
fix(repeater) resize scroller correctly, fixes #5027 (final)
1 parent 1b1f4fd commit 25cb8d6

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

src/components/virtualRepeat/virtual-repeater.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -574,21 +574,21 @@ VirtualRepeatController.prototype.getItemSize = function() {
574574
* @private
575575
*/
576576
VirtualRepeatController.prototype.virtualRepeatUpdate_ = function(items, oldItems) {
577-
var itemsLength = items ? items.length : 0;
577+
var itemsLength = items && items.length || 0;
578578
var lengthChanged = false;
579579

580-
if (itemsLength !== this.itemsLength) {
581-
lengthChanged = true;
582-
this.itemsLength = itemsLength;
583-
}
584-
585580
// If the number of items shrank, scroll up to the top.
586581
if (this.items && itemsLength < this.items.length && this.container.getScrollOffset() !== 0) {
587582
this.items = items;
588583
this.container.resetScroll();
589584
return;
590585
}
591586

587+
if (itemsLength !== this.itemsLength) {
588+
lengthChanged = true;
589+
this.itemsLength = itemsLength;
590+
}
591+
592592
this.items = items;
593593
if (items !== oldItems || lengthChanged) {
594594
this.updateIndexes_();

src/components/virtualRepeat/virtual-repeater.spec.js

+68
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,74 @@ describe('<md-virtual-repeat>', function() {
408408
expect(getRepeated().length).toBe(numItemRenderers);
409409
});
410410

411+
it('should resize the scroller correctly when item length changes (vertical)', function() {
412+
scope.items = createItems(200);
413+
createRepeater();
414+
scope.$apply();
415+
$$rAF.flush();
416+
expect(sizer[0].offsetHeight).toBe(200 * ITEM_SIZE);
417+
418+
// Scroll down half way
419+
scroller[0].scrollTop = 100 * ITEM_SIZE;
420+
scroller.triggerHandler('scroll');
421+
scope.$apply();
422+
$$rAF.flush();
423+
424+
// Remove some items
425+
scope.items = createItems(20);
426+
scope.$apply();
427+
$$rAF.flush();
428+
expect(scroller[0].scrollTop).toBe(0);
429+
expect(sizer[0].offsetHeight).toBe(20 * ITEM_SIZE);
430+
431+
// Scroll down half way
432+
scroller[0].scrollTop = 10 * ITEM_SIZE;
433+
scroller.triggerHandler('scroll');
434+
scope.$apply();
435+
$$rAF.flush();
436+
437+
// Add more items
438+
scope.items = createItems(250);
439+
scope.$apply();
440+
$$rAF.flush();
441+
expect(scroller[0].scrollTop).toBe(100);
442+
expect(sizer[0].offsetHeight).toBe(250 * ITEM_SIZE);
443+
});
444+
445+
it('should resize the scroller correctly when item length changes (horizontal)', function() {
446+
container.attr({'md-orient-horizontal': ''});
447+
scope.items = createItems(200);
448+
createRepeater();
449+
scope.$apply();
450+
$$rAF.flush();
451+
expect(sizer[0].offsetWidth).toBe(200 * ITEM_SIZE);
452+
453+
// Scroll right half way
454+
scroller[0].scrollLeft = 100 * ITEM_SIZE;
455+
scroller.triggerHandler('scroll');
456+
scope.$apply();
457+
$$rAF.flush();
458+
459+
// Remove some items
460+
scope.items = createItems(20);
461+
scope.$apply();
462+
$$rAF.flush();
463+
expect(scroller[0].scrollLeft).toBe(0);
464+
expect(sizer[0].offsetWidth).toBe(20 * ITEM_SIZE);
465+
466+
// Scroll right half way
467+
scroller[0].scrollLeft = 10 * ITEM_SIZE;
468+
scroller.triggerHandler('scroll');
469+
scope.$apply();
470+
$$rAF.flush();
471+
472+
// Add more items
473+
scope.items = createItems(250);
474+
scope.$apply();
475+
$$rAF.flush();
476+
expect(sizer[0].offsetWidth).toBe(250 * ITEM_SIZE);
477+
});
478+
411479
it('should update topIndex when scrolling', function() {
412480
container.attr({'md-top-index': 'topIndex'});
413481
scope.items = createItems(NUM_ITEMS);

0 commit comments

Comments
 (0)