当前位置:   article > 正文

React Antd4.x Upload实现图片上传后拖拽换顺序功能_antd的upload怎么将上传的文件列表和进度条位置改变

antd的upload怎么将上传的文件列表和进度条位置改变

功能说明

做项目使用React Antd4.x Upload上传图片,实现批量上传。

后期项目迭代更新,要求拖拽图片调整图片顺序。

框架不支持,自己借助包写了一个上传且可以拖拽图片换位置的功能。

下面是思路具体步骤:

1、框架Upload实现批量上传,预览,删除。

2、Upload旁边增加拖拽换位置的按钮。

3、点击按钮弹窗,弹窗中实现拖拽。

设计原因:上传与拖拽互不影响。

功能实现

安装“array-move”

$ npm i array-move

安装“react-sortable-hoc"

$ npm install react-sortable-hoc --save

封装拖拽弹窗组件

  1. /* eslint-disable react-hooks/exhaustive-deps */
  2. import React, { useEffect, useState, } from "react";
  3. import { Modal, } from 'antd';
  4. import { SortableContainer, SortableElement, } from 'react-sortable-hoc';
  5. import { arrayMoveImmutable } from 'array-move';
  6. import './style.less';
  7. import CTSTooltip from '@/components/CTSTooltip';
  8. const handlePreview = (value,) => {
  9. if (value) {
  10. const image = new Image();
  11. image.src = value;
  12. const imageWindow = window.open(value);
  13. imageWindow.document.write(image.outerHTML);
  14. }
  15. }
  16. const handleOver = (value) => {
  17. document.getElementById(`img-operate-wrap${value}`).style.display = 'block';
  18. }
  19. const handleOut = (value) => {
  20. document.getElementById(`img-operate-wrap${value}`).style.display = 'none';
  21. }
  22. const SortableItem = SortableElement(({ value, onDelete }) => <div onMouseOut={() => handleOut(value)} onMouseOver={() => handleOver(value)} className='img-box-wrap'>
  23. <img alt='' className='img-box' src={value}></img>
  24. <div className='img-operate-default' id={`img-operate-wrap${value}`}>
  25. <div className='img-icon-wrap'>
  26. <CTSTooltip title="预览" iconName="icon-yulan" onTooltipIcon={() => handlePreview(value)} />
  27. <CTSTooltip className="del" title="删除" iconName="icon-lajitong" onTooltipIcon={() => onDelete(value)} />
  28. </div>
  29. </div>
  30. </div>
  31. );
  32. const SortableList = SortableContainer(({ items, onDelete }) => {
  33. return <div style={{ display: 'flex', flexWrap: 'wrap' }}>{
  34. items && items.map((value, index) => (
  35. <SortableItem key={`item-${value.url}`} index={index} onDelete={onDelete} value={value.url} />
  36. ))}
  37. </div>
  38. });
  39. const CTSChangePosition = ({
  40. visible,
  41. fileListAll,
  42. onConfirm,
  43. onCancel,
  44. loading = false,
  45. }) => {
  46. const [fileTempList, setFileTempList] = useState(fileListAll);
  47. useEffect(() => {
  48. setFileTempList(fileListAll);
  49. }, []);
  50. const handleOk = () => {
  51. // console.log("final ----", fileTempList);
  52. onConfirm(fileTempList);
  53. }
  54. const onSortEnd = ({ oldIndex, newIndex }) => {
  55. let finalList = arrayMoveImmutable(fileTempList, oldIndex, newIndex);
  56. setFileTempList(finalList);
  57. };
  58. const handleDelete = (value) => {
  59. // console.log("value - value", value, fileTempList);
  60. if (value) {
  61. let dealFileList = fileTempList.filter(item => item.url !== value);
  62. // console.log("dealFileList", dealFileList,);
  63. setFileTempList(dealFileList);
  64. }
  65. }
  66. return (<Modal width="70%" loading={loading} title={'拖拽调整图片顺序'} onOk={handleOk} maskClosable={false} onCancel={onCancel} visible={visible} >
  67. <SortableList distance={10} axis="xy" items={fileTempList} onSortEnd={onSortEnd} onDelete={(value) => handleDelete(value)} />
  68. </Modal>)
  69. }
  70. export default CTSChangePosition;
  1. .img-box-wrap {
  2. position: relative;
  3. z-index: 9999;
  4. .img-box {
  5. width: 104px;
  6. height: 104px;
  7. margin-right: 8px;
  8. margin-bottom: 8px;
  9. border: 1px dashed #d9d9d9;
  10. background: white;
  11. }
  12. .img-operate-default {
  13. width: 104px;
  14. height: 104px;
  15. margin-right: 8px;
  16. margin-bottom: 8px;
  17. display: none;
  18. position: absolute;
  19. top: 0;
  20. left: 0;
  21. opacity: 0.5;
  22. background-color: black;
  23. .iconfont {
  24. color: white;
  25. font-size: 20px;
  26. }
  27. .icon-lajitong {
  28. margin-left: 6px;
  29. }
  30. .icon-yulan {
  31. margin-right: 6px;
  32. }
  33. .img-icon-wrap {
  34. width: 104px;
  35. height: 104px;
  36. display: flex;
  37. justify-content: center;
  38. align-items: center;
  39. color: white;
  40. }
  41. }
  42. }

组件的引用:

  1. <Button className='btn' onClick={() => handleChangeImagePosition()}>调整图片顺序</Button>
  2. {visible && <CTSChangePosition visible={visible} fileListAll={fileListAll} onConfirm={(fileListDeal) => handleModalConfirm(fileListDeal)} onCancel={() => handleModalClose()} />}
  1. const handleChangeImagePosition = () => {
  2. setVisible(true);
  3. }
  4. const handleModalClose = () => {
  5. setVisible(false);
  6. }
  7. const handleModalConfirm = (fileListDeal) => {
  8. setFileListAll(fileListDeal);
  9. if (form) {
  10. form.setFieldsValue({
  11. [`${name[0]}`]: {
  12. [name[1]]: fileListDeal
  13. }
  14. })
  15. }
  16. handleModalClose();
  17. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/339137
推荐阅读
相关标签
  

闽ICP备14008679号