Skip to content

Commit

Permalink
feat(tag-input): support scroll in scroll type
Browse files Browse the repository at this point in the history
  • Loading branch information
uyarn committed Oct 12, 2023
1 parent bdc4c93 commit 6ee0153
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/_common
7 changes: 7 additions & 0 deletions src/tag-input/hooks/useTagScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
onMounted, onUnmounted, ref, toRefs,
} from '@vue/composition-api';
import isFunction from 'lodash/isFunction';

import { TdTagInputProps } from '../type';

export default function useTagScroll(props: TdTagInputProps) {
Expand All @@ -16,6 +17,7 @@ export default function useTagScroll(props: TdTagInputProps) {
const scrollDistance = ref(0);
const scrollElement = ref<HTMLElement>();
const mouseEnterTimer = ref();
const isScrollable = ref(false); // 设置可滚动

const updateScrollElement = (element: HTMLElement) => {
const inputElement = element.children[0] as HTMLElement;
Expand All @@ -34,6 +36,9 @@ export default function useTagScroll(props: TdTagInputProps) {
const scrollToRight = () => {
updateScrollDistance();
scrollTo(scrollDistance.value);
setTimeout(() => {
isScrollable.value = true;
}, 200);
};

const scrollToLeft = () => {
Expand Down Expand Up @@ -65,6 +70,7 @@ export default function useTagScroll(props: TdTagInputProps) {

const scrollToLeftOnLeave = () => {
if (excessTagsDisplayType.value !== 'scroll') return;
isScrollable.value = false; // 离开焦点不可滚动
scrollTo(0);
clearTimeout(mouseEnterTimer.value);
};
Expand Down Expand Up @@ -95,5 +101,6 @@ export default function useTagScroll(props: TdTagInputProps) {
onWheel,
scrollToRightOnEnter,
scrollToLeftOnLeave,
isScrollable,
};
}
17 changes: 14 additions & 3 deletions src/tag-input/tag-input.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
defineComponent, computed, toRefs, ref, nextTick,
defineComponent, computed, toRefs, ref, nextTick, watch,
} from '@vue/composition-api';

import { CloseCircleFilledIcon as TdCloseCircleFilledIcon } from 'tdesign-icons-vue';
Expand Down Expand Up @@ -62,7 +62,7 @@ export default defineComponent({
);

const {
scrollToRight, onWheel, scrollToRightOnEnter, scrollToLeftOnLeave, tagInputRef,
scrollToRight, onWheel, scrollToRightOnEnter, scrollToLeftOnLeave, tagInputRef, isScrollable,
} = useTagScroll(props);
// handle tag add and remove
const {
Expand Down Expand Up @@ -126,6 +126,17 @@ export default defineComponent({
context.emit('clear', ctx);
};

// 支持在超长滚动场景下滚动选项进行操作
watch(
() => isScrollable.value,
(v) => {
const scrollElementClass = `${classPrefix.value}-input__prefix`;
const scrollElement = tagInputRef.value.$el.querySelector(`.${scrollElementClass}`);
if (v) scrollElement.classList.add(`${scrollElementClass}--scrollable`);
else scrollElement.classList.remove(`${scrollElementClass}--scrollable`);
},
);

return {
tagValue,
tInputValue,
Expand Down Expand Up @@ -228,7 +239,7 @@ export default defineComponent({
click: this.onInnerClick,
compositionstart: this.onInputCompositionstart,
compositionend: this.onInputCompositionend,
mousewheel: this.onWheel,
// scroll: this.onWheel,
}}
/>
);
Expand Down

0 comments on commit 6ee0153

Please sign in to comment.