From d405f60c7fbea5870eecb420aec21294434c744a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Fri, 17 Jun 2022 17:11:51 +0800 Subject: [PATCH] add list drag demo (include data integrity) --- doric-demo/src/ListDragDemo.ts | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 doric-demo/src/ListDragDemo.ts diff --git a/doric-demo/src/ListDragDemo.ts b/doric-demo/src/ListDragDemo.ts new file mode 100644 index 00000000..b9a7f281 --- /dev/null +++ b/doric-demo/src/ListDragDemo.ts @@ -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, index: number, ...elements: Array) { + 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) + } +} \ No newline at end of file