From 7b188d21283a11ebf3571bd03eb659ca2335d20d Mon Sep 17 00:00:00 2001 From: sunhao Date: Mon, 27 May 2024 18:50:01 +0800 Subject: [PATCH] * sortable: fix sortable not work on first time. --- lib/sortable/src/components/sortable-list.ts | 31 ++----------------- lib/sortable/src/components/sortable-tree.ts | 27 +--------------- lib/sortable/src/types/sortable-list-state.ts | 2 -- lib/sortable/src/types/sortable-tree-state.ts | 2 -- 4 files changed, 4 insertions(+), 58 deletions(-) diff --git a/lib/sortable/src/components/sortable-list.ts b/lib/sortable/src/components/sortable-list.ts index c0a80622f8..a1b8f3a989 100644 --- a/lib/sortable/src/components/sortable-list.ts +++ b/lib/sortable/src/components/sortable-list.ts @@ -2,13 +2,13 @@ import {List} from '@zui/list/src/component'; import {Sortable} from '../vanilla/sortable'; import type {RenderableProps} from 'preact'; -import type {SortableEvent} from 'sortablejs'; +import type {MoveEvent, SortableEvent} from 'sortablejs'; import type {ClassNameLike} from '@zui/core'; import type {SortableListProps, SortableListState, SortableOptions} from '../types'; -import {Item} from '@zui/common-list/src/types'; export class SortableList

extends List { - static defaultProps: Partial = { + static defaultProps = { + ...List.defaultProps, sortable: true, }; @@ -26,30 +26,6 @@ export class SortableList

): Item[] { - const items = super._getItems(props); - const {orders} = this.state; - if (orders) { - const newItems: Item[] = [...items]; - const orderMap = orders.reduce>((map, order, index) => { - map.set(order, index); - return map; - }, new Map()); - return newItems.sort((a, b) => { - const aOrder = orderMap.get(a.key!); - const bOrder = orderMap.get(b.key!); - if (aOrder === undefined) { - return bOrder === undefined ? 0 : 1; - } - if (bOrder === undefined) { - return -1; - } - return aOrder - bOrder; - }); - } - return items; - } - protected _getClassName(props: RenderableProps

): ClassNameLike { return [super._getClassName(props), 'sortable-list']; } @@ -67,7 +43,6 @@ export class SortableList

{ const orders = this.getOrders(); - this.setState({orders}); this.props.onSort?.call(this, event, orders); userOptions.onSort?.call(this, event); }, diff --git a/lib/sortable/src/components/sortable-tree.ts b/lib/sortable/src/components/sortable-tree.ts index 29f0161296..ce14220f15 100644 --- a/lib/sortable/src/components/sortable-tree.ts +++ b/lib/sortable/src/components/sortable-tree.ts @@ -4,7 +4,6 @@ import {Sortable} from '../vanilla/sortable'; import type {RenderableProps} from 'preact'; import type {MoveEvent, SortableEvent} from 'sortablejs'; import type {ClassNameLike} from '@zui/core'; -import type {Item} from '@zui/common-list'; import type {SortableTreeProps, SortableTreeState, SortableOptions} from '../types'; export class SortableTree

extends Tree { @@ -29,30 +28,6 @@ export class SortableTree

): Item[] { - const items = super._getItems(props); - const {orders} = this.state; - if (orders) { - const newItems: Item[] = [...items]; - const orderMap = orders.reduce>((map, order, index) => { - map.set(order, index); - return map; - }, new Map()); - return newItems.sort((a, b) => { - const aOrder = orderMap.get(a.key!); - const bOrder = orderMap.get(b.key!); - if (aOrder === undefined) { - return bOrder === undefined ? 0 : 1; - } - if (bOrder === undefined) { - return -1; - } - return aOrder - bOrder; - }); - } - return items; - } - protected _getClassName(props: RenderableProps

): ClassNameLike { return [super._getClassName(props), 'sortable-tree']; } @@ -68,10 +43,10 @@ export class SortableTree

{ const orders = this.getOrders(); - this.setState({orders}); onSort?.call(this, event, orders, parentKey); userOptions.onSort?.call(this, event); }, diff --git a/lib/sortable/src/types/sortable-list-state.ts b/lib/sortable/src/types/sortable-list-state.ts index 54e07609f6..1af2d24b42 100644 --- a/lib/sortable/src/types/sortable-list-state.ts +++ b/lib/sortable/src/types/sortable-list-state.ts @@ -1,6 +1,4 @@ -import type {ItemKey} from '@zui/common-list'; import type {ListState} from '@zui/list'; export interface SortableListState extends ListState { - orders: ItemKey[] } diff --git a/lib/sortable/src/types/sortable-tree-state.ts b/lib/sortable/src/types/sortable-tree-state.ts index d3ca12d54d..b0856a9ceb 100644 --- a/lib/sortable/src/types/sortable-tree-state.ts +++ b/lib/sortable/src/types/sortable-tree-state.ts @@ -1,6 +1,4 @@ -import type {ItemKey} from '@zui/common-list'; import type {ListState} from '@zui/list'; export interface SortableTreeState extends ListState { - orders: ItemKey[] }