Skip to content

Commit

Permalink
feat: 兼容 pickerValueDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
KuangPF committed Jul 7, 2019
1 parent 5251b98 commit 535cbd4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
17 changes: 5 additions & 12 deletions src/mpvue-picker/mpvuePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@
</template>

<script>
import { transformDateToIndex, getDatePickerIndex, fixPickerValueDefault } from '../utils/index.js';
import { transformDateToIndex, getDatePickerIndex, getDays, fixPickerValueDefault } from '../utils/index.js';
/* 由于 getMonth 返回 0-11(1月-12月),因此在设置的时候 month-1 */
const MIN_DATE = new Date(1900, 0, 1); // 最小支持日期 1990-01-01
const MAX_DATE = new Date(2099, 11, 31); // 最大支持日期 20199-12-31
const MAX_DATE = new Date(2099, 11, 31); // 最大支持日期 2099-12-31
/* eslint-disable-next-line */
const NOW_DATE = new Date(); // 当前日期
export default {
Expand Down Expand Up @@ -163,7 +163,7 @@ export default {
for (let i = 0; i < 12; i++) {
monthList.push({ label: i + 1 + '', value: i + 1 });
}
let dayLength = this.getDays(MIN_DATE.getFullYear() + initPickerValue[0], initPickerValue[1] + 1);
let dayLength = getDays(MIN_DATE.getFullYear() + initPickerValue[0], initPickerValue[1] + 1);
for (let i = 0; i < dayLength; i++) {
dayList.push({ label: i + 1 + '', value: i + 1 });
}
Expand Down Expand Up @@ -373,7 +373,7 @@ export default {
} else if (mode === 'timeSelector') {
pickerLable = `${this.pickerValueHour[value[0]].label}-${this.pickerValueMinute[value[1]].label}`;
pickerGetValue.push(this.pickerValueHour[value[0]].value);
pickerGetValue.push(this.pickerValueHour[value[1]].value);
pickerGetValue.push(this.pickerValueMinute[value[1]].value);
} else if (mode === 'multiSelector') {
for (let i = 0; i < value.length; i++) {
if (i > 0) {
Expand Down Expand Up @@ -433,7 +433,7 @@ export default {
return tempPickerValue;
},
getDaysList(year, month, value) {
let dayLength = this.getDays(year, month);
let dayLength = getDays(year, month);
value[2] = dayLength < this.pickerValueDay.length && this.pickerValue[2] > dayLength - 1 ? dayLength - 1 : this.pickerValue[2];
if (dayLength !== this.pickerValueDay.length) {
let dayList = [];
Expand All @@ -443,13 +443,6 @@ export default {
this.pickerValueDay = dayList;
}
this.pickerValue = value;
},
/* 计算一个月多少天 ( month 传正常的月份数,不用 -1) */
getDays(year, month) {
if (month > 12 || month < 0) { return -1; }
month = parseInt(month, 10);
var date = new Date(year, month, 0);
return date.getDate();
}
}
};
Expand Down
52 changes: 48 additions & 4 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,67 @@ const getDatePickerIndex = pickerIndex => {
let tempPickerIndex = [];
// 支持传入 Date 对象
/* eslint-disable-next-line */
if (Object.prototype.toString.call(pickerIndex) === "[object Date]" && !isNaN(pickerIndex.getTime())) {
if (Object.prototype.toString.call(pickerIndex) === '[object Date]' && !isNaN(pickerIndex.getTime())) {
tempPickerIndex = transformDateToIndex(pickerIndex);
} else {
tempPickerIndex = transformDateToIndex(new Date(`${pickerIndex[0]}/${pickerIndex[1]}/${pickerIndex[2]}`));
if (isNaN(new Date(`${pickerIndex[0]}/${pickerIndex[1]}/${pickerIndex[2]}`).getTime())) {
tempPickerIndex = transformDateToIndex(); // 兼容错误日期
} else {
tempPickerIndex = transformDateToIndex(new Date(`${pickerIndex[0]}/${pickerIndex[1]}/${pickerIndex[2]}`));
}
}
if (tempPickerIndex[0] > 199) {
console.warn('最大日期范围支持:1990-01-01 ~ 2099-12-31');
tempPickerIndex[0] = 199;
}
return tempPickerIndex;
};

const getDays = (year, month) => {
if (month > 12 || month < 0) {
return -1;
}
month = parseInt(month, 10);
var date = new Date(year, month, 0);
return date.getDate();
};

/**
* 兼容 pickerValueArray 值
*
* @param {*} pickerValue 索引值
* @param {*} mode 模式值
* @param {*} pickerValueArray picker 数组
*/
const fixPickerValueDefault = (pickerValue, mode, pickerValueArray) => {
let fixPickerValue = pickerValue;
// TODO
if (mode === 'selector') {
// 单列
fixPickerValue[0] = Math.min(fixPickerValue[0], pickerValueArray.length - 1);
} else if (mode === 'multiSelector') {
// 多列
fixPickerValue = fixPickerValue.map((item, index) => {
return Math.min(item, pickerValueArray[index].length - 1);
});
} else if (mode === 'timeSelector') {
// 时间选择器
fixPickerValue = fixPickerValue.map((item, index) => {
return index > 0 ? Math.min(item, 59) : Math.min(item, 23);
});
} else if (mode === 'multiLinkageSelector') {
let tempIndexFirst, tempIndexSecond;
fixPickerValue = fixPickerValue.map((item, index) => {
if (index === 0) {
tempIndexFirst = Math.min(item, pickerValueArray.length - 1);
return tempIndexFirst;
} else if (index === 1) {
tempIndexSecond = Math.min(item, pickerValueArray[tempIndexFirst].children.length - 1);
return tempIndexSecond;
} else if (index === 2) {
return Math.min(item, pickerValueArray[tempIndexFirst].children[tempIndexSecond].children.length - 1);
}
});
}
return fixPickerValue;
};
export { transformDateToIndex, getDatePickerIndex, fixPickerValueDefault };
export { transformDateToIndex, getDatePickerIndex, getDays, fixPickerValueDefault };

0 comments on commit 535cbd4

Please sign in to comment.