1 |
- import{M as dt,aJ as G,E as nt,d as at,ax as K,I as ht,a_ as gt,aW as ot,U as W,S as vt,ai as xt,at as yt,ak as st,a3 as z,i as f,G as j,aq as lt,as as F,ab as A,H as Tt,J as it,a0 as bt,aj as Ct,W as Q,ad as It,aS as wt,aY as X,b3 as Ot}from"./index.6b776f4b.js";function $(e){if(!dt(e))return e;if(Array.isArray(e))return e.map(r=>$(r));if(G(e)){const r={};return Object.keys(e).forEach(i=>{r[i]=$(e[i])}),r}return e}const Z=200,tt=300,St=15,[ct,J]=nt("picker-column");function Mt(e){const{transform:r}=window.getComputedStyle(e),i=r.slice(7,r.length-1).split(", ")[5];return Number(i)}const ut=Symbol(ct),q=e=>G(e)&&e.disabled;var kt=at({name:ct,props:{textKey:K(String),readonly:Boolean,allowHtml:Boolean,className:ht,itemHeight:K(Number),defaultIndex:gt(0),swipeDuration:K(j),initialOptions:ot(),visibleItemCount:K(j)},emits:["change"],setup(e,{emit:r,slots:i}){let h,g,O,v,c;const b=W(),o=vt({index:e.defaultIndex,offset:0,duration:0,options:$(e.initialOptions)}),y=xt(),m=()=>o.options.length,H=()=>e.itemHeight*(+e.visibleItemCount-1)/2,U=n=>{n=F(n,0,m());for(let s=n;s<m();s++)if(!q(o.options[s]))return s;for(let s=n-1;s>=0;s--)if(!q(o.options[s]))return s},d=(n,s)=>{n=U(n)||0;const u=-n*e.itemHeight,x=()=>{n!==o.index&&(o.index=n,s&&r("change",n))};h&&u!==o.offset?c=x:x(),o.offset=u},N=n=>{JSON.stringify(n)!==JSON.stringify(o.options)&&(o.options=$(n),d(e.defaultIndex))},E=n=>{h||e.readonly||(c=null,o.duration=Z,d(n,!0))},C=n=>G(n)&&e.textKey in n?n[e.textKey]:n,S=n=>F(Math.round(-n/e.itemHeight),0,m()-1),P=(n,s)=>{const u=Math.abs(n/s);n=o.offset+u/.003*(n<0?-1:1);const x=S(n);o.duration=+e.swipeDuration,d(x,!0)},M=()=>{h=!1,o.duration=0,c&&(c(),c=null)},D=n=>{if(!e.readonly){if(y.start(n),h){const s=Mt(b.value);o.offset=Math.min(0,s-H()),g=o.offset}else g=o.offset;o.duration=0,O=Date.now(),v=g,c=null}},Y=n=>{if(e.readonly)return;y.move(n),y.isVertical()&&(h=!0,lt(n,!0)),o.offset=F(g+y.deltaY.value,-(m()*e.itemHeight),e.itemHeight);const s=Date.now();s-O>tt&&(O=s,v=o.offset)},k=()=>{if(e.readonly)return;const n=o.offset-v,s=Date.now()-O;if(s<tt&&Math.abs(n)>St){P(n,s);return}const x=S(o.offset);o.duration=Z,d(x,!0),setTimeout(()=>{h=!1},0)},L=()=>{const n={height:`${e.itemHeight}px`};return o.options.map((s,u)=>{const x=C(s),B=q(s),p={role:"button",style:n,tabindex:B?-1:0,class:J("item",{disabled:B,selected:u===o.index}),onClick:()=>E(u)},V={class:"van-ellipsis",[e.allowHtml?"innerHTML":"textContent"]:x};return f("li",p,[i.option?i.option(s):f("div",V,null)])})},R=n=>{const{options:s}=o;for(let u=0;u<s.length;u++)if(C(s[u])===n)return d(u)},_=()=>o.options[o.index];return d(o.index),yt(ut),st({state:o,setIndex:d,getValue:_,setValue:R,setOptions:N,stopMomentum:M}),z(()=>e.initialOptions,N),z(()=>e.defaultIndex,n=>d(n)),()=>f("div",{class:[J(),e.className],onTouchstart:D,onTouchmove:Y,onTouchend:k,onTouchcancel:k},[f("ul",{ref:b,style:{transform:`translate3d(0, ${o.offset+H()}px, 0)`,transitionDuration:`${o.duration}ms`,transitionProperty:o.duration?"all":"none"},class:J("wrapper"),onTransitionend:M},[L()])])}});const[Nt,T,et]=nt("picker"),Et={title:String,loading:Boolean,readonly:Boolean,allowHtml:Boolean,itemHeight:A(44),showToolbar:Tt,swipeDuration:A(1e3),visibleItemCount:A(6),cancelButtonText:String,confirmButtonText:String},Ht=it({},Et,{columns:ot(),valueKey:String,defaultIndex:A(0),toolbarPosition:bt("top"),columnsFieldNames:Object});var Dt=at({name:Nt,props:Ht,emits:["confirm","cancel","change"],setup(e,{emit:r,slots:i}){const h=W(!1),g=W([]),{text:O,values:v,children:c}=it({text:e.valueKey||"text",values:"values",children:"children"},e.columnsFieldNames),{children:b,linkChildren:o}=Ct(ut);o();const y=Q(()=>It(e.itemHeight)),m=Q(()=>{const t=e.columns[0];if(typeof t=="object"){if(c in t)return"cascade";if(v in t)return"object"}return"plain"}),H=()=>{var t;const a=[];let l={[c]:e.columns};for(;l&&l[c];){const I=l[c];let w=(t=l.defaultIndex)!=null?t:+e.defaultIndex;for(;I[w]&&I[w].disabled;)if(w<I.length-1)w++;else{w=0;break}a.push({[v]:l[c],className:l.className,defaultIndex:w}),l=I[w]}g.value=a},U=()=>{const{columns:t}=e;m.value==="plain"?g.value=[{[v]:t}]:m.value==="cascade"?H():g.value=t,h.value=g.value.some(a=>a[v]&&a[v].length!==0)},d=()=>b.map(t=>t.state.index),N=(t,a)=>{const l=b[t];l&&(l.setOptions(a),h.value=!0)},E=t=>{let a={[c]:e.columns};const l=d();for(let I=0;I<=t;I++)a=a[c][l[I]];for(;a&&a[c];)t++,N(t,a[c]),a=a[c][a.defaultIndex||0]},C=t=>b[t],S=t=>{const a=C(t);if(a)return a.getValue()},P=(t,a)=>{const l=C(t);l&&(l.setValue(a),m.value==="cascade"&&E(t))},M=t=>{const a=C(t);if(a)return a.state.index},D=(t,a)=>{const l=C(t);l&&(l.setIndex(a),m.value==="cascade"&&E(t))},Y=t=>{const a=C(t);if(a)return a.state.options},k=()=>b.map(t=>t.getValue()),L=t=>{t.forEach((a,l)=>{P(l,a)})},R=t=>{t.forEach((a,l)=>{D(l,a)})},_=t=>{m.value==="plain"?r(t,S(0),M(0)):r(t,k(),d())},n=t=>{m.value==="cascade"&&E(t),m.value==="plain"?r("change",S(0),M(0)):r("change",k(),t)},s=()=>{b.forEach(t=>t.stopMomentum()),_("confirm")},u=()=>_("cancel"),x=()=>{if(i.title)return i.title();if(e.title)return f("div",{class:[T("title"),"van-ellipsis"]},[e.title])},B=()=>{const t=e.cancelButtonText||et("cancel");return f("button",{type:"button",class:[T("cancel"),X],onClick:u},[i.cancel?i.cancel():t])},p=()=>{const t=e.confirmButtonText||et("confirm");return f("button",{type:"button",class:[T("confirm"),X],onClick:s},[i.confirm?i.confirm():t])},V=()=>{if(e.showToolbar){const t=i.toolbar||i.default;return f("div",{class:T("toolbar")},[t?t():[B(),x(),p()]])}},rt=()=>g.value.map((t,a)=>{var l;return f(kt,{textKey:O,readonly:e.readonly,allowHtml:e.allowHtml,className:t.className,itemHeight:y.value,defaultIndex:(l=t.defaultIndex)!=null?l:+e.defaultIndex,swipeDuration:e.swipeDuration,initialOptions:t[v],visibleItemCount:e.visibleItemCount,onChange:()=>n(a)},{option:i.option})}),ft=t=>{if(h.value){const a={height:`${y.value}px`},l={backgroundSize:`100% ${(t-y.value)/2}px`};return[f("div",{class:T("mask"),style:l},null),f("div",{class:[Ot,T("frame")],style:a},null)]}},mt=()=>{const t=y.value*+e.visibleItemCount,a={height:`${t}px`};return f("div",{class:T("columns"),style:a,onTouchmove:lt},[rt(),ft(t)])};return z(()=>e.columns,U,{immediate:!0}),st({confirm:s,getValues:k,setValues:L,getIndexes:d,setIndexes:R,getColumnIndex:M,setColumnIndex:D,getColumnValue:S,setColumnValue:P,getColumnValues:Y,setColumnValues:N}),()=>{var t,a;return f("div",{class:T()},[e.toolbarPosition==="top"?V():null,e.loading?f(wt,{class:T("loading")},null):null,(t=i["columns-top"])==null?void 0:t.call(i),mt(),(a=i["columns-bottom"])==null?void 0:a.call(i),e.toolbarPosition==="bottom"?V():null])}}});export{Et as p,Dt as s};
|