index-688d0edc.js 7.9 KB

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