index.bcc8eff6.js 6.7 KB

1
  1. import{L as me,aI as G,A as ne,d as ae,aw as B,H as he,aZ as ge,aV as oe,S as q,Q as ve,ah as xe,as as ye,aj as se,a2 as z,j as f,E as Q,ap as le,ar as Y,aa as p,G as Te,I as ie,$ as be,ai as Ce,V as X,ac as Ie,aS as we,aX as Z,b2 as Oe}from"./index.e6d6174e.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 W=200,ee=300,Se=15,[ce,J]=ne("picker-column");function ke(t){const{transform:r}=window.getComputedStyle(t),i=r.slice(7,r.length-1).split(", ")[5];return Number(i)}const ue=Symbol(ce),j=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(Q),initialOptions:oe(),visibleItemCount:B(Q)},emits:["change"],setup(t,{emit:r,slots:i}){let h,g,O,v,c;const b=q(),o=ve({index:t.defaultIndex,offset:0,duration:0,options:A(t.initialOptions)}),y=xe(),d=()=>o.options.length,E=()=>t.itemHeight*(+t.visibleItemCount-1)/2,$=n=>{n=Y(n,0,d());for(let s=n;s<d();s++)if(!j(o.options[s]))return s;for(let s=n-1;s>=0;s--)if(!j(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},M=n=>{JSON.stringify(n)!==JSON.stringify(o.options)&&(o.options=A(n),m(t.defaultIndex))},P=n=>{h||t.readonly||(c=null,o.duration=W,m(n,!0))},C=n=>G(n)&&t.textKey in n?n[t.textKey]:n,S=n=>Y(Math.round(-n/t.itemHeight),0,d()-1),H=(n,s)=>{const u=Math.abs(n/s);n=o.offset+u/.003*(n<0?-1:1);const x=S(n);o.duration=+t.swipeDuration,m(x,!0)},k=()=>{h=!1,o.duration=0,c&&(c(),c=null)},D=n=>{if(!t.readonly){if(y.start(n),h){const s=ke(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}},L=n=>{if(t.readonly)return;y.move(n),y.isVertical()&&(h=!0,le(n,!0)),o.offset=Y(g+y.deltaY.value,-(d()*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)>Se){H(n,s);return}const x=S(o.offset);o.duration=W,m(x,!0),setTimeout(()=>{h=!1},0)},F=()=>{const n={height:`${t.itemHeight}px`};return o.options.map((s,u)=>{const x=C(s),_=j(s),U={role:"button",style:n,tabindex:_?-1:0,class:J("item",{disabled:_,selected:u===o.index}),onClick:()=>P(u)},K={class:"van-ellipsis",[t.allowHtml?"innerHTML":"textContent"]:x};return f("li",U,[i.option?i.option(s):f("div",K,null)])})},R=n=>{const{options:s}=o;for(let u=0;u<s.length;u++)if(C(s[u])===n)return m(u)},V=()=>o.options[o.index];return m(o.index),ye(ue),se({state:o,setIndex:m,getValue:V,setValue:R,setOptions:M,stopMomentum:k}),z(()=>t.initialOptions,M),z(()=>t.defaultIndex,n=>m(n)),()=>f("div",{class:[J(),t.className],onTouchstart:D,onTouchmove:L,onTouchend:N,onTouchcancel:N},[f("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:k},[F()])])}});const[Me,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:Me,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=q(!1),g=q([]),{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=X(()=>Ie(t.itemHeight)),d=X(()=>{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;d.value==="plain"?g.value=[{[v]:e}]:d.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),M=(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++,M(e,a[c]),a=a[c][a.defaultIndex||0]},C=e=>b[e],S=e=>{const a=C(e);if(a)return a.getValue()},H=(e,a)=>{const l=C(e);l&&(l.setValue(a),d.value==="cascade"&&P(e))},k=e=>{const a=C(e);if(a)return a.state.index},D=(e,a)=>{const l=C(e);l&&(l.setIndex(a),d.value==="cascade"&&P(e))},L=e=>{const a=C(e);if(a)return a.state.options},N=()=>b.map(e=>e.getValue()),F=e=>{e.forEach((a,l)=>{H(l,a)})},R=e=>{e.forEach((a,l)=>{D(l,a)})},V=e=>{d.value==="plain"?r(e,S(0),k(0)):r(e,N(),m())},n=e=>{d.value==="cascade"&&P(e),d.value==="plain"?r("change",S(0),k(0)):r("change",N(),e)},s=()=>{b.forEach(e=>e.stopMomentum()),V("confirm")},u=()=>V("cancel"),x=()=>{if(i.title)return i.title();if(t.title)return f("div",{class:[T("title"),"van-ellipsis"]},[t.title])},_=()=>{const e=t.cancelButtonText||te("cancel");return f("button",{type:"button",class:[T("cancel"),Z],onClick:u},[i.cancel?i.cancel():e])},U=()=>{const e=t.confirmButtonText||te("confirm");return f("button",{type:"button",class:[T("confirm"),Z],onClick:s},[i.confirm?i.confirm():e])},K=()=>{if(t.showToolbar){const e=i.toolbar||i.default;return f("div",{class:T("toolbar")},[e?e():[_(),x(),U()]])}},re=()=>g.value.map((e,a)=>{var l;return f(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})}),fe=e=>{if(h.value){const a={height:`${y.value}px`},l={backgroundSize:`100% ${(e-y.value)/2}px`};return[f("div",{class:T("mask"),style:l},null),f("div",{class:[Oe,T("frame")],style:a},null)]}},de=()=>{const e=y.value*+t.visibleItemCount,a={height:`${e}px`};return f("div",{class:T("columns"),style:a,onTouchmove:le},[re(),fe(e)])};return z(()=>t.columns,$,{immediate:!0}),se({confirm:s,getValues:N,setValues:F,getIndexes:m,setIndexes:R,getColumnIndex:k,setColumnIndex:D,getColumnValue:S,setColumnValue:H,getColumnValues:L,setColumnValues:M}),()=>{var e,a;return f("div",{class:T()},[t.toolbarPosition==="top"?K():null,t.loading?f(we,{class:T("loading")},null):null,(e=i["columns-top"])==null?void 0:e.call(i),de(),(a=i["columns-bottom"])==null?void 0:a.call(i),t.toolbarPosition==="bottom"?K():null])}}});export{Pe as p,De as s};