From 10f187b26f744e99e191e637b7d2bbba311248ee Mon Sep 17 00:00:00 2001 From: wind_ Date: Thu, 4 Sep 2025 11:33:55 +0800 Subject: [PATCH] ForEach supports onMove Signed-off-by: wind_ Change-Id: I4da47d07ea9aa094285d64f89c1490e67831e5a6 --- .../src/handwritten/component/forEach.ts | 86 ++++++++++++++++--- 1 file changed, 75 insertions(+), 11 deletions(-) diff --git a/frameworks/bridge/arkts_frontend/koala_projects/arkoala-arkts/arkui-ohos/src/handwritten/component/forEach.ts b/frameworks/bridge/arkts_frontend/koala_projects/arkoala-arkts/arkui-ohos/src/handwritten/component/forEach.ts index f3d6ceebe5b..47c009de4e0 100644 --- a/frameworks/bridge/arkts_frontend/koala_projects/arkoala-arkts/arkui-ohos/src/handwritten/component/forEach.ts +++ b/frameworks/bridge/arkts_frontend/koala_projects/arkoala-arkts/arkui-ohos/src/handwritten/component/forEach.ts @@ -17,42 +17,97 @@ // HANDWRITTEN, DO NOT REGENERATE import { int32, hashCodeFromString } from '@koalaui/common'; -import { memoEntry2, __context, NodeAttach } from '@koalaui/runtime'; +import { memoEntry2, __context, NodeAttach, remember } from '@koalaui/runtime'; import { InteropNativeModule } from '@koalaui/interop'; import { SyntaxItemPeer, ForEachNodePeer } from '../handwritten/RepeatImpl'; +import { DynamicNode, ArkCommonMethodComponent, CommonMethod, OnMoveHandler, ItemDragEventHandler } from './common'; +import { LazyForEachOps } from './arkui-custom' +import { NodeHolder } from "../handwritten/LazyForEachImpl" + +export interface ForEachAttribute extends CommonMethod, DynamicNode { + arr: () => Array; + setForEachOptions(arr: () => Array, + /** @memo */ + itemGenerator: (item: T, index: number) => void, + keyGenerator?: (item: T, index: number) => string): this { + return this; + } + onMove(handler?: OnMoveHandler): this { + return this; + } + onMove(handler?: OnMoveHandler, eventHandler?: ItemDragEventHandler): this { + return this; + } +} + +export class ArkForEachComponent extends ArkCommonMethodComponent implements ForEachAttribute { + arr: () => Array = () => new Array(); + /** @memo */ + itemGenerator: (item: T, index: number) => void = (item: T, index: number) => {}; + keyGenerator?: (item: T, index: number) => string = undefined; + onMoveEvent?: OnMoveHandler = undefined; + itemDragEvent?: ItemDragEventHandler = undefined; + + public onMove(handler?: OnMoveHandler): this { + this.onMoveEvent = handler; + return this; + } + + public onMove(handler?: OnMoveHandler, eventHandler?: ItemDragEventHandler): this { + this.onMoveEvent = handler; + this.itemDragEvent = eventHandler; + return this; + } + + public setForEachOptions(arr: () => Array, + /** @memo */ + itemGenerator: (item: T, index: number) => void, + keyGenerator?: (item: T, index: number) => string): this { + this.arr = arr; + this.itemGenerator = itemGenerator; + this.keyGenerator = keyGenerator; + return this; + } +} + +function onMoveFromTo(moveFrom: int32, moveTo: int32): void {} /** @memo */ -export function ForEach( - arr: () => Array, +export function ForEachImpl( /** @memo */ - itemGenerator: (item: T, index: number) => void, - keyGenerator?: (item: T, index: number) => string, + style: ((attributes: ForEachAttribute) => void) | undefined ) { - if (arr === null || arr === undefined) { + const receiver = remember(() => { + return new ArkForEachComponent() + }) + style?.(receiver) + if (receiver.arr === null || receiver.arr === undefined) { InteropNativeModule._NativeLog('input array function is null or undefined error. Application error!'); return; } - if (typeof itemGenerator !== 'function') { + if (typeof receiver.itemGenerator !== 'function') { InteropNativeModule._NativeLog('item generator function missing. Application error!'); return; } - if (keyGenerator !== undefined && typeof keyGenerator !== 'function') { + if (receiver.keyGenerator !== undefined && typeof receiver.keyGenerator !== 'function') { InteropNativeModule._NativeLog('key generator is not a function. Application error!'); return; } + let nodeHolder = remember(() => new NodeHolder()) /** @memo */ const createAndUpdate = (): void => { - const array: Array = arr(); + const array: Array = receiver.arr(); if (array === null || array === undefined) { InteropNativeModule._NativeLog('input array is null or undefined error. Application error!'); return; } const length: number = array.length; - const key = (element: T, index: int32): int32 => keyGenerator ? hashCodeFromString(keyGenerator!(element, (index as number))) : index; + const key = (element: T, index: int32): int32 => + receiver.keyGenerator ? hashCodeFromString(receiver.keyGenerator!(element, (index as number))) : index; /** @memo */ const action = (element: T, index: int32): void => { NodeAttach(() => SyntaxItemPeer.create(), (node: SyntaxItemPeer) => { - itemGenerator(element, (index as number)); + receiver.itemGenerator(element, (index as number)); }); }; for (let i = 0; i < length; i++) { @@ -62,5 +117,14 @@ export function ForEach( } NodeAttach(() => ForEachNodePeer.create(), (node: ForEachNodePeer) => { createAndUpdate(); + nodeHolder.node = node; }); + + /** + * provide onMove callbacks to the backend if onMove is set,and reset callbacks if onMove is unset + */ + LazyForEachOps.SyncOnMoveOps(nodeHolder.node!.getPeerPtr(), + onMoveFromTo, + receiver.onMoveEvent, + receiver.itemDragEvent) } -- Gitee