From a598741e32c99fa91223f9a63119d8ee0e82258e Mon Sep 17 00:00:00 2001 From: myxvisual Date: Tue, 8 Aug 2017 10:40:01 +0800 Subject: [PATCH] feat: Add Tabs support css-in-js and custom animate #Close #11 --- src/Tabs/index.tsx | 101 +++++++++++++++++++++++++++++++++------------ 1 file changed, 75 insertions(+), 26 deletions(-) diff --git a/src/Tabs/index.tsx b/src/Tabs/index.tsx index 0c834d9d..354dd879 100644 --- a/src/Tabs/index.tsx +++ b/src/Tabs/index.tsx @@ -27,6 +27,26 @@ export interface DataProps { * Custom set render `Tab Title` method. */ renderTitle?: (title?: string) => React.ReactNode; + /** + * If true, will add animate to tabs in out. + */ + useAnimate?: boolean; + /** + * Set tabs animate mode. + */ + animateMode?: "in" | "out" | "in-out"; + /** + * Set tabs animate speed. + */ + animateSpeed?: number; + /** + * Set tab animate enter style. + */ + animateEnterStyle?: React.CSSProperties; + /** + * Set tab animate leave style. + */ + animateLeaveStyle?: React.CSSProperties; } export interface TabsProps extends DataProps, React.HTMLAttributes {} @@ -37,7 +57,18 @@ export interface TabsState { export class Tabs extends React.Component { static defaultProps: TabsProps = { - renderTitle: (title: string) => title + renderTitle: (title: string) => title, + useAnimate: true, + animateMode: "in", + animateSpeed: 500, + animateEnterStyle: { + transform: "translateX(0)", + opacity: 1 + }, + animateLeaveStyle: { + transform: "translateX(100%)", + opacity: 0 + } }; state: TabsState = { @@ -65,11 +96,17 @@ export class Tabs extends React.Component { children, tabStyle, renderTitle, + useAnimate, + animateMode, + animateSpeed, + animateEnterStyle, + animateLeaveStyle, + className, + style, ...attributes } = this.props; const { theme } = this.context; const { tabFocusIndex } = this.state; - const styles = getStyles(this); const childrenArray = React.Children.toArray(children); const isAvailableArray = childrenArray && childrenArray.length > 0; @@ -77,18 +114,28 @@ export class Tabs extends React.Component { const currTab: Tab | false = tabs && tabs[tabFocusIndex] as any; const tabTitle = currTab && currTab.props.title; + const inlineStyles = getStyles(this); + const styles = theme.prepareStyles({ + className: "tabs", + styles: inlineStyles + }); + + const normalRender = ( +
+ {currTab} +
+ ); + return (
-
+
{tabs && tabs.map((tab, index) => ( this.setState({ tabFocusIndex: index })} > @@ -96,22 +143,18 @@ export class Tabs extends React.Component { ))}
- -
- {currTab} -
-
+ {useAnimate ? ( + + {normalRender} + + ) : normalRender}
); } @@ -156,12 +199,18 @@ function getStyles(Tabs: Tabs): { fontWeight: "lighter", cursor: "pointer", fontSize: 18, - padding: 4, - marginRight: 4, + padding: "4px 12px", transition: "all .25s", ...tabTitleStyle }), titleFocus: prefixStyle({ + color: theme.baseHigh, + fontWeight: "lighter", + cursor: "pointer", + fontSize: 18, + padding: "4px 12px", + transition: "all .25s", + ...tabTitleStyle, borderBottom: `2px solid ${theme.accent}`, ...tabTitleFocusStyle }),