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/tabs Tabs组件对齐vue #515

Merged
merged 10 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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 site/mobile/mobile.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default {
{
title: 'Tabs 选项卡',
name: 'tabs',
component: () => import('tdesign-mobile-react/tabs/_example/index.jsx'),
component: () => import('tdesign-mobile-react/tabs/_example/index.tsx'),
},
{
title: 'Input 输入框',
Expand Down
4 changes: 2 additions & 2 deletions src/sticky/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import _Sticky from './Sticky';

import { TdStickyProps } from './type';
import './style';

export const Sticky = _Sticky;
export type StickyProps = TdStickyProps;

export default Sticky;

64 changes: 28 additions & 36 deletions src/tabs/TabPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,40 @@
import React, { FC, useContext } from 'react';
import classnames from 'classnames';
import useConfig from '../_util/useConfig';
import React, { FC, useContext, useEffect, useMemo, useState } from 'react';
import { TdTabPanelProps } from './type';
epoll-j marked this conversation as resolved.
Show resolved Hide resolved
import TabContext from './context';
import { usePrefixClass } from '../hooks/useClass';
import parseTNode from '../_util/parseTNode';

const TabPanel: FC<TdTabPanelProps> = (props) => {
const { value, label, disabled } = props;
const { value, lazy, destroyOnHide, children, panel } = props;

const { classPrefix } = useConfig();
const tabPrefix = classPrefix || '';
const tabPanelClass = usePrefixClass('tab-panel');
const tabClass = usePrefixClass('tabs');
const tabProps = useContext(TabContext);
const { activeKey, horiRef, vetiRef, onChange } = tabProps;
const { activeKey } = tabProps;
const isActive = useMemo(() => value === activeKey, [activeKey, value]);
const [isMount, setIsMount] = useState(lazy ? isActive : true);

useEffect(() => {
if (isActive) {
if (!isMount) {
setIsMount(true);
}
} else if (destroyOnHide) {
setIsMount(false);
}
}, [destroyOnHide, isActive, isMount, lazy]);

const change = () => {
if (disabled) return;
onChange && onChange(value);
};
return (
<div
ref={(ref) => {
if (activeKey === value) {
vetiRef.current = ref;
}
}}
className={classnames(
`${tabPrefix}-tabs__nav-item`,
activeKey === value && `${tabPrefix}-is-active`,
disabled && `${tabPrefix}-is-disabled`,
)}
key={value}
onClick={() => change()}
>
<span
ref={(ref) => {
if (activeKey === value) {
horiRef.current = ref;
}
}}
className={`${tabPrefix}-tabs__nav-item-btn`}
isMount && (
<div
style={{ display: isActive ? 'block' : 'none' }}
className={`${tabPanelClass} ${tabClass}__panel`}
key={value}
>
{label}
</span>
</div>
{parseTNode(children, panel)}
</div>
)
);
};

TabPanel.displayName = 'TabPanel';
export default TabPanel;
Loading
Loading