UNPKG

ll-package

Version:

2 lines (1 loc) 7.5 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const C=require("../../../@babel/runtime/helpers/esm/objectSpread2.js"),s=require("../../../@babel/runtime/helpers/esm/extends.js"),o=require("vue"),_=require("./BaseSelect.js"),de=require("./OptionList.js"),se=require("./hooks/useOptions.js"),fe=require("./SelectContext.js"),ve=require("./hooks/useId.js"),p=require("./utils/valueUtil.js"),me=require("./utils/warningPropsUtil.js"),he=require("./utils/commonUtil.js"),be=require("./hooks/useFilterOptions.js"),ge=require("./hooks/useCache.js"),q=require("../_util/vue-types/index.js"),Se=require("../_util/props-util/initDefaultProps.js"),H=require("../_util/hooks/useMergedState.js"),W=require("../_util/hooks/useState.js"),Ve=require("../_util/toReactive.js"),Oe=require("../_util/omit.js"),pe=["inputValue"];function B(){return s.default(s.default({},_.baseSelectPropsWithoutPrivate()),{prefixCls:String,id:String,backfill:{type:Boolean,default:void 0},fieldNames:Object,inputValue:String,searchValue:String,onSearch:Function,autoClearSearchValue:{type:Boolean,default:void 0},onSelect:Function,onDeselect:Function,filterOption:{type:[Boolean,Function],default:void 0},filterSort:Function,optionFilterProp:String,optionLabelProp:String,options:Array,defaultActiveFirstOption:{type:Boolean,default:void 0},virtual:{type:Boolean,default:void 0},listHeight:Number,listItemHeight:Number,menuItemSelectedIcon:q.default.any,mode:String,labelInValue:{type:Boolean,default:void 0},value:q.default.any,defaultValue:q.default.any,onChange:Function,children:Array})}function ye(t){return!t||typeof t!="object"}const Ie=o.defineComponent({compatConfig:{MODE:3},name:"VcSelect",inheritAttrs:!1,props:Se.default(B(),{prefixCls:"vc-select",autoClearSearchValue:!0,listHeight:200,listItemHeight:20,dropdownMatchSelectWidth:!0}),setup(t,j){let{expose:T,attrs:U,slots:$}=j;const x=ve.default(o.toRef(t,"id")),f=o.computed(()=>_.isMultiple(t.mode)),y=o.computed(()=>!!(!t.options&&t.children)),K=o.computed(()=>t.filterOption===void 0&&t.mode==="combobox"?!1:t.filterOption),d=o.computed(()=>p.fillFieldNames(t.fieldNames,y.value)),[v,b]=H.default("",{value:o.computed(()=>t.searchValue!==void 0?t.searchValue:t.inputValue),postState:e=>e||""}),A=se.default(o.toRef(t,"options"),o.toRef(t,"children"),d),{valueOptions:I,labelOptions:z,options:D}=A,R=e=>he.toArray(e).map(a=>{var u,n;let c,O,h,E;ye(a)?c=a:(h=a.key,O=a.label,c=(u=a.value)!==null&&u!==void 0?u:h);const i=I.value.get(c);return i&&(O===void 0&&(O=i==null?void 0:i[t.optionLabelProp||d.value.label]),h===void 0&&(h=(n=i==null?void 0:i.key)!==null&&n!==void 0?n:c),E=i==null?void 0:i.disabled),{label:O,value:c,key:h,disabled:E,option:i}}),[G,J]=H.default(t.defaultValue,{value:o.toRef(t,"value")}),Q=o.computed(()=>{var e;const l=R(G.value);return t.mode==="combobox"&&!(!((e=l[0])===null||e===void 0)&&e.value)?[]:l}),[r,L]=ge.default(Q,I),X=o.computed(()=>{if(!t.mode&&r.value.length===1){const e=r.value[0];if(e.value===null&&(e.label===null||e.label===void 0))return[]}return r.value.map(e=>{var l;return s.default(s.default({},e),{label:(l=typeof e.label=="function"?e.label():e.label)!==null&&l!==void 0?l:e.value})})}),w=o.computed(()=>new Set(r.value.map(e=>e.value)));o.watchEffect(()=>{var e;if(t.mode==="combobox"){const l=(e=r.value[0])===null||e===void 0?void 0:e.value;l!=null&&b(String(l))}},{flush:"post"});const M=(e,l)=>{const a=l??e;return{[d.value.value]:e,[d.value.label]:a}},P=o.shallowRef();o.watchEffect(()=>{if(t.mode!=="tags"){P.value=D.value;return}const e=D.value.slice(),l=a=>I.value.has(a);[...r.value].sort((a,u)=>a.value<u.value?-1:1).forEach(a=>{const u=a.value;l(u)||e.push(M(u,a.label))}),P.value=e});const F=be.default(P,d,v,K,o.toRef(t,"optionFilterProp")),N=o.computed(()=>t.mode!=="tags"||!v.value||F.value.some(e=>e[t.optionFilterProp||"value"]===v.value)?F.value:[M(v.value),...F.value]),Y=o.computed(()=>t.filterSort?[...N.value].sort((e,l)=>t.filterSort(e,l)):N.value),k=o.computed(()=>p.flattenOptions(Y.value,{fieldNames:d.value,childrenAsData:y.value})),m=e=>{const l=R(e);if(J(l),t.onChange&&(l.length!==r.value.length||l.some((a,u)=>{var n;return((n=r.value[u])===null||n===void 0?void 0:n.value)!==(a==null?void 0:a.value)}))){const a=t.labelInValue?l.map(n=>s.default(s.default({},n),{originLabel:n.label,label:typeof n.label=="function"?n.label():n.label})):l.map(n=>n.value),u=l.map(n=>p.injectPropsWithOption(L(n.value)));t.onChange(f.value?a:a[0],f.value?u:u[0])}},[Z,g]=W.default(null),[ee,te]=W.default(0),le=o.computed(()=>t.defaultActiveFirstOption!==void 0?t.defaultActiveFirstOption:t.mode!=="combobox"),ae=function(e,l){let{source:a="keyboard"}=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{};te(l),t.backfill&&t.mode==="combobox"&&e!==null&&a==="keyboard"&&g(String(e))},S=(e,l)=>{const a=()=>{var u;const n=L(e),c=n==null?void 0:n[d.value.label];return[t.labelInValue?{label:typeof c=="function"?c():c,originLabel:c,value:e,key:(u=n==null?void 0:n.key)!==null&&u!==void 0?u:e}:e,p.injectPropsWithOption(n)]};if(l&&t.onSelect){const[u,n]=a();t.onSelect(u,n)}else if(!l&&t.onDeselect){const[u,n]=a();t.onDeselect(u,n)}},ne=(e,l)=>{let a;const u=f.value?l.selected:!0;u?a=f.value?[...r.value,e]:[e]:a=r.value.filter(n=>n.value!==e),m(a),S(e,u),t.mode==="combobox"?g(""):(!f.value||t.autoClearSearchValue)&&(b(""),g(""))},ue=(e,l)=>{m(e),(l.type==="remove"||l.type==="clear")&&l.values.forEach(a=>{S(a.value,!1)})},oe=(e,l)=>{var a;if(b(e),g(null),l.source==="submit"){const u=(e||"").trim();if(u){const n=Array.from(new Set([...w.value,u]));m(n),S(u,!0),b("")}return}l.source!=="blur"&&(t.mode==="combobox"&&m(e),(a=t.onSearch)===null||a===void 0||a.call(t,e))},ie=e=>{let l=e;t.mode!=="tags"&&(l=e.map(u=>{const n=z.value.get(u);return n==null?void 0:n.value}).filter(u=>u!==void 0));const a=Array.from(new Set([...w.value,...l]));m(a),a.forEach(u=>{S(u,!0)})},re=o.computed(()=>t.virtual!==!1&&t.dropdownMatchSelectWidth!==!1);fe.useProvideSelectProps(Ve.toReactive(s.default(s.default({},A),{flattenOptions:k,onActiveValue:ae,defaultActiveFirstOption:le,onSelect:ne,menuItemSelectedIcon:o.toRef(t,"menuItemSelectedIcon"),rawValues:w,fieldNames:d,virtual:re,listHeight:o.toRef(t,"listHeight"),listItemHeight:o.toRef(t,"listItemHeight"),childrenAsData:y}))),process.env.NODE_ENV!=="production"&&o.watchEffect(()=>{me.default(t)},{flush:"post"});const V=o.ref();T({focus(){var e;(e=V.value)===null||e===void 0||e.focus()},blur(){var e;(e=V.value)===null||e===void 0||e.blur()},scrollTo(e){var l;(l=V.value)===null||l===void 0||l.scrollTo(e)}});const ce=o.computed(()=>Oe.default(t,["id","mode","prefixCls","backfill","fieldNames","inputValue","searchValue","onSearch","autoClearSearchValue","onSelect","onDeselect","dropdownMatchSelectWidth","filterOption","filterSort","optionFilterProp","optionLabelProp","options","children","defaultActiveFirstOption","menuItemSelectedIcon","virtual","listHeight","listItemHeight","value","defaultValue","labelInValue","onChange"]));return()=>o.createVNode(_.default,C.default(C.default(C.default({},ce.value),U),{},{id:x,prefixCls:t.prefixCls,ref:V,omitDomProps:pe,mode:t.mode,displayValues:X.value,onDisplayValuesChange:ue,searchValue:v.value,onSearch:oe,onSearchSplit:ie,dropdownMatchSelectWidth:t.dropdownMatchSelectWidth,OptionList:de.default,emptyOptions:!k.value.length,activeValue:Z.value,activeDescendantId:`${x}_list_${ee.value}`}),$)}});exports.default=Ie;exports.selectProps=B;