ant-design-vue/components/vc-virtual-list/List.tsx

515 lines
14 KiB
Vue
Raw Normal View History

2021-06-26 01:35:40 +00:00
import type { PropType, Component, CSSProperties } from 'vue';
2020-10-01 09:20:10 +00:00
import {
2021-12-24 06:15:10 +00:00
onMounted,
onUpdated,
2020-10-01 09:20:10 +00:00
ref,
defineComponent,
watchEffect,
computed,
nextTick,
onBeforeUnmount,
reactive,
2021-06-22 07:33:11 +00:00
watch,
2020-10-01 09:20:10 +00:00
} from 'vue';
2021-06-26 01:35:40 +00:00
import type { Key } from '../_util/type';
2020-09-26 14:52:40 +00:00
import Filler from './Filler';
2020-09-28 11:14:00 +00:00
import Item from './Item';
import ScrollBar from './ScrollBar';
import useHeights from './hooks/useHeights';
import useScrollTo from './hooks/useScrollTo';
import useFrameWheel from './hooks/useFrameWheel';
import useMobileTouchMove from './hooks/useMobileTouchMove';
import useOriginScroll from './hooks/useOriginScroll';
import PropTypes from '../_util/vue-types';
import classNames from '../_util/classNames';
2021-06-26 01:35:40 +00:00
import type { RenderFunc, SharedConfig } from './interface';
2020-12-10 09:40:46 +00:00
import supportsPassive from '../_util/supportsPassive';
2020-09-28 11:14:00 +00:00
const EMPTY_DATA = [];
2020-09-26 14:52:40 +00:00
2020-10-01 09:20:10 +00:00
const ScrollStyle: CSSProperties = {
2020-09-26 14:52:40 +00:00
overflowY: 'auto',
overflowAnchor: 'none',
};
3.0 ready (#4523) * refactor: transfer、tooltip (#4306) * refactor(transfer): use composition api (#4135) * refactor(transfer): use composition api * fix: remove console * refactor(tooltip): use composition api (#4059) * refactor(tooltip): use composition api * chore: useConfigInject * fix: remove useless * style: format code * refactor: transfer * refactor: tooltip Co-authored-by: ajuner <106791576@qq.com> * Refactor mentions (#4341) * refactor(mentions): use compositionAPI (#4313) * refactor: mentions * refactor: mentions Co-authored-by: ajuner <106791576@qq.com> * Refactor progress (#4358) * fix: timepicker error border not show #4331 * fix(UploadDragger): fix UploadDrager no export (#4334) * refactor(switch): support customize checked value #4329 (#4332) * refactor(switch): support customize checked value #4329 * test: add test case * refactor: update props name * refactor: update ts * refactor: optimize * style: uncheckedValue to unCheckedValue * test: update snap * feat: udpate switch ts * docs: remove ie11 * fix: tree-select throw error when use slot title * fix: TypeScript definition of Table interface for typescript 4.3.5 (#4353) * fix type for typescript 4.3.5 * Update interface.ts close #4296 * fix: dropdown submenu style error #4351 close #4351 * fix(notification): 完善notification类型 (#4346) * refactor(progress): use composition API (#4355) * refactor(progress): use composition API * refactor(vc-progress): update * refactor: progress * refactor: progress * fix: timepicker error border not show #4331 * fix(UploadDragger): fix UploadDrager no export (#4334) * refactor(switch): support customize checked value #4329 (#4332) * refactor(switch): support customize checked value #4329 * test: add test case * refactor: update props name * refactor: update ts * refactor: optimize * style: uncheckedValue to unCheckedValue * test: update snap * feat: udpate switch ts * docs: remove ie11 * fix: tree-select throw error when use slot title * fix: TypeScript definition of Table interface for typescript 4.3.5 (#4353) * fix type for typescript 4.3.5 * Update interface.ts close #4296 * fix: dropdown submenu style error #4351 close #4351 * fix(notification): 完善notification类型 (#4346) * refactor(progress): use composition API (#4355) * refactor(progress): use composition API * refactor(vc-progress): update * refactor: progress * refactor: progress Co-authored-by: Jarvis <35361626+fanhaoyuan@users.noreply.github.com> Co-authored-by: John <John60676@qq.com> Co-authored-by: 艾斯特洛 <axetroy.dev@gmail.com> Co-authored-by: zanllp <qc@zanllp.cn> * docs: add changelog * refactor: tree * refactor: tree * style: lint * refactor: tree * 热factor: tree * refactor: tree * refactor: tree * refactor: tree * refactor: directory tree * refactor: tree * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * style: lint format * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * fix: upload ts error * fix: update tree title render & switchIcon * test: update tree test * feat: add VirtualScroll tree * refactor: datePicker & calendar & trigger (#4522) * style: update * test: update calendar test * test: update test * test: update test * refactor: slider * feat: update slider css * refactor: slider to ts * refactor: slider to ts * perf: update default itemHeight * test: update * fix: uddate ts type * fix: update skeleton * fix: update skeleton * refactor: update vc-pagination * refactor: pagination * refactor: timeline * refactor: steps * refactor: collapse * refactor: collapse * refactor: popconfirm * refactor: popover * refactor: dropdown * doc: merge doc * chore: vite for dev (#4602) * style: js to jsx * doc: add site * style: lint * style: format ts type * doc: update * style: format code * style: format site * doc: update * style: dmeo * style: format scripts * chore: remove sub-modules * chore: update vite * site: add site build * test: update snap * doc(select): add tip (#4606) * refactor: table (#4641) * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * fix: column not pass to cell * doc: uppate table * fix: update bodyCell headerCell * doc: remove examples * refactor: table * fix: table title not work * fix: table selection * fix: table checkStrictly * refactor: table * fix: table template error * feat: table support summary * test: update snap * perf: table * docs(table): fix ajax demo (#4639) * test: update table * refactor: remove old table * doc: update table doc * doc: update doc * doc: update select * doc: update summary Co-authored-by: John <John60676@qq.com> * doc: update doc * fix: menu arrow not work * test: update * doc: add next site * style: format * doc: update * doc: update site script * fix: expand icon not fixed * feat: use renderSlot * test: update table snap * feat: confirm support reactively * feat: configProvider.config * feat: message support configprovider.config * feat: notification support configprovider.config * doc: update doc * fix: typescript compile error * style: add import eslint * doc: update demo * chore: set transpileOnly true * style: fix eslint error * test: update snap * doc: update * test: mock date * test: update snap * chore: remove gulp-typescript (#4675) * feat: V3 form (#4678) * chore: update husky * perf: update formItem * perf: useInjectFormItemContext * fix: table ts error * doc: add Customized Form Controls demo * feat: export useInjectFormItemContext * doc: update form doc * doc: update doc * doc: update doc * feat: autocomplete support option slot * doc: update * feat: add form item rest * style: remove omit.js * refactor: autocomplete * doc: add changelog to site * doc: update site anchor * doc: update doc layout * test: update table test * doc: update * chore: udpate gulp script * chore: udpate gulp script * doc: add changelog * doc: update * test: ignore some test wait vue-test-utils * fix: form id error #4582 close #4582 * doc: add select Responsive demo * doc: remove temp doc Co-authored-by: ajuner <106791576@qq.com> Co-authored-by: Jarvis <35361626+fanhaoyuan@users.noreply.github.com> Co-authored-by: John <John60676@qq.com> Co-authored-by: 艾斯特洛 <axetroy.dev@gmail.com> Co-authored-by: zanllp <qc@zanllp.cn> Co-authored-by: Amour1688 <lcz_1996@foxmail.com>
2021-09-25 08:51:32 +00:00
export type ScrollAlign = 'top' | 'bottom' | 'auto';
export type ScrollConfig =
| {
index: number;
align?: ScrollAlign;
offset?: number;
}
| {
key: Key;
align?: ScrollAlign;
offset?: number;
};
export type ScrollTo = (arg: number | ScrollConfig) => void;
2020-10-01 09:20:10 +00:00
function renderChildren<T>(
list: T[],
startIndex: number,
endIndex: number,
setNodeRef: (item: T, element: HTMLElement) => void,
renderFunc: RenderFunc<T>,
{ getKey }: SharedConfig<T>,
) {
2020-09-28 11:14:00 +00:00
return list.slice(startIndex, endIndex + 1).map((item, index) => {
const eleIndex = startIndex + index;
const node = renderFunc(item, eleIndex, {
// style: status === 'MEASURE_START' ? { visibility: 'hidden' } : {},
});
const key = getKey(item);
return (
2020-10-17 04:14:13 +00:00
<Item key={key} setRef={ele => setNodeRef(item, ele as HTMLElement)}>
2020-09-28 11:14:00 +00:00
{node}
</Item>
);
});
}
2021-06-22 07:33:11 +00:00
export interface ListState {
2020-10-01 09:20:10 +00:00
scrollTop: number;
scrollMoving: boolean;
}
2020-09-28 11:14:00 +00:00
2020-10-01 09:20:10 +00:00
const List = defineComponent({
2020-09-26 14:52:40 +00:00
name: 'List',
2020-11-07 07:37:17 +00:00
inheritAttrs: false,
2020-10-01 09:20:10 +00:00
props: {
prefixCls: PropTypes.string,
data: PropTypes.array,
height: PropTypes.number,
itemHeight: PropTypes.number,
/** If not match virtual scroll condition, Set List still use height of container. */
fullHeight: PropTypes.looseBool,
2020-10-01 09:20:10 +00:00
itemKey: {
3.0 ready (#4523) * refactor: transfer、tooltip (#4306) * refactor(transfer): use composition api (#4135) * refactor(transfer): use composition api * fix: remove console * refactor(tooltip): use composition api (#4059) * refactor(tooltip): use composition api * chore: useConfigInject * fix: remove useless * style: format code * refactor: transfer * refactor: tooltip Co-authored-by: ajuner <106791576@qq.com> * Refactor mentions (#4341) * refactor(mentions): use compositionAPI (#4313) * refactor: mentions * refactor: mentions Co-authored-by: ajuner <106791576@qq.com> * Refactor progress (#4358) * fix: timepicker error border not show #4331 * fix(UploadDragger): fix UploadDrager no export (#4334) * refactor(switch): support customize checked value #4329 (#4332) * refactor(switch): support customize checked value #4329 * test: add test case * refactor: update props name * refactor: update ts * refactor: optimize * style: uncheckedValue to unCheckedValue * test: update snap * feat: udpate switch ts * docs: remove ie11 * fix: tree-select throw error when use slot title * fix: TypeScript definition of Table interface for typescript 4.3.5 (#4353) * fix type for typescript 4.3.5 * Update interface.ts close #4296 * fix: dropdown submenu style error #4351 close #4351 * fix(notification): 完善notification类型 (#4346) * refactor(progress): use composition API (#4355) * refactor(progress): use composition API * refactor(vc-progress): update * refactor: progress * refactor: progress * fix: timepicker error border not show #4331 * fix(UploadDragger): fix UploadDrager no export (#4334) * refactor(switch): support customize checked value #4329 (#4332) * refactor(switch): support customize checked value #4329 * test: add test case * refactor: update props name * refactor: update ts * refactor: optimize * style: uncheckedValue to unCheckedValue * test: update snap * feat: udpate switch ts * docs: remove ie11 * fix: tree-select throw error when use slot title * fix: TypeScript definition of Table interface for typescript 4.3.5 (#4353) * fix type for typescript 4.3.5 * Update interface.ts close #4296 * fix: dropdown submenu style error #4351 close #4351 * fix(notification): 完善notification类型 (#4346) * refactor(progress): use composition API (#4355) * refactor(progress): use composition API * refactor(vc-progress): update * refactor: progress * refactor: progress Co-authored-by: Jarvis <35361626+fanhaoyuan@users.noreply.github.com> Co-authored-by: John <John60676@qq.com> Co-authored-by: 艾斯特洛 <axetroy.dev@gmail.com> Co-authored-by: zanllp <qc@zanllp.cn> * docs: add changelog * refactor: tree * refactor: tree * style: lint * refactor: tree * 热factor: tree * refactor: tree * refactor: tree * refactor: tree * refactor: directory tree * refactor: tree * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * style: lint format * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * fix: upload ts error * fix: update tree title render & switchIcon * test: update tree test * feat: add VirtualScroll tree * refactor: datePicker & calendar & trigger (#4522) * style: update * test: update calendar test * test: update test * test: update test * refactor: slider * feat: update slider css * refactor: slider to ts * refactor: slider to ts * perf: update default itemHeight * test: update * fix: uddate ts type * fix: update skeleton * fix: update skeleton * refactor: update vc-pagination * refactor: pagination * refactor: timeline * refactor: steps * refactor: collapse * refactor: collapse * refactor: popconfirm * refactor: popover * refactor: dropdown * doc: merge doc * chore: vite for dev (#4602) * style: js to jsx * doc: add site * style: lint * style: format ts type * doc: update * style: format code * style: format site * doc: update * style: dmeo * style: format scripts * chore: remove sub-modules * chore: update vite * site: add site build * test: update snap * doc(select): add tip (#4606) * refactor: table (#4641) * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * fix: column not pass to cell * doc: uppate table * fix: update bodyCell headerCell * doc: remove examples * refactor: table * fix: table title not work * fix: table selection * fix: table checkStrictly * refactor: table * fix: table template error * feat: table support summary * test: update snap * perf: table * docs(table): fix ajax demo (#4639) * test: update table * refactor: remove old table * doc: update table doc * doc: update doc * doc: update select * doc: update summary Co-authored-by: John <John60676@qq.com> * doc: update doc * fix: menu arrow not work * test: update * doc: add next site * style: format * doc: update * doc: update site script * fix: expand icon not fixed * feat: use renderSlot * test: update table snap * feat: confirm support reactively * feat: configProvider.config * feat: message support configprovider.config * feat: notification support configprovider.config * doc: update doc * fix: typescript compile error * style: add import eslint * doc: update demo * chore: set transpileOnly true * style: fix eslint error * test: update snap * doc: update * test: mock date * test: update snap * chore: remove gulp-typescript (#4675) * feat: V3 form (#4678) * chore: update husky * perf: update formItem * perf: useInjectFormItemContext * fix: table ts error * doc: add Customized Form Controls demo * feat: export useInjectFormItemContext * doc: update form doc * doc: update doc * doc: update doc * feat: autocomplete support option slot * doc: update * feat: add form item rest * style: remove omit.js * refactor: autocomplete * doc: add changelog to site * doc: update site anchor * doc: update doc layout * test: update table test * doc: update * chore: udpate gulp script * chore: udpate gulp script * doc: add changelog * doc: update * test: ignore some test wait vue-test-utils * fix: form id error #4582 close #4582 * doc: add select Responsive demo * doc: remove temp doc Co-authored-by: ajuner <106791576@qq.com> Co-authored-by: Jarvis <35361626+fanhaoyuan@users.noreply.github.com> Co-authored-by: John <John60676@qq.com> Co-authored-by: 艾斯特洛 <axetroy.dev@gmail.com> Co-authored-by: zanllp <qc@zanllp.cn> Co-authored-by: Amour1688 <lcz_1996@foxmail.com>
2021-09-25 08:51:32 +00:00
type: [String, Number, Function] as PropType<Key | ((item: Record<string, any>) => Key)>,
2020-10-01 09:20:10 +00:00
required: true,
},
component: {
type: [String, Object] as PropType<string | Component>,
},
/** Set `false` will always use real scroll instead of virtual one */
virtual: PropTypes.looseBool,
2020-10-01 09:20:10 +00:00
children: PropTypes.func,
onScroll: PropTypes.func,
2020-10-07 14:49:01 +00:00
onMousedown: PropTypes.func,
onMouseenter: PropTypes.func,
onVisibleChange: Function as PropType<(visibleList: any[], fullList: any[]) => void>,
2020-10-01 09:20:10 +00:00
},
3.0 ready (#4523) * refactor: transfer、tooltip (#4306) * refactor(transfer): use composition api (#4135) * refactor(transfer): use composition api * fix: remove console * refactor(tooltip): use composition api (#4059) * refactor(tooltip): use composition api * chore: useConfigInject * fix: remove useless * style: format code * refactor: transfer * refactor: tooltip Co-authored-by: ajuner <106791576@qq.com> * Refactor mentions (#4341) * refactor(mentions): use compositionAPI (#4313) * refactor: mentions * refactor: mentions Co-authored-by: ajuner <106791576@qq.com> * Refactor progress (#4358) * fix: timepicker error border not show #4331 * fix(UploadDragger): fix UploadDrager no export (#4334) * refactor(switch): support customize checked value #4329 (#4332) * refactor(switch): support customize checked value #4329 * test: add test case * refactor: update props name * refactor: update ts * refactor: optimize * style: uncheckedValue to unCheckedValue * test: update snap * feat: udpate switch ts * docs: remove ie11 * fix: tree-select throw error when use slot title * fix: TypeScript definition of Table interface for typescript 4.3.5 (#4353) * fix type for typescript 4.3.5 * Update interface.ts close #4296 * fix: dropdown submenu style error #4351 close #4351 * fix(notification): 完善notification类型 (#4346) * refactor(progress): use composition API (#4355) * refactor(progress): use composition API * refactor(vc-progress): update * refactor: progress * refactor: progress * fix: timepicker error border not show #4331 * fix(UploadDragger): fix UploadDrager no export (#4334) * refactor(switch): support customize checked value #4329 (#4332) * refactor(switch): support customize checked value #4329 * test: add test case * refactor: update props name * refactor: update ts * refactor: optimize * style: uncheckedValue to unCheckedValue * test: update snap * feat: udpate switch ts * docs: remove ie11 * fix: tree-select throw error when use slot title * fix: TypeScript definition of Table interface for typescript 4.3.5 (#4353) * fix type for typescript 4.3.5 * Update interface.ts close #4296 * fix: dropdown submenu style error #4351 close #4351 * fix(notification): 完善notification类型 (#4346) * refactor(progress): use composition API (#4355) * refactor(progress): use composition API * refactor(vc-progress): update * refactor: progress * refactor: progress Co-authored-by: Jarvis <35361626+fanhaoyuan@users.noreply.github.com> Co-authored-by: John <John60676@qq.com> Co-authored-by: 艾斯特洛 <axetroy.dev@gmail.com> Co-authored-by: zanllp <qc@zanllp.cn> * docs: add changelog * refactor: tree * refactor: tree * style: lint * refactor: tree * 热factor: tree * refactor: tree * refactor: tree * refactor: tree * refactor: directory tree * refactor: tree * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * style: lint format * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * fix: upload ts error * fix: update tree title render & switchIcon * test: update tree test * feat: add VirtualScroll tree * refactor: datePicker & calendar & trigger (#4522) * style: update * test: update calendar test * test: update test * test: update test * refactor: slider * feat: update slider css * refactor: slider to ts * refactor: slider to ts * perf: update default itemHeight * test: update * fix: uddate ts type * fix: update skeleton * fix: update skeleton * refactor: update vc-pagination * refactor: pagination * refactor: timeline * refactor: steps * refactor: collapse * refactor: collapse * refactor: popconfirm * refactor: popover * refactor: dropdown * doc: merge doc * chore: vite for dev (#4602) * style: js to jsx * doc: add site * style: lint * style: format ts type * doc: update * style: format code * style: format site * doc: update * style: dmeo * style: format scripts * chore: remove sub-modules * chore: update vite * site: add site build * test: update snap * doc(select): add tip (#4606) * refactor: table (#4641) * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * fix: column not pass to cell * doc: uppate table * fix: update bodyCell headerCell * doc: remove examples * refactor: table * fix: table title not work * fix: table selection * fix: table checkStrictly * refactor: table * fix: table template error * feat: table support summary * test: update snap * perf: table * docs(table): fix ajax demo (#4639) * test: update table * refactor: remove old table * doc: update table doc * doc: update doc * doc: update select * doc: update summary Co-authored-by: John <John60676@qq.com> * doc: update doc * fix: menu arrow not work * test: update * doc: add next site * style: format * doc: update * doc: update site script * fix: expand icon not fixed * feat: use renderSlot * test: update table snap * feat: confirm support reactively * feat: configProvider.config * feat: message support configprovider.config * feat: notification support configprovider.config * doc: update doc * fix: typescript compile error * style: add import eslint * doc: update demo * chore: set transpileOnly true * style: fix eslint error * test: update snap * doc: update * test: mock date * test: update snap * chore: remove gulp-typescript (#4675) * feat: V3 form (#4678) * chore: update husky * perf: update formItem * perf: useInjectFormItemContext * fix: table ts error * doc: add Customized Form Controls demo * feat: export useInjectFormItemContext * doc: update form doc * doc: update doc * doc: update doc * feat: autocomplete support option slot * doc: update * feat: add form item rest * style: remove omit.js * refactor: autocomplete * doc: add changelog to site * doc: update site anchor * doc: update doc layout * test: update table test * doc: update * chore: udpate gulp script * chore: udpate gulp script * doc: add changelog * doc: update * test: ignore some test wait vue-test-utils * fix: form id error #4582 close #4582 * doc: add select Responsive demo * doc: remove temp doc Co-authored-by: ajuner <106791576@qq.com> Co-authored-by: Jarvis <35361626+fanhaoyuan@users.noreply.github.com> Co-authored-by: John <John60676@qq.com> Co-authored-by: 艾斯特洛 <axetroy.dev@gmail.com> Co-authored-by: zanllp <qc@zanllp.cn> Co-authored-by: Amour1688 <lcz_1996@foxmail.com>
2021-09-25 08:51:32 +00:00
setup(props, { expose }) {
2020-09-28 11:14:00 +00:00
// ================================= MISC =================================
2020-11-07 07:08:42 +00:00
const useVirtual = computed(() => {
2020-11-04 10:59:08 +00:00
const { height, itemHeight, virtual } = props;
return !!(virtual !== false && height && itemHeight);
2020-11-07 07:08:42 +00:00
});
2020-09-28 11:14:00 +00:00
const inVirtual = computed(() => {
2020-11-04 10:59:08 +00:00
const { height, itemHeight, data } = props;
return useVirtual.value && data && itemHeight * data.length > height;
2020-09-28 11:14:00 +00:00
});
2020-10-01 09:20:10 +00:00
const state = reactive<ListState>({
2020-09-28 11:14:00 +00:00
scrollTop: 0,
scrollMoving: false,
2021-06-22 07:33:11 +00:00
});
const mergedData = computed(() => {
return props.data || EMPTY_DATA;
2020-09-28 11:14:00 +00:00
});
2020-10-17 04:14:13 +00:00
const componentRef = ref<HTMLDivElement>();
2020-11-04 10:59:08 +00:00
const fillerInnerRef = ref<HTMLDivElement>();
const scrollBarRef = ref<any>(); // Hack on scrollbar to enable flash call
2020-09-28 11:14:00 +00:00
// =============================== Item Key ===============================
2020-10-01 09:20:10 +00:00
const getKey = (item: Record<string, any>) => {
2020-09-28 11:14:00 +00:00
if (typeof props.itemKey === 'function') {
return props.itemKey(item);
2020-09-26 14:52:40 +00:00
}
2021-06-22 07:33:11 +00:00
return item?.[props.itemKey];
2020-09-28 11:14:00 +00:00
};
2020-09-26 14:52:40 +00:00
2020-09-28 11:14:00 +00:00
const sharedConfig = {
getKey,
};
2020-09-26 14:52:40 +00:00
2020-09-28 11:14:00 +00:00
// ================================ Scroll ================================
2020-10-01 09:20:10 +00:00
function syncScrollTop(newTop: number | ((prev: number) => number)) {
let value: number;
2020-09-28 11:14:00 +00:00
if (typeof newTop === 'function') {
value = newTop(state.scrollTop);
} else {
value = newTop;
2020-09-26 14:52:40 +00:00
}
2020-09-28 11:14:00 +00:00
const alignedTop = keepInRange(value);
2020-09-26 14:52:40 +00:00
2020-10-01 09:20:10 +00:00
if (componentRef.value) {
componentRef.value.scrollTop = alignedTop;
2020-09-29 07:16:56 +00:00
}
2020-09-28 13:21:43 +00:00
state.scrollTop = alignedTop;
2020-09-28 11:14:00 +00:00
}
2020-09-26 14:52:40 +00:00
2020-09-28 11:14:00 +00:00
// ================================ Height ================================
const [setInstance, collectHeight, heights, updatedMark] = useHeights(
mergedData,
getKey,
null,
null,
);
2021-12-24 06:15:10 +00:00
const calRes = reactive<{
2021-06-22 07:33:11 +00:00
scrollHeight?: number;
start?: number;
end?: number;
offset?: number;
2021-12-24 06:15:10 +00:00
}>({
scrollHeight: undefined,
start: 0,
end: 0,
offset: undefined,
});
const offsetHeight = ref(0);
onMounted(() => {
nextTick(() => {
offsetHeight.value = fillerInnerRef.value?.offsetHeight || 0;
});
});
onUpdated(() => {
nextTick(() => {
offsetHeight.value = fillerInnerRef.value?.offsetHeight || 0;
});
});
watch(
[useVirtual, mergedData],
() => {
if (!useVirtual.value) {
Object.assign(calRes, {
scrollHeight: undefined,
start: 0,
end: mergedData.value.length - 1,
offset: undefined,
});
}
},
{ immediate: true },
);
watch(
[useVirtual, mergedData, offsetHeight, inVirtual],
() => {
// Always use virtual scroll bar in avoid shaking
if (useVirtual.value && !inVirtual.value) {
Object.assign(calRes, {
scrollHeight: offsetHeight.value,
start: 0,
end: mergedData.value.length - 1,
offset: undefined,
});
}
},
{ immediate: true },
);
2021-06-22 07:33:11 +00:00
watch(
[
inVirtual,
useVirtual,
() => state.scrollTop,
mergedData,
updatedMark,
heights,
() => props.height,
2021-12-24 06:15:10 +00:00
offsetHeight,
],
2021-06-22 07:33:11 +00:00
() => {
2021-12-24 06:15:10 +00:00
if (!useVirtual.value || !inVirtual.value) {
return;
}
2020-09-26 14:52:40 +00:00
2021-12-24 06:15:10 +00:00
let itemTop = 0;
let startIndex: number | undefined;
let startOffset: number | undefined;
let endIndex: number | undefined;
const dataLen = mergedData.value.length;
const data = mergedData.value;
for (let i = 0; i < dataLen; i += 1) {
const item = data[i];
const key = getKey(item);
const cacheHeight = heights.value[key];
const currentItemBottom =
itemTop + (cacheHeight === undefined ? props.itemHeight! : cacheHeight);
if (currentItemBottom >= state.scrollTop && startIndex === undefined) {
startIndex = i;
startOffset = itemTop;
2021-06-22 07:33:11 +00:00
}
2020-09-26 14:52:40 +00:00
2021-12-24 06:15:10 +00:00
// Check item bottom in the range. We will render additional one item for motion usage
if (currentItemBottom > state.scrollTop + props.height! && endIndex === undefined) {
endIndex = i;
2021-06-22 07:33:11 +00:00
}
2020-09-26 14:52:40 +00:00
2021-12-24 06:15:10 +00:00
itemTop = currentItemBottom;
}
// Fallback to normal if not match. This code should never reach
/* istanbul ignore next */
if (startIndex === undefined) {
startIndex = 0;
startOffset = 0;
}
if (endIndex === undefined) {
endIndex = dataLen - 1;
}
2021-12-24 06:15:10 +00:00
// Give cache to improve scroll experience
endIndex = Math.min(endIndex + 1, dataLen);
Object.assign(calRes, {
scrollHeight: itemTop,
start: startIndex,
end: endIndex,
offset: startOffset,
});
2021-06-22 07:33:11 +00:00
},
2021-12-24 06:15:10 +00:00
{ immediate: true },
2021-06-22 07:33:11 +00:00
);
2020-09-28 11:14:00 +00:00
// =============================== In Range ===============================
2021-12-24 06:15:10 +00:00
const maxScrollHeight = computed(() => calRes.scrollHeight! - props.height!);
2020-09-26 14:52:40 +00:00
2020-10-01 09:20:10 +00:00
function keepInRange(newScrollTop: number) {
2021-06-22 07:33:11 +00:00
let newTop = newScrollTop;
2020-09-28 11:14:00 +00:00
if (!Number.isNaN(maxScrollHeight.value)) {
newTop = Math.min(newTop, maxScrollHeight.value);
}
2021-06-22 07:33:11 +00:00
newTop = Math.max(newTop, 0);
2020-09-28 11:14:00 +00:00
return newTop;
}
2020-09-26 14:52:40 +00:00
2020-09-28 11:14:00 +00:00
const isScrollAtTop = computed(() => state.scrollTop <= 0);
const isScrollAtBottom = computed(() => state.scrollTop >= maxScrollHeight.value);
2020-09-26 14:52:40 +00:00
2020-09-28 11:14:00 +00:00
const originScroll = useOriginScroll(isScrollAtTop, isScrollAtBottom);
2020-09-26 14:52:40 +00:00
2020-09-28 11:14:00 +00:00
// ================================ Scroll ================================
2020-10-01 09:20:10 +00:00
function onScrollBar(newScrollTop: number) {
2020-09-28 11:14:00 +00:00
const newTop = newScrollTop;
syncScrollTop(newTop);
}
2020-09-26 14:52:40 +00:00
2021-06-22 07:33:11 +00:00
// When data size reduce. It may trigger native scroll event back to fit scroll position
2020-10-01 09:20:10 +00:00
function onFallbackScroll(e: UIEvent) {
const { scrollTop: newScrollTop } = e.currentTarget as Element;
2020-10-18 09:37:52 +00:00
if (Math.abs(newScrollTop - state.scrollTop) >= 1) {
2020-09-28 11:14:00 +00:00
syncScrollTop(newScrollTop);
2020-09-26 14:52:40 +00:00
}
2020-09-28 11:14:00 +00:00
// Trigger origin onScroll
props.onScroll?.(e);
}
// Since this added in global,should use ref to keep update
const [onRawWheel, onFireFoxScroll] = useFrameWheel(
2020-11-04 10:59:08 +00:00
useVirtual,
2020-09-28 11:14:00 +00:00
isScrollAtTop,
isScrollAtBottom,
offsetY => {
syncScrollTop(top => {
const newTop = top + offsetY;
return newTop;
2020-09-26 14:52:40 +00:00
});
2020-09-28 11:14:00 +00:00
},
);
2020-09-26 14:52:40 +00:00
2020-09-28 11:14:00 +00:00
// Mobile touch move
2020-11-04 10:59:08 +00:00
useMobileTouchMove(useVirtual, componentRef, (deltaY, smoothOffset) => {
2020-09-28 11:14:00 +00:00
if (originScroll(deltaY, smoothOffset)) {
return false;
2020-09-26 14:52:40 +00:00
}
2020-10-01 09:20:10 +00:00
onRawWheel({ preventDefault() {}, deltaY } as WheelEvent);
2020-09-28 11:14:00 +00:00
return true;
});
// Firefox only
2020-10-01 09:20:10 +00:00
function onMozMousePixelScroll(e: MouseEvent) {
2020-11-04 10:59:08 +00:00
if (useVirtual.value) {
e.preventDefault();
}
}
const removeEventListener = () => {
2020-10-01 09:20:10 +00:00
if (componentRef.value) {
2020-12-10 09:40:46 +00:00
componentRef.value.removeEventListener(
'wheel',
onRawWheel,
2020-12-18 10:54:18 +00:00
supportsPassive ? ({ passive: false } as EventListenerOptions) : false,
2020-12-10 09:40:46 +00:00
);
2020-10-17 04:14:13 +00:00
componentRef.value.removeEventListener('DOMMouseScroll', onFireFoxScroll as any);
componentRef.value.removeEventListener('MozMousePixelScroll', onMozMousePixelScroll as any);
}
};
2020-09-28 11:14:00 +00:00
watchEffect(() => {
nextTick(() => {
2020-10-01 09:20:10 +00:00
if (componentRef.value) {
removeEventListener();
2020-12-10 09:40:46 +00:00
componentRef.value.addEventListener(
'wheel',
onRawWheel,
2020-12-18 10:54:18 +00:00
supportsPassive ? ({ passive: false } as EventListenerOptions) : false,
2020-12-10 09:40:46 +00:00
);
2020-10-17 04:14:13 +00:00
componentRef.value.addEventListener('DOMMouseScroll', onFireFoxScroll as any);
componentRef.value.addEventListener('MozMousePixelScroll', onMozMousePixelScroll as any);
2020-09-26 14:52:40 +00:00
}
2020-09-28 11:14:00 +00:00
});
});
2020-09-26 14:52:40 +00:00
onBeforeUnmount(() => {
removeEventListener();
});
2020-09-28 11:14:00 +00:00
// ================================= Ref ==================================
const scrollTo = useScrollTo(
componentRef,
2021-06-22 07:33:11 +00:00
mergedData,
2020-09-28 11:14:00 +00:00
heights,
2020-09-29 07:16:56 +00:00
props,
2020-09-28 11:14:00 +00:00
getKey,
collectHeight,
syncScrollTop,
2020-11-04 10:59:08 +00:00
() => {
scrollBarRef.value?.delayHidden();
},
2020-09-28 11:14:00 +00:00
);
2020-09-26 14:52:40 +00:00
3.0 ready (#4523) * refactor: transfer、tooltip (#4306) * refactor(transfer): use composition api (#4135) * refactor(transfer): use composition api * fix: remove console * refactor(tooltip): use composition api (#4059) * refactor(tooltip): use composition api * chore: useConfigInject * fix: remove useless * style: format code * refactor: transfer * refactor: tooltip Co-authored-by: ajuner <106791576@qq.com> * Refactor mentions (#4341) * refactor(mentions): use compositionAPI (#4313) * refactor: mentions * refactor: mentions Co-authored-by: ajuner <106791576@qq.com> * Refactor progress (#4358) * fix: timepicker error border not show #4331 * fix(UploadDragger): fix UploadDrager no export (#4334) * refactor(switch): support customize checked value #4329 (#4332) * refactor(switch): support customize checked value #4329 * test: add test case * refactor: update props name * refactor: update ts * refactor: optimize * style: uncheckedValue to unCheckedValue * test: update snap * feat: udpate switch ts * docs: remove ie11 * fix: tree-select throw error when use slot title * fix: TypeScript definition of Table interface for typescript 4.3.5 (#4353) * fix type for typescript 4.3.5 * Update interface.ts close #4296 * fix: dropdown submenu style error #4351 close #4351 * fix(notification): 完善notification类型 (#4346) * refactor(progress): use composition API (#4355) * refactor(progress): use composition API * refactor(vc-progress): update * refactor: progress * refactor: progress * fix: timepicker error border not show #4331 * fix(UploadDragger): fix UploadDrager no export (#4334) * refactor(switch): support customize checked value #4329 (#4332) * refactor(switch): support customize checked value #4329 * test: add test case * refactor: update props name * refactor: update ts * refactor: optimize * style: uncheckedValue to unCheckedValue * test: update snap * feat: udpate switch ts * docs: remove ie11 * fix: tree-select throw error when use slot title * fix: TypeScript definition of Table interface for typescript 4.3.5 (#4353) * fix type for typescript 4.3.5 * Update interface.ts close #4296 * fix: dropdown submenu style error #4351 close #4351 * fix(notification): 完善notification类型 (#4346) * refactor(progress): use composition API (#4355) * refactor(progress): use composition API * refactor(vc-progress): update * refactor: progress * refactor: progress Co-authored-by: Jarvis <35361626+fanhaoyuan@users.noreply.github.com> Co-authored-by: John <John60676@qq.com> Co-authored-by: 艾斯特洛 <axetroy.dev@gmail.com> Co-authored-by: zanllp <qc@zanllp.cn> * docs: add changelog * refactor: tree * refactor: tree * style: lint * refactor: tree * 热factor: tree * refactor: tree * refactor: tree * refactor: tree * refactor: directory tree * refactor: tree * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * style: lint format * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * refactor: tree-select * fix: upload ts error * fix: update tree title render & switchIcon * test: update tree test * feat: add VirtualScroll tree * refactor: datePicker & calendar & trigger (#4522) * style: update * test: update calendar test * test: update test * test: update test * refactor: slider * feat: update slider css * refactor: slider to ts * refactor: slider to ts * perf: update default itemHeight * test: update * fix: uddate ts type * fix: update skeleton * fix: update skeleton * refactor: update vc-pagination * refactor: pagination * refactor: timeline * refactor: steps * refactor: collapse * refactor: collapse * refactor: popconfirm * refactor: popover * refactor: dropdown * doc: merge doc * chore: vite for dev (#4602) * style: js to jsx * doc: add site * style: lint * style: format ts type * doc: update * style: format code * style: format site * doc: update * style: dmeo * style: format scripts * chore: remove sub-modules * chore: update vite * site: add site build * test: update snap * doc(select): add tip (#4606) * refactor: table (#4641) * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * refactor: table * fix: column not pass to cell * doc: uppate table * fix: update bodyCell headerCell * doc: remove examples * refactor: table * fix: table title not work * fix: table selection * fix: table checkStrictly * refactor: table * fix: table template error * feat: table support summary * test: update snap * perf: table * docs(table): fix ajax demo (#4639) * test: update table * refactor: remove old table * doc: update table doc * doc: update doc * doc: update select * doc: update summary Co-authored-by: John <John60676@qq.com> * doc: update doc * fix: menu arrow not work * test: update * doc: add next site * style: format * doc: update * doc: update site script * fix: expand icon not fixed * feat: use renderSlot * test: update table snap * feat: confirm support reactively * feat: configProvider.config * feat: message support configprovider.config * feat: notification support configprovider.config * doc: update doc * fix: typescript compile error * style: add import eslint * doc: update demo * chore: set transpileOnly true * style: fix eslint error * test: update snap * doc: update * test: mock date * test: update snap * chore: remove gulp-typescript (#4675) * feat: V3 form (#4678) * chore: update husky * perf: update formItem * perf: useInjectFormItemContext * fix: table ts error * doc: add Customized Form Controls demo * feat: export useInjectFormItemContext * doc: update form doc * doc: update doc * doc: update doc * feat: autocomplete support option slot * doc: update * feat: add form item rest * style: remove omit.js * refactor: autocomplete * doc: add changelog to site * doc: update site anchor * doc: update doc layout * test: update table test * doc: update * chore: udpate gulp script * chore: udpate gulp script * doc: add changelog * doc: update * test: ignore some test wait vue-test-utils * fix: form id error #4582 close #4582 * doc: add select Responsive demo * doc: remove temp doc Co-authored-by: ajuner <106791576@qq.com> Co-authored-by: Jarvis <35361626+fanhaoyuan@users.noreply.github.com> Co-authored-by: John <John60676@qq.com> Co-authored-by: 艾斯特洛 <axetroy.dev@gmail.com> Co-authored-by: zanllp <qc@zanllp.cn> Co-authored-by: Amour1688 <lcz_1996@foxmail.com>
2021-09-25 08:51:32 +00:00
expose({
scrollTo,
});
2020-09-28 11:14:00 +00:00
const componentStyle = computed(() => {
2020-10-01 09:20:10 +00:00
let cs: CSSProperties | null = null;
2020-09-28 11:14:00 +00:00
if (props.height) {
cs = { [props.fullHeight ? 'height' : 'maxHeight']: props.height + 'px', ...ScrollStyle };
2020-11-04 10:59:08 +00:00
if (useVirtual.value) {
2020-10-01 09:20:10 +00:00
cs!.overflowY = 'hidden';
2020-09-28 11:14:00 +00:00
if (state.scrollMoving) {
2020-10-01 09:20:10 +00:00
cs!.pointerEvents = 'none';
2020-09-28 11:14:00 +00:00
}
}
2020-09-26 14:52:40 +00:00
}
2020-09-28 11:14:00 +00:00
return cs;
});
// ================================ Effect ================================
/** We need told outside that some list not rendered */
watch(
[() => calRes.start, () => calRes.end, mergedData],
() => {
if (props.onVisibleChange) {
const renderList = mergedData.value.slice(calRes.start, calRes.end + 1);
props.onVisibleChange(renderList, mergedData.value);
}
},
{ flush: 'post' },
);
2020-09-28 11:14:00 +00:00
return {
state,
2021-06-22 07:33:11 +00:00
mergedData,
2020-09-28 11:14:00 +00:00
componentStyle,
onFallbackScroll,
onScrollBar,
componentRef,
2020-11-04 10:59:08 +00:00
useVirtual,
2020-09-28 11:14:00 +00:00
calRes,
collectHeight,
setInstance,
sharedConfig,
2020-11-04 10:59:08 +00:00
scrollBarRef,
2020-11-07 07:08:42 +00:00
fillerInnerRef,
2020-09-28 11:14:00 +00:00
};
2020-09-26 14:52:40 +00:00
},
render() {
const {
2020-09-28 11:14:00 +00:00
prefixCls = 'rc-virtual-list',
2020-09-26 14:52:40 +00:00
height,
itemHeight,
2020-09-28 11:14:00 +00:00
// eslint-disable-next-line no-unused-vars
2020-09-29 07:16:56 +00:00
fullHeight,
2020-09-26 14:52:40 +00:00
data,
itemKey,
virtual,
2020-09-28 11:14:00 +00:00
component: Component = 'div',
onScroll,
2021-12-23 15:23:01 +00:00
children = this.$slots.default,
2020-09-30 08:21:04 +00:00
style,
class: className,
2020-09-26 14:52:40 +00:00
...restProps
2020-10-07 14:49:01 +00:00
} = { ...this.$props, ...this.$attrs } as any;
2020-09-28 11:14:00 +00:00
const mergedClassName = classNames(prefixCls, className);
2021-06-22 07:33:11 +00:00
const { scrollTop } = this.state;
2020-09-28 11:14:00 +00:00
const { scrollHeight, offset, start, end } = this.calRes;
const {
componentStyle,
onFallbackScroll,
onScrollBar,
2020-11-04 10:59:08 +00:00
useVirtual,
2020-09-28 11:14:00 +00:00
collectHeight,
sharedConfig,
setInstance,
2021-06-22 07:33:11 +00:00
mergedData,
2020-09-28 11:14:00 +00:00
} = this;
return (
<div
style={{
...style,
position: 'relative',
}}
class={mergedClassName}
{...restProps}
>
2020-09-26 14:52:40 +00:00
<Component
2020-09-28 11:14:00 +00:00
class={`${prefixCls}-holder`}
style={componentStyle}
2020-10-01 09:20:10 +00:00
ref="componentRef"
2020-09-28 11:14:00 +00:00
onScroll={onFallbackScroll}
2020-09-26 14:52:40 +00:00
>
2020-09-28 11:14:00 +00:00
<Filler
prefixCls={prefixCls}
height={scrollHeight}
offset={offset}
onInnerResize={collectHeight}
2020-11-04 10:59:08 +00:00
ref="fillerInnerRef"
2021-12-24 06:15:10 +00:00
v-slots={{
default: () =>
renderChildren(mergedData, start, end, setInstance, children, sharedConfig),
}}
></Filler>
2020-09-26 14:52:40 +00:00
</Component>
2020-11-04 10:59:08 +00:00
{useVirtual && (
2020-09-28 11:14:00 +00:00
<ScrollBar
2020-11-04 10:59:08 +00:00
ref="scrollBarRef"
2020-09-28 11:14:00 +00:00
prefixCls={prefixCls}
scrollTop={scrollTop}
height={height}
scrollHeight={scrollHeight}
count={mergedData.length}
onScroll={onScrollBar}
onStartMove={() => {
this.state.scrollMoving = true;
}}
onStopMove={() => {
this.state.scrollMoving = false;
}}
/>
)}
</div>
2020-09-26 14:52:40 +00:00
);
},
2020-10-01 09:20:10 +00:00
});
2020-09-28 11:14:00 +00:00
export default List;