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

refactor: rename indicatorLength with indicatorSize #672

Merged
merged 2 commits into from
Aug 30, 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 docs/examples/indicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default () => {

return (
<React.StrictMode>
<Tabs tabBarExtraContent="extra" items={items} getIndicatorLength={(origin) => origin - 16} />
<Tabs tabBarExtraContent="extra" items={items} indicatorSize={(origin) => origin - 16} />
</React.StrictMode>
);
};
8 changes: 4 additions & 4 deletions src/TabNavList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import ExtraContent from './ExtraContent';
import OperationNode from './OperationNode';
import TabNode from './TabNode';
import useIndicator from '../hooks/useIndicator';
import type { GetIndicatorLength } from '../hooks/useIndicator';
import type { GetIndicatorSize } from '../hooks/useIndicator';

export interface TabNavListProps {
id: string;
Expand All @@ -50,7 +50,7 @@ export interface TabNavListProps {
children?: (node: React.ReactElement) => React.ReactElement;
getPopupContainer?: (node: HTMLElement) => HTMLElement;
popupClassName?: string;
indicatorLength?: GetIndicatorLength;
indicatorSize?: GetIndicatorSize;
}

const getSize = (refObj: React.RefObject<HTMLElement>): SizeInfo => {
Expand Down Expand Up @@ -82,7 +82,7 @@ function TabNavList(props: TabNavListProps, ref: React.Ref<HTMLDivElement>) {
children,
onTabClick,
onTabScroll,
indicatorLength,
indicatorSize,
} = props;
const containerRef = useRef<HTMLDivElement>();
const extraLeftRef = useRef<HTMLDivElement>();
Expand Down Expand Up @@ -369,7 +369,7 @@ function TabNavList(props: TabNavListProps, ref: React.Ref<HTMLDivElement>) {
activeTabOffset,
horizontal: tabPositionTopOrBottom,
rtl,
indicatorLength,
indicatorSize,
})

// ========================= Effect ========================
Expand Down
8 changes: 4 additions & 4 deletions src/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
import TabContext from './TabContext';
import TabNavListWrapper from './TabNavList/Wrapper';
import useAnimateConfig from './hooks/useAnimateConfig';
import type { GetIndicatorLength } from './hooks/useIndicator';
import type { GetIndicatorSize } from './hooks/useIndicator';

/**
* Should added antd:
Expand Down Expand Up @@ -71,7 +71,7 @@ export interface TabsProps
popupClassName?: string;

// Indicator
indicatorLength?: GetIndicatorLength;
indicatorSize?: GetIndicatorSize;
}

function Tabs(
Expand Down Expand Up @@ -99,7 +99,7 @@ function Tabs(
onTabScroll,
getPopupContainer,
popupClassName,
indicatorLength,
indicatorSize,
...restProps
}: TabsProps,
ref: React.Ref<HTMLDivElement>,
Expand Down Expand Up @@ -185,7 +185,7 @@ function Tabs(
panes: null,
getPopupContainer,
popupClassName,
indicatorLength,
indicatorSize,
};

return (
Expand Down
21 changes: 11 additions & 10 deletions src/hooks/useIndicator.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import React, { useEffect, useRef, useState } from 'react';
import type React from 'react';
import { useEffect, useRef, useState } from 'react';
import raf from 'rc-util/lib/raf';
import { TabOffset } from '../interface';
import type { TabOffset } from '../interface';

export type GetIndicatorLength = number | ((origin: number) => number);
export type GetIndicatorSize = number | ((origin: number) => number);

export type UseIndicator = (options: {
activeTabOffset: TabOffset,
horizontal: boolean;
rtl: boolean;
indicatorLength: GetIndicatorLength;
indicatorSize: GetIndicatorSize;
}) => {
style: React.CSSProperties;
}
Expand All @@ -17,17 +18,17 @@ const useIndicator: UseIndicator = ({
activeTabOffset,
horizontal,
rtl,
indicatorLength,
indicatorSize,
}) => {
const [inkStyle, setInkStyle] = useState<React.CSSProperties>();
const inkBarRafRef = useRef<number>();

const getLength = (origin: number) => {
if (typeof indicatorLength === 'function') {
return indicatorLength(origin);
if (typeof indicatorSize === 'function') {
return indicatorSize(origin);
}
if (typeof indicatorLength === 'number') {
return indicatorLength;
if (typeof indicatorSize === 'number') {
return indicatorSize;
}
return origin;
}
Expand Down Expand Up @@ -63,7 +64,7 @@ const useIndicator: UseIndicator = ({
});

return cleanInkBarRaf;
}, [activeTabOffset, horizontal, rtl, indicatorLength]);
}, [activeTabOffset, horizontal, rtl, indicatorSize]);

return {
style: inkStyle,
Expand Down
6 changes: 3 additions & 3 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -584,12 +584,12 @@ describe('Tabs.Basic', () => {
render(<Tabs items={[{key: 1 as any, label: 'test'}]} />)
})

it('support getIndicatorLength', async () => {
const { container, rerender } = render(getTabs({ indicatorLength: 10 }));
it('support indicatorSize', async () => {
const { container, rerender } = render(getTabs({ indicatorSize: 10 }));
await waitFakeTimer();
expect(container.querySelector('.rc-tabs-ink-bar')).toHaveStyle({ width: '10px' });

rerender(getTabs({ indicatorLength: (origin) => origin - 2 }));
rerender(getTabs({ indicatorSize: (origin) => origin - 2 }));
await waitFakeTimer();
expect(container.querySelector('.rc-tabs-ink-bar')).toHaveStyle({ width: '18px' });
})
Expand Down
Loading