List.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _extends from "@babel/runtime/helpers/esm/extends";
  3. import { resolveDirective as _resolveDirective, createVNode as _createVNode } from "vue";
  4. var __rest = this && this.__rest || function (s, e) {
  5. var t = {};
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  7. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  8. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  9. }
  10. return t;
  11. };
  12. import { shallowRef, toRaw, onMounted, onUpdated, defineComponent, watchEffect, computed, nextTick, onBeforeUnmount, reactive, watch } from 'vue';
  13. import Filler from './Filler';
  14. import Item from './Item';
  15. import ScrollBar from './ScrollBar';
  16. import useHeights from './hooks/useHeights';
  17. import useScrollTo from './hooks/useScrollTo';
  18. import useFrameWheel from './hooks/useFrameWheel';
  19. import useMobileTouchMove from './hooks/useMobileTouchMove';
  20. import useOriginScroll from './hooks/useOriginScroll';
  21. import PropTypes from '../_util/vue-types';
  22. import classNames from '../_util/classNames';
  23. import supportsPassive from '../_util/supportsPassive';
  24. const EMPTY_DATA = [];
  25. const ScrollStyle = {
  26. overflowY: 'auto',
  27. overflowAnchor: 'none'
  28. };
  29. function renderChildren(list, startIndex, endIndex, setNodeRef, renderFunc, _ref) {
  30. let {
  31. getKey
  32. } = _ref;
  33. return list.slice(startIndex, endIndex + 1).map((item, index) => {
  34. const eleIndex = startIndex + index;
  35. const node = renderFunc(item, eleIndex, {
  36. // style: status === 'MEASURE_START' ? { visibility: 'hidden' } : {},
  37. });
  38. const key = getKey(item);
  39. return _createVNode(Item, {
  40. "key": key,
  41. "setRef": ele => setNodeRef(item, ele)
  42. }, {
  43. default: () => [node]
  44. });
  45. });
  46. }
  47. const List = defineComponent({
  48. compatConfig: {
  49. MODE: 3
  50. },
  51. name: 'List',
  52. inheritAttrs: false,
  53. props: {
  54. prefixCls: String,
  55. data: PropTypes.array,
  56. height: Number,
  57. itemHeight: Number,
  58. /** If not match virtual scroll condition, Set List still use height of container. */
  59. fullHeight: {
  60. type: Boolean,
  61. default: undefined
  62. },
  63. itemKey: {
  64. type: [String, Number, Function],
  65. required: true
  66. },
  67. component: {
  68. type: [String, Object]
  69. },
  70. /** Set `false` will always use real scroll instead of virtual one */
  71. virtual: {
  72. type: Boolean,
  73. default: undefined
  74. },
  75. children: Function,
  76. onScroll: Function,
  77. onMousedown: Function,
  78. onMouseenter: Function,
  79. onVisibleChange: Function
  80. },
  81. setup(props, _ref2) {
  82. let {
  83. expose
  84. } = _ref2;
  85. // ================================= MISC =================================
  86. const useVirtual = computed(() => {
  87. const {
  88. height,
  89. itemHeight,
  90. virtual
  91. } = props;
  92. return !!(virtual !== false && height && itemHeight);
  93. });
  94. const inVirtual = computed(() => {
  95. const {
  96. height,
  97. itemHeight,
  98. data
  99. } = props;
  100. return useVirtual.value && data && itemHeight * data.length > height;
  101. });
  102. const state = reactive({
  103. scrollTop: 0,
  104. scrollMoving: false
  105. });
  106. const data = computed(() => {
  107. return props.data || EMPTY_DATA;
  108. });
  109. const mergedData = shallowRef([]);
  110. watch(data, () => {
  111. mergedData.value = toRaw(data.value).slice();
  112. }, {
  113. immediate: true
  114. });
  115. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  116. const itemKey = shallowRef(_item => undefined);
  117. watch(() => props.itemKey, val => {
  118. if (typeof val === 'function') {
  119. itemKey.value = val;
  120. } else {
  121. itemKey.value = item => item === null || item === void 0 ? void 0 : item[val];
  122. }
  123. }, {
  124. immediate: true
  125. });
  126. const componentRef = shallowRef();
  127. const fillerInnerRef = shallowRef();
  128. const scrollBarRef = shallowRef(); // Hack on scrollbar to enable flash call
  129. // =============================== Item Key ===============================
  130. const getKey = item => {
  131. return itemKey.value(item);
  132. };
  133. const sharedConfig = {
  134. getKey
  135. };
  136. // ================================ Scroll ================================
  137. function syncScrollTop(newTop) {
  138. let value;
  139. if (typeof newTop === 'function') {
  140. value = newTop(state.scrollTop);
  141. } else {
  142. value = newTop;
  143. }
  144. const alignedTop = keepInRange(value);
  145. if (componentRef.value) {
  146. componentRef.value.scrollTop = alignedTop;
  147. }
  148. state.scrollTop = alignedTop;
  149. }
  150. // ================================ Height ================================
  151. const [setInstance, collectHeight, heights, updatedMark] = useHeights(mergedData, getKey, null, null);
  152. const calRes = reactive({
  153. scrollHeight: undefined,
  154. start: 0,
  155. end: 0,
  156. offset: undefined
  157. });
  158. const offsetHeight = shallowRef(0);
  159. onMounted(() => {
  160. nextTick(() => {
  161. var _a;
  162. offsetHeight.value = ((_a = fillerInnerRef.value) === null || _a === void 0 ? void 0 : _a.offsetHeight) || 0;
  163. });
  164. });
  165. onUpdated(() => {
  166. nextTick(() => {
  167. var _a;
  168. offsetHeight.value = ((_a = fillerInnerRef.value) === null || _a === void 0 ? void 0 : _a.offsetHeight) || 0;
  169. });
  170. });
  171. watch([useVirtual, mergedData], () => {
  172. if (!useVirtual.value) {
  173. _extends(calRes, {
  174. scrollHeight: undefined,
  175. start: 0,
  176. end: mergedData.value.length - 1,
  177. offset: undefined
  178. });
  179. }
  180. }, {
  181. immediate: true
  182. });
  183. watch([useVirtual, mergedData, offsetHeight, inVirtual], () => {
  184. // Always use virtual scroll bar in avoid shaking
  185. if (useVirtual.value && !inVirtual.value) {
  186. _extends(calRes, {
  187. scrollHeight: offsetHeight.value,
  188. start: 0,
  189. end: mergedData.value.length - 1,
  190. offset: undefined
  191. });
  192. }
  193. if (componentRef.value) {
  194. state.scrollTop = componentRef.value.scrollTop;
  195. }
  196. }, {
  197. immediate: true
  198. });
  199. watch([inVirtual, useVirtual, () => state.scrollTop, mergedData, updatedMark, () => props.height, offsetHeight], () => {
  200. if (!useVirtual.value || !inVirtual.value) {
  201. return;
  202. }
  203. let itemTop = 0;
  204. let startIndex;
  205. let startOffset;
  206. let endIndex;
  207. const dataLen = mergedData.value.length;
  208. const data = mergedData.value;
  209. const scrollTop = state.scrollTop;
  210. const {
  211. itemHeight,
  212. height
  213. } = props;
  214. const scrollTopHeight = scrollTop + height;
  215. for (let i = 0; i < dataLen; i += 1) {
  216. const item = data[i];
  217. const key = getKey(item);
  218. let cacheHeight = heights.get(key);
  219. if (cacheHeight === undefined) {
  220. cacheHeight = itemHeight;
  221. }
  222. const currentItemBottom = itemTop + cacheHeight;
  223. if (startIndex === undefined && currentItemBottom >= scrollTop) {
  224. startIndex = i;
  225. startOffset = itemTop;
  226. }
  227. // Check item bottom in the range. We will render additional one item for motion usage
  228. if (endIndex === undefined && currentItemBottom > scrollTopHeight) {
  229. endIndex = i;
  230. }
  231. itemTop = currentItemBottom;
  232. }
  233. // When scrollTop at the end but data cut to small count will reach this
  234. if (startIndex === undefined) {
  235. startIndex = 0;
  236. startOffset = 0;
  237. endIndex = Math.ceil(height / itemHeight);
  238. }
  239. if (endIndex === undefined) {
  240. endIndex = dataLen - 1;
  241. }
  242. // Give cache to improve scroll experience
  243. endIndex = Math.min(endIndex + 1, dataLen);
  244. _extends(calRes, {
  245. scrollHeight: itemTop,
  246. start: startIndex,
  247. end: endIndex,
  248. offset: startOffset
  249. });
  250. }, {
  251. immediate: true
  252. });
  253. // =============================== In Range ===============================
  254. const maxScrollHeight = computed(() => calRes.scrollHeight - props.height);
  255. function keepInRange(newScrollTop) {
  256. let newTop = newScrollTop;
  257. if (!Number.isNaN(maxScrollHeight.value)) {
  258. newTop = Math.min(newTop, maxScrollHeight.value);
  259. }
  260. newTop = Math.max(newTop, 0);
  261. return newTop;
  262. }
  263. const isScrollAtTop = computed(() => state.scrollTop <= 0);
  264. const isScrollAtBottom = computed(() => state.scrollTop >= maxScrollHeight.value);
  265. const originScroll = useOriginScroll(isScrollAtTop, isScrollAtBottom);
  266. // ================================ Scroll ================================
  267. function onScrollBar(newScrollTop) {
  268. const newTop = newScrollTop;
  269. syncScrollTop(newTop);
  270. }
  271. // When data size reduce. It may trigger native scroll event back to fit scroll position
  272. function onFallbackScroll(e) {
  273. var _a;
  274. const {
  275. scrollTop: newScrollTop
  276. } = e.currentTarget;
  277. if (newScrollTop !== state.scrollTop) {
  278. syncScrollTop(newScrollTop);
  279. }
  280. // Trigger origin onScroll
  281. (_a = props.onScroll) === null || _a === void 0 ? void 0 : _a.call(props, e);
  282. }
  283. // Since this added in global,should use ref to keep update
  284. const [onRawWheel, onFireFoxScroll] = useFrameWheel(useVirtual, isScrollAtTop, isScrollAtBottom, offsetY => {
  285. syncScrollTop(top => {
  286. const newTop = top + offsetY;
  287. return newTop;
  288. });
  289. });
  290. // Mobile touch move
  291. useMobileTouchMove(useVirtual, componentRef, (deltaY, smoothOffset) => {
  292. if (originScroll(deltaY, smoothOffset)) {
  293. return false;
  294. }
  295. onRawWheel({
  296. preventDefault() {},
  297. deltaY
  298. });
  299. return true;
  300. });
  301. // Firefox only
  302. function onMozMousePixelScroll(e) {
  303. if (useVirtual.value) {
  304. e.preventDefault();
  305. }
  306. }
  307. const removeEventListener = () => {
  308. if (componentRef.value) {
  309. componentRef.value.removeEventListener('wheel', onRawWheel, supportsPassive ? {
  310. passive: false
  311. } : false);
  312. componentRef.value.removeEventListener('DOMMouseScroll', onFireFoxScroll);
  313. componentRef.value.removeEventListener('MozMousePixelScroll', onMozMousePixelScroll);
  314. }
  315. };
  316. watchEffect(() => {
  317. nextTick(() => {
  318. if (componentRef.value) {
  319. removeEventListener();
  320. componentRef.value.addEventListener('wheel', onRawWheel, supportsPassive ? {
  321. passive: false
  322. } : false);
  323. componentRef.value.addEventListener('DOMMouseScroll', onFireFoxScroll);
  324. componentRef.value.addEventListener('MozMousePixelScroll', onMozMousePixelScroll);
  325. }
  326. });
  327. });
  328. onBeforeUnmount(() => {
  329. removeEventListener();
  330. });
  331. // ================================= Ref ==================================
  332. const scrollTo = useScrollTo(componentRef, mergedData, heights, props, getKey, collectHeight, syncScrollTop, () => {
  333. var _a;
  334. (_a = scrollBarRef.value) === null || _a === void 0 ? void 0 : _a.delayHidden();
  335. });
  336. expose({
  337. scrollTo
  338. });
  339. const componentStyle = computed(() => {
  340. let cs = null;
  341. if (props.height) {
  342. cs = _extends({
  343. [props.fullHeight ? 'height' : 'maxHeight']: props.height + 'px'
  344. }, ScrollStyle);
  345. if (useVirtual.value) {
  346. cs.overflowY = 'hidden';
  347. if (state.scrollMoving) {
  348. cs.pointerEvents = 'none';
  349. }
  350. }
  351. }
  352. return cs;
  353. });
  354. // ================================ Effect ================================
  355. /** We need told outside that some list not rendered */
  356. watch([() => calRes.start, () => calRes.end, mergedData], () => {
  357. if (props.onVisibleChange) {
  358. const renderList = mergedData.value.slice(calRes.start, calRes.end + 1);
  359. props.onVisibleChange(renderList, mergedData.value);
  360. }
  361. }, {
  362. flush: 'post'
  363. });
  364. const delayHideScrollBar = () => {
  365. var _a;
  366. (_a = scrollBarRef.value) === null || _a === void 0 ? void 0 : _a.delayHidden();
  367. };
  368. return {
  369. state,
  370. mergedData,
  371. componentStyle,
  372. onFallbackScroll,
  373. onScrollBar,
  374. componentRef,
  375. useVirtual,
  376. calRes,
  377. collectHeight,
  378. setInstance,
  379. sharedConfig,
  380. scrollBarRef,
  381. fillerInnerRef,
  382. delayHideScrollBar
  383. };
  384. },
  385. render() {
  386. const _a = _extends(_extends({}, this.$props), this.$attrs),
  387. {
  388. prefixCls = 'rc-virtual-list',
  389. height,
  390. itemHeight,
  391. // eslint-disable-next-line no-unused-vars
  392. fullHeight,
  393. data,
  394. itemKey,
  395. virtual,
  396. component: Component = 'div',
  397. onScroll,
  398. children = this.$slots.default,
  399. style,
  400. class: className
  401. } = _a,
  402. restProps = __rest(_a, ["prefixCls", "height", "itemHeight", "fullHeight", "data", "itemKey", "virtual", "component", "onScroll", "children", "style", "class"]);
  403. const mergedClassName = classNames(prefixCls, className);
  404. const {
  405. scrollTop
  406. } = this.state;
  407. const {
  408. scrollHeight,
  409. offset,
  410. start,
  411. end
  412. } = this.calRes;
  413. const {
  414. componentStyle,
  415. onFallbackScroll,
  416. onScrollBar,
  417. useVirtual,
  418. collectHeight,
  419. sharedConfig,
  420. setInstance,
  421. mergedData,
  422. delayHideScrollBar
  423. } = this;
  424. return _createVNode("div", _objectSpread({
  425. "style": _extends(_extends({}, style), {
  426. position: 'relative'
  427. }),
  428. "class": mergedClassName
  429. }, restProps), [_createVNode(Component, {
  430. "class": `${prefixCls}-holder`,
  431. "style": componentStyle,
  432. "ref": "componentRef",
  433. "onScroll": onFallbackScroll,
  434. "onMouseenter": delayHideScrollBar
  435. }, {
  436. default: () => [_createVNode(Filler, {
  437. "prefixCls": prefixCls,
  438. "height": scrollHeight,
  439. "offset": offset,
  440. "onInnerResize": collectHeight,
  441. "ref": "fillerInnerRef"
  442. }, {
  443. default: () => renderChildren(mergedData, start, end, setInstance, children, sharedConfig)
  444. })]
  445. }), useVirtual && _createVNode(ScrollBar, {
  446. "ref": "scrollBarRef",
  447. "prefixCls": prefixCls,
  448. "scrollTop": scrollTop,
  449. "height": height,
  450. "scrollHeight": scrollHeight,
  451. "count": mergedData.length,
  452. "onScroll": onScrollBar,
  453. "onStartMove": () => {
  454. this.state.scrollMoving = true;
  455. },
  456. "onStopMove": () => {
  457. this.state.scrollMoving = false;
  458. }
  459. }, null)]);
  460. }
  461. });
  462. export default List;