Skip to content

Commit 9da3875

Browse files
author
Claudéric Demers
authored
chore: refactoring and moving files around (#508)
* chore: Refactoring and moving files around * fix: issue with react-window basic setup list * Update logic to omit consumed props
1 parent 5a7fdda commit 9da3875

File tree

11 files changed

+356
-337
lines changed

11 files changed

+356
-337
lines changed

src/.stories/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,13 @@ storiesOf('Other | Virtualization libraries / react-window', module)
620620
items={getItems(500, 59)}
621621
itemHeight={59}
622622
helperClass={style.stylizedHelper}
623+
onSortEnd={(ref) => {
624+
// We need to inform React Window that the order of the items has changed
625+
const instance = ref.getWrappedInstance();
626+
const list = instance.refs.VirtualList;
627+
628+
list.forceUpdate();
629+
}}
623630
/>
624631
</div>
625632
);
@@ -632,7 +639,7 @@ storiesOf('Other | Virtualization libraries / react-window', module)
632639
items={getItems(500)}
633640
helperClass={style.stylizedHelper}
634641
onSortEnd={(ref) => {
635-
// We need to inform React Virtualized that the item heights have changed
642+
// We need to inform React Window that the item heights have changed
636643
const instance = ref.getWrappedInstance();
637644
const list = instance.refs.VirtualList;
638645

src/AutoScroller/index.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
export default class AutoScroller {
2+
constructor(container, onScrollCallback) {
3+
this.container = container;
4+
this.onScrollCallback = onScrollCallback;
5+
}
6+
7+
clear() {
8+
clearInterval(this.interval);
9+
this.interval = null;
10+
}
11+
12+
update({translate, minTranslate, maxTranslate, width, height}) {
13+
const direction = {
14+
x: 0,
15+
y: 0,
16+
};
17+
const speed = {
18+
x: 1,
19+
y: 1,
20+
};
21+
const acceleration = {
22+
x: 10,
23+
y: 10,
24+
};
25+
26+
const {
27+
scrollTop,
28+
scrollLeft,
29+
scrollHeight,
30+
scrollWidth,
31+
clientHeight,
32+
clientWidth,
33+
} = this.container;
34+
35+
const isTop = scrollTop === 0;
36+
const isBottom = scrollHeight - scrollTop - clientHeight === 0;
37+
const isLeft = scrollLeft === 0;
38+
const isRight = scrollWidth - scrollLeft - clientWidth === 0;
39+
40+
if (translate.y >= maxTranslate.y - height / 2 && !isBottom) {
41+
// Scroll Down
42+
direction.y = 1;
43+
speed.y =
44+
acceleration.y *
45+
Math.abs((maxTranslate.y - height / 2 - translate.y) / height);
46+
} else if (translate.x >= maxTranslate.x - width / 2 && !isRight) {
47+
// Scroll Right
48+
direction.x = 1;
49+
speed.x =
50+
acceleration.x *
51+
Math.abs((maxTranslate.x - width / 2 - translate.x) / width);
52+
} else if (translate.y <= minTranslate.y + height / 2 && !isTop) {
53+
// Scroll Up
54+
direction.y = -1;
55+
speed.y =
56+
acceleration.y *
57+
Math.abs((translate.y - height / 2 - minTranslate.y) / height);
58+
} else if (translate.x <= minTranslate.x + width / 2 && !isLeft) {
59+
// Scroll Left
60+
direction.x = -1;
61+
speed.x =
62+
acceleration.x *
63+
Math.abs((translate.x - width / 2 - minTranslate.x) / width);
64+
}
65+
66+
if (this.interval) {
67+
this.clear();
68+
this.isAutoScrolling = false;
69+
}
70+
71+
if (direction.x !== 0 || direction.y !== 0) {
72+
this.interval = setInterval(() => {
73+
this.isAutoScrolling = true;
74+
const offset = {
75+
left: speed.x * direction.x,
76+
top: speed.y * direction.y,
77+
};
78+
this.container.scrollTop += offset.top;
79+
this.container.scrollLeft += offset.left;
80+
81+
this.onScrollCallback(offset);
82+
}, 5);
83+
}
84+
}
85+
}
File renamed without changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default function defaultGetHelperDimensions({node}) {
2+
return {
3+
height: node.offsetHeight,
4+
width: node.offsetWidth,
5+
};
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default function defaultShouldCancelStart(event) {
2+
// Cancel sorting if the event target is an `input`, `textarea`, `select` or `option`
3+
const disabledElements = ['input', 'textarea', 'select', 'option', 'button'];
4+
5+
if (disabledElements.indexOf(event.target.tagName.toLowerCase()) !== -1) {
6+
// Return true to cancel sorting
7+
return true;
8+
}
9+
10+
return false;
11+
}

0 commit comments

Comments
 (0)