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: add new prop scrollPosition #756

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ ReactDom.render(
| destroyInactiveTabPane | boolean | false | whether destroy inactive TabPane when change tab |
| active | boolean | false | active feature of tab item |
| tabKey | string | - | key linked to tab |
| scrollPosition | `'start' \| 'end' \| 'center' \| 'auto'` | `'auto'` | scroll position of tab<br>`'start'` means active tab will be at the leftmost, `'end'` means active tab will be at the rightmost, `'center'` means active tab will be at the center, `'auto'` means active tab will be visible regardless of scroll position |


### TabPane(support in older versions)
Expand Down
8 changes: 8 additions & 0 deletions docs/demo/scroll-position.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: scroll-position
nav:
title: Demo
path: /demo
---

<code src="../examples/scroll-position.tsx"></code>
76 changes: 76 additions & 0 deletions docs/examples/scroll-position.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import type { ScrollPosition } from '@/interface';
import React from 'react';
import '../../assets/index.less';
import type { TabsProps } from '../../src';
import Tabs from '../../src';

const items: TabsProps['items'] = [];
for (let i = 0; i < 50; i += 1) {
items.push({
key: String(i),
label: `Tab ${i}`,
children: `Content of ${i}`,
});
}
export default () => {
const [scrollPosition, setScrollPosition] = React.useState<ScrollPosition>('end');

return (
<>
<div style={{ marginBottom: 10, display: 'flex', gap: 10 }}>
<label>
Start
<input
type="radio"
name="scrollPosition"
value="start"
checked={scrollPosition === 'start'}
onChange={e => setScrollPosition(e.target.value as ScrollPosition)}
/>
</label>
<label>
Center
<input
type="radio"
name="scrollPosition"
value="center"
checked={scrollPosition === 'center'}
onChange={e => setScrollPosition(e.target.value as ScrollPosition)}
/>
</label>
<label>
End
<input
type="radio"
name="scrollPosition"
value="end"
checked={scrollPosition === 'end'}
onChange={e => setScrollPosition(e.target.value as ScrollPosition)}
/>
</label>
<label>
Auto
<input
type="radio"
name="scrollPosition"
value="auto"
checked={scrollPosition === 'auto'}
onChange={e => setScrollPosition(e.target.value as ScrollPosition)}
/>
</label>
</div>
<div style={{ maxWidth: 550 }}>
<Tabs items={items} scrollPosition={scrollPosition} />
</div>

<div style={{ maxHeight: 550 }}>
<Tabs
style={{ height: 550 }}
items={items}
scrollPosition={scrollPosition}
tabPosition="left"
/>
</div>
</>
);
};
55 changes: 42 additions & 13 deletions src/TabNavList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
MoreProps,
OnTabScroll,
RenderTabBar,
ScrollPosition,
SizeInfo,
TabBarExtraContent,
TabPosition,
Expand Down Expand Up @@ -55,6 +56,7 @@
size?: GetIndicatorSize;
align?: 'start' | 'center' | 'end';
};
scrollPosition?: ScrollPosition;
}

const getTabSize = (tab: HTMLElement, containerRect: { left: number; top: number }) => {
Expand Down Expand Up @@ -104,6 +106,7 @@
editable,
locale,
tabPosition,
scrollPosition,
tabBarGutter,
children,
onTabClick,
Expand Down Expand Up @@ -150,7 +153,8 @@
const addSizeValue = getUnitValue(addSize, tabPositionTopOrBottom);
const operationSizeValue = getUnitValue(operationSize, tabPositionTopOrBottom);

const needScroll = Math.floor(containerExcludeExtraSizeValue) < Math.floor(tabContentSizeValue + addSizeValue);
const needScroll =
Math.floor(containerExcludeExtraSizeValue) < Math.floor(tabContentSizeValue + addSizeValue);
const visibleTabContentValue = needScroll
? containerExcludeExtraSizeValue - operationSizeValue
: containerExcludeExtraSizeValue - addSizeValue;
Expand Down Expand Up @@ -264,19 +268,36 @@
// ============ Align with top & bottom ============
let newTransform = transformLeft;

// RTL
if (rtl) {
if (tabOffset.right < transformLeft) {
// RTL logic
if (scrollPosition === 'auto') {
if (tabOffset.right < transformLeft) {
newTransform = tabOffset.right;
} else if (tabOffset.right + tabOffset.width > transformLeft + visibleTabContentValue) {
newTransform = tabOffset.right + tabOffset.width - visibleTabContentValue;
}
} else if (scrollPosition === 'start') {
newTransform = tabOffset.right;
} else if (tabOffset.right + tabOffset.width > transformLeft + visibleTabContentValue) {
} else if (scrollPosition === 'end') {
newTransform = tabOffset.right + tabOffset.width - visibleTabContentValue;
} else if (scrollPosition === 'center') {
newTransform = tabOffset.right + tabOffset.width / 2 - visibleTabContentValue / 2;

Check warning on line 284 in src/TabNavList/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/TabNavList/index.tsx#L284

Added line #L284 was not covered by tests
}
} else {
// LTR logic
if (scrollPosition === 'auto') {
if (tabOffset.left < -transformLeft) {
newTransform = -tabOffset.left;
} else if (tabOffset.left + tabOffset.width > -transformLeft + visibleTabContentValue) {
newTransform = -(tabOffset.left + tabOffset.width - visibleTabContentValue);
}
} else if (scrollPosition === 'start') {
newTransform = -tabOffset.left;

Check warning on line 295 in src/TabNavList/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/TabNavList/index.tsx#L295

Added line #L295 was not covered by tests
} else if (scrollPosition === 'end') {
newTransform = -(tabOffset.left + tabOffset.width - visibleTabContentValue);

Check warning on line 297 in src/TabNavList/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/TabNavList/index.tsx#L297

Added line #L297 was not covered by tests
} else if (scrollPosition === 'center') {
newTransform = -(tabOffset.left + tabOffset.width / 2 - visibleTabContentValue / 2);

Check warning on line 299 in src/TabNavList/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/TabNavList/index.tsx#L299

Added line #L299 was not covered by tests
}
}
// LTR
else if (tabOffset.left < -transformLeft) {
newTransform = -tabOffset.left;
} else if (tabOffset.left + tabOffset.width > -transformLeft + visibleTabContentValue) {
newTransform = -(tabOffset.left + tabOffset.width - visibleTabContentValue);
}

setTransformTop(0);
Expand All @@ -285,10 +306,18 @@
// ============ Align with left & right ============
let newTransform = transformTop;

if (tabOffset.top < -transformTop) {
if (scrollPosition === 'auto') {
if (tabOffset.top < -transformTop) {
newTransform = -tabOffset.top;
} else if (tabOffset.top + tabOffset.height > -transformTop + visibleTabContentValue) {
newTransform = -(tabOffset.top + tabOffset.height - visibleTabContentValue);
}
} else if (scrollPosition === 'start') {
newTransform = -tabOffset.top;
} else if (tabOffset.top + tabOffset.height > -transformTop + visibleTabContentValue) {
} else if (scrollPosition === 'end') {
newTransform = -(tabOffset.top + tabOffset.height - visibleTabContentValue);
} else if (scrollPosition === 'center') {
newTransform = -(tabOffset.top + tabOffset.height / 2 - visibleTabContentValue / 2);

Check warning on line 320 in src/TabNavList/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/TabNavList/index.tsx#L320

Added line #L320 was not covered by tests
}

setTransformLeft(0);
Expand Down Expand Up @@ -323,7 +352,6 @@
onTabClick(key, e);
}}
onFocus={() => {
scrollToTab(key);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个部分有点多余,因为useEffect里每当activeKey更换的时候会触发scrollToTab,所以没必要在这里触发多一次

doLockAnimation();
if (!tabsWrapperRef.current) {
return;
Expand Down Expand Up @@ -411,6 +439,7 @@
stringify(activeTabOffset),
stringify(tabOffsets as any),
tabPositionTopOrBottom,
scrollPosition,
]);

// Should recalculate when rtl changed
Expand Down
5 changes: 5 additions & 0 deletions src/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
MoreProps,
OnTabScroll,
RenderTabBar,
ScrollPosition,
Tab,
TabBarExtraContent,
TabPosition,
Expand Down Expand Up @@ -72,6 +73,8 @@ export interface TabsProps
size?: GetIndicatorSize;
align?: 'start' | 'center' | 'end';
};

scrollPosition?: ScrollPosition;
}

const Tabs = React.forwardRef<HTMLDivElement, TabsProps>((props, ref) => {
Expand Down Expand Up @@ -99,6 +102,7 @@ const Tabs = React.forwardRef<HTMLDivElement, TabsProps>((props, ref) => {
getPopupContainer,
popupClassName,
indicator,
scrollPosition = 'auto',
...restProps
} = props;
const tabs = React.useMemo<Tab[]>(
Expand Down Expand Up @@ -182,6 +186,7 @@ const Tabs = React.forwardRef<HTMLDivElement, TabsProps>((props, ref) => {
getPopupContainer,
popupClassName,
indicator,
scrollPosition,
};

return (
Expand Down
10 changes: 6 additions & 4 deletions src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { DropdownProps } from 'rc-dropdown/lib/Dropdown';
import type { CSSMotionProps } from 'rc-motion';
import type React from 'react';
import type { TabNavListProps } from './TabNavList';
import type { TabPaneProps } from './TabPanelList/TabPane';
import { DropdownProps } from 'rc-dropdown/lib/Dropdown';

export type TriggerProps = {
trigger?: 'hover' | 'click';
}
};
export type moreIcon = React.ReactNode;
export type MoreProps = {
icon?: moreIcon,
icon?: moreIcon;
} & Omit<DropdownProps, 'children'>;

export type SizeInfo = [width: number, height: number];
Expand Down Expand Up @@ -45,7 +45,7 @@ type RenderTabBarProps = {
mobile: boolean;
editable: EditableConfig;
locale: TabsLocale;
more: MoreProps,
more: MoreProps;
tabBarGutter: number;
onTabClick: (key: string, e: React.MouseEvent | React.KeyboardEvent) => void;
onTabScroll: OnTabScroll;
Expand Down Expand Up @@ -89,3 +89,5 @@ export type TabBarExtraPosition = 'left' | 'right';
export type TabBarExtraMap = Partial<Record<TabBarExtraPosition, React.ReactNode>>;

export type TabBarExtraContent = React.ReactNode | TabBarExtraMap;

export type ScrollPosition = 'start' | 'end' | 'center' | 'auto';