taro h5列表拖拽排序 --- sortablejs 和 react-sortable-hoc

12 篇文章 0 订阅

描述:列表,拖拽排序,只测试了h5

一、sortablejs

文档:http://www.sortablejs.com/

1.安装sortablejs
2、引入

import Sortable from 'sortablejs'

3、页面

const [list, setList] = useState([{
  id: 'item-1',
  content: '选项1'
}, {
  id: 'item-2',
  content: '选项2'
}, {
  id: 'item-3',
  content: '选项3'
}])

const getListRef = (instance) => {
  new Sortable(instance, {
    animation: 150,
    ghostClass: '自定义的类名',
  });
}

return (
    <View ref={getListRef}>
      {
        list.map((item, index) => {
          return (
           <View key={`item-${index}`}>
             { item.content }
           </View>
          )
        })
      }
    </View>
)

!!!注意
ghostClass控制的不时拖动中的影子,而是拖动的最终落在目标位置的item
在这里插入图片描述
4、item中有Switch的bug
在这里插入图片描述
解决方法:在switch外套个容器,设置宽高和switch一致,超出隐藏

另外,拖动中的重影没有直接的属性去除,还没有找到好的方法…

二、react-sortable-hoc

文档:https://www.npmjs.com/package/react-sortable-hoc
中文文档:https://www.5axxw.com/wiki/content/hrpw3t
1、安装 react-sortable-hoc和array-move
2、引入

import { arrayMoveImmutable } from 'array-move'
import { SortableContainer, SortableElement } from 'react-sortable-hoc'

3、单个item

const SortableItem = SortableElement(({value}) => {
  return (
    <View className="tab-item">
      <Image src='' className="logo" />
      {value.label}
    </View>
  )
})

4、列表

const SortableList = SortableContainer(({items, onSortEnd}) => {
  return (
    <View className="tab-list">
      {items.map((value, index) => (
        <SortableItem key={`tab-item-${index}`} index={index} value={value} />
      ))}
    </View>
  )
})

5、页面

const [items, setItems] = useState([{
    label: '选项1',
    checked: true
  }, {
    label: '选项2',
    checked: false
  }])

  useEffect(() => {
    // 这里可以监听到items
    console.log(items)
  }, [items])

  const onSortEnd = ({oldIndex, newIndex}) => {
    setItems(arrayMoveImmutable(items, oldIndex, newIndex))
  };
  return (
    <SortableList items={items} onSortEnd={onSortEnd} />
  )
.tab-item {
  display: flex;
  align-items: center;
  padding: 10px;
  margin-bottom: 20px;
  color: #fff;
  background: #ccc;
  .logo {
    margin-right: 10px;
    width: 50px;
    height: 50px;
    background: yellow;
  }
}

!!!注意点
设置样式的时候,tab-item要单独设置,不要嵌套在其他元素下,否则会出现鼠标按下和拖动时,元素样式消失
在这里插入图片描述

常用的属性: (加在SortableList上)
pressDelay:number, 按下之后多少毫秒可以排序,不能与distance同用
Distance: number, 按下之后鼠标移动多少像素可以拖动元素
Helpclass: string , 按下去的元素的样式名称
hideSortableGhost:bolean, 拖动时是否隐藏重影, 默认时隐藏的

默认只要按下就会在body上插入一个item, 可以用pressDelay 或distance控制时间
在这里插入图片描述

6、item中有Switch的bug
在这里插入图片描述
解决方法:在switch外套个容器,设置宽高和switch一致,超出隐藏

7、点击switch, 获取chang的值
思路:子组件item传值给最外层SortableList,通过useState更改列表中switch值,最后在effect中监听到列表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Taro 是一个类 React 的跨端框架,支持使用 React 的语法编写页面。如果要在 Taro 中接入 React Router,可以按照以下步骤操作: 1. 安装 `react-router-dom`: ```bash npm install react-router-dom --save ``` 2. 在 `app.js` 中引入 `BrowserRouter` 并包裹 `Provider` 和 `Router`: ```javascript import Taro, { Component } from '@tarojs/taro' import { Provider } from '@tarojs/redux' import { BrowserRouter as Router } from 'react-router-dom' import configStore from './store' import './app.less' const store = configStore() class App extends Component { componentDidMount () {} componentDidShow () {} componentDidHide () {} componentDidCatchError () {} render () { return ( <Provider store={store}> <Router> {this.props.children} </Router> </Provider> ) } } export default App ``` 3. 在页面中使用 `Route`、`Link` 等组件: ```javascript import Taro, { Component } from '@tarojs/taro' import { View, Text } from '@tarojs/components' import { Link, Route, Switch } from 'react-router-dom' import './index.less' class Index extends Component { render () { return ( <View className='index'> <Text>Hello world!</Text> <Link to='/detail'>Go to detail page</Link> <Switch> <Route path='/detail' component={Detail} /> </Switch> </View> ) } } class Detail extends Component { render () { return ( <View className='detail'> <Text>Detail page</Text> <Link to='/'>Go back to index page</Link> </View> ) } } export default Index ``` 通过以上步骤,就可以在 Taro 中接入 React Router 了,实现更加灵活的路由控制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Misha韩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值