index-06479965.js 7.0 KB

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