From 12e4ee76399617e38610724b061140db7ff7e3ad Mon Sep 17 00:00:00 2001 From: 2betop <2betop.cn@gmail.com> Date: Tue, 26 May 2020 09:37:25 +0800 Subject: [PATCH] =?UTF-8?q?Tabs=20=E4=BC=98=E5=8C=96=EF=BC=8C=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E6=98=BE=E7=A4=BA=E7=AC=AC=E4=B8=80=E4=B8=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Tabs.tsx | 49 +++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/components/Tabs.tsx b/src/components/Tabs.tsx index dbe54c610..a78a25413 100644 --- a/src/components/Tabs.tsx +++ b/src/components/Tabs.tsx @@ -7,7 +7,8 @@ import React from 'react'; import {Schema} from '../types'; import Transition, {ENTERED, ENTERING} from 'react-transition-group/Transition'; -import {ClassNamesFn, themeable} from '../theme'; +import {ClassNamesFn, themeable, ThemeProps} from '../theme'; +import uncontrollable from 'uncontrollable'; const transitionStyles: { [propName: string]: string; @@ -16,14 +17,13 @@ const transitionStyles: { [ENTERED]: 'in' }; -export interface TabProps { +export interface TabProps extends ThemeProps { title?: string; // 标题 icon?: string; disabled?: boolean | string; eventKey: string | number; tab?: Schema; className?: string; - classnames?: ClassNamesFn; activeKey?: string | number; reload?: boolean; mountOnEnter?: boolean; @@ -31,14 +31,12 @@ export interface TabProps { toolbar?: React.ReactNode; } -export interface TabsProps { - mode?: '' | 'line' | 'card' | 'radio' | 'vertical'; +export interface TabsProps extends ThemeProps { + mode: '' | 'line' | 'card' | 'radio' | 'vertical'; tabsMode?: '' | 'line' | 'card' | 'radio' | 'vertical'; additionBtns?: React.ReactNode; onSelect?: (key: string | number) => void; - classPrefix: string; - classnames: ClassNamesFn; - activeKey: string | number; + activeKey?: string | number; contentClassName: string; className?: string; tabs?: Array; @@ -62,8 +60,10 @@ export class Tabs extends React.Component { return; } - const {classnames: cx, activeKey} = this.props; + const {classnames: cx, activeKey: activeKeyProp} = this.props; const {eventKey, disabled, icon, title, toolbar} = child.props; + const activeKey = + activeKeyProp === undefined && index === 0 ? eventKey : activeKeyProp; return (
  • { return; } - const {activeKey, classnames} = this.props; + const {activeKey: activeKeyProp, classnames} = this.props; + const eventKey = child.props.eventKey; + const activeKey = + activeKeyProp === undefined && index === 0 ? eventKey : activeKeyProp; return React.cloneElement(child, { ...child.props, @@ -108,6 +111,7 @@ export class Tabs extends React.Component { children, additionBtns, toolbar, + activeKey: activeKeyProps } = this.props; if (!Array.isArray(children)) { @@ -142,7 +146,7 @@ export class Tabs extends React.Component { } } -export class Tab extends React.PureComponent { +class TabComponent extends React.PureComponent { contentDom: any; contentRef = (ref: any) => (this.contentDom = ref); @@ -172,15 +176,12 @@ export class Tab extends React.PureComponent { return (
    {children}
    @@ -191,4 +192,10 @@ export class Tab extends React.PureComponent { } } -export default themeable(Tabs); +export const Tab = themeable(TabComponent); + +export default themeable( + uncontrollable(Tabs, { + activeKey: 'onSelect' + }) +);