Skip to content

Commit

Permalink
✨ feat(sortable-list): 导出 SortableListDispatchPayload 类型定义
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jun 16, 2023
1 parent f977fb5 commit f47782b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
"@testing-library/user-event": "^14.4.3",
"@types/color": "^3",
"@types/json-schema": "^7.0.11",
"@types/lodash-es": "^4",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@umijs/lint": "^4.0.0",
Expand Down
29 changes: 22 additions & 7 deletions src/SortableList/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,41 +97,56 @@ group:
| record | `(index: number) => Record<string, any>` | 生成初始值逻辑 |
| creatorButtonText | `string` | 新增一行按钮文案 |

### ListDataDispatchPayload
### SortableListDispatchPayload

组件通过 `onChange` 以及 `ForwardRef` 的方式暴露底层事件,你可以细粒度地控制列表的增删改查,移动,以及根据事件细粒度控制后续的行为链路。

```jsx | pure
```ts
// 新增节点
interface AddItemAction {
type: 'addItem';
/**
* 新增的节点
*/
item: any;
/**
* 新增节点的位置,不传则默认在最后
*/
index?: number;
}

// 移动节点
interface MoveItemAction {
type: 'moveItem';
/**
* 当前节点id
* 当前节点的唯一标识符
*/
activeId: UniqueIdentifier;
activeId: string | number;
/**
* 目标节点id
* 目标节点的唯一标识符
*/
targetId: UniqueIdentifier;
targetId: string | number;
}

// 移除节点
interface RemoveItemAction {
type: 'removeItem';
/**
* 要移除的节点的位置
*/
index: number;
}

// 修改节点content内容
// 修改节点 content 内容
interface UpdateItemAction {
type: 'updateItem';
/**
* 要修改的节点的位置
*/
index: number;
/**
* 修改后的节点内容
*/
item: any;
}
```
2 changes: 2 additions & 0 deletions src/SortableList/index.tsx → src/SortableList/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * from './container';

// hooks 和相关类型定义
export type { SortableListRef } from './container/StoreUpdater';
export { useSortableList } from './hooks/useSortableList';
export type { SortableListInstance } from './hooks/useSortableList';
export type { SortableListDispatchPayload } from './store';
export type { CreatorButtonProps } from './type';
3 changes: 2 additions & 1 deletion src/SortableList/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ const { useStore, useStoreApi, Provider } = createContext<StoreApi<Store>>();

// ========= 导出 ========= //
export type { OnChange, State } from './initialState';
export type { SortableListDispatchPayload } from './listDataReducer';
export type { Store } from './store';
export { createStore, useStore, Provider, useStoreApi };
export { Provider, createStore, useStore, useStoreApi };
4 changes: 2 additions & 2 deletions src/SortableList/store/initialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import type {
RenderItemProps,
UniqueIdentifier,
} from '../type';
import { ListDataDispatchPayload } from './listDataReducer';
import { SortableListDispatchPayload } from './listDataReducer';

export type OnChange<T = any> = (values: T[], event: ListDataDispatchPayload) => void;
export type OnChange<T = any> = (values: T[], event: SortableListDispatchPayload) => void;

export interface State<T = any> {
/*
Expand Down
41 changes: 35 additions & 6 deletions src/SortableList/store/listDataReducer.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,75 @@
import type { UniqueIdentifier } from '@dnd-kit/core';
import { arrayMove } from '@dnd-kit/sortable';
import { produce } from 'immer';
import merge from 'lodash.merge';
import { merge } from 'lodash-es';

import type { KeyManager } from '../type';

// 新增节点
interface AddItemAction {
/**
* 动作类型
*/
type: 'addItem';
/**
* 新增的节点
*/
item: any;
/**
* 新增节点的位置
* @default undefined
*/
index?: number;
}

// 移动节点
interface MoveItemAction {
/**
* 动作类型
*/
type: 'moveItem';
/**
* 当前节点id
* 当前节点的唯一标识符
*/
activeId: UniqueIdentifier;
/**
* 目标节点id
* 目标节点的唯一标识符
*/
targetId: UniqueIdentifier;
}

// 移除节点
interface RemoveItemAction {
/**
* 动作类型
*/
type: 'removeItem';
/**
* 要移除的节点的位置
*/
index: number;
}

// 修改节点content内容
// 修改节点 content 内容
interface UpdateItemAction {
/**
* 动作类型
*/
type: 'updateItem';
/**
* 要修改的节点的位置
*/
index: number;
/**
* 修改后的节点内容
*/
item: any;
}

/**
* 内部的更新方法
*/
export type ListDataDispatchPayload =
export type SortableListDispatchPayload =
| MoveItemAction
| AddItemAction
| RemoveItemAction
Expand All @@ -49,7 +78,7 @@ export type ListDataDispatchPayload =
export const listDataReducer = (
value: any[],
keyManager: KeyManager,
payload: ListDataDispatchPayload,
payload: SortableListDispatchPayload,
) => {
switch (payload.type) {
case 'moveItem':
Expand Down

0 comments on commit f47782b

Please sign in to comment.