-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #605 from XiaoMi/develop
2.2.0
- Loading branch information
Showing
24 changed files
with
409 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
import React, { Component } from 'react' | ||
import PropTypes from 'prop-types' | ||
import Icon from '../icon' | ||
import classNames from 'classnames' | ||
import './style/index' | ||
|
||
// const url = 'http://i1.mifile.cn/f/i/hiui/docs/' | ||
class Carousel extends Component { | ||
constructor (props) { | ||
super(props) | ||
this.rootRef = React.createRef() | ||
const defaultActive = props.defaultActive | ||
this.state = { | ||
rootWidth: 0, | ||
showArrow: false, | ||
active: (defaultActive >= props.children.length || defaultActive < 0) ? 0 : defaultActive | ||
} | ||
this.timer = null | ||
} | ||
|
||
componentDidMount () { | ||
this.setState({ | ||
rootWidth: this.rootRef.current.clientWidth | ||
}, () => { | ||
if (this.props.duration) { | ||
this.autoPage() | ||
} | ||
}) | ||
} | ||
|
||
goTo (page) { | ||
if (page > this.props.children.length || page < 0) { | ||
return | ||
} | ||
this.setState({ | ||
active: page | ||
}) | ||
} | ||
componentWillUnmount () { | ||
this.timer && window.clearInterval(this.timer) | ||
} | ||
|
||
autoPage () { | ||
this.timer = window.setInterval(() => { | ||
this.preNextEvent(1) | ||
}, this.props.duration) | ||
} | ||
|
||
pageEvent (active) { | ||
this.setState({ | ||
active | ||
}) | ||
} | ||
|
||
preNextEvent (val) { | ||
let active = this.state.active + val | ||
if (active >= this.props.children.length) { | ||
active = 0 | ||
} | ||
this.setState({ | ||
active | ||
}) | ||
} | ||
|
||
renderDot (type, key, index, active) { | ||
if (type === 'sign') { | ||
return <li className='hi-carousel__dot hi-carousel__dot--sign' /> | ||
} else { | ||
const cls = classNames( | ||
'hi-carousel__dot', | ||
active === index && 'hi-carousel__dot--active' | ||
) | ||
return ( | ||
<li | ||
className={cls} | ||
key={index} | ||
onClick={this.pageEvent.bind(this, index)} | ||
/> | ||
) | ||
} | ||
} | ||
|
||
mouseEvent (type) { | ||
let showArrow = true | ||
if (type === 'over') { | ||
this.timer && window.clearInterval(this.timer) | ||
} else { | ||
showArrow = false | ||
this.props.duration && this.autoPage() | ||
} | ||
this.setState({showArrow}) | ||
} | ||
render () { | ||
const { rootWidth, active, showArrow } = this.state | ||
const { showDots, showArrows } = this.props | ||
const children = React.Children.toArray(this.props.children) | ||
const arrowCls = classNames( | ||
'hi-carousel__arrows', | ||
showArrow && 'hi-carousel__arrows--show' | ||
) | ||
return <div | ||
className='hi-carousel' | ||
ref={this.rootRef} | ||
onMouseOver={this.mouseEvent.bind(this, 'over')} | ||
onMouseOut={this.mouseEvent.bind(this, 'out')}> | ||
<div | ||
className='hi-carousel__container' | ||
style={{ | ||
width: rootWidth * children.length | ||
}} | ||
> | ||
{ | ||
children.map((child, index) => { | ||
return React.cloneElement(child, { | ||
key: index, | ||
style: { | ||
position: 'relative', | ||
opacity: active === index ? 1 : 0, | ||
transition: 'opacity 300ms ease 0s', | ||
left: -(rootWidth * index), | ||
width: rootWidth, | ||
display: 'inline-block', | ||
fontSize: 24, | ||
...child.props.style | ||
} | ||
}) | ||
}) | ||
} | ||
</div> | ||
{ | ||
showArrows && <ul className={arrowCls}> | ||
<li className='hi-carousel__arrow' onClick={this.preNextEvent.bind(this, -1)}> | ||
<Icon name='left' /> | ||
</li> | ||
<li className='hi-carousel__arrow' onClick={this.preNextEvent.bind(this, 1)}> | ||
<Icon name='right' /> | ||
</li> | ||
</ul> | ||
} | ||
{ | ||
showDots && <ul className='hi-carousel__dots'> | ||
{ | ||
children.map((_, index) => { | ||
const cls = classNames('hi-carousel__dot', active === index && 'hi-carousel__dot--active') | ||
return <li | ||
className={cls} | ||
key={index} | ||
onClick={this.pageEvent.bind(this, index)} | ||
/> | ||
}) | ||
} | ||
</ul> | ||
} | ||
</div> | ||
} | ||
} | ||
|
||
Carousel.propTypes = { | ||
duration: PropTypes.number, | ||
onClick: PropTypes.func, | ||
beforeChange: PropTypes.func, | ||
afterChange: PropTypes.func, | ||
showDots: PropTypes.bool, | ||
showArrows: PropTypes.bool, | ||
defaultActive: PropTypes.number | ||
} | ||
Carousel.defaultProps = { | ||
duration: 0, | ||
onClick: () => {}, | ||
beforeChange: () => {}, | ||
afterChange: () => {}, | ||
showDots: true, | ||
showArrows: true, | ||
defaultActive: 0 | ||
} | ||
|
||
export default Carousel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import './index.scss' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
.hi-carousel { | ||
width: 100%; | ||
color: rgba(153, 153, 153, 1); | ||
overflow: hidden; | ||
position: relative; | ||
|
||
&__container { | ||
height: 100%; | ||
transition: all 500ms; | ||
} | ||
|
||
&__dots { | ||
position: absolute; | ||
bottom: 24px; | ||
margin: 0; | ||
padding: 0; | ||
left: 50%; | ||
transform: translateX(-50%); | ||
} | ||
|
||
&__dot { | ||
width: 24px; | ||
height: 4px; | ||
background: rgba(255, 255, 255, 0.4); | ||
border-radius: 2px; | ||
display: inline-block; | ||
margin-left: 4px; | ||
cursor: pointer; | ||
transition: all 1s; | ||
|
||
&--active:not(.hi-carousel__dot--sign) { | ||
background: rgba(255, 255, 255, 1); | ||
width: 48px; | ||
} | ||
|
||
&--sign { | ||
width: 24px; | ||
} | ||
} | ||
|
||
&__arrows { | ||
position: absolute; | ||
width: 100%; | ||
list-style: none; | ||
margin: 0; | ||
padding: 0; | ||
top: 50%; | ||
transform: translateY(-28px); | ||
display: flex; | ||
justify-content: space-between; | ||
visibility: hidden; | ||
|
||
&--show { | ||
visibility: visible; | ||
} | ||
} | ||
|
||
&__arrow { | ||
width: 56px; | ||
height: 56px; | ||
background: rgba(0, 0, 0, 0.25); | ||
border-radius: 50%; | ||
text-align: center; | ||
line-height: 56px; | ||
color: #fff; | ||
font-size: 32px; | ||
margin-left: 24px; | ||
cursor: pointer; | ||
|
||
&:last-child { | ||
margin-right: 24px; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.