generated from arvinxx/monorepo-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
225 additions
and
218 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
187 changes: 0 additions & 187 deletions
187
packages/sortable-list/src/hooks/useSortableList.test.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
|
||
import { createStore } from './index'; | ||
|
||
const useStore = createStore(); | ||
describe('useStore', () => { | ||
it('默认值', () => { | ||
const { result } = renderHook(() => useStore()); | ||
|
||
expect(result.current.data).toEqual([]); | ||
}); | ||
|
||
describe('updateTableStore', () => { | ||
describe('添加item', () => { | ||
it('基础添加', () => { | ||
const { result } = renderHook(() => useStore()); | ||
|
||
act(() => { | ||
result.current.addItem({ id: '333' }); | ||
}); | ||
|
||
expect(result.current.data).toEqual([{ id: '333' }]); | ||
}); | ||
|
||
it('携带索引的添加', () => { | ||
const { result } = renderHook(() => useStore()); | ||
|
||
act(() => { | ||
result.current.internalUpdateData([ | ||
{ id: '1' }, | ||
{ id: '2' }, | ||
{ id: '3' }, | ||
]); | ||
|
||
result.current.addItem({ id: '4' }, 0); | ||
}); | ||
|
||
expect(result.current.data).toEqual([ | ||
{ id: '4' }, | ||
{ id: '1' }, | ||
{ id: '2' }, | ||
{ id: '3' }, | ||
]); | ||
}); | ||
}); | ||
it('删除item', () => { | ||
const { result } = renderHook(() => useStore()); | ||
|
||
act(() => { | ||
result.current.internalUpdateData([ | ||
{ id: '1' }, | ||
{ id: '2' }, | ||
{ id: '3' }, | ||
]); | ||
result.current.removeItem('2'); | ||
}); | ||
|
||
expect(result.current.data).toEqual([{ id: '1' }, { id: '3' }]); | ||
}); | ||
it('重排序item', () => { | ||
const { result } = renderHook(() => useStore()); | ||
|
||
act(() => { | ||
result.current.internalUpdateData([ | ||
{ id: '1' }, | ||
{ id: '2' }, | ||
{ id: '3' }, | ||
]); | ||
result.current.reorder(2, 0); | ||
}); | ||
|
||
expect(result.current.data).toEqual([ | ||
{ id: '3' }, | ||
{ id: '1' }, | ||
{ id: '2' }, | ||
]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import create from 'zustand'; | ||
import createContext from 'zustand/context'; | ||
|
||
import { arrayMove } from '@dnd-kit/sortable'; | ||
import produce from 'immer'; | ||
import initialState from './initialState'; | ||
import type { SortableListStore } from '../types'; | ||
|
||
const createStore = () => | ||
create<SortableListStore>((set, get) => ({ | ||
// 内部值 | ||
...initialState, | ||
activateItem: (id) => { | ||
set({ activeId: id }); | ||
}, | ||
deactivateItem: () => { | ||
set({ activeId: null }); | ||
}, | ||
// 内部更新 data 方法 | ||
internalUpdateData: (data) => { | ||
const { onDataChange } = get(); | ||
set({ data }); | ||
|
||
if (onDataChange) { | ||
onDataChange(data); | ||
} | ||
}, | ||
|
||
syncOutsideData: (data) => { | ||
set({ data }); | ||
}, | ||
|
||
// 重新排序 | ||
reorder: (startIndex, endIndex) => { | ||
const { data, internalUpdateData } = get(); | ||
const nextData = produce(data, (state) => { | ||
if (startIndex !== endIndex) { | ||
return arrayMove(state, startIndex, endIndex); | ||
} | ||
|
||
return state; | ||
}); | ||
|
||
internalUpdateData(nextData); | ||
}, | ||
// 添加元素 | ||
addItem: (item, addIndex) => { | ||
const { data, internalUpdateData } = get(); | ||
const nextData = produce(data, (state) => { | ||
if (typeof addIndex !== 'number') { | ||
// 如果没有提供添加位的 index 值,默认添加在最后 | ||
state.push(item); | ||
} | ||
|
||
state.splice(addIndex, 0, item); | ||
}); | ||
|
||
internalUpdateData(nextData); | ||
}, | ||
removeItem: (id) => { | ||
const { data, internalUpdateData } = get(); | ||
const nextData = data.filter((item) => item.id !== id); | ||
|
||
internalUpdateData(nextData); | ||
}, | ||
})); | ||
|
||
const { Provider, useStore, useStoreApi } = createContext<SortableListStore>(); | ||
|
||
export { Provider, useStore, createStore, useStoreApi }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { SortableListState } from '../types'; | ||
|
||
const initialState: SortableListState = { | ||
data: [], | ||
onDataChange: null, | ||
activeId: null, | ||
}; | ||
|
||
export default initialState; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { getIndexOfActiveItem } from './utils'; | ||
|
||
const list = [{ id: '123' }, { id: '3245' }]; | ||
|
||
describe('getIndexOfActiveItem', () => { | ||
it('找到 index', () => { | ||
const index = getIndexOfActiveItem(list, '123'); | ||
expect(index).toEqual(0); | ||
}); | ||
it('没找到 index', () => { | ||
const index = getIndexOfActiveItem(list, '135'); | ||
|
||
expect(index).toEqual(-1); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
packages/sortable-list/src/utils.ts → packages/sortable-list/src/store/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.