web建站教程
     
  1. 首页
  2. 前端UI组件库
  3. AI项目和框架
  4. AIGC工具
  5. 百度echarts
  6. 地图大全
  7. 前端知识
  8. 更多
    vuejs
    js入门
    php入门
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站
    AI应用

reactjs如何实现列表可拖动排序

1180 ℃

reactjs如何实现列表可拖动排序,下面web建站小编给大家介绍一下实现代码!

实现代码如下:

import React, { useState,useEffect } from "react";
import { Input,Radio, Button,Space,Checkbox,Form  } from "antd";
import { DeleteOutline, CloseCircleOutline,UnorderedListOutline } from 'antd-mobile-icons'
import { Dialog, Toast, Divider } from 'antd-mobile'
import {
  sortableContainer,
  sortableElement,
  sortableHandle,
} from 'react-sortable-hoc';
import {arrayMoveImmutable} from 'array-move';

const RadioComponent = (props) => {
  const {onDelete,onListDate,componentIndex,setIsEdit,isEdit,componentTitle,componentDate,previewVisible} = props;
  const [radioList,setRadioList] = useState([])
  const [remark, setRemark] = useState(false)
  const [required, setRequired] = useState(false)
  const [radioTitle, setRadioTitle] = useState('')
  const [id, setId] = useState(2)
  const [radioId, setRadioId] = useState(111211)
  useEffect(()=>{
    if(componentDate !== undefined){
      setRadioList(componentDate)
    }else{
      setRadioList([{id:0,value:''},{id:1,value:''}])
    }
  },[componentIndex])

  useEffect(()=>{
    if(isEdit === false && previewVisible === undefined){
      onListDate(radioList,radioTitle,required,remark) 
    }
  },[isEdit])
 
  const onChange = (e) => {
    console.log(e.target.value);
    setRequired(e.target.checked)
  };

  // 添加备注
  const addRemark = ()=>{
    setRemark(true)
  }

  // 删除备注
  const deleteRemark = ()=>{
    setRemark(false)
  }

  // 删除选项
  const deleteRadio = (item)=>{
    console.log(item);
    if(radioList.indexOf(item) > -1){
      radioList.splice(radioList.indexOf(item),1)
    }
    setRadioList([...radioList])
  }

  const SortableItem = sortableElement(({item,num}) => (
    <li>
      <Radio key = {item.id} value = {item.value} style = {{width:'100%',position:'relative'}} >
      <Input style = {{border:'none',width:'96%'}} placeholder = {`选项${num+1}`} defaultValue={item.value}
        onBlur = {(e)=>{
        item.value = e.target.value
        console.log(item.value);
        setRadioList([...radioList])}}
        readOnly = {isEdit == true ? '':'none'}></Input>
        <DragHandle  />
      <CloseCircleOutline  onClick = {()=>{deleteRadio(item)}} style = {{position:'absolute',right:'10px',top:'10px',display:isEdit == true ? '':'none'}}/>
    </Radio>
    </li>
  ));

  const onSortEnd = ({oldIndex, newIndex}) => {
    var arry1 = arrayMoveImmutable(radioList,oldIndex,newIndex)
    setRadioList(arry1);
  }; 

  const DragHandle = sortableHandle(() => <UnorderedListOutline  style = {{position:'absolute',right:'30px',top:'10px',display:isEdit == true ? '':'none'}}/>);
  const SortableContainer = sortableContainer(({children}) => {
  return <ul>{children}</ul>;

});

    return(
      <div id = {componentIndex} style = {{backgroundColor:'#fff', paddingTop:'10px',paddingLeft:'20px'}} >
        <span style = {{display: required == true ? '':'none',color:'red',fontSize:'20px'}}>*</span>
          <span style={{fontWeight:'bold',fontSize:'14px'}}>{componentIndex} [单选]</span>
        <Input placeholder = "请输入问题" defaultValue = {radioTitle === ''? componentTitle:componentTitle} autoFocus style = {{width:'80%',border:'none',paddingLfet:'5px'}} onBlur={e=>{setRadioTitle(e.target.value);}} readOnly = {isEdit == true ? '':'none'} >
        </Input>
        <Radio.Group style = {{display:'block'}}>
          <SortableContainer onSortEnd={onSortEnd} useDragHandle  >
           {
              radioList.map((item,index)=>{
                return(
                  <SortableItem key={`item-${item.id}`} index={index} item = {item} num={index} />
                )
              })
            }
          </SortableContainer>
        </Radio.Group>
        <div style = {{display:remark == true ? '':'none',fontSize:'14px'}}>
          <span>备注</span><Input style={{border:'none',width:'80%'}} placeholder='请输入' readOnly = {isEdit == true ? '':'none'}></Input>
          <DeleteOutline onClick={deleteRemark} style={{float:'right',margin:'10px',display:isEdit == true ? '':'none'}}/>
        </div>
        <div style={{display:isEdit == true ? '':'none'}}>
        <Button type="link" onClick={()=>{setRadioList([...radioList,{id:id,value:''}]);setId(id+1);console.log(radioList);}}>添加选项</Button>
          <span style={{display:remark == false ? '':'none'}}>|</span>
        <Button type="link" style={{display:remark == false ? '':'none'}} onClick={addRemark}>添加[备注]项</Button>
        <div style={{borderTop:'1px #d7d7d7 solid ',paddingTop:'10px',marginTop:'15px',marginLeft:'-15px'}}>
        <Checkbox onChange={onChange}>必填</Checkbox>
        <DeleteOutline  
          onClick={async () => {
          const result = await Dialog.confirm({
            content: '是否确定删除该题目?',
          })
          if (result) {
            Toast.show({ content: '点击了确认', position: 'bottom' })
            onDelete(componentIndex)
          } else {
            Toast.show({ content: '点击了取消', position: 'bottom' })
          }
          }} style={{float:'right',margin:'10px'}}  />
        </div>
        </div>
      </div>
    )
}

export default RadioComponent

根据需求安装脚手架!

Vue.js与React主要区别在哪?(附示例)

react语法中如何移除节点

react开发打包build发现路径不对解决

nginx部署react刷新出现404如何解决

reactjs如何做一个五星评价功能

标签: react 拖动排序

上面是“reactjs如何实现列表可拖动排序”的全面内容,想了解更多关于 reactjs 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_2972.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 首页 > reactjs
Trae:新一代免费的AI编程工具

在线育儿补贴计算器

快来看看你到底可以领到多少补贴!生活小工具
上一篇:
下一篇:
x 打工人ai神器