SwipeAction滑动删除

author: 闫申申

定义

模拟短信、微信聊天列表滑动删除的组件。

图片展示

SwipeAction 滑动删除 - 图1

代码演示

  1. import SwipeAction from 'pile/dist/components/swipeaction'
  2. const {SwipeAction, Layouts} = pile,
  3. {
  4. Layout,
  5. LayoutHd,
  6. LayoutHdTitle,
  7. LayoutHdAside,
  8. LayoutBd,
  9. LayoutFt,
  10. Items,
  11. Item,
  12. ItemAside,
  13. ItemContent,
  14. ItemTitle,
  15. ItemDesc,
  16. ItemHd,
  17. ItemBd,
  18. ItemFt,
  19. ItemLink
  20. } = Layouts,
  21. {SwipeItem,SwipeItems} = SwipeAction
  22. const _SwipeAction = React.createClass({
  23. getInitialState() {
  24. return {
  25. shows : [true,true,true],
  26. displacements : [false,false,false],
  27. disTouchs :[true,false,true],
  28. touchDefaults : [false,false,false],
  29. shows2 : [true],
  30. displacements2 : [false],
  31. disTouchs2 :[true],
  32. touchDefaults2 : [false,false,false],
  33. num : 3
  34. }
  35. },
  36. singleStateVal(key,index,defaultkey){
  37. this.state[key][index] = defaultkey
  38. return this.state[key]
  39. },
  40. lotStateVal(len,key){
  41. let newArr = []
  42. for (var i = 0; i < len; i++) {
  43. let newKey = key
  44. newArr.push(newKey)
  45. }
  46. return newArr
  47. },
  48. otherStateVal(len,index,key,otherkey){
  49. let newArr = []
  50. for (var i = 0; i < len; i++) {
  51. let newKey = i == index ? key : otherkey
  52. newArr.push(newKey)
  53. }
  54. return newArr
  55. },
  56. _confirmdel(o){
  57. let len = this.state.displacements.length
  58. this.setState({
  59. shows : this.singleStateVal("shows",o,false),
  60. displacements : this.singleStateVal("displacements",o,true),
  61. touchDefaults : this.lotStateVal(len,false)
  62. })
  63. },
  64. _confirmcancel(o){
  65. let len = this.state.displacements.length
  66. this.setState({
  67. displacements : this.singleStateVal("displacements",o,false),
  68. touchDefaults : this.lotStateVal(len,false)
  69. })
  70. },
  71. _confirmcancel2(o){
  72. alert("我是自定义按钮的点击事件")
  73. },
  74. _confirmdel2(o){
  75. this.setState({
  76. shows2 : this.singleStateVal("shows2",o,false),
  77. displacements2 : this.singleStateVal("displacements2",o,true)
  78. })
  79. },
  80. _itemsTouchBack(o){
  81. let len = this.state.displacements.length
  82. this.setState({
  83. displacements : this.otherStateVal(len,o.index,o.touchState,false),
  84. touchDefaults : this.lotStateVal(len,o.touchState)
  85. })
  86. },
  87. _itemsTouchBack2(o){
  88. let len = this.state.displacements2.length
  89. this.setState({
  90. displacements2 : this.otherStateVal(len,o.index,o.touchState,false),
  91. touchDefaults2 : this.lotStateVal(len,o.touchState)
  92. })
  93. },
  94. changeState(){
  95. this.setState({
  96. num :1
  97. })
  98. },
  99. render() {
  100. let self = this,
  101. {shows,displacements,disTouchs,shows2,displacements2,disTouchs2,touchDefaults,touchDefaults2} = this.state
  102. return (
  103. <div className="libs-intr">
  104. // 多列滑动操作
  105. <SwipeItems className="mt-10"
  106. shows={shows}
  107. displacements={displacements}
  108. touchDefaults={touchDefaults}
  109. itemsTouchBack={self._itemsTouchBack}
  110. disTouchs={disTouchs}
  111. buttons={[[
  112. {
  113. type: 'delet',
  114. label: '删除',
  115. onClick: self._confirmdel.bind(self,0)
  116. },{
  117. type: 'cancel',
  118. label: '取消',
  119. onClick: self._confirmcancel.bind(self,0)
  120. }
  121. ],[],[
  122. {
  123. type: 'delet',
  124. label: '删除',
  125. onClick: self._confirmdel.bind(self,2)
  126. }
  127. ]]}>
  128. <Item>
  129. <ItemHd>
  130. <ItemTitle>我有两个按钮</ItemTitle>
  131. </ItemHd>
  132. <ItemBd>
  133. <ItemContent>
  134. <ItemDesc>出发地:西二旗地铁站</ItemDesc>
  135. <ItemDesc>目的地:北京首都国际机场T2航站楼</ItemDesc>
  136. </ItemContent>
  137. </ItemBd>
  138. </Item>
  139. <Item>
  140. <ItemHd>
  141. <ItemTitle>我不可以滑动</ItemTitle>
  142. </ItemHd>
  143. <ItemBd>
  144. <ItemContent>
  145. <ItemDesc>出发地:西二旗地铁站</ItemDesc>
  146. <ItemDesc>目的地:北京首都国际机场T2航站楼</ItemDesc>
  147. </ItemContent>
  148. </ItemBd>
  149. </Item>
  150. <Item>
  151. <ItemHd>
  152. <ItemTitle>我有一个按钮</ItemTitle>
  153. </ItemHd>
  154. <ItemBd>
  155. <ItemContent>
  156. <ItemDesc>出发地:西二旗地铁站</ItemDesc>
  157. <ItemDesc>目的地:北京首都国际机场T2航站楼</ItemDesc>
  158. </ItemContent>
  159. </ItemBd>
  160. </Item>
  161. </SwipeItems>
  162. // 单列滑动操作
  163. <SwipeItems className="mt-10"
  164. itemsTouchBack={self._itemsTouchBack2}
  165. touchDefaults={touchDefaults2}
  166. shows={shows2}
  167. displacements={displacements2}
  168. disTouchs={disTouchs2}
  169. buttons={[[
  170. {
  171. type: 'delet',
  172. label: '删除',
  173. onClick: self._confirmdel2.bind(self,0)
  174. },{
  175. label: '自定义按钮',
  176. onClick: self._confirmcancel2.bind(self,0)
  177. }
  178. ]]}>
  179. <Item>
  180. <ItemHd>
  181. <ItemTitle>单列可自定义按钮</ItemTitle>
  182. </ItemHd>
  183. <ItemBd>
  184. <ItemContent>
  185. <ItemDesc>出发地:西二旗地铁站</ItemDesc>
  186. <ItemDesc>目的地:北京首都国际机场T2航站楼</ItemDesc>
  187. </ItemContent>
  188. </ItemBd>
  189. </Item>
  190. </SwipeItems>
  191. </div>
  192. )
  193. }
  194. })

属性

参数 描述 数据类型 默认值
shows 数据是否展开 子值为true或者false array
displacements 是否位移(已滑动状态) 子值为true或者false array
buttons 按钮信息 { type: 'cancel', label: '取消', onClick: self._confirmcancel } array
disTouchs 是否可以拖动 array
touchDefaults 是否有因素影响拖动 array
itemsTouchBack 滑动后回调函数 function

原文: https://didi.github.io/pile.js/docs/2017/08/develop-components-swipeaction.html