Skip to content

Commit

Permalink
feat(tabs): scroll the active tab to the center position (#1663)
Browse files Browse the repository at this point in the history
* feat(tabs): scroll the active tab to the center position

#1648

* test: fixed the issue in the tabs test case where 'scrollTo is not a function', and fix eslint

* fix: fix lint

* fix: fix test unit

---------

Co-authored-by: anlyyao <[email protected]>
  • Loading branch information
Faxxicy and anlyyao authored Jan 13, 2025
1 parent 93b8e85 commit 9140653
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/cascader/__test__/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import Radio from '../../radio/index';
import { Tabs as TTabs, TabPanel as TTabPanel } from '../../tabs';
import prefixConfig from '../../config';

// scrollTo isn't implemented in JSDOM,see: https://github.com/vuejs/vue-test-utils/issues/319
Element.prototype.scrollTo = () => {};

const { prefix } = prefixConfig;

config.global.stubs = {
Expand Down
7 changes: 5 additions & 2 deletions src/tabs/__test__/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { sleep } from '../../shared/util';
import Sticky from '../../sticky/index';
import { trigger } from '../../image-viewer/__test__/touch';

// scrollTo isn't implemented in JSDOM,see: https://github.com/vuejs/vue-test-utils/issues/319
Element.prototype.scrollTo = () => {};

const prefix = 't';
const name = `${prefix}-tabs`;
const list = [
Expand Down Expand Up @@ -60,7 +63,7 @@ describe('Tabs', () => {
let theme = '';
const wrapper = mount(Tabs, {
props: {
theme: theme,
theme,
},
});
const navWrap = wrapper.find(`.${name}__wrapper`);
Expand Down Expand Up @@ -160,7 +163,7 @@ describe('Tabs', () => {
};
const wrapper = mount(Tabs, {
props: {
size: size,
size,
},
});
const $tabs = wrapper.findComponent(Tabs);
Expand Down
8 changes: 2 additions & 6 deletions src/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,9 @@ export default defineComponent({
if (!tab) return;
const tabLeft = tab?.offsetLeft;
const tabWidth = tab?.offsetWidth;
const navScrollScrollLeft = navScroll.value.scrollLeft;
const navScrollWidth = navScroll.value.offsetWidth;
if (tabLeft + tabWidth - navScrollScrollLeft > navScrollWidth) {
navScroll.value.scrollTo({ left: tabLeft + tabWidth - navScrollWidth, behavior: 'smooth' });
} else if (tabLeft < navScrollScrollLeft) {
navScroll.value.scrollTo({ left: tabLeft, behavior: 'smooth' });
}
const scrollDistance = tabLeft - navScrollWidth / 2 + tabWidth / 2;
navScroll.value.scrollTo({ left: scrollDistance, behavior: 'smooth' });

Check failure on line 131 in src/tabs/tabs.tsx

View workflow job for this annotation

GitHub Actions / call-test-build / test

Unhandled error

TypeError: navScroll.value.scrollTo is not a function ❯ moveToActiveTab src/tabs/tabs.tsx:131:25 ❯ Timeout._onTimeout src/tabs/tabs.tsx:138:9 ❯ listOnTimeout node:internal/timers:569:17 ❯ processTimers node:internal/timers:512:7 This error originated in "src/tabs/__test__/demo.test.jsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "src/tabs/__test__/demo.test.jsx". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 131 in src/tabs/tabs.tsx

View workflow job for this annotation

GitHub Actions / call-test-build / test

Unhandled error

TypeError: navScroll.value.scrollTo is not a function ❯ moveToActiveTab src/tabs/tabs.tsx:131:25 ❯ Timeout._onTimeout src/tabs/tabs.tsx:138:9 ❯ listOnTimeout node:internal/timers:569:17 ❯ processTimers node:internal/timers:512:7 This error originated in "src/tabs/__test__/demo.test.jsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.
}
};

Expand Down

0 comments on commit 9140653

Please sign in to comment.