|
1 | 1 | /** |
2 | 2 | * 当标签数量过多时,输入框显示不下,则需要滚动查看,以下为滚动逻辑 |
3 | | - * 如果标签过多时的处理方式,是标签省略,则不需要此功能 |
4 | 3 | */ |
5 | | -import { useEffect, useRef, useState } from 'react'; |
6 | | -import { isFunction } from 'lodash-es'; |
| 4 | +import { useEffect, useRef } from 'react'; |
| 5 | + |
| 6 | +import useConfig from '../hooks/useConfig'; |
| 7 | +import { |
| 8 | + getScrollContainer, |
| 9 | + handleWheelScroll, |
| 10 | + scrollToLeft as scrollToLeftBase, |
| 11 | + scrollToRight as scrollToRightBase, |
| 12 | +} from './utils/tagInputScroll'; |
7 | 13 |
|
8 | 14 | import type { WheelEvent } from 'react'; |
9 | 15 | import type { InputRef } from '../input'; |
10 | 16 | import type { TdTagInputProps } from './type'; |
11 | 17 |
|
12 | | -let mouseEnterTimer = null; |
13 | | - |
14 | 18 | export default function useTagScroll(props: TdTagInputProps) { |
15 | | - const { excessTagsDisplayType = 'scroll', disabled } = props; |
| 19 | + const { excessTagsDisplayType, disabled } = props; |
16 | 20 | const readOnly = props.readOnly || props.readonly; |
| 21 | + const { classPrefix: prefix } = useConfig(); |
17 | 22 |
|
| 23 | + /** 标签输入框组件 ref */ |
18 | 24 | const tagInputRef = useRef<InputRef>(null); |
19 | | - |
20 | | - // 允许向右滚动的最大距离 |
21 | | - const [scrollDistance, setScrollDistance] = useState(0); |
22 | | - const [scrollElement, setScrollElement] = useState<HTMLDivElement>(); |
23 | | - |
24 | | - const updateScrollElement = (element: HTMLDivElement) => { |
25 | | - const scrollElement = element.children[0] as HTMLDivElement; |
26 | | - setScrollElement(scrollElement); |
27 | | - }; |
28 | | - |
29 | | - const updateScrollDistance = () => { |
30 | | - if (!scrollElement) return; |
31 | | - setScrollDistance(scrollElement.scrollWidth - scrollElement.clientWidth); |
32 | | - }; |
33 | | - |
34 | | - const scrollTo = (distance: number) => { |
35 | | - if (isFunction(scrollElement?.scroll)) { |
36 | | - scrollElement.scroll({ left: distance, behavior: 'smooth' }); |
| 25 | + /** 滚动容器元素缓存(.input__prefix) */ |
| 26 | + const scrollElementRef = useRef<HTMLElement>(); |
| 27 | + /** 进入防抖定时器 */ |
| 28 | + const mouseEnterTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 29 | + |
| 30 | + /** 是否为可交互的 scroll 模式 */ |
| 31 | + const isScrollMode = excessTagsDisplayType === 'scroll' && !readOnly && !disabled; |
| 32 | + |
| 33 | + /** 获取滚动容器(带缓存) */ |
| 34 | + const getScrollElement = () => { |
| 35 | + const root = tagInputRef.current?.currentElement as HTMLElement; |
| 36 | + if (!root) return scrollElementRef.current; |
| 37 | + if (scrollElementRef.current?.parentElement !== root) { |
| 38 | + const found = getScrollContainer(root, prefix); |
| 39 | + if (found) scrollElementRef.current = found; |
37 | 40 | } |
| 41 | + return scrollElementRef.current; |
38 | 42 | }; |
39 | 43 |
|
| 44 | + /** 滚动到最右侧并开启 scrollable,用于:悬浮进入、tag 变化后定位末尾 */ |
40 | 45 | const scrollToRight = () => { |
41 | | - updateScrollDistance(); |
42 | | - scrollTo(scrollDistance); |
| 46 | + const el = getScrollElement(); |
| 47 | + if (el) scrollToRightBase(el, prefix); |
43 | 48 | }; |
44 | 49 |
|
45 | | - const scrollToLeft = () => { |
46 | | - scrollTo(0); |
| 50 | + /** 处理滚轮事件,绑定到 TInput 的 onWheel */ |
| 51 | + const onWheel = ({ e }: { e: WheelEvent<HTMLDivElement> }) => { |
| 52 | + if (!isScrollMode) return; |
| 53 | + const el = getScrollElement(); |
| 54 | + if (el) handleWheelScroll(el, e); |
47 | 55 | }; |
48 | 56 |
|
49 | | - // TODO:MAC 电脑横向滚动,Windows 纵向滚动。当前只处理了横向滚动 |
50 | | - const onWheel = ({ e }: { e: WheelEvent<HTMLDivElement> }) => { |
51 | | - if (readOnly || disabled) return; |
52 | | - if (!scrollElement) return; |
53 | | - if (e.deltaX > 0) { |
54 | | - const distance = Math.min(scrollElement.scrollLeft + 120, scrollDistance); |
55 | | - scrollTo(distance); |
56 | | - } else { |
57 | | - const distance = Math.max(scrollElement.scrollLeft - 120, 0); |
58 | | - scrollTo(distance); |
| 57 | + /** 清除防抖定时器 */ |
| 58 | + const clearEnterTimer = () => { |
| 59 | + if (mouseEnterTimerRef.current) { |
| 60 | + clearTimeout(mouseEnterTimerRef.current); |
| 61 | + mouseEnterTimerRef.current = null; |
59 | 62 | } |
60 | 63 | }; |
61 | 64 |
|
62 | | - // 鼠标 hover,自动滑动到最右侧,以便输入新标签 |
| 65 | + /** 鼠标悬浮进入:延迟 100ms 后滚动到最右侧 */ |
63 | 66 | const scrollToRightOnEnter = () => { |
64 | | - if (excessTagsDisplayType !== 'scroll') return; |
65 | | - // 一闪而过的 mousenter 不需要执行 |
66 | | - mouseEnterTimer = setTimeout(() => { |
| 67 | + if (!isScrollMode) return; |
| 68 | + clearEnterTimer(); |
| 69 | + mouseEnterTimerRef.current = setTimeout(() => { |
67 | 70 | scrollToRight(); |
68 | | - clearTimeout(mouseEnterTimer); |
| 71 | + mouseEnterTimerRef.current = null; |
69 | 72 | }, 100); |
70 | 73 | }; |
71 | 74 |
|
| 75 | + /** 鼠标离开:滚回最左并关闭 scrollable */ |
72 | 76 | const scrollToLeftOnLeave = () => { |
73 | | - if (excessTagsDisplayType !== 'scroll') return; |
74 | | - scrollTo(0); |
75 | | - clearTimeout(mouseEnterTimer); |
76 | | - }; |
77 | | - |
78 | | - const clearScroll = () => { |
79 | | - clearTimeout(mouseEnterTimer); |
80 | | - }; |
81 | | - |
82 | | - const initScroll = (element: HTMLDivElement) => { |
83 | | - if (!element) return; |
84 | | - updateScrollElement(element); |
| 77 | + if (!isScrollMode) return; |
| 78 | + clearEnterTimer(); |
| 79 | + const el = getScrollElement(); |
| 80 | + if (el) scrollToLeftBase(el, prefix); |
85 | 81 | }; |
86 | 82 |
|
87 | | - useEffect(() => { |
88 | | - initScroll(tagInputRef?.current?.currentElement); |
89 | | - return clearScroll; |
90 | | - // eslint-disable-next-line react-hooks/exhaustive-deps |
91 | | - }, []); |
| 83 | + useEffect(() => clearEnterTimer, []); |
92 | 84 |
|
93 | 85 | return { |
94 | | - initScroll, |
95 | | - clearScroll, |
96 | 86 | tagInputRef, |
97 | | - scrollElement, |
98 | | - scrollDistance, |
99 | | - scrollTo, |
100 | 87 | scrollToRight, |
101 | | - scrollToLeft, |
102 | | - updateScrollElement, |
103 | | - updateScrollDistance, |
104 | 88 | onWheel, |
105 | 89 | scrollToRightOnEnter, |
106 | 90 | scrollToLeftOnLeave, |
|
0 commit comments