ant-design/components/tour/useMergedType.ts
iola1999 26555fcd12
fix: tour arrow color (#41761)
* fix: tour arrow color

* test: updte

* fix: duplicate class name

* test: update snapshot

* chore: speedup

* fix: update current step props by onChange

* chore: optimization

* fix: props.current may change

* chore: save

* test: coverage

* chore: optimization

* chore: add useMergedType

* chore: save

* chore: optimization

* chore: optimization

* chore: optimization

* chore: move file

* refactor: useMergedType hook

* chore: save

* chore: optimization

* test: ci

* chore: remove useMemo

* chore: use nullish coalescing operator

* chore: rename setInnerCurrent to updateInnerCurrent
2023-04-19 13:13:50 +08:00

32 lines
906 B
TypeScript

import useMergedState from 'rc-util/lib/hooks/useMergedState';
import { useLayoutEffect } from 'react';
import type { TourProps } from './interface';
interface Props {
defaultType?: string;
steps?: TourProps['steps'];
current?: number;
defaultCurrent?: number;
}
/**
* returns the merged type of a step or the default type.
*/
const useMergedType = ({ defaultType, steps = [], current, defaultCurrent }: Props) => {
const [innerCurrent, updateInnerCurrent] = useMergedState<number | undefined>(defaultCurrent, {
value: current,
});
useLayoutEffect(() => {
if (current === undefined) return;
updateInnerCurrent(current);
}, [current]);
const innerType = typeof innerCurrent === 'number' ? steps[innerCurrent]?.type : defaultType;
const currentMergedType = innerType ?? defaultType;
return { currentMergedType, updateInnerCurrent };
};
export default useMergedType;