1 |
- import{M as me,aJ as G,E as ne,d as ae,ax as B,I as he,a_ as ge,aY as oe,U as j,S as ve,ai as xe,at as ye,ak as se,a3 as z,j as d,G as W,aq as le,as as R,ab as p,H as Te,J as ie,a0 as be,aj as Ce,W as Q,ad as Ie,aT as we,aU as X,b9 as Oe}from"./ResizeObserver.es.39b48a47.js";function A(t){if(!me(t))return t;if(Array.isArray(t))return t.map(r=>A(r));if(G(t)){const r={};return Object.keys(t).forEach(i=>{r[i]=A(t[i])}),r}return t}const Z=200,ee=300,ke=15,[ce,J]=ne("picker-column");function Me(t){const{transform:r}=window.getComputedStyle(t),i=r.slice(7,r.length-1).split(", ")[5];return Number(i)}const ue=Symbol(ce),q=t=>G(t)&&t.disabled;var Ne=ae({name:ce,props:{textKey:B(String),readonly:Boolean,allowHtml:Boolean,className:he,itemHeight:B(Number),defaultIndex:ge(0),swipeDuration:B(W),initialOptions:oe(),visibleItemCount:B(W)},emits:["change"],setup(t,{emit:r,slots:i}){let h,g,O,v,c;const b=j(),o=ve({index:t.defaultIndex,offset:0,duration:0,options:A(t.initialOptions)}),y=xe(),f=()=>o.options.length,E=()=>t.itemHeight*(+t.visibleItemCount-1)/2,$=n=>{n=R(n,0,f());for(let s=n;s<f();s++)if(!q(o.options[s]))return s;for(let s=n-1;s>=0;s--)if(!q(o.options[s]))return s},m=(n,s)=>{n=$(n)||0;const u=-n*t.itemHeight,x=()=>{n!==o.index&&(o.index=n,s&&r("change",n))};h&&u!==o.offset?c=x:x(),o.offset=u},S=n=>{JSON.stringify(n)!==JSON.stringify(o.options)&&(o.options=A(n),m(t.defaultIndex))},P=n=>{h||t.readonly||(c=null,o.duration=Z,m(n,!0))},C=n=>G(n)&&t.textKey in n?n[t.textKey]:n,k=n=>R(Math.round(-n/t.itemHeight),0,f()-1),H=(n,s)=>{const u=Math.abs(n/s);n=o.offset+u/.003*(n<0?-1:1);const x=k(n);o.duration=+t.swipeDuration,m(x,!0)},M=()=>{h=!1,o.duration=0,c&&(c(),c=null)},D=n=>{if(!t.readonly){if(y.start(n),h){const s=Me(b.value);o.offset=Math.min(0,s-E()),g=o.offset}else g=o.offset;o.duration=0,O=Date.now(),v=g,c=null}},U=n=>{if(t.readonly)return;y.move(n),y.isVertical()&&(h=!0,le(n,!0)),o.offset=R(g+y.deltaY.value,-(f()*t.itemHeight),t.itemHeight);const s=Date.now();s-O>ee&&(O=s,v=o.offset)},N=()=>{if(t.readonly)return;const n=o.offset-v,s=Date.now()-O;if(s<ee&&Math.abs(n)>ke){H(n,s);return}const x=k(o.offset);o.duration=Z,m(x,!0),setTimeout(()=>{h=!1},0)},Y=()=>{const n={height:`${t.itemHeight}px`};return o.options.map((s,u)=>{const x=C(s),V=q(s),L={role:"button",style:n,tabindex:V?-1:0,class:J("item",{disabled:V,selected:u===o.index}),onClick:()=>P(u)},K={class:"van-ellipsis",[t.allowHtml?"innerHTML":"textContent"]:x};return d("li",L,[i.option?i.option(s):d("div",K,null)])})},F=n=>{const{options:s}=o;for(let u=0;u<s.length;u++)if(C(s[u])===n)return m(u)},_=()=>o.options[o.index];return m(o.index),ye(ue),se({state:o,setIndex:m,getValue:_,setValue:F,setOptions:S,stopMomentum:M}),z(()=>t.initialOptions,S),z(()=>t.defaultIndex,n=>m(n)),()=>d("div",{class:[J(),t.className],onTouchstart:D,onTouchmove:U,onTouchend:N,onTouchcancel:N},[d("ul",{ref:b,style:{transform:`translate3d(0, ${o.offset+E()}px, 0)`,transitionDuration:`${o.duration}ms`,transitionProperty:o.duration?"all":"none"},class:J("wrapper"),onTransitionend:M},[Y()])])}});const[Se,T,te]=ne("picker"),Pe={title:String,loading:Boolean,readonly:Boolean,allowHtml:Boolean,itemHeight:p(44),showToolbar:Te,swipeDuration:p(1e3),visibleItemCount:p(6),cancelButtonText:String,confirmButtonText:String},Ee=ie({},Pe,{columns:oe(),valueKey:String,defaultIndex:p(0),toolbarPosition:be("top"),columnsFieldNames:Object});var De=ae({name:Se,props:Ee,emits:["confirm","cancel","change"],setup(t,{emit:r,slots:i}){i.default&&console.warn('[Vant] Picker: "default" slot is deprecated, please use "toolbar" slot instead.'),t.valueKey&&console.warn('[Vant] Picker: "valueKey" prop is deprecated, please use "columnsFieldNames" prop instead.');const h=j(!1),g=j([]),{text:O,values:v,children:c}=ie({text:t.valueKey||"text",values:"values",children:"children"},t.columnsFieldNames),{children:b,linkChildren:o}=Ce(ue);o();const y=Q(()=>Ie(t.itemHeight)),f=Q(()=>{const e=t.columns[0];if(typeof e=="object"){if(c in e)return"cascade";if(v in e)return"object"}return"plain"}),E=()=>{var e;const a=[];let l={[c]:t.columns};for(;l&&l[c];){const I=l[c];let w=(e=l.defaultIndex)!=null?e:+t.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},$=()=>{const{columns:e}=t;f.value==="plain"?g.value=[{[v]:e}]:f.value==="cascade"?E():g.value=e,h.value=g.value.some(a=>a[v]&&a[v].length!==0)},m=()=>b.map(e=>e.state.index),S=(e,a)=>{const l=b[e];l&&(l.setOptions(a),h.value=!0)},P=e=>{let a={[c]:t.columns};const l=m();for(let I=0;I<=e;I++)a=a[c][l[I]];for(;a&&a[c];)e++,S(e,a[c]),a=a[c][a.defaultIndex||0]},C=e=>b[e],k=e=>{const a=C(e);if(a)return a.getValue()},H=(e,a)=>{const l=C(e);l&&(l.setValue(a),f.value==="cascade"&&P(e))},M=e=>{const a=C(e);if(a)return a.state.index},D=(e,a)=>{const l=C(e);l&&(l.setIndex(a),f.value==="cascade"&&P(e))},U=e=>{const a=C(e);if(a)return a.state.options},N=()=>b.map(e=>e.getValue()),Y=e=>{e.forEach((a,l)=>{H(l,a)})},F=e=>{e.forEach((a,l)=>{D(l,a)})},_=e=>{f.value==="plain"?r(e,k(0),M(0)):r(e,N(),m())},n=e=>{f.value==="cascade"&&P(e),f.value==="plain"?r("change",k(0),M(0)):r("change",N(),e)},s=()=>{b.forEach(e=>e.stopMomentum()),_("confirm")},u=()=>_("cancel"),x=()=>{if(i.title)return i.title();if(t.title)return d("div",{class:[T("title"),"van-ellipsis"]},[t.title])},V=()=>{const e=t.cancelButtonText||te("cancel");return d("button",{type:"button",class:[T("cancel"),X],onClick:u},[i.cancel?i.cancel():e])},L=()=>{const e=t.confirmButtonText||te("confirm");return d("button",{type:"button",class:[T("confirm"),X],onClick:s},[i.confirm?i.confirm():e])},K=()=>{if(t.showToolbar){const e=i.toolbar||i.default;return d("div",{class:T("toolbar")},[e?e():[V(),x(),L()]])}},re=()=>g.value.map((e,a)=>{var l;return d(Ne,{textKey:O,readonly:t.readonly,allowHtml:t.allowHtml,className:e.className,itemHeight:y.value,defaultIndex:(l=e.defaultIndex)!=null?l:+t.defaultIndex,swipeDuration:t.swipeDuration,initialOptions:e[v],visibleItemCount:t.visibleItemCount,onChange:()=>n(a)},{option:i.option})}),de=e=>{if(h.value){const a={height:`${y.value}px`},l={backgroundSize:`100% ${(e-y.value)/2}px`};return[d("div",{class:T("mask"),style:l},null),d("div",{class:[Oe,T("frame")],style:a},null)]}},fe=()=>{const e=y.value*+t.visibleItemCount,a={height:`${e}px`};return d("div",{class:T("columns"),style:a,onTouchmove:le},[re(),de(e)])};return z(()=>t.columns,$,{immediate:!0}),se({confirm:s,getValues:N,setValues:Y,getIndexes:m,setIndexes:F,getColumnIndex:M,setColumnIndex:D,getColumnValue:k,setColumnValue:H,getColumnValues:U,setColumnValues:S}),()=>{var e,a;return d("div",{class:T()},[t.toolbarPosition==="top"?K():null,t.loading?d(we,{class:T("loading")},null):null,(e=i["columns-top"])==null?void 0:e.call(i),fe(),(a=i["columns-bottom"])==null?void 0:a.call(i),t.toolbarPosition==="bottom"?K():null])}}});export{Pe as p,De as s};
|