使用react-sortable-hoc实现拖拽
如图:
提示:下面案例可供参考
1.文件1
代码如下(示例):文件名称:./dragcomponents
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | import * as React from 'react' import { sortableContainer, sortableElement, sortableHandle, } from "react-sortable-hoc" ; // 拖拽的关键组件 const Sortable: React.FC = (props) => { const { dataSource = [], ComSortItem, sortEnd } = props; // 拖拽时原列表替换 function arrayMoveMutable(array, fromIndex, toIndex) { const startIndex = fromIndex = 0 && startIndex { return <div>{children}</div>; }); // 拖拽ico const DragHandle = sortableHandle((value1, sortIndex1) => ( <div id= "ListItem" > <div> </div> </div> )); function handleDelete(index) { const List = [...dataSource]; List.splice(index, 1) sortEnd(List); } // 数据更新 function updateData(val, index) { const List = [...dataSource]; List[index] = val; sortEnd(List); } // 拖拽体 const SortableItem = sortableElement(({ value, sortIndex }) => { return ( // <div id="ListItem"> // // </div> ); }); // 拖拽后回调 const onSortEnd = ({ oldIndex, newIndex }) => { const List = arrayMoveImmutable(dataSource, oldIndex, newIndex); sortEnd(List); }; return ( {dataSource.length > 0 && dataSource.map((value, index) => ( ))} > ); } export default Sortable; |
2.文件2
代码如下(示例):文件名称’./usedrag’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import * as React from 'react' import { Checkbox } from 'antd' import Sortable from './dragcomponents' import './index.scss' const _ = require( 'lodash' ) import store from './store' import { SAVE_RENDER_ALL_DATA } from './actionType' const Usedrag: React.FC = (props) => { const { state, dispatch } = React.useContext(store); // 自定义拖拽体 const {upDateRenderData} = props const showdata ={...props.renderData} function AddForm(showdata) { return ( <div> <span></span>{showdata.data.valuedata.fieldName}</div> <div> <span>控件标签显示名称<span>{showdata.data.valuedata.labelName}</span></span> <span>所占列宽<span>{showdata.data.valuedata.span}</span></span> { /* */ } </div> |
)
}
const updateSource = (val) => {
const arrdata: any = _.cloneDeep(props.renderData)
const arr: any = _.cloneDeep(val)
if(JSON.stringify(arrdata) !== JSON.stringify(arr)){
for (let i = 0; i
}
sortEnd={(val) => {
updateSource(val)
}}
/>
sortEnd={(val) => {
updateSource(val)
}}
/>