import type { RafFrame } from '../../_util/raf'; import raf from '../../_util/raf'; import type { ComputedRef, Ref, UnwrapRef } from 'vue'; import { ref, onBeforeUnmount, watch } from 'vue'; import type { ValueTextConfig } from './useValueTexts'; import useValueTexts from './useValueTexts'; export default function useHoverValue( valueText: Ref, { formatList, generateConfig, locale }: ValueTextConfig, ): [ComputedRef, (date: DateType) => void, (immediately?: boolean) => void] { const innerValue = ref(null); let rafId: RafFrame; function setValue(val: DateType, immediately = false) { raf.cancel(rafId); if (immediately) { innerValue.value = val as UnwrapRef; return; } rafId = raf(() => { innerValue.value = val as UnwrapRef; }); } const [, firstText] = useValueTexts(innerValue as Ref, { formatList, generateConfig, locale, }); function onEnter(date: DateType) { setValue(date); } function onLeave(immediately = false) { setValue(null, immediately); } watch(valueText, () => { onLeave(true); }); onBeforeUnmount(() => { raf.cancel(rafId); }); return [firstText, onEnter, onLeave]; }