Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 143 additions & 112 deletions src/CSSMotionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as React from 'react';
import type { CSSMotionProps } from './CSSMotion';
import OriginCSSMotion, { isRefNotConsumed } from './CSSMotion';
import useIsomorphicLayoutEffect from './hooks/useIsomorphicLayoutEffect';
import type { KeyObject } from './util/diff';
import {
diffKeys,
Expand Down Expand Up @@ -67,6 +68,49 @@ export interface CSSMotionListState {
keyEntities: KeyObject[];
}

function getDerivedKeyEntities(
keys: CSSMotionListProps['keys'],
keyEntities: KeyObject[],
) {
const parsedKeyObjects = parseKeys(keys);
const mixedKeyEntities = diffKeys(keyEntities, parsedKeyObjects);

return mixedKeyEntities.filter(entity => {
const prevEntity = keyEntities.find(({ key }) => entity.key === key);

// Remove if already mark as removed
if (
prevEntity &&
prevEntity.status === STATUS_REMOVED &&
entity.status === STATUS_REMOVE
) {
return false;
}
return true;
});
}

function shallowEqualKeyObject(origin: KeyObject, target: KeyObject) {
const originRecord = origin as Record<string, any>;
const targetRecord = target as Record<string, any>;
const originKeys = Object.keys(originRecord);
const targetKeys = Object.keys(targetRecord);

return (
originKeys.length === targetKeys.length &&
originKeys.every(key => originRecord[key] === targetRecord[key])
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
Comment thread
QDyanbing marked this conversation as resolved.

function isSameKeyEntities(origin: KeyObject[], target: KeyObject[]) {
return (
origin.length === target.length &&
origin.every((entity, index) =>
shallowEqualKeyObject(entity, target[index]),
)
);
}
Comment thread
QDyanbing marked this conversation as resolved.

/**
* Generate a CSSMotionList component with config
* @param transitionSupport No need since CSSMotionList no longer depends on transition support
Expand All @@ -75,123 +119,110 @@ export interface CSSMotionListState {
export function genCSSMotionList(
transitionSupport: boolean,
CSSMotion = OriginCSSMotion,
): React.ComponentClass<CSSMotionListProps> {
class CSSMotionList extends React.Component<
CSSMotionListProps,
CSSMotionListState
> {
static defaultProps = {
component: 'div',
};

state: CSSMotionListState = {
keyEntities: [],
};

static getDerivedStateFromProps(
{ keys }: CSSMotionListProps,
{ keyEntities }: CSSMotionListState,
) {
const parsedKeyObjects = parseKeys(keys);
const mixedKeyEntities = diffKeys(keyEntities, parsedKeyObjects);

return {
keyEntities: mixedKeyEntities.filter(entity => {
const prevEntity = keyEntities.find(({ key }) => entity.key === key);

// Remove if already mark as removed
if (
prevEntity &&
prevEntity.status === STATUS_REMOVED &&
entity.status === STATUS_REMOVE
) {
return false;
}
return true;
}),
};
}
): React.ComponentType<CSSMotionListProps> {
const CSSMotionList: React.FC<CSSMotionListProps> = props => {
const {
component = 'div',
children,
onVisibleChanged,
onAllRemoved,
...restProps
} = props;
const { keys } = restProps;

const [keyEntities, setKeyEntities] = React.useState<KeyObject[]>(() =>
getDerivedKeyEntities(keys, []),
);
const restKeysCountRef = React.useRef<number | null>(null);
const onAllRemovedRef = React.useRef(onAllRemoved);

onAllRemovedRef.current = onAllRemoved;

useIsomorphicLayoutEffect(() => {
setKeyEntities(prevKeyEntities => {
const nextKeyEntities = getDerivedKeyEntities(keys, prevKeyEntities);

return isSameKeyEntities(prevKeyEntities, nextKeyEntities)
? prevKeyEntities
: nextKeyEntities;
});
});
Comment thread
QDyanbing marked this conversation as resolved.
Outdated

// ZombieJ: Return the count of rest keys. It's safe to refactor if need more info.
removeKey = (removeKey: React.Key) => {
this.setState(
prevState => {
const nextKeyEntities = prevState.keyEntities.map(entity => {
if (entity.key !== removeKey) return entity;
return {
...entity,
status: STATUS_REMOVED,
};
});
useIsomorphicLayoutEffect(() => {
if (restKeysCountRef.current !== null) {
const restKeysCount = restKeysCountRef.current;
restKeysCountRef.current = null;

if (restKeysCount === 0) {
onAllRemovedRef.current?.();
}
}
}, [keyEntities]);
Comment thread
QDyanbing marked this conversation as resolved.
Outdated

// ZombieJ: Return the count of rest keys. It's safe to refactor if need more info.
const removeKey = React.useCallback((removedKey: React.Key) => {
setKeyEntities(prevKeyEntities => {
const nextKeyEntities = prevKeyEntities.map(entity => {
if (entity.key !== removedKey) return entity;
return {
keyEntities: nextKeyEntities,
...entity,
status: STATUS_REMOVED,
};
},
() => {
const { keyEntities } = this.state;
const restKeysCount = keyEntities.filter(
({ status }) => status !== STATUS_REMOVED,
).length;

if (restKeysCount === 0 && this.props.onAllRemoved) {
this.props.onAllRemoved();
}
},
);
};

render() {
const { keyEntities } = this.state;
const {
component,
children,
onVisibleChanged,
onAllRemoved,
...restProps
} = this.props;

const Component = component || React.Fragment;

const motionProps: CSSMotionProps = {};
MOTION_PROP_NAMES.forEach(prop => {
motionProps[prop] = restProps[prop];
delete restProps[prop];
});

restKeysCountRef.current = nextKeyEntities.filter(
({ status }) => status !== STATUS_REMOVED,
).length;

return isSameKeyEntities(prevKeyEntities, nextKeyEntities)
? prevKeyEntities
: nextKeyEntities;
});
delete restProps.keys;

return (
<Component {...restProps}>
{keyEntities.map(({ status, ...eventProps }, index) => {
const visible = status === STATUS_ADD || status === STATUS_KEEP;
return (
<CSSMotion
{...motionProps}
key={eventProps.key}
visible={visible}
eventProps={eventProps}
onVisibleChanged={changedVisible => {
onVisibleChanged?.(changedVisible, { key: eventProps.key });

if (!changedVisible) {
this.removeKey(eventProps.key);
}
}}
>
{isRefNotConsumed(children)
? props =>
(children as ChildrenWithoutRef)({
...props,
index,
})
: (props, ref) => children({ ...props, index }, ref)}
</CSSMotion>
);
})}
</Component>
);
}
}
}, []);
Comment thread
QDyanbing marked this conversation as resolved.

const Component = component || React.Fragment;

const motionProps: CSSMotionProps = {};
MOTION_PROP_NAMES.forEach(prop => {
motionProps[prop] = restProps[prop];
delete restProps[prop];
});
delete restProps.keys;

return (
<Component {...restProps}>
{keyEntities.map(({ status, ...eventProps }, index) => {
const visible = status === STATUS_ADD || status === STATUS_KEEP;
return (
<CSSMotion
{...motionProps}
key={eventProps.key}
visible={visible}
eventProps={eventProps}
onVisibleChanged={changedVisible => {
onVisibleChanged?.(changedVisible, { key: eventProps.key });

if (!changedVisible) {
removeKey(eventProps.key);
}
}}
>
{isRefNotConsumed(children)
? motionChildrenProps =>
(children as ChildrenWithoutRef)({
...motionChildrenProps,
index,
})
: (motionChildrenProps, ref) =>
children({ ...motionChildrenProps, index }, ref)}
</CSSMotion>
);
})}
</Component>
);
};

CSSMotionList.displayName = 'CSSMotionList';

return CSSMotionList;
}
Expand Down
Loading