diff --git a/packages/hooks/src/useHistoryTravel/index.ts b/packages/hooks/src/useHistoryTravel/index.ts index 24124fa543..1f3c497d8b 100644 --- a/packages/hooks/src/useHistoryTravel/index.ts +++ b/packages/hooks/src/useHistoryTravel/index.ts @@ -31,7 +31,25 @@ const split = (step: number, targetArr: T[]) => { }; }; -export default function useHistoryTravel(initialValue?: T, maxLength: number = 0) { +export default function useHistoryTravel( + initialValue?: T, + options?: + | { + maxLength: number; + manual: boolean; + } + | number, +) { + let maxLength = 0; + let manual = false; + + if (typeof options === 'number') { + maxLength = options; + } else if (typeof options === 'object') { + maxLength = options.maxLength; + manual = options.manual; + } + const [history, setHistory] = useState>({ present: initialValue, past: [], @@ -69,6 +87,14 @@ export default function useHistoryTravel(initialValue?: T, maxLength: number }); }; + const updateValueWithoutRecord = (val: T) => { + setHistory({ + present: val, + future: future, + past: past, + }); + }; + const _forward = (step: number = 1) => { if (future.length === 0) { return; @@ -109,7 +135,8 @@ export default function useHistoryTravel(initialValue?: T, maxLength: number value: present, backLength: past.length, forwardLength: future.length, - setValue: useMemoizedFn(updateValue), + setValue: manual ? useMemoizedFn(updateValueWithoutRecord) : useMemoizedFn(updateValue), + commit: useMemoizedFn(updateValue), go: useMemoizedFn(go), back: useMemoizedFn(() => { go(-1);