Skip to content

Commit

Permalink
Merge pull request #8 from iamyoki/feature
Browse files Browse the repository at this point in the history
feat: finished useListTransition(useTransitionGroup)
  • Loading branch information
iamyoki authored Dec 30, 2021
2 parents d2fb662 + 1289cff commit 90521cb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './useTransition';
export * from './useSwitchTransition';
export * from './useTransitionGroup';
export * from './useListTransition';
export * from './Transition';
export * from './SwitchTransition';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Fragment, useEffect, useState} from 'react';
import {Fragment, useEffect, useRef, useState} from 'react';
import {Stage} from '..';
import {insertArray} from '../helpers/insertArray';
import {setAnimationFrameTimeout} from '../helpers/setAnimationFrameTimeout';
Expand All @@ -16,11 +16,12 @@ type ItemWithKey<Item> = {
index: number;
};

export function useTransitionGroup<Item>(list: Array<Item>, timeout: number) {
export function useListTransition<Item>(list: Array<Item>, timeout: number) {
const keyRef = useRef(0);
// change list to our list form with extra information.
const initialList: Array<ItemWithState<Item>> = list.map((item, key) => ({
item,
key,
key: keyRef.current,
stage: 'enter',
}));

Expand All @@ -38,15 +39,18 @@ export function useTransitionGroup<Item>(list: Array<Item>, timeout: number) {

// 1 add new items into list state
if (newItemsWithIndex.length > 0) {
setListState((prevListState) => {
return newItemsWithIndex.reduce((prev, {item, index}, i) => {
return insertArray(prev, index, {
item,
key: prevListState.length + 1 + i,
stage: 'from',
});
}, prevListState);
});
keyRef.current++;
setListState((prevListState) =>
newItemsWithIndex.reduce(
(prev, {item, index}, i) =>
insertArray(prev, index, {
item,
key: keyRef.current,
stage: 'from',
}),
prevListState
)
);
}

// 2 enter those new items immediatly
Expand Down Expand Up @@ -86,8 +90,6 @@ export function useTransitionGroup<Item>(list: Array<Item>, timeout: number) {
);
}, timeout);
}

console.log(listState);
},
[list, listState, timeout]
);
Expand Down

0 comments on commit 90521cb

Please sign in to comment.