Skip to content

Commit

Permalink
add list drag demo (include data integrity)
Browse files Browse the repository at this point in the history
  • Loading branch information
jingpeng authored and penfeizhou committed Jun 17, 2022
1 parent 63398a4 commit d405f60
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions doric-demo/src/ListDragDemo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Panel, Group, layoutConfig, Color, stack, list, listItem, text, loge } from "doric"
import { colors } from "./utils"

@Entry
class ListDragDemo extends Panel {

private data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

build(root: Group) {
function insertAt(array: Array<any>, index: number, ...elements: Array<any>) {
array.splice(index, 0, ...elements);
}

stack(
[
list({
itemCount: this.data.length,
renderItem: (idx) => {
return listItem(text({
text: `Item :${this.data[idx]}`,
layoutConfig: layoutConfig().just(),
width: root.width,
height: 50,
textColor: Color.WHITE,
backgroundColor: colors[idx % colors.length],
}))
},
layoutConfig: layoutConfig().most(),
canDrag: true,
onDragged: (from, to) => {
const tmp = this.data[from]
this.data.splice(from, 1)
insertAt(this.data, to, tmp)
loge(this.data)
}
})
],
{
layoutConfig: layoutConfig().most()
}
).in(root)
}
}

0 comments on commit d405f60

Please sign in to comment.