Skip to content

Commit a1f1db9

Browse files
[fixed] 修复滚动快速出现的bug
1 parent 95b0919 commit a1f1db9

File tree

5 files changed

+59
-46
lines changed

5 files changed

+59
-46
lines changed

README.md

+35-30
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
[![Travis][build-badge]][build] [![npm package][npm-badge]][npm] [![Coveralls][coveralls-badge]][coveralls]
33

44
一个移动端时间选择器react组件
5+
---------------------------------------
56

67

78
安装
89
------------
910
Using [npm](https://www.npmjs.com/):
10-
`$ npm install react-mobile-datepicker --save`
11+
12+
$ npm install react-mobile-datepicker --save
13+
1114

1215
然后,使用模块加载工具流,支持common.js或ES2015模块,例如[webpack](https://github.com/webpack/webpack)
1316

@@ -26,7 +29,7 @@ require('react-mobile-datepicker/dist/react-mobile-datepicker.css');
2629
### 例子
2730

2831
```js
29-
import React from 'react';
32+
import React, { Component } from 'react';
3033
import { render } from 'react-dom';
3134
import InfiniteCalendar from 'react-mobile-datepicker';
3235
import 'react-mobile-datepicker/dist/react-mobile-datepicker.css'; // only needs to be imported once
@@ -35,42 +38,44 @@ import 'react-mobile-datepicker/dist/react-mobile-datepicker.css'; // only needs
3538
var today = new Date();
3639
var minDate = Number(new Date()) - (24*60*60*1000) * 7; // One week before today
3740

41+
class Wrap extends Component {
42+
state = {
43+
isOpen: true,
44+
}
45+
render() {
46+
return (
47+
<DatePicker
48+
isOpen={this.state.isOpen}
49+
startDate={today}
50+
minDate={minDate}
51+
onCancel={() => { this.state.isOpen = false; }}
52+
onSelect={(time) => { console.log(time); }} />
53+
);
54+
}
55+
}
56+
57+
3858
render(
39-
<DatePicker
40-
isOpen={true}
41-
startDate={today}
42-
minDate={minDate}
43-
onCancel={() => { isOpen = false; }}
44-
onSelect={(time) => { console.log(time); }} />,
59+
<Wrap />,
4560
document.getElementById('root')
4661
);
4762
```
4863

4964

50-
目录
65+
Prop Types
5166
------------
52-
```
53-
.
54-
├── .DS_Store
55-
├── .babelrc
56-
├── .eslintignore
57-
├── .eslintrc
58-
├── .git
59-
├── .gitignore
60-
├── .npmignore
61-
├── .travis.yml
62-
├── CHANGELOG.md
63-
├── README.md
64-
├── dist
65-
├── examples
66-
├── lib
67-
├── node_modules
68-
├── package.json
69-
├── scripts
70-
├── test
71-
└── webpack.config.js
7267

73-
```
68+
| Property | Type | Default | Description |
69+
| ------------- | ------------- | ---------------- |
70+
| btnColor | String | #fff | 完成按钮颜色 |
71+
| dateColor | String | #fff | 日期文字颜色 |
72+
| layerBackground | String | #ffa70b | 背景颜色 |
73+
| isOpen | Boolean | true | 是否显示 |
74+
| startDate | Date | new Date() | 初始日期 |
75+
| minDate | Date | 前一周 | 最小日期 |
76+
| maxDate | Date | new Date() | 最小日期 |
77+
| onSelect | Function | () => {} | 点击完成后的回调函数, Date对象作为参数 |
78+
| onCancel | Function | () => {} | 隐藏时间选择器的回调函数 |
7479

7580
Changelog
7681
-------------

examples/basic/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { nextTime } from '../../lib/time.js';
99
<DatePicker
1010
isOpen={isOpen}
1111
startDate={nextTime(new Date(), -1)}
12+
minDate={nextTime(new Date(), -3)}
1213
onCancel={() => { isOpen = false; }}
1314
onSelect={(time) => { console.log(time); }} />
1415
);

lib/DatePicker.js

+22-14
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,19 @@ class DatePicker extends Component {
5454
_moveToNext(direction) {
5555
const scroll = this.refs.scroll;
5656
const angle = this.angle;
57-
if (direction === 1) {
58-
this._moveTo(scroll, angle + 22.5);
57+
const { maxDate, minDate } = this.props;
58+
59+
const date = (direction === 1) ?
60+
this.state.dates.find(value =>
61+
value.value.getTime() > nextTime(maxDate, 0).getTime() &&
62+
angle + direction * 22.5 + value.angle === 0) :
63+
this.state.dates.find(value =>
64+
value.value.getTime() < nextTime(minDate, 0).getTime() &&
65+
angle + direction * 22.5 + value.angle === 0);
66+
if (date) {
67+
this._moveTo(scroll, angle);
5968
} else {
60-
this._moveTo(scroll, angle - 22.5);
69+
this._moveTo(scroll, angle + direction * 22.5);
6170
}
6271
}
6372

@@ -134,7 +143,7 @@ class DatePicker extends Component {
134143
}
135144

136145
render() {
137-
const { layerBackground } = this.props;
146+
const { layerBackground, btnColor } = this.props;
138147
const scrollStyle = {
139148
[TRANSFORM]: `rotateX(${this.state.angle}deg)`,
140149
};
@@ -150,6 +159,7 @@ class DatePicker extends Component {
150159
style={datePickerStyle}>
151160
<p className="datepicker-navbar">
152161
<span
162+
style={{ color: btnColor }}
153163
className="datepicker-finish-btn"
154164
onClick={this.handleFinishBtnClick}>完成</span>
155165
</p>
@@ -180,24 +190,22 @@ DatePicker.propTypes = {
180190
dateColor: PropTypes.string,
181191
layerBackground: PropTypes.string,
182192
isOpen: PropTypes.bool,
183-
startDate: PropTypes.oneOfType([
184-
PropTypes.object,
185-
PropTypes.number,
186-
]),
187-
minDate: PropTypes.oneOfType([
188-
PropTypes.object,
189-
PropTypes.number,
190-
]),
193+
startDate: PropTypes.object,
194+
minDate: PropTypes.object,
195+
maxDate: PropTypes.object,
191196
onSelect: PropTypes.func,
192197
onCancel: PropTypes.func,
193198
};
194199

195200
DatePicker.defaultProps = {
196201
touchLen: 40,
197202
dateColor: '#fff',
203+
btnColor: '#fff',
204+
isOpen: true,
198205
layerBackground: '#ffa70b',
199-
startDate: new Date(),
200-
minDate: new Date(2016, 3, 7),
206+
startDate: nextTime(new Date(), 0),
207+
minDate: nextTime(new Date(), -30),
208+
maxDate: nextTime(new Date(), 0),
201209
onSelect: () => {},
202210
onCancel: () => {},
203211
};

lib/index.css

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
.datepicker-finish-btn {
1717
font: 12px/18px 微软雅黑;
18-
color: #fff;
1918
line-height: 31px;
2019
font-size: 14px;
2120
float: right;

lib/time.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function convertDate(timestamp, formate) {
1717
}
1818

1919

20-
export function nextTime(now, index = 1) {
20+
export function nextTime(now = new Date(), index = 1) {
2121
if (Object.prototype.toString.call(now, null) !== '[object Date]') {
2222
throw new Error('参数类型不对');
2323
}

0 commit comments

Comments
 (0)