@aplus-frontend/ui
Version:
2 lines (1 loc) • 5 kB
JavaScript
;Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("vue"),u=require("lodash-unified"),S=require("@aplus-frontend/antdv");require("../../hooks/index.js");const I=require("../hooks/use-default-placeholder.js"),d=require("./use-select-options.js"),T=require("../../hooks/useControllableValue.js"),P={key:0},k=e.defineComponent({name:"ApFieldSelect",__name:"index",props:{mode:{default:"edit"},class:{},style:{},defaultValue:{},notFoundContent:{},suffixIcon:{},itemIcon:{},size:{},bordered:{type:Boolean,default:void 0},showArrow:{type:Boolean,default:void 0},variant:{},maxCount:{},transitionName:{},choiceTransitionName:{},popupClassName:{},dropdownClassName:{},placement:{},status:{},children:{},listHeight:{},onMouseenter:{},onMouseleave:{},tabindex:{},onClick:{},onFocus:{},onBlur:{},onChange:{},onKeydown:{},onKeyup:{},onMousedown:{},onSelect:{},open:{type:Boolean,default:void 0},animation:{},direction:{},disabled:{type:Boolean,default:void 0},prefixCls:{},id:{},autofocus:{type:Boolean,default:void 0},getPopupContainer:{},virtual:{type:Boolean,default:void 0},dropdownMatchSelectWidth:{},placeholder:{},loading:{type:Boolean,default:void 0},prefix:{},clearIcon:{},allowClear:{type:Boolean,default:!0},fieldNames:{},inputValue:{},searchValue:{},autoClearSearchValue:{type:Boolean,default:void 0},onDeselect:{},filterOption:{},filterSort:{},optionFilterProp:{},optionLabelProp:{},options:{},defaultActiveFirstOption:{type:Boolean,default:void 0},listItemHeight:{},onClickOption:{},menuItemSelectedIcon:{},labelInValue:{type:Boolean,default:void 0},showSearch:{type:Boolean,default:void 0},tagRender:{},optionLabelRender:{},onClear:{},defaultOpen:{type:Boolean,default:void 0},onDropdownVisibleChange:{},maxTagTextLength:{},maxTagCount:{},maxTagPlaceholder:{},tokenSeparators:{},removeIcon:{},dropdownStyle:{},dropdownRender:{},dropdownAlign:{},showAction:{},onPopupScroll:{},onInputKeyDown:{},emptyText:{default:"--"},request:{},multiple:{type:Boolean,default:void 0},value:{},"onUpdate:value":{},searchMode:{default:"filter"},refetchOnFocus:{type:Boolean,default:!1},lazy:{type:Boolean,default:!1},addAll:{type:[Boolean,Number]},params:{},searchDelay:{default:300}},emits:["update:value"],setup(g,{expose:C,emit:B}){let r=0;const o=g,_=u.debounce(t=>{if(!u.isFunction(o.request))return;r+=1;const n=r;c([]),a.value=!0,o.request(t).then(l=>{r===n&&(c(l),a.value=!1)})},o.searchDelay),L=(t,n)=>{const l=o.fieldNames?.label||"label";return typeof n?.[l]=="string"&&n[l].toLowerCase().indexOf(t.toLowerCase())>-1},{options:i,updateOptions:c}=d.useSelectOptions(o),m=e.ref(),a=e.ref(o?.loading||!1),b=B;async function f(t=!0){if(!u.isFunction(o.request))return;a.value=!0,r+=1;const n=r;try{t&&c([]);const l=await o.request(void 0,o.params);if(n!==r)return;c(l)}finally{a.value=!1}}function w(t){o.onFocus?.(t),o.refetchOnFocus&&r>0&&!a.value&&f(!0)}e.onMounted(()=>{o.lazy||f()}),e.watch(()=>o.params,()=>{f()},{deep:!0});const{value:p,updateValue:v}=T.useControllableValue(o,b),F=I.useDefaultPlaceholder("Select",o),O=e.computed(()=>{const t=e.unref(p);if(!t)return o.emptyText;const n=o.fieldNames?.label||"label",l=o.fieldNames?.value||"value";return u.isArray(t)?t.map(s=>e.unref(i).find(x=>x[l]===s)?.[n]).filter(Boolean).join("、")||o.emptyText:e.unref(i).find(s=>s[l]===t)?.[n]||o.emptyText}),A=e.computed(()=>{const t=!!o?.showSearch,n=o.searchMode;let l={};return t&&(l=n==="request"?{defaultActiveFirstOption:!1,onSearch:_,notFoundContent:a.value?void 0:null,filterOption:!1,suffixIcon:null}:{filterOption:u.isNil(o?.filterOption)?L:o?.filterOption}),{...u.omit(o,["mode","value","onUpdate:value","onSearch","request","searchMode","onFocus","onBeforeSelect"]),placeholder:e.unref(F),...l}}),y=e.computed(()=>i.value.map(t=>t[o.fieldNames?.value||"value"]).filter(t=>t!==d.SELECT_ALL_VALUE_KEY)),V=e.computed(()=>{if(!o.multiple||!o.addAll)return e.unref(p);if(i.value.some(t=>t[o.fieldNames?.value||"value"]===d.SELECT_ALL_VALUE_KEY)){const t=e.unref(y),n=e.unref(p)||[];if(t.length&&t.every(l=>n?.includes(l)))return[d.SELECT_ALL_VALUE_KEY]}return e.unref(p)});function E(t){return o.multiple&&t.includes(d.SELECT_ALL_VALUE_KEY)?v(e.unref(y)):v(t)}function N(){m.value?.focus()}function q(){m.value?.blur()}return C({focus:N,blur:q,request:f}),(t,n)=>t.mode==="read"?(e.openBlock(),e.createElementBlock("span",P,e.toDisplayString(O.value),1)):(e.openBlock(),e.createBlock(e.unref(S.Select),e.mergeProps({key:1,ref_key:"selectRef",ref:m,"allow-clear":""},A.value,{mode:t.multiple?"multiple":void 0,value:V.value,options:e.unref(i),loading:a.value,"onUpdate:value":E,onFocus:w}),e.createSlots({_:2},[e.renderList(t.$slots,(l,h)=>({name:h,fn:e.withCtx(s=>[e.renderSlot(t.$slots,h,e.normalizeProps(e.guardReactiveProps(s||{})))])})),a.value?{name:"notFoundContent",fn:e.withCtx(()=>[e.createVNode(e.unref(S.Spin),{size:"small"})]),key:"0"}:void 0]),1040,["mode","value","options","loading"]))}});exports.default=k;