index-6d1e9935.js 7.4 KB

1
  1. import{j as V,ay as ge,A as E,am as G,d as $,t as K,ai as I,q as _,i as y,aj as Te,ae as oe,ap as le,k as xe,ak as ce,g as d,al as ie,as as X,ac as se,p as J,ao as Y,v as Oe,n as pe,E as C,aN as ke,w as j,aa as L,a7 as Ce,m as ye,y as Z,aO as Ee,D as Pe}from"./index-1d4a9afa.js";import{T as Se,a as we}from"./index-22c9ea74.js";const[Ne,O,ee]=V("picker"),ue=e=>e.find(o=>!o.disabled)||e[0];function Be(e,o){const n=e[0];if(n){if(Array.isArray(n))return"multiple";if(o.children in n)return"cascade"}return"default"}function Q(e,o){o=G(o,0,e.length);for(let n=o;n<e.length;n++)if(!e[n].disabled)return n;for(let n=o-1;n>=0;n--)if(!e[n].disabled)return n;return 0}const te=(e,o,n)=>o!==void 0&&!!e.find(i=>i[n.value]===o);function W(e,o,n){const i=e.findIndex(h=>h[n.value]===o),l=Q(e,i);return e[l]}function De(e,o,n){const i=[];let l={[o.children]:e},h=0;for(;l&&l[o.children];){const f=l[o.children],b=n.value[h];if(l=ge(b)?W(f,b,o):void 0,!l&&f.length){const u=ue(f)[o.value];l=W(f,u,o)}h++,i.push(f)}return i}function He(e){const{transform:o}=window.getComputedStyle(e),n=o.slice(7,o.length-1).split(", ")[5];return Number(n)}function Me(e){return E({text:"text",value:"value",children:"children"},e)}const ne=200,ae=300,Ie=15,[re,q]=V("picker-column"),de=Symbol(re);var _e=$({name:re,props:{value:K,fields:I(Object),options:_(),readonly:Boolean,allowHtml:Boolean,optionHeight:I(Number),swipeDuration:I(K),visibleOptionNum:I(K)},emits:["change","clickOption"],setup(e,{emit:o,slots:n}){let i,l,h,f,b;const u=y(),g=y(),m=y(0),v=y(0),T=Te(),p=()=>e.options.length,S=()=>e.optionHeight*(+e.visibleOptionNum-1)/2,k=c=>{const r=Q(e.options,c),t=-r*e.optionHeight,a=()=>{const s=e.options[r][e.fields.value];s!==e.value&&o("change",s)};i&&t!==m.value?b=a:a(),m.value=t},x=()=>e.readonly||!e.options.length,A=c=>{i||x()||(b=null,v.value=ne,k(c),o("clickOption",e.options[c]))},w=c=>G(Math.round(-c/e.optionHeight),0,p()-1),N=(c,r)=>{const t=Math.abs(c/r);c=m.value+t/.003*(c<0?-1:1);const a=w(c);v.value=+e.swipeDuration,k(a)},B=()=>{i=!1,v.value=0,b&&(b(),b=null)},U=c=>{if(!x()){if(T.start(c),i){const r=He(g.value);m.value=Math.min(0,r-S())}v.value=0,l=m.value,h=Date.now(),f=l,b=null}},F=c=>{if(x())return;T.move(c),T.isVertical()&&(i=!0,ie(c,!0)),m.value=G(l+T.deltaY.value,-(p()*e.optionHeight),e.optionHeight);const r=Date.now();r-h>ae&&(h=r,f=m.value)},D=()=>{if(x())return;const c=m.value-f,r=Date.now()-h;if(r<ae&&Math.abs(c)>Ie){N(c,r);return}const a=w(m.value);v.value=ne,k(a),setTimeout(()=>{i=!1},0)},H=()=>{const c={height:`${e.optionHeight}px`};return e.options.map((r,t)=>{const a=r[e.fields.text],{disabled:s}=r,M=r[e.fields.value],P={role:"button",style:c,tabindex:s?-1:0,class:[q("item",{disabled:s,selected:M===e.value}),r.className],onClick:()=>A(t)},be={class:"van-ellipsis",[e.allowHtml?"innerHTML":"textContent"]:a};return d("li",P,[n.option?n.option(r,t):d("div",be,null)])})};return oe(de),le({stopMomentum:B}),xe(()=>{const c=e.options.findIndex(a=>a[e.fields.value]===e.value),t=-Q(e.options,c)*e.optionHeight;m.value=t}),ce("touchmove",F,{target:u}),()=>d("div",{ref:u,class:q(),onTouchstartPassive:U,onTouchend:D,onTouchcancel:D},[d("ul",{ref:g,style:{transform:`translate3d(0, ${m.value+S()}px, 0)`,transitionDuration:`${v.value}ms`,transitionProperty:v.value?"all":"none"},class:q("wrapper"),onTransitionend:B},[H()])])}});const[Ve]=V("picker-toolbar"),R={title:String,cancelButtonText:String,confirmButtonText:String},me=["cancel","confirm","title","toolbar"],$e=Object.keys(R);var fe=$({name:Ve,props:R,emits:["confirm","cancel"],setup(e,{emit:o,slots:n}){const i=()=>{if(n.title)return n.title();if(e.title)return d("div",{class:[O("title"),"van-ellipsis"]},[e.title])},l=()=>o("cancel"),h=()=>o("confirm"),f=()=>{const u=e.cancelButtonText||ee("cancel");return d("button",{type:"button",class:[O("cancel"),X],onClick:l},[n.cancel?n.cancel():u])},b=()=>{const u=e.confirmButtonText||ee("confirm");return d("button",{type:"button",class:[O("confirm"),X],onClick:h},[n.confirm?n.confirm():u])};return()=>d("div",{class:O("toolbar")},[n.toolbar?n.toolbar():[f(),i(),b()]])}});const[ve,z]=V("picker-group"),he=Symbol(ve),Re=E({tabs:_(),nextStepText:String},R);$({name:ve,props:Re,emits:["confirm","cancel"],setup(e,{emit:o,slots:n}){const i=y(0),{children:l,linkChildren:h}=se(he);h();const f=()=>i.value<e.tabs.length-1&&e.nextStepText,b=()=>{f()?i.value++:o("confirm",l.map(g=>g.confirm()))},u=()=>o("cancel");return()=>{var g;const m=(g=n.default)==null?void 0:g.call(n),v=f()?e.nextStepText:e.confirmButtonText;return d("div",{class:z()},[d(fe,{title:e.title,cancelButtonText:e.cancelButtonText,confirmButtonText:v,onConfirm:b,onCancel:u},J(n,me)),d(Se,{active:i.value,"onUpdate:active":T=>i.value=T,class:z("tabs"),shrink:!0,animated:!0,lazyRender:!1},{default:()=>[e.tabs.map((T,p)=>d(we,{title:T,titleClass:z("tab-title")},{default:()=>[m==null?void 0:m[p]]}))]})])}}});const Ae=E({loading:Boolean,readonly:Boolean,allowHtml:Boolean,optionHeight:Y(44),showToolbar:Oe,swipeDuration:Y(1e3),visibleOptionNum:Y(6)},R),Ue=E({},Ae,{columns:_(),modelValue:_(),toolbarPosition:pe("top"),columnsFieldNames:Object});var Fe=$({name:Ne,props:Ue,emits:["confirm","cancel","change","clickOption","update:modelValue"],setup(e,{emit:o,slots:n}){const i=y(),l=y(e.modelValue.slice(0)),{parent:h}=oe(he),{children:f,linkChildren:b}=se(de);b();const u=C(()=>Me(e.columnsFieldNames)),g=C(()=>ke(e.optionHeight)),m=C(()=>Be(e.columns,u.value)),v=C(()=>{const{columns:t}=e;switch(m.value){case"multiple":return t;case"cascade":return De(t,u.value,l);default:return[t]}}),T=C(()=>v.value.some(t=>t.length)),p=C(()=>v.value.map((t,a)=>W(t,l.value[a],u.value))),S=C(()=>v.value.map((t,a)=>t.findIndex(s=>s[u.value.value]===l.value[a]))),k=(t,a)=>{if(l.value[t]!==a){const s=l.value.slice(0);s[t]=a,l.value=s}},x=()=>({selectedValues:l.value.slice(0),selectedOptions:p.value,selectedIndexes:S.value}),A=(t,a)=>{k(a,t),m.value==="cascade"&&l.value.forEach((s,M)=>{const P=v.value[M];te(P,s,u.value)||k(M,P.length?P[0][u.value.value]:void 0)}),Z(()=>{o("change",E({columnIndex:a},x()))})},w=(t,a)=>o("clickOption",E({columnIndex:a,currentOption:t},x())),N=()=>{f.forEach(a=>a.stopMomentum());const t=x();return Z(()=>{o("confirm",t)}),t},B=()=>o("cancel",x()),U=()=>v.value.map((t,a)=>d(_e,{value:l.value[a],fields:u.value,options:t,readonly:e.readonly,allowHtml:e.allowHtml,optionHeight:g.value,swipeDuration:e.swipeDuration,visibleOptionNum:e.visibleOptionNum,onChange:s=>A(s,a),onClickOption:s=>w(s,a)},{option:n.option})),F=t=>{if(T.value){const a={height:`${g.value}px`},s={backgroundSize:`100% ${(t-g.value)/2}px`};return[d("div",{class:O("mask"),style:s},null),d("div",{class:[Ee,O("frame")],style:a},null)]}},D=()=>{const t=g.value*+e.visibleOptionNum,a={height:`${t}px`};return d("div",{ref:i,class:O("columns"),style:a},[U(),F(t)])},H=()=>{if(e.showToolbar&&!h)return d(fe,ye(J(e,$e),{onConfirm:N,onCancel:B}),J(n,me))};j(v,t=>{t.forEach((a,s)=>{a.length&&!te(a,l.value[s],u.value)&&k(s,ue(a)[u.value.value])})},{immediate:!0});let c;return j(()=>e.modelValue,t=>{!L(t,l.value)&&!L(t,c)&&(l.value=t.slice(0),c=t.slice(0))},{deep:!0}),j(l,t=>{L(t,e.modelValue)||(c=t.slice(0),o("update:modelValue",c))},{immediate:!0}),ce("touchmove",ie,{target:i}),le({confirm:N,getSelectedOptions:()=>p.value}),()=>{var t,a;return d("div",{class:O()},[e.toolbarPosition==="top"?H():null,e.loading?d(Ce,{class:O("loading")},null):null,(t=n["columns-top"])==null?void 0:t.call(n),D(),(a=n["columns-bottom"])==null?void 0:a.call(n),e.toolbarPosition==="bottom"?H():null])}}});const je=Pe(Fe);export{je as P,Ae as p};