diff --git a/components/Swipe/Swipe.jsx b/components/Swipe/Swipe.jsx index 8cb4ef1eb..a4a482685 100644 --- a/components/Swipe/Swipe.jsx +++ b/components/Swipe/Swipe.jsx @@ -233,6 +233,10 @@ class Swipe extends Component { const newItems = React.Children.map(items, (element, index) => { return cloneElement(element, { key: index, + className: classnames({ + [`${props.prefixCls}-item`]: true, + [element.props.className]: !!element.props.className, + }), }); }); @@ -290,7 +294,7 @@ class Swipe extends Component { } render() { - const { prefixCls, className, height, children } = this.props; + const { prefixCls, className, height, showPagination, children } = this.props; const classes = classnames({ [`${prefixCls}`]: true, @@ -322,23 +326,27 @@ class Swipe extends Component { onTouchEnd={event => this.onTouchEnd(event)}> { this.state.items } -
- -
+ { + showPagination + ?
+ +
+ : null + } ); } @@ -355,6 +363,7 @@ Swipe.propTypes = { autoPlayIntervalTime: PropTypes.number, moveDistanceRatio: PropTypes.number, moveTimeSpan: PropTypes.number, + showPagination: PropTypes.bool, onChange: PropTypes.func, onChangeEnd: PropTypes.func, }; @@ -370,6 +379,7 @@ Swipe.defaultProps = { autoPlayIntervalTime: 3000, moveDistanceRatio: 0.5, moveTimeSpan: 300, + showPagination: true, onChange() {}, onChangeEnd() {}, }; diff --git a/components/Tab/Tab.jsx b/components/Tab/Tab.jsx index 9b280a06a..16c221c03 100644 --- a/components/Tab/Tab.jsx +++ b/components/Tab/Tab.jsx @@ -1,7 +1,8 @@ import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; -import TabContainer from './TabContainer'; +import TabPanel from './TabPanel'; +import Swipe from '../Swipe'; function getSelectIndex(children) { let selectIndex; @@ -13,7 +14,7 @@ function getSelectIndex(children) { return selectIndex; } -class TabGroup extends PureComponent { +class Tab extends PureComponent { constructor(props) { super(props); this.state = { @@ -24,21 +25,14 @@ class TabGroup extends PureComponent { componentWillReceiveProps(nextProps) { if ('value' in nextProps || getSelectIndex(nextProps.children)) { this.setState({ - value: nextProps.value, + value: nextProps.value || nextProps.defaultValue || getSelectIndex(nextProps.children) || 0, }); nextProps.onChange(nextProps.value); } } - getTitleItemCls(idx) { - const { prefixCls } = this.props; - return idx === this.state.value - ? `${prefixCls}-header-item active` - : `${prefixCls}-header-item`; - } - render() { - const { prefixCls, className, theme, lineWidth, disabled, children, onChange } = this.props; + const { prefixCls, className, theme, lineWidth, disabled, canSwipe, children, onChange } = this.props; const classes = classnames({ [`${prefixCls}`]: true, @@ -47,37 +41,59 @@ class TabGroup extends PureComponent { }); // 渲染选项 - const itemsRender = React.Children.map(children, (item, $index) => { - const itemClasses = classnames({ + const tabsRender = React.Children.map(children, (item, $index) => { + const itemCls = classnames({ [`${prefixCls}-header-item`]: true, + [item.props.className]: !!item.props.className, disabled: disabled || item.props.disabled, active: this.state.value === $index, - [item.className]: !!item.className, }); return (
  • { if (disabled || item.props.disabled) return; this.setState({ value: $index }); - onChange($index); + typeof onChange === 'function' && onChange($index); + canSwipe && this.swipe.onSlideTo($index); }}> - { this.tabItem = tabItem; }}>{item.props.title} + {item.props.title}
  • ); }); // 渲染内容 - const contentRender = React.Children.map(children, (item, $index) => { - return ( - - {item.props.children} - + let contentRender; + + if (canSwipe) { + contentRender = ( + { this.swipe = ele; }} + onChange={(value) => { + this.setState({ value }); + typeof onChange === 'function' && onChange(value); + }}> + { + React.Children.map(children, (item) => { + return ( +
    {item.props.children}
    + ); + }) + } +
    ); - }); + } else { + contentRender = React.Children.map(children, (item, $index) => { + return ( + + ); + }); + } const lineStyle = { width: `${100 / children.length}%`, @@ -102,7 +118,7 @@ class TabGroup extends PureComponent { return (
    -
      {itemsRender}
    +
      {tabsRender}
    {lineInnerRender}
    @@ -113,22 +129,24 @@ class TabGroup extends PureComponent { } } -TabGroup.propTypes = { +Tab.propTypes = { prefixCls: PropTypes.string, className: PropTypes.string, theme: PropTypes.oneOf(['default', 'primary', 'info', 'success', 'warning', 'error']), lineWidth: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), disabled: PropTypes.bool, + canSwipe: PropTypes.bool, onChange: PropTypes.func, }; -TabGroup.defaultProps = { +Tab.defaultProps = { prefixCls: 'za-tab', className: null, theme: 'primary', lineWidth: null, disabled: false, + canSwipe: false, onChange() {}, }; -export default TabGroup; +export default Tab; diff --git a/components/Tab/TabContainer.jsx b/components/Tab/TabPanel.jsx similarity index 86% rename from components/Tab/TabContainer.jsx rename to components/Tab/TabPanel.jsx index a977b7cd8..0f38c773b 100644 --- a/components/Tab/TabContainer.jsx +++ b/components/Tab/TabPanel.jsx @@ -2,7 +2,7 @@ import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; -class TabContainer extends PureComponent { +class TabPanel extends PureComponent { constructor(props) { super(props); this.state = { @@ -35,14 +35,14 @@ class TabContainer extends PureComponent { } } -TabContainer.propTypes = { +TabPanel.propTypes = { prefixCls: PropTypes.string, className: PropTypes.string, }; -TabContainer.defaultProps = { +TabPanel.defaultProps = { prefixCls: 'za-tab-container-item', className: null, }; -export default TabContainer; +export default TabPanel; \ No newline at end of file diff --git a/components/Tab/index.js b/components/Tab/index.js index 4b6d01954..67b490cb4 100644 --- a/components/Tab/index.js +++ b/components/Tab/index.js @@ -1,6 +1,6 @@ import Tab from './Tab'; -import TabItem from './TabContainer'; +import TabPanel from './TabPanel'; -Tab.Item = TabItem; +Tab.Panel = TabPanel; export default Tab; diff --git a/examples/pages/SwipePage.jsx b/examples/pages/SwipePage.jsx index 7c02dd76d..8e1295e9f 100644 --- a/examples/pages/SwipePage.jsx +++ b/examples/pages/SwipePage.jsx @@ -6,29 +6,16 @@ import { Panel, Swipe, Button } from '../../components'; import '../styles/pages/SwipePage'; const ITEMS = [ - { - url: '#', - img: require('../images/banners/1.png'), - }, - { - url: '#', - img: require('../images/banners/2.png'), - }, - { - url: '#', - img: require('../images/banners/3.png'), - }, + require('../images/banners/1.png'), + require('../images/banners/2.png'), + require('../images/banners/3.png'), ]; function contentRender() { return ITEMS.map((item, i) => { return ( -
    -
    - - {item.title} - -
    +
    +
    ); }); diff --git a/examples/pages/TabPage.jsx b/examples/pages/TabPage.jsx index 9895276e9..7b42b0ed8 100644 --- a/examples/pages/TabPage.jsx +++ b/examples/pages/TabPage.jsx @@ -15,7 +15,7 @@ class TabPage extends Component { } render() { - const { Item } = Tab; + const TabPanel = Tab.Panel; return ( @@ -27,12 +27,28 @@ class TabPage extends Component { { console.log(i); }}> - +
    选项卡1内容
    -
    - + +
    选项卡2内容
    -
    + +
    +
    + + + + + 可滑动 + + + { console.log(i); }}> + +
    试试点我左滑
    +
    + +
    试试点我右滑
    +
    @@ -43,24 +59,24 @@ class TabPage extends Component { - - - + + + - - - + + + - - - + + + - - - + + + @@ -77,21 +93,21 @@ class TabPage extends Component { activeIndex: i, }); }}> - - - + + + console.log(i)}> - +
    选项卡1内容
    -
    - +
    +
    选项卡2内容
    -
    - + +
    选项卡3内容
    -
    + @@ -102,15 +118,15 @@ class TabPage extends Component { - +
    选项卡1内容
    -
    - + +
    选项卡2内容
    -
    - + +
    选项卡3内容
    -
    +
    @@ -121,15 +137,15 @@ class TabPage extends Component { - +
    选项卡1内容
    -
    - + +
    选项卡2内容
    -
    - + +
    选项卡3内容
    -
    +
    @@ -140,15 +156,15 @@ class TabPage extends Component { - +
    选项卡1内容
    -
    - + +
    选项卡2内容
    -
    - + +
    选项卡3内容
    -
    +
    diff --git a/styles/components/Swipe.scss b/styles/components/Swipe.scss index 3f5b22942..9dcf10c07 100644 --- a/styles/components/Swipe.scss +++ b/styles/components/Swipe.scss @@ -3,11 +3,13 @@ $prefixCls: za-swipe; .#{$prefixCls} { position: relative; overflow: hidden; + font-size: 0; &-item { display: inline-block; width: 100%; height: 100%; + font-size: r(16); } &-pagination {