vuux
Version:
Vue3 Nuxt3 Nuxt4 组件库
2 lines (1 loc) • 3.45 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("../day/index.cjs"),c=require("vue"),y=require("../date.cjs"),K=(M,d,Y)=>{const o=c.ref(c.toRaw(d.value)),{startKey:h,endKey:f}=M,m=new Date,w=m.getFullYear(),$=m.getMonth()+1,S=m.getDate(),P=s(`${w}-${$}-${S}`),e=c.reactive({year1:0,year2:0,month1:0,month2:0}),v=c.reactive({leftPanel:y.getMonthDate(1),rightPanel:y.getMonthDate(2)}),r=t=>{t==="left"?v.leftPanel=y.getMonthDate(e.year1,e.month1):v.rightPanel=y.getMonthDate(e.year2,e.month2)};r("left"),r("right");const b=()=>{e.month1>1?e.month1-=1:(e.year1-=1,e.month1=12),r("left")},_=()=>{e.month1<12?e.month1+=1:(e.year1+=1,e.month1=1),e.year1>e.year2&&(e.year2=e.year1),e.year1===e.year2&&e.month1>=e.month2&&(e.month1===12?(e.month2=1,e.year2+=1):e.month2=e.month1+1),r("left"),r("right")},k=()=>{e.month2>1?e.month2-=1:(e.year2-=1,e.month2=12),e.year2<e.year1&&(e.year1=e.year2),e.year1===e.year2&&e.month2<=e.month1&&(e.month2===1?(e.month1=12,e.year1-=1):e.month1=e.month2-1),r("left"),r("right")},x=()=>{e.month2<12?e.month2+=1:(e.year2+=1,e.month2=1),r("right")},B=()=>{e.year1-=1,r("left")},A=()=>{e.year1+=1,e.year1>e.year2&&(e.year2=e.year1),e.year1===e.year2&&e.month1>=e.month2&&(e.month1===12?(e.month2=1,e.year2+=1):e.month2=e.month1+1),r("left"),r("right")},N=()=>{e.year2-=1,e.year2<e.year1&&(e.year1=e.year2),e.year1===e.year2&&e.month2<=e.month1&&(e.month2===1?(e.month1=12,e.year1-=1):e.month1=e.month2-1),r("left"),r("right")},R=()=>{e.year2+=1,r("right")};let D=0,u="";const p=t=>{if(t.date!=t.showDate||D%2===0)return;const a=`${t.year}-${t.month}-${t.showDate}`;o.value={[h]:u,[f]:a}},q=(t,a)=>{if(a=="left"&&t.month!==e.month1||a=="right"&&t.month!==e.month2||(D+=1,!o.value)||t.date!=t.showDate)return;const n=`${t.year}-${t.month}-${t.showDate}`,l=s(n);D%2===0?(l.isBefore(s(o.value[h]))?(o.value[h]=s(n).format("YYYY-MM-DD"),o.value[f]=s(u).format("YYYY-MM-DD")):(o.value[h]=s(u).format("YYYY-MM-DD"),o.value[f]=s(n).format("YYYY-MM-DD")),d.value={...o.value},Y("change",o.value)):(o.value[h]=n,o.value[f]="",u=n)},F=c.computed(()=>(t,a)=>{if(!a.year||!a.month||!a.showDate)return;const n=s(`${a.year}-${a.month}-${a.showDate}`),l=s(o.value[h]),i=s(o.value[f]);switch(t){case"today":if(P.isSame(n)&&a.date===a.showDate)return!0;break;case"rangeMonth":if(a.date===a.showDate)return"current-month";break;case"notRangeMonth":if(a.date!==a.showDate)return"";break;case"selected":if((l.isSame(n)||i.isSame(n))&&a.date===a.showDate)return"is-selected";break;case"rangeSelected":if(a.date!==a.showDate)return;if(l.isSame(n))return l.isAfter(i)?"is-selecteds-end":"is-selecteds-start";if(i.isSame(n))return l.isAfter(i)?"is-selecteds-start":"is-selecteds-end";if(l.isBefore(i)){if(l.isBefore(n)&&i.isAfter(n)&&a.date===a.showDate)return"is-selecteds"}else if(i.isBefore(n)&&l.isAfter(n))return"is-selecteds";break;default:return""}}),g=()=>{let t={...d.value};t||(t={[h]:void 0,[f]:void 0}),o.value=t,e.year1=s(t[h]).year(),e.month1=s(t[h]).month()+1,e.year2=s(t[f]).year(),e.month2=s(t[f]).month()+1,e.year1===e.year2&&e.month1===e.month2&&(e.month2+=1),e.month2>12&&(e.year2+=1,e.month2=1),r("left"),r("right")};return c.onMounted(()=>{g()}),{state:v,dateState:e,computeClass:F,init:g,setValue:q,hoverValueFn:p,handlePrveYear1:B,handlePrveMonth1:b,handleNextMonth1:_,handleNextYear1:A,handlePrveYear2:N,handlePrveMonth2:k,handleNextMonth2:x,handleNextYear2:R}};exports.useRange=K;