From beed7f2e1172531fe691384a50c8b0457f3a80d8 Mon Sep 17 00:00:00 2001 From: bowen <54492610+jiaowoxiaobala@users.noreply.github.com> Date: Thu, 2 Nov 2023 11:26:35 +0800 Subject: [PATCH] feat(Upload): file list add drag func (#3227). resolve #3179 --- src/components/Upload/src/BasicUpload.vue | 2 ++ src/components/Upload/src/FileList.vue | 40 ++++++++++++++++++++--- src/components/Upload/src/UploadModal.vue | 8 ++++- src/components/Upload/src/props.ts | 29 ++++++++++++++++ src/hooks/web/useSortable.ts | 8 +++-- src/utils/types.ts | 2 +- 6 files changed, 80 insertions(+), 9 deletions(-) diff --git a/src/components/Upload/src/BasicUpload.vue b/src/components/Upload/src/BasicUpload.vue index 9f4b348ec..6152c6b98 100644 --- a/src/components/Upload/src/BasicUpload.vue +++ b/src/components/Upload/src/BasicUpload.vue @@ -22,6 +22,8 @@ - import { defineComponent, CSSProperties, watch, nextTick } from 'vue'; import { fileListProps } from './props'; - import { isFunction } from '/@/utils/is'; + import { isFunction, isDef } from '/@/utils/is'; + import { useSortable } from '/@/hooks/web/useSortable'; import { useModalContext } from '/@/components/Modal/src/hooks/useModalContext'; + import { defineComponent, CSSProperties, watch, nextTick, ref, onMounted } from 'vue'; export default defineComponent({ name: 'FileList', props: fileListProps, - setup(props) { + setup(props, { emit }) { const modalFn = useModalContext(); + const sortableContainer = ref(); + watch( () => props.dataSource, () => { @@ -17,6 +20,35 @@ }); }, ); + + if (props.openDrag) { + onMounted(() => + useSortable(sortableContainer, { + ...props.dragOptions, + onEnd: ({ oldIndex, newIndex }) => { + // position unchanged + if (oldIndex === newIndex) { + return; + } + const { onAfterEnd } = props.dragOptions; + + if (isDef(oldIndex) && isDef(newIndex)) { + const data = [...props.dataSource]; + + const [oldItem] = data.splice(oldIndex, 1); + data.splice(newIndex, 0, oldItem); + + nextTick(() => { + emit('update:dataSource', data); + + isFunction(onAfterEnd) && onAfterEnd(data); + }); + } + }, + }).initSortable(), + ); + } + return () => { const { columns, actionColumn, dataSource } = props; const columnList = [...columns, actionColumn]; @@ -46,7 +78,7 @@ })} - + {dataSource.map((record = {}, index) => { return ( diff --git a/src/components/Upload/src/UploadModal.vue b/src/components/Upload/src/UploadModal.vue index 528b0341f..8b8378e7c 100644 --- a/src/components/Upload/src/UploadModal.vue +++ b/src/components/Upload/src/UploadModal.vue @@ -39,7 +39,13 @@ - +