[bugfix] DatetimePicker: incorrect value when use minMinute (#1724)

This commit is contained in:
neverland 2018-08-30 17:34:01 +08:00 committed by GitHub
parent dee56c75b7
commit 1471f6d5b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 14 deletions

View File

@ -17,6 +17,7 @@
<script> <script>
import Picker from '../picker'; import Picker from '../picker';
import create from '../utils/create'; import create from '../utils/create';
import { range } from '../utils';
const currentYear = new Date().getFullYear(); const currentYear = new Date().getFullYear();
const isValidDate = date => Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime()); const isValidDate = date => Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime());
@ -178,11 +179,11 @@ export default create({
// time type // time type
if (!isDateType) { if (!isDateType) {
const [hour, minute] = value.split(':'); let [hour, minute] = value.split(':');
let correctedHour = Math.max(hour, this.minHour); hour = this.pad(range(hour, this.minHour, this.maxHour));
correctedHour = this.pad(Math.min(correctedHour, this.maxHour)); minute = this.pad(range(minute, this.minMinute, this.maxMinute));
return `${correctedHour}:${minute}`; return `${hour}:${minute}`;
} }
// date type // date type

View File

@ -27,10 +27,9 @@
<script> <script>
import create from '../utils/create'; import create from '../utils/create';
import deepClone from '../utils/deep-clone'; import deepClone from '../utils/deep-clone';
import { isObj } from '../utils'; import { isObj, range } from '../utils';
const DEFAULT_DURATION = 200; const DEFAULT_DURATION = 200;
const range = (num, arr) => Math.min(Math.max(num, arr[0]), arr[1]);
export default create({ export default create({
name: 'picker-column', name: 'picker-column',
@ -67,7 +66,8 @@ export default create({
}, },
destroyed() { destroyed() {
this.$parent.children && this.$parent.children.splice(this.$parent.children.indexOf(this), 1); const { children } = this.$parent;
children && children.splice(children.indexOf(this), 1);
}, },
watch: { watch: {
@ -87,7 +87,7 @@ export default create({
columnStyle() { columnStyle() {
return { return {
height: (this.itemHeight * this.visibleItemCount) + 'px' height: this.itemHeight * this.visibleItemCount + 'px'
}; };
}, },
@ -115,25 +115,27 @@ export default create({
onTouchMove(event) { onTouchMove(event) {
const deltaY = event.touches[0].clientY - this.startY; const deltaY = event.touches[0].clientY - this.startY;
this.offset = range(this.startOffset + deltaY, [ this.offset = range(
this.startOffset + deltaY,
-(this.count * this.itemHeight), -(this.count * this.itemHeight),
this.itemHeight this.itemHeight
]); );
}, },
onTouchEnd() { onTouchEnd() {
if (this.offset !== this.startOffset) { if (this.offset !== this.startOffset) {
this.duration = DEFAULT_DURATION; this.duration = DEFAULT_DURATION;
const index = range(Math.round(-this.offset / this.itemHeight), [ const index = range(
Math.round(-this.offset / this.itemHeight),
0, 0,
this.count - 1 this.count - 1
]); );
this.setIndex(index, true); this.setIndex(index, true);
} }
}, },
adjustIndex(index) { adjustIndex(index) {
index = range(index, [0, this.count]); index = range(index, 0, this.count);
for (let i = index; i < this.count; i++) { for (let i = index; i < this.count; i++) {
if (!this.isDisabled(this.options[i])) return i; if (!this.isDisabled(this.options[i])) return i;
} }
@ -147,7 +149,9 @@ export default create({
}, },
getOptionText(option) { getOptionText(option) {
return isObj(option) && this.valueKey in option ? option[this.valueKey] : option; return isObj(option) && this.valueKey in option
? option[this.valueKey]
: option;
}, },
setIndex(index, userAction) { setIndex(index, userAction) {

View File

@ -32,8 +32,13 @@ function isAndroid() {
return isServer ? false : /android/.test(navigator.userAgent.toLowerCase()); return isServer ? false : /android/.test(navigator.userAgent.toLowerCase());
} }
function range(num, min, max) {
return Math.min(Math.max(num, min), max);
};
export { export {
get, get,
range,
isObj, isObj,
isDef, isDef,
isServer, isServer,