Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tag-input): support scroll when exceed type is scroll #2846

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/_common
2 changes: 1 addition & 1 deletion src/tag-input/_example/excess.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
export default {
data() {
return {
tags: ['Vue', 'React'],
tags: ['Vue', 'React', 'VueNext', 'MiniProgram', 'Angular', 'Starter Kit'],
};
},
};
Expand Down
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,
};
}
16 changes: 14 additions & 2 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,18 @@ export default defineComponent({
context.emit('clear', ctx);
};

// 支持在超长滚动场景下滚动选项进行操作
watch(
() => isScrollable.value,
(v) => {
if (props.excessTagsDisplayType !== 'scroll') return;
const scrollElementClass = `${classPrefix.value}-input__prefix`;
uyarn marked this conversation as resolved.
Show resolved Hide resolved
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
152 changes: 152 additions & 0 deletions test/snap/__snapshots__/csr.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -131955,6 +131955,82 @@ exports[`csr snapshot test > csr test ./src/tag-input/_example/excess.vue 1`] =
/>
</svg>
</div>
<div
class="t-tag t-tag--default t-tag--dark t-tag--close"
>
<span>
VueNext
</span>
<svg
class="t-icon t-icon-close t-tag__icon-close"
fill="none"
height="1em"
viewBox="0 0 24 24"
width="1em"
>
<path
d="M7.05 5.64L12 10.59l4.95-4.95 1.41 1.41L13.41 12l4.95 4.95-1.41 1.41L12 13.41l-4.95 4.95-1.41-1.41L10.59 12 5.64 7.05l1.41-1.41z"
fill="currentColor"
/>
</svg>
</div>
<div
class="t-tag t-tag--default t-tag--dark t-tag--close"
>
<span>
MiniProgram
</span>
<svg
class="t-icon t-icon-close t-tag__icon-close"
fill="none"
height="1em"
viewBox="0 0 24 24"
width="1em"
>
<path
d="M7.05 5.64L12 10.59l4.95-4.95 1.41 1.41L13.41 12l4.95 4.95-1.41 1.41L12 13.41l-4.95 4.95-1.41-1.41L10.59 12 5.64 7.05l1.41-1.41z"
fill="currentColor"
/>
</svg>
</div>
<div
class="t-tag t-tag--default t-tag--dark t-tag--close"
>
<span>
Angular
</span>
<svg
class="t-icon t-icon-close t-tag__icon-close"
fill="none"
height="1em"
viewBox="0 0 24 24"
width="1em"
>
<path
d="M7.05 5.64L12 10.59l4.95-4.95 1.41 1.41L13.41 12l4.95 4.95-1.41 1.41L12 13.41l-4.95 4.95-1.41-1.41L10.59 12 5.64 7.05l1.41-1.41z"
fill="currentColor"
/>
</svg>
</div>
<div
class="t-tag t-tag--default t-tag--dark t-tag--close"
>
<span>
Starter Kit
</span>
<svg
class="t-icon t-icon-close t-tag__icon-close"
fill="none"
height="1em"
viewBox="0 0 24 24"
width="1em"
>
<path
d="M7.05 5.64L12 10.59l4.95-4.95 1.41 1.41L13.41 12l4.95 4.95-1.41 1.41L12 13.41l-4.95 4.95-1.41-1.41L10.59 12 5.64 7.05l1.41-1.41z"
fill="currentColor"
/>
</svg>
</div>
</div>
<input
autocomplete=""
Expand Down Expand Up @@ -132024,6 +132100,82 @@ exports[`csr snapshot test > csr test ./src/tag-input/_example/excess.vue 1`] =
/>
</svg>
</div>
<div
class="t-tag t-tag--default t-tag--dark t-tag--close"
>
<span>
VueNext
</span>
<svg
class="t-icon t-icon-close t-tag__icon-close"
fill="none"
height="1em"
viewBox="0 0 24 24"
width="1em"
>
<path
d="M7.05 5.64L12 10.59l4.95-4.95 1.41 1.41L13.41 12l4.95 4.95-1.41 1.41L12 13.41l-4.95 4.95-1.41-1.41L10.59 12 5.64 7.05l1.41-1.41z"
fill="currentColor"
/>
</svg>
</div>
<div
class="t-tag t-tag--default t-tag--dark t-tag--close"
>
<span>
MiniProgram
</span>
<svg
class="t-icon t-icon-close t-tag__icon-close"
fill="none"
height="1em"
viewBox="0 0 24 24"
width="1em"
>
<path
d="M7.05 5.64L12 10.59l4.95-4.95 1.41 1.41L13.41 12l4.95 4.95-1.41 1.41L12 13.41l-4.95 4.95-1.41-1.41L10.59 12 5.64 7.05l1.41-1.41z"
fill="currentColor"
/>
</svg>
</div>
<div
class="t-tag t-tag--default t-tag--dark t-tag--close"
>
<span>
Angular
</span>
<svg
class="t-icon t-icon-close t-tag__icon-close"
fill="none"
height="1em"
viewBox="0 0 24 24"
width="1em"
>
<path
d="M7.05 5.64L12 10.59l4.95-4.95 1.41 1.41L13.41 12l4.95 4.95-1.41 1.41L12 13.41l-4.95 4.95-1.41-1.41L10.59 12 5.64 7.05l1.41-1.41z"
fill="currentColor"
/>
</svg>
</div>
<div
class="t-tag t-tag--default t-tag--dark t-tag--close"
>
<span>
Starter Kit
</span>
<svg
class="t-icon t-icon-close t-tag__icon-close"
fill="none"
height="1em"
viewBox="0 0 24 24"
width="1em"
>
<path
d="M7.05 5.64L12 10.59l4.95-4.95 1.41 1.41L13.41 12l4.95 4.95-1.41 1.41L12 13.41l-4.95 4.95-1.41-1.41L10.59 12 5.64 7.05l1.41-1.41z"
fill="currentColor"
/>
</svg>
</div>
</div>
<input
autocomplete=""
Expand Down
Loading
Loading