提交 39cdfebb 编写于 作者: richard_1015's avatar richard_1015

Merge branch 'next' of https://github.com/jdf2e/nutui into next

...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<h2>基础用法</h2> <h2>基础用法</h2>
<div> <div>
<nut-cell <nut-cell
:showIcon="true" :show-icon="true"
title="选择单个日期" title="选择单个日期"
:desc="date ? `${date} ${dateWeek}` : '请选择'" :desc="date ? `${date} ${dateWeek}` : '请选择'"
@click="openSwitch('isVisible')" @click="openSwitch('isVisible')"
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
<div> <div>
<nut-cell <nut-cell
:showIcon="true" :show-icon="true"
title="选择日期区间" title="选择日期区间"
:desc="date1 ? `${date1[0]}至${date1[1]}` : '请选择'" :desc="date1 ? `${date1[0]}至${date1[1]}` : '请选择'"
@click="openSwitch('isVisible1')" @click="openSwitch('isVisible1')"
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
<h2>自定义日历-自动回填</h2> <h2>自定义日历-自动回填</h2>
<div> <div>
<nut-cell <nut-cell
:showIcon="true" :show-icon="true"
title="选择日期" title="选择日期"
:desc="date3 ? date3 : '请选择'" :desc="date3 ? date3 : '请选择'"
@click="openSwitch('isVisible3')" @click="openSwitch('isVisible3')"
...@@ -107,29 +107,29 @@ export default createDemo({ ...@@ -107,29 +107,29 @@ export default createDemo({
isVisible3: false, isVisible3: false,
date3: '' date3: ''
}); });
const openSwitch = param => { const openSwitch = (param: string) => {
state[`${param}`] = true; state[`${param}`] = true;
}; };
const closeSwitch = param => { const closeSwitch = (param: string) => {
state[`${param}`] = false; state[`${param}`] = false;
}; };
const setChooseValue = param => { const setChooseValue = (param: string) => {
state.date = param[3]; state.date = param[3];
state.dateWeek = param[4]; state.dateWeek = param[4];
}; };
const setChooseValue1 = param => { const setChooseValue1 = (param: string) => {
state.date1 = [...[param[0][3], param[1][3]]]; state.date1 = [...[param[0][3], param[1][3]]];
}; };
const setChooseValue2 = param => { const setChooseValue2 = (param: string) => {
state.date2 = param[3]; state.date2 = param[3];
console.log(state.date2); console.log(state.date2);
}; };
const setChooseValue3 = param => { const setChooseValue3 = (param: string) => {
state.date3 = param[3]; state.date3 = param[3];
}; };
......
...@@ -201,12 +201,13 @@ setup() { ...@@ -201,12 +201,13 @@ setup() {
| poppable | 是否弹窗状态展示 | Boolean | true | | poppable | 是否弹窗状态展示 | Boolean | true |
| is-auto-back-fill | 自动回填 | Boolean | false | | is-auto-back-fill | 自动回填 | Boolean | false |
| title | 显示标题 | String | ‘日期选择’ | | title | 显示标题 | String | ‘日期选择’ |
| default-value | 默认值,日期选择 String 格式,区间选择 Array 格式 | String / Array | null | | default-value | 默认值,日期选择 String 格式,区间选择 Array 格式 | String Array | null |
| start-date | 开始日期, 如果不限制开始日期传 null | String | 今天 | | start-date | 开始日期, 如果不限制开始日期传 null | String | 今天 |
| end-date | 结束日期,如果不限制结束日期传 null | String | 距离今天 365 天 | | end-date | 结束日期,如果不限制结束日期传 null | String | 距离今天 365 天 |
### Events ### Events
| 事件名 | 说明 | 回调参数 | | 事件名 | 说明 | 回调参数 |
| ----------------- | --------------------- | -------------- |
| choose | 选择之后或是点击确认按钮触发 | 日期数组(包含年月日和星期) | choose | 选择之后或是点击确认按钮触发 | 日期数组(包含年月日和星期)
| close | 关闭时触发 | - | close | 关闭时触发 | -
<template> <template>
<nut-popup <nut-popup
v-if="poppable" v-if="poppable"
v-model:show="state.childIsVisible" v-model:show="childIsVisible"
position="bottom" position="bottom"
round round
:closeable="true" :closeable="true"
...@@ -99,7 +99,7 @@ export default create({ ...@@ -99,7 +99,7 @@ export default create({
emit('close'); emit('close');
}; };
const choose = param => { const choose = (param: string) => {
close(); close();
emit('choose', param); emit('choose', param);
}; };
...@@ -123,7 +123,7 @@ export default create({ ...@@ -123,7 +123,7 @@ export default create({
close, close,
choose, choose,
calendarRef, calendarRef,
state, ...toRefs(state),
...toRefs(props) ...toRefs(props)
}; };
} }
......
...@@ -108,6 +108,16 @@ interface CalendarState { ...@@ -108,6 +108,16 @@ interface CalendarState {
isRange: boolean; isRange: boolean;
timer: number; timer: number;
} }
interface Day {
day: string | number;
type: string;
}
interface MonthInfo {
curData: string[] | string;
title: string;
monthData: Day[];
}
export default create({ export default create({
props: { props: {
...@@ -176,35 +186,35 @@ export default create({ ...@@ -176,35 +186,35 @@ export default create({
}); });
// 日期转化成数组 // 日期转化成数组
const splitDate = date => { const splitDate = (date: string) => {
return date.split('-'); return date.split('-');
}; };
const isStart = currDate => { const isStart = (currDate: string) => {
return Utils.isEqual(state.currDate[0], currDate); return Utils.isEqual(state.currDate[0], currDate);
}; };
const isEnd = currDate => { const isEnd = (currDate: string) => {
return Utils.isEqual(state.currDate[1], currDate); return Utils.isEqual(state.currDate[1], currDate);
}; };
// 获取当前数据 // 获取当前数据
const getCurrDate = (day, month, isRange?) => { const getCurrDate = (day: Day, month: MonthInfo, isRange?: boolean) => {
return isRange return isRange
? month.curData[3] + ? month.curData[3] +
'-' + '-' +
month.curData[4] + month.curData[4] +
'-' + '-' +
Utils.getNumTwoBit(day.day) Utils.getNumTwoBit(+day.day)
: month.curData[0] + : month.curData[0] +
'-' + '-' +
month.curData[1] + month.curData[1] +
'-' + '-' +
Utils.getNumTwoBit(day.day); Utils.getNumTwoBit(+day.day);
}; };
// 获取样式 // 获取样式
const getClass = (day, month, isRange?) => { const getClass = (day: Day, month: MonthInfo, isRange?: boolean) => {
const currDate = getCurrDate(day, month, isRange); const currDate = getCurrDate(day, month, isRange);
if (day.type == 'curr') { if (day.type == 'curr') {
if ( if (
...@@ -244,14 +254,19 @@ export default create({ ...@@ -244,14 +254,19 @@ export default create({
}; };
// 选中数据 // 选中数据
const chooseDay = (day, month, isFirst, isRange?) => { const chooseDay = (
day: Day,
month: MonthInfo,
isFirst: boolean,
isRange?: boolean
) => {
if (getClass(day, month, isRange) != `${state.dayPrefix}-disabled`) { if (getClass(day, month, isRange) != `${state.dayPrefix}-disabled`) {
let days = [...month.curData]; let days = [...month.curData];
days = isRange ? days.splice(3) : days.splice(0, 3); days = isRange ? days.splice(3) : days.splice(0, 3);
days[2] = days[2] =
typeof day.day == 'number' ? Utils.getNumTwoBit(day.day) : day.day; typeof day.day == 'number' ? Utils.getNumTwoBit(day.day) : day.day;
days[3] = `${days[0]}-${days[1]}-${days[2]}`; days[3] = `${days[0]}-${days[1]}-${days[2]}`;
days[4] = Utils.getWhatDay(days[0], days[1], days[2]); days[4] = Utils.getWhatDay(+days[0], +days[1], +days[2]);
if (!state.isRange) { if (!state.isRange) {
state.currDate = days[3]; state.currDate = days[3];
state.chooseData = [...days]; state.chooseData = [...days];
...@@ -283,7 +298,7 @@ export default create({ ...@@ -283,7 +298,7 @@ export default create({
}; };
// 获取当前月数据 // 获取当前月数据
const getCurrData = type => { const getCurrData = (type: string) => {
const monthData = const monthData =
type == 'prev' type == 'prev'
? state.monthsData[0] ? state.monthsData[0]
...@@ -304,7 +319,7 @@ export default create({ ...@@ -304,7 +319,7 @@ export default create({
}; };
// 获取日期状态 // 获取日期状态
const getDaysStatus = (days, type) => { const getDaysStatus = (days: number, type: string) => {
// 修复:当某个月的1号是周日时,月份下方会空出来一行 // 修复:当某个月的1号是周日时,月份下方会空出来一行
if (type == 'prev' && days >= 7) { if (type == 'prev' && days >= 7) {
days -= 7; days -= 7;
...@@ -318,19 +333,19 @@ export default create({ ...@@ -318,19 +333,19 @@ export default create({
}; };
// 获取月数据 // 获取月数据
const getMonth = (curData, type) => { const getMonth = (curData: string[], type: string) => {
const preMonthDays = Utils.getMonthPreDay(curData[0], curData[1]); const preMonthDays = Utils.getMonthPreDay(+curData[0], +curData[1]);
const currMonthDays = Utils.getMonthDays(curData[0], curData[1]); const currMonthDays = Utils.getMonthDays(curData[0], curData[1]);
const title = { const title = {
year: curData[0], year: curData[0],
month: curData[1] month: curData[1]
}; };
const monthInfo = { const monthInfo: MonthInfo = {
curData: curData, curData: curData,
title: `${title.year}${title.month}月`, title: `${title.year}${title.month}月`,
monthData: [ monthData: [
...getDaysStatus(preMonthDays, 'prev'), ...(getDaysStatus(preMonthDays, 'prev') as Day[]),
...getDaysStatus(currMonthDays, 'curr') ...(getDaysStatus(currMonthDays, 'curr') as Day[])
] ]
}; };
if (type == 'next') { if (type == 'next') {
...@@ -364,8 +379,8 @@ export default create({ ...@@ -364,8 +379,8 @@ export default create({
// 初始化数据 // 初始化数据
const initData = () => { const initData = () => {
// 初始化开始结束数据 // 初始化开始结束数据
state.startData = props.startDate ? splitDate(props.startDate) : null; state.startData = props.startDate ? splitDate(props.startDate) : '';
state.endData = props.endDate ? splitDate(props.endDate) : null; state.endData = props.endDate ? splitDate(props.endDate) : '';
// 初始化当前日期 // 初始化当前日期
if (!props.defaultValue) { if (!props.defaultValue) {
...@@ -409,7 +424,7 @@ export default create({ ...@@ -409,7 +424,7 @@ export default create({
state.currDate = props.endDate; state.currDate = props.endDate;
} }
state.defaultData = [...splitDate(state.currDate)]; state.defaultData = [...splitDate(state.currDate as string)];
} }
getMonth(state.defaultData, 'next'); getMonth(state.defaultData, 'next');
...@@ -442,7 +457,7 @@ export default create({ ...@@ -442,7 +457,7 @@ export default create({
}; };
// 区间选择&&当前月&&选中态 // 区间选择&&当前月&&选中态
const isActive = (day, month) => { const isActive = (day: Day, month: MonthInfo) => {
return ( return (
state.isRange && state.isRange &&
day.type == 'curr' && day.type == 'curr' &&
...@@ -451,7 +466,7 @@ export default create({ ...@@ -451,7 +466,7 @@ export default create({
}; };
// 是否有开始提示 // 是否有开始提示
const isStartTip = (day, month) => { const isStartTip = (day: Day, month: MonthInfo) => {
if (isActive(day, month)) { if (isActive(day, month)) {
return isStart(getCurrDate(day, month)); return isStart(getCurrDate(day, month));
} else { } else {
...@@ -460,12 +475,12 @@ export default create({ ...@@ -460,12 +475,12 @@ export default create({
}; };
// 是否有结束提示 // 是否有结束提示
const isEndTip = (day, month) => { const isEndTip = (day: Day, month: MonthInfo) => {
return isActive(day, month); return isActive(day, month);
}; };
// 是否有是当前日期 // 是否有是当前日期
const isCurrDay = (month, day) => { const isCurrDay = (month: any, day: string) => {
const date = `${month.curData[0]}-${month.curData[1]}-${day}`; const date = `${month.curData[0]}-${month.curData[1]}-${day}`;
return Utils.isEqual(date, Utils.date2Str(new Date())); return Utils.isEqual(date, Utils.date2Str(new Date()));
}; };
...@@ -496,7 +511,7 @@ export default create({ ...@@ -496,7 +511,7 @@ export default create({
}; };
// 设置月份滚动距离和速度 // 设置月份滚动距离和速度
const setTransform = (translateY = 0, type?, time = 1000) => { const setTransform = (translateY = 0, type?: string, time = 1000) => {
if (monthsPanel?.value) { if (monthsPanel?.value) {
if (type === 'end') { if (type === 'end') {
monthsPanel.value.style.webkitTransition = `transform ${time}ms cubic-bezier(0.19, 1, 0.22, 1)`; monthsPanel.value.style.webkitTransition = `transform ${time}ms cubic-bezier(0.19, 1, 0.22, 1)`;
...@@ -514,7 +529,7 @@ export default create({ ...@@ -514,7 +529,7 @@ export default create({
}; };
// 计算滚动方向和距离 // 计算滚动方向和距离
const setMove = (move, type?, time?) => { const setMove = (move: number, type?: string, time?: number) => {
let updateMove = move + state.transformY; let updateMove = move + state.transformY;
const h = months.value?.offsetHeight || 0; const h = months.value?.offsetHeight || 0;
const offsetHeight = monthsPanel.value?.offsetHeight || 0; const offsetHeight = monthsPanel.value?.offsetHeight || 0;
...@@ -552,19 +567,19 @@ export default create({ ...@@ -552,19 +567,19 @@ export default create({
}; };
// 监听touch开始 // 监听touch开始
const touchStart = event => { const touchStart = (event: TouchEvent) => {
const changedTouches = event.changedTouches[0]; const changedTouches = event.changedTouches[0];
state.touchParams.startY = changedTouches.pageY; state.touchParams.startY = changedTouches.pageY;
state.touchParams.startTime = event.timestamp || Date.now(); state.touchParams.startTime = event.timeStamp || Date.now();
state.transformY = state.scrollDistance; state.transformY = state.scrollDistance;
}; };
// 监听touchmove // 监听touchmove
const touchMove = event => { const touchMove = (event: TouchEvent) => {
//event.preventDefault(); //event.preventDefault();
const changedTouches = event.changedTouches[0]; const changedTouches = event.changedTouches[0];
state.touchParams.lastY = changedTouches.pageY; state.touchParams.lastY = changedTouches.pageY;
state.touchParams.lastTime = event.timestamp || Date.now(); state.touchParams.lastTime = event.timeStamp || Date.now();
const move = state.touchParams.lastY - state.touchParams.startY; const move = state.touchParams.lastY - state.touchParams.startY;
if (Math.abs(move) < 5) { if (Math.abs(move) < 5) {
return false; return false;
...@@ -573,10 +588,10 @@ export default create({ ...@@ -573,10 +588,10 @@ export default create({
}; };
// 监听touchend // 监听touchend
const touchEnd = event => { const touchEnd = (event: TouchEvent) => {
const changedTouches = event.changedTouches[0]; const changedTouches = event.changedTouches[0];
state.touchParams.lastY = changedTouches.pageY; state.touchParams.lastY = changedTouches.pageY;
state.touchParams.lastTime = event.timestamp || Date.now(); state.touchParams.lastTime = event.timeStamp || Date.now();
let move = state.touchParams.lastY - state.touchParams.startY; let move = state.touchParams.lastY - state.touchParams.startY;
if (Math.abs(move) < 5) { if (Math.abs(move) < 5) {
return false; return false;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册