Skip to content

Commit

Permalink
♻️ refactor: 重构 store
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Mar 8, 2022
1 parent 490e16d commit 5563979
Show file tree
Hide file tree
Showing 12 changed files with 225 additions and 218 deletions.
17 changes: 0 additions & 17 deletions packages/sortable-list/src/hooks/useActiveItem.ts

This file was deleted.

187 changes: 0 additions & 187 deletions packages/sortable-list/src/hooks/useSortableList.test.ts

This file was deleted.

79 changes: 79 additions & 0 deletions packages/sortable-list/src/store/index.test.ts
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' },
]);
});
});
});
70 changes: 70 additions & 0 deletions packages/sortable-list/src/store/index.ts
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 };
9 changes: 9 additions & 0 deletions packages/sortable-list/src/store/initialState.ts
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;
15 changes: 15 additions & 0 deletions packages/sortable-list/src/store/utils.test.ts
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);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SortableItemList } from './types';
import type { SortableItemList } from '../types';

export const getIndexOfActiveItem = <
T extends SortableItemList = SortableItemList,
Expand Down
2 changes: 1 addition & 1 deletion packages/sortable-list/src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
UniqueIdentifier,
} from '@dnd-kit/core';
import type { CSSProperties, ReactElement, Ref } from 'react';
import type { SortableItem } from './data';
import type { SortableItem } from './store';

export type RenderItem<T = SortableItem> = (
item: T,
Expand Down
Loading

0 comments on commit 5563979

Please sign in to comment.