api-dict-select 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -0,0 +1,212 @@
1
+ import { defineComponent as z, useModel as f, ref as h, computed as U, watch as p, openBlock as B, createBlock as D, unref as L, withCtx as G, createElementBlock as H, Fragment as P, renderList as Q, mergeModels as N } from "vue";
2
+ import { ElSelect as W, ElOption as X } from "element-plus";
3
+ const M = /* @__PURE__ */ new Map(), Y = () => M, Z = (n) => {
4
+ if (n) {
5
+ M.delete(n);
6
+ return;
7
+ }
8
+ M.clear();
9
+ }, I = /* @__PURE__ */ z({
10
+ __name: "DictSelect",
11
+ props: /* @__PURE__ */ N({
12
+ placeholder: { default: "全部" },
13
+ disabled: { type: Boolean, default: !1 },
14
+ optionLabel: { default: "k" },
15
+ optionValue: { default: "v" },
16
+ optionDisabledField: { default: "text" },
17
+ optionDisabledValue: { type: [String, Number, Boolean], default: "disable" },
18
+ filter: { type: Boolean, default: !1 },
19
+ clearable: { type: Boolean, default: !0 },
20
+ vModelToNumber: { type: Boolean, default: !1 },
21
+ vModelToString: { type: Boolean, default: !1 },
22
+ isBuffer: { type: Boolean, default: !0 },
23
+ isLabel: { type: Boolean, default: !1 },
24
+ api: {},
25
+ mock: { default: () => [] },
26
+ multiple: { type: Boolean, default: !1 },
27
+ param: { default: () => ({}) },
28
+ callApiIfNeeded: { type: Boolean, default: !0 },
29
+ cacheKey: { default: "" },
30
+ transform: { type: Function, default: void 0 },
31
+ immediate: { type: Boolean, default: !0 },
32
+ autoClearInvalidValue: { type: Boolean, default: !0 }
33
+ }, {
34
+ modelValue: {},
35
+ modelModifiers: {},
36
+ options: {},
37
+ optionsModifiers: {},
38
+ response: {},
39
+ responseModifiers: {},
40
+ loading: { type: Boolean },
41
+ loadingModifiers: {},
42
+ selectedOption: {},
43
+ selectedOptionModifiers: {}
44
+ }),
45
+ emits: /* @__PURE__ */ N(["label", "allData", "options", "loaded", "error", "onClear"], ["update:modelValue", "update:options", "update:response", "update:loading", "update:selectedOption"]),
46
+ setup(n, { expose: w, emit: E }) {
47
+ const e = n, u = f(n, "modelValue"), O = f(n, "options"), T = f(n, "response"), K = f(n, "loading"), A = f(n, "selectedOption"), i = h([]), v = h(!1), r = h(), b = Y();
48
+ let m = 0;
49
+ const s = E, k = (l) => {
50
+ var a, t, o;
51
+ return ((a = l == null ? void 0 : l.result) == null ? void 0 : a.list) || ((t = l == null ? void 0 : l.result) == null ? void 0 : t.data) || ((o = l == null ? void 0 : l.result) == null ? void 0 : o.result) || (l == null ? void 0 : l.result) || (l == null ? void 0 : l.data) || l || [];
52
+ }, F = () => !e.param || Object.keys(e.param).length === 0 ? !0 : Object.values(e.param).every(
53
+ (l) => l != null && l !== ""
54
+ ), C = () => {
55
+ var a;
56
+ return `${e.cacheKey || ((a = e.api) == null ? void 0 : a.name) || "dict-select"}:${JSON.stringify(e.param || {})}`;
57
+ }, c = () => e.multiple ? Array.isArray(u.value) ? i.value.filter(
58
+ (l) => u.value.includes(l[e.optionValue])
59
+ ) : [] : i.value.find(
60
+ (l) => l[e.optionValue] === u.value
61
+ ), d = () => {
62
+ O.value = i.value, T.value = r.value, K.value = v.value, A.value = c();
63
+ }, j = (l) => {
64
+ e.vModelToNumber && l.forEach((a) => {
65
+ a[e.optionValue] = Number(a[e.optionValue]);
66
+ }), e.vModelToString && l.forEach((a) => {
67
+ a[e.optionValue] = String(a[e.optionValue]);
68
+ });
69
+ }, J = () => {
70
+ if (!e.autoClearInvalidValue)
71
+ return;
72
+ if (e.multiple) {
73
+ if (Array.isArray(u.value) && u.value.length > 0) {
74
+ const a = u.value.filter(
75
+ (t) => i.value.some((o) => o[e.optionValue] === t)
76
+ );
77
+ a.length !== u.value.length && (u.value = a);
78
+ }
79
+ return;
80
+ }
81
+ i.value.findIndex(
82
+ (a) => a[e.optionValue] === u.value
83
+ ) === -1 && (u.value = void 0);
84
+ }, R = () => {
85
+ s("onClear");
86
+ }, y = (l, a, t) => {
87
+ i.value = l, r.value = a, j(i.value), J(), d(), s("loaded", {
88
+ options: i.value,
89
+ response: r.value,
90
+ fromCache: t
91
+ });
92
+ }, g = async (l = !1) => {
93
+ if (e.mock.length > 0) {
94
+ y(e.mock, e.mock, !1);
95
+ return;
96
+ }
97
+ if (!e.api) {
98
+ i.value = [], r.value = void 0, d();
99
+ return;
100
+ }
101
+ if (!e.callApiIfNeeded || !F()) {
102
+ i.value = [], r.value = void 0, d();
103
+ return;
104
+ }
105
+ const a = C();
106
+ if (!l && e.isBuffer && b.has(a)) {
107
+ const o = b.get(a);
108
+ y((o == null ? void 0 : o.options) || [], o == null ? void 0 : o.response, !0);
109
+ return;
110
+ }
111
+ const t = ++m;
112
+ v.value = !0, d();
113
+ try {
114
+ const o = await e.api(e.param);
115
+ if (t !== m)
116
+ return;
117
+ const S = e.transform ? e.transform(k(o), o) : k(o), x = Array.isArray(S) ? S : [];
118
+ e.isBuffer && b.set(a, {
119
+ options: x,
120
+ response: o
121
+ }), y(x, o, !1);
122
+ } catch (o) {
123
+ if (t !== m)
124
+ return;
125
+ s("error", o), i.value = [], r.value = void 0, d();
126
+ } finally {
127
+ t === m && (v.value = !1, d());
128
+ }
129
+ }, q = async (l = !0) => {
130
+ await g(l);
131
+ }, $ = () => {
132
+ Z(C());
133
+ }, V = U(() => {
134
+ if (e.multiple) {
135
+ const a = c();
136
+ return Array.isArray(a) ? a.map((t) => t[e.optionLabel]) : [];
137
+ }
138
+ const l = c();
139
+ return !l || Array.isArray(l) ? "" : l[e.optionLabel] ?? "";
140
+ });
141
+ return p(
142
+ () => i.value,
143
+ () => {
144
+ s("options", i.value), O.value = i.value;
145
+ }
146
+ ), p(
147
+ () => e.mock,
148
+ () => {
149
+ if (e.mock.length > 0) {
150
+ y(e.mock, e.mock, !1);
151
+ return;
152
+ }
153
+ e.api || (i.value = [], r.value = void 0, d());
154
+ }
155
+ ), p(
156
+ () => e.api,
157
+ () => {
158
+ e.immediate && g();
159
+ },
160
+ { immediate: e.immediate }
161
+ ), p(
162
+ () => e.param,
163
+ (l, a) => {
164
+ JSON.stringify(l) !== JSON.stringify(a) && g();
165
+ },
166
+ { deep: !0 }
167
+ ), p(
168
+ () => [u.value, i.value],
169
+ () => {
170
+ const l = c();
171
+ A.value = l, e.isLabel && s(
172
+ "label",
173
+ Array.isArray(V.value) ? V.value.join(",") : V.value
174
+ ), s("allData", l);
175
+ },
176
+ { immediate: !0, deep: !0 }
177
+ ), w({
178
+ refresh: q,
179
+ clearCache: $,
180
+ getOptions: () => i.value,
181
+ getResponse: () => r.value,
182
+ getSelectedOption: c
183
+ }), (l, a) => (B(), D(L(W), {
184
+ modelValue: u.value,
185
+ "onUpdate:modelValue": a[0] || (a[0] = (t) => u.value = t),
186
+ filterable: e.filter,
187
+ disabled: e.disabled,
188
+ placeholder: e.placeholder,
189
+ clearable: e.clearable,
190
+ multiple: e.multiple,
191
+ loading: v.value,
192
+ onClear: R
193
+ }, {
194
+ default: G(() => [
195
+ (B(!0), H(P, null, Q(i.value, (t) => (B(), D(L(X), {
196
+ key: t[e.optionValue],
197
+ label: t[e.optionLabel],
198
+ value: t[e.optionValue],
199
+ disabled: t[e.optionDisabledField] === e.optionDisabledValue
200
+ }, null, 8, ["label", "value", "disabled"]))), 128))
201
+ ]),
202
+ _: 1
203
+ }, 8, ["modelValue", "filterable", "disabled", "placeholder", "clearable", "multiple", "loading"]));
204
+ }
205
+ }), _ = I;
206
+ _.install = (n) => {
207
+ n.component("DictSelect", I);
208
+ };
209
+ export {
210
+ I as DictSelect,
211
+ _ as default
212
+ };
@@ -0,0 +1 @@
1
+ (function(d,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("vue"),require("element-plus")):typeof define=="function"&&define.amd?define(["exports","vue","element-plus"],t):(d=typeof globalThis<"u"?globalThis:d||self,t(d.DictSelect={},d.Vue,d.ElementPlus))})(this,function(d,t,B){"use strict";const b=new Map,x=()=>b,L=r=>{if(r){b.delete(r);return}b.clear()},g=t.defineComponent({__name:"DictSelect",props:t.mergeModels({placeholder:{default:"全部"},disabled:{type:Boolean,default:!1},optionLabel:{default:"k"},optionValue:{default:"v"},optionDisabledField:{default:"text"},optionDisabledValue:{type:[String,Number,Boolean],default:"disable"},filter:{type:Boolean,default:!1},clearable:{type:Boolean,default:!0},vModelToNumber:{type:Boolean,default:!1},vModelToString:{type:Boolean,default:!1},isBuffer:{type:Boolean,default:!0},isLabel:{type:Boolean,default:!1},api:{},mock:{default:()=>[]},multiple:{type:Boolean,default:!1},param:{default:()=>({})},callApiIfNeeded:{type:Boolean,default:!0},cacheKey:{default:""},transform:{type:Function,default:void 0},immediate:{type:Boolean,default:!0},autoClearInvalidValue:{type:Boolean,default:!0}},{modelValue:{},modelModifiers:{},options:{},optionsModifiers:{},response:{},responseModifiers:{},loading:{type:Boolean},loadingModifiers:{},selectedOption:{},selectedOptionModifiers:{}}),emits:t.mergeModels(["label","allData","options","loaded","error","onClear"],["update:modelValue","update:options","update:response","update:loading","update:selectedOption"]),setup(r,{expose:N,emit:T}){const e=r,u=t.useModel(r,"modelValue"),k=t.useModel(r,"options"),E=t.useModel(r,"response"),I=t.useModel(r,"loading"),S=t.useModel(r,"selectedOption"),n=t.ref([]),m=t.ref(!1),s=t.ref(),h=x();let v=0;const f=T,A=l=>{var a,o,i;return((a=l==null?void 0:l.result)==null?void 0:a.list)||((o=l==null?void 0:l.result)==null?void 0:o.data)||((i=l==null?void 0:l.result)==null?void 0:i.result)||(l==null?void 0:l.result)||(l==null?void 0:l.data)||l||[]},j=()=>!e.param||Object.keys(e.param).length===0?!0:Object.values(e.param).every(l=>l!=null&&l!==""),C=()=>{var a;return`${e.cacheKey||((a=e.api)==null?void 0:a.name)||"dict-select"}:${JSON.stringify(e.param||{})}`},p=()=>e.multiple?Array.isArray(u.value)?n.value.filter(l=>u.value.includes(l[e.optionValue])):[]:n.value.find(l=>l[e.optionValue]===u.value),c=()=>{k.value=n.value,E.value=s.value,I.value=m.value,S.value=p()},K=l=>{e.vModelToNumber&&l.forEach(a=>{a[e.optionValue]=Number(a[e.optionValue])}),e.vModelToString&&l.forEach(a=>{a[e.optionValue]=String(a[e.optionValue])})},q=()=>{if(!e.autoClearInvalidValue)return;if(e.multiple){if(Array.isArray(u.value)&&u.value.length>0){const a=u.value.filter(o=>n.value.some(i=>i[e.optionValue]===o));a.length!==u.value.length&&(u.value=a)}return}n.value.findIndex(a=>a[e.optionValue]===u.value)===-1&&(u.value=void 0)},F=()=>{f("onClear")},y=(l,a,o)=>{n.value=l,s.value=a,K(n.value),q(),c(),f("loaded",{options:n.value,response:s.value,fromCache:o})},V=async(l=!1)=>{if(e.mock.length>0){y(e.mock,e.mock,!1);return}if(!e.api){n.value=[],s.value=void 0,c();return}if(!e.callApiIfNeeded||!j()){n.value=[],s.value=void 0,c();return}const a=C();if(!l&&e.isBuffer&&h.has(a)){const i=h.get(a);y((i==null?void 0:i.options)||[],i==null?void 0:i.response,!0);return}const o=++v;m.value=!0,c();try{const i=await e.api(e.param);if(o!==v)return;const D=e.transform?e.transform(A(i),i):A(i),w=Array.isArray(D)?D:[];e.isBuffer&&h.set(a,{options:w,response:i}),y(w,i,!1)}catch(i){if(o!==v)return;f("error",i),n.value=[],s.value=void 0,c()}finally{o===v&&(m.value=!1,c())}},J=async(l=!0)=>{await V(l)},R=()=>{L(C())},M=t.computed(()=>{if(e.multiple){const a=p();return Array.isArray(a)?a.map(o=>o[e.optionLabel]):[]}const l=p();return!l||Array.isArray(l)?"":l[e.optionLabel]??""});return t.watch(()=>n.value,()=>{f("options",n.value),k.value=n.value}),t.watch(()=>e.mock,()=>{if(e.mock.length>0){y(e.mock,e.mock,!1);return}e.api||(n.value=[],s.value=void 0,c())}),t.watch(()=>e.api,()=>{e.immediate&&V()},{immediate:e.immediate}),t.watch(()=>e.param,(l,a)=>{JSON.stringify(l)!==JSON.stringify(a)&&V()},{deep:!0}),t.watch(()=>[u.value,n.value],()=>{const l=p();S.value=l,e.isLabel&&f("label",Array.isArray(M.value)?M.value.join(","):M.value),f("allData",l)},{immediate:!0,deep:!0}),N({refresh:J,clearCache:R,getOptions:()=>n.value,getResponse:()=>s.value,getSelectedOption:p}),(l,a)=>(t.openBlock(),t.createBlock(t.unref(B.ElSelect),{modelValue:u.value,"onUpdate:modelValue":a[0]||(a[0]=o=>u.value=o),filterable:e.filter,disabled:e.disabled,placeholder:e.placeholder,clearable:e.clearable,multiple:e.multiple,loading:m.value,onClear:F},{default:t.withCtx(()=>[(t.openBlock(!0),t.createElementBlock(t.Fragment,null,t.renderList(n.value,o=>(t.openBlock(),t.createBlock(t.unref(B.ElOption),{key:o[e.optionValue],label:o[e.optionLabel],value:o[e.optionValue],disabled:o[e.optionDisabledField]===e.optionDisabledValue},null,8,["label","value","disabled"]))),128))]),_:1},8,["modelValue","filterable","disabled","placeholder","clearable","multiple","loading"]))}}),O=g;O.install=r=>{r.component("DictSelect",g)},d.DictSelect=g,d.default=O,Object.defineProperties(d,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
@@ -0,0 +1,92 @@
1
+ import type { ApiFunction, OptionItem, TransformFunction } from './types';
2
+ type __VLS_Props = {
3
+ placeholder?: string;
4
+ disabled?: boolean;
5
+ optionLabel?: string;
6
+ optionValue?: string;
7
+ optionDisabledField?: string;
8
+ optionDisabledValue?: string | number | boolean;
9
+ filter?: boolean;
10
+ clearable?: boolean;
11
+ vModelToNumber?: boolean;
12
+ vModelToString?: boolean;
13
+ isBuffer?: boolean;
14
+ isLabel?: boolean;
15
+ api?: ApiFunction;
16
+ mock?: OptionItem[];
17
+ multiple?: boolean;
18
+ param?: Record<string, any>;
19
+ callApiIfNeeded?: boolean;
20
+ cacheKey?: string;
21
+ transform?: TransformFunction;
22
+ immediate?: boolean;
23
+ autoClearInvalidValue?: boolean;
24
+ };
25
+ type __VLS_PublicProps = __VLS_Props & {
26
+ modelValue?: any;
27
+ 'options'?: OptionItem[] | undefined;
28
+ 'response'?: any;
29
+ 'loading'?: boolean | undefined;
30
+ 'selectedOption'?: OptionItem | OptionItem[] | undefined;
31
+ };
32
+ declare const _default: import("vue").DefineComponent<__VLS_PublicProps, {
33
+ refresh: (force?: boolean) => Promise<void>;
34
+ clearCache: () => void;
35
+ getOptions: () => OptionItem[];
36
+ getResponse: () => any;
37
+ getSelectedOption: () => OptionItem | OptionItem[] | undefined;
38
+ }, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
39
+ options: (args_0: OptionItem[]) => any;
40
+ label: (args_0: string) => any;
41
+ allData: (args_0: OptionItem | OptionItem[] | undefined) => any;
42
+ loaded: (args_0: {
43
+ options: OptionItem[];
44
+ response: any;
45
+ fromCache: boolean;
46
+ }) => any;
47
+ error: (args_0: unknown) => any;
48
+ onClear: () => any;
49
+ "update:modelValue": (value: any) => any;
50
+ "update:options": (value: OptionItem[] | undefined) => any;
51
+ "update:response": (value: any) => any;
52
+ "update:loading": (value: boolean | undefined) => any;
53
+ "update:selectedOption": (value: OptionItem | OptionItem[] | undefined) => any;
54
+ }, string, import("vue").PublicProps, Readonly<__VLS_PublicProps> & Readonly<{
55
+ onOptions?: ((args_0: OptionItem[]) => any) | undefined;
56
+ onLabel?: ((args_0: string) => any) | undefined;
57
+ onAllData?: ((args_0: OptionItem | OptionItem[] | undefined) => any) | undefined;
58
+ onLoaded?: ((args_0: {
59
+ options: OptionItem[];
60
+ response: any;
61
+ fromCache: boolean;
62
+ }) => any) | undefined;
63
+ onError?: ((args_0: unknown) => any) | undefined;
64
+ onOnClear?: (() => any) | undefined;
65
+ "onUpdate:modelValue"?: ((value: any) => any) | undefined;
66
+ "onUpdate:options"?: ((value: OptionItem[] | undefined) => any) | undefined;
67
+ "onUpdate:response"?: ((value: any) => any) | undefined;
68
+ "onUpdate:loading"?: ((value: boolean | undefined) => any) | undefined;
69
+ "onUpdate:selectedOption"?: ((value: OptionItem | OptionItem[] | undefined) => any) | undefined;
70
+ }>, {
71
+ placeholder: string;
72
+ disabled: boolean;
73
+ optionLabel: string;
74
+ optionValue: string;
75
+ optionDisabledField: string;
76
+ optionDisabledValue: string | number | boolean;
77
+ filter: boolean;
78
+ clearable: boolean;
79
+ vModelToNumber: boolean;
80
+ vModelToString: boolean;
81
+ isBuffer: boolean;
82
+ isLabel: boolean;
83
+ mock: OptionItem[];
84
+ multiple: boolean;
85
+ param: Record<string, any>;
86
+ callApiIfNeeded: boolean;
87
+ cacheKey: string;
88
+ transform: TransformFunction;
89
+ immediate: boolean;
90
+ autoClearInvalidValue: boolean;
91
+ }, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
92
+ export default _default;
@@ -0,0 +1,7 @@
1
+ export type OptionItem = Record<string, any>;
2
+ export interface CacheEntry {
3
+ options: OptionItem[];
4
+ response: any;
5
+ }
6
+ export declare const getOptionCache: () => Map<string, CacheEntry>;
7
+ export declare const clearOptionCache: (key?: string) => void;
@@ -0,0 +1,10 @@
1
+ import type { App } from 'vue';
2
+ import DictSelect from './DictSelect.vue';
3
+ import type { DictSelectExpose, OptionItem } from './types';
4
+ type DictSelectPlugin = typeof DictSelect & {
5
+ install: (app: App) => void;
6
+ };
7
+ declare const plugin: DictSelectPlugin;
8
+ export default plugin;
9
+ export { DictSelect };
10
+ export type { DictSelectExpose, OptionItem };
@@ -0,0 +1,10 @@
1
+ export type OptionItem = Record<string, any>;
2
+ export type ApiFunction = (params?: Record<string, any>) => Promise<any>;
3
+ export type TransformFunction = (raw: any, response: any) => OptionItem[];
4
+ export interface DictSelectExpose {
5
+ refresh: (force?: boolean) => Promise<void>;
6
+ clearCache: () => void;
7
+ getOptions: () => OptionItem[];
8
+ getResponse: () => any;
9
+ getSelectedOption: () => OptionItem | OptionItem[] | undefined;
10
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-dict-select",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "A reusable select component that loads options from an API automatically.",
5
5
  "type": "module",
6
6
  "main": "dist/index.umd.cjs",