-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
208 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
|
||
import {Component} from 'react'; | ||
import * as React from 'react'; | ||
import * as classNames from 'classnames'; | ||
import {IBaseComponent} from '../template/component'; | ||
import Icon from '../Icon'; | ||
import { RenderInRootDom } from '../utils/renderInRootDom'; | ||
import { backTop } from './assist'; | ||
|
||
export interface IBackTopProps extends IBaseComponent { | ||
/** | ||
* 距离底部距离 | ||
*/ | ||
bottom?: number; | ||
/** | ||
* 距离右侧距离 | ||
*/ | ||
right?: number; | ||
/** | ||
* 触发backtop的高度 | ||
*/ | ||
height?: number; | ||
/** | ||
* 动画时间 | ||
*/ | ||
duration?: number; | ||
/** | ||
* 回调函数 | ||
*/ | ||
onBackTop?: () => void; | ||
} | ||
|
||
export interface IBackTopState { | ||
/** | ||
* 是否显示backtop | ||
*/ | ||
show: boolean; | ||
} | ||
|
||
/** | ||
* **返回顶部**-返回页面顶部的操作按钮。 | ||
*/ | ||
class BackTop extends Component<IBackTopProps, IBackTopState> { | ||
state = { | ||
show: false, | ||
}; | ||
|
||
static defaultProps = { | ||
height: 200, | ||
bottom: 50, | ||
right: 50, | ||
duration: 500, | ||
}; | ||
|
||
scrollListener = () => { | ||
const height = this.props.height ? this.props.height : 200; | ||
const top = document.documentElement.scrollTop || document.body.scrollTop; | ||
const show = top >= height; | ||
this.setState({ | ||
show, | ||
}); | ||
} | ||
|
||
componentDidMount() { | ||
window.addEventListener('scroll', this.scrollListener, false); | ||
window.addEventListener('resize', this.scrollListener, false); | ||
} | ||
|
||
componentWillUnMount() { | ||
window.removeEventListener('scroll', this.scrollListener, false); | ||
window.removeEventListener('resize', this.scrollListener, false); | ||
} | ||
render() { | ||
const { | ||
className, style, children, | ||
bottom, right, duration, | ||
onBackTop, | ||
...otherProps} = this.props; | ||
const preCls = 'yoshino-backtop'; | ||
const clsName = classNames( | ||
preCls, className, | ||
); | ||
const backtopStyle = { | ||
right: `${right}px`, | ||
bottom: `${bottom}px`, | ||
}; | ||
return ( | ||
<RenderInRootDom boxClass='backtop'> | ||
<div | ||
className={clsName} | ||
style={{ | ||
...style, | ||
...backtopStyle, | ||
}} | ||
{...otherProps} | ||
onClick={backTop.bind(this, duration, onBackTop)} | ||
> | ||
{ | ||
this.state.show ? children ? children : ( | ||
<div className={`${preCls}-default`}> | ||
<Icon type='chevron-up'/> | ||
</div> | ||
) : null | ||
} | ||
</div> | ||
</RenderInRootDom> | ||
); | ||
} | ||
} | ||
|
||
export default BackTop; |
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,9 @@ | ||
### 默认 | ||
```js | ||
<BackTop/> | ||
``` | ||
|
||
### 自定义 | ||
```js | ||
<BackTop bottom="100" right="20" height="100" onBackTop={() => alert('到顶了!')}>回到顶部</BackTop> | ||
``` |
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,12 @@ | ||
import * as React from 'react'; | ||
import {Simulate, renderIntoDocument, scryRenderedComponentsWithType} from 'react-dom/test-utils'; | ||
import {findDOMNode} from 'react-dom'; | ||
import Backtop from '../index'; | ||
|
||
describe('多选', () => { | ||
test('点击其中一个选项后可通过 onChange 拿到最新的值', () => { | ||
const component = renderIntoDocument( | ||
<Backtop/>, | ||
) as Backtop; | ||
}); | ||
}); |
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,14 @@ | ||
import * as renderer from 'react-test-renderer'; | ||
import * as React from 'react'; | ||
import Backtop from '../index'; | ||
|
||
describe('Props', () => { | ||
|
||
test('默认', () => { | ||
const component = renderer.create( | ||
<Backtop/>, | ||
); | ||
const tree = component.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
}); |
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,20 @@ | ||
export const backTop = (durations: number, callback: () => void | undefined) => { | ||
let dom: HTMLElement; | ||
if (document.documentElement.scrollTop) { | ||
dom = document.documentElement; | ||
} else { | ||
dom = document.body; | ||
} | ||
const scrollTop = dom.scrollTop; | ||
// tslint:disable | ||
for (let i = 60; i >= 0; i--) { | ||
setTimeout(((i) => { | ||
return () => { | ||
dom.scrollTop = scrollTop * i / 60; | ||
if (i === 0 && typeof callback === 'function') { | ||
callback(); | ||
} | ||
}; | ||
})(i), durations * (1 - i / 60)); | ||
} | ||
}; |
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,5 @@ | ||
import Backtop from './Backtop'; | ||
import { IBackTopProps } from './Backtop'; | ||
|
||
export {IBackTopProps}; | ||
export default Backtop; |
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,28 @@ | ||
@import './var.less'; | ||
|
||
@backtop-prefix-cls: ~"@{css-prefix}-backtop"; | ||
.@{backtop-prefix-cls} { | ||
position: fixed; | ||
z-index: 100; | ||
cursor: pointer; | ||
display: block; | ||
border-radius: 4px; | ||
|
||
&-default { | ||
background-color: @primary-color; | ||
border-radius: @border-radius-base; | ||
box-shadow: 0 1px 3px rgba(0,0,0,.2); | ||
transition: all .2s ease-in-out; | ||
display: block; | ||
|
||
:hover { | ||
background-color: @primary-color-hover; | ||
} | ||
} | ||
|
||
i { | ||
color: #fff; | ||
font-size: 24px; | ||
padding: 8px 12px; | ||
} | ||
} |
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,2 @@ | ||
@import '../../styles/var.less'; | ||
@import '../../styles/common/reset.less'; |
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import * as React from 'react'; | ||
import { Backtop } from '../../../../components/'; | ||
import { BackTop } from '../../../../components/'; | ||
const img = require('./fire.png'); | ||
export default function () { | ||
return ( | ||
<Backtop bottom={50} right={100} height={1} onBackTop={() => alert('到顶了!')}> | ||
<BackTop bottom={50} right={100} height={1} onBackTop={() => alert('到顶了!')}> | ||
<img style={{width: 100, height: 100}} src={img}/> | ||
</Backtop> | ||
</BackTop> | ||
) | ||
} |
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import * as React from 'react'; | ||
import { Backtop } from '../../../../components/'; | ||
import { BackTop } from '../../../../components/'; | ||
|
||
export default function () { | ||
return ( | ||
<div> | ||
<Backtop/> | ||
<BackTop/> | ||
</div> | ||
) | ||
} |
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