@qin-ui/element-plus-pro 1.0.0

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/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # @qin-ui/element-plus-pro
2
+
3
+ 基于 Element Plus 的 Pro 组件库,提供:
4
+
5
+ - `ProForm`
6
+ - `ProTable`
7
+ - `ProComponentProvider`
8
+ - `useForm`
9
+ - `useTable`
10
+
11
+ ## 安装
12
+
13
+ ```bash
14
+ pnpm add @qin-ui/element-plus-pro element-plus
15
+ ```
16
+
17
+ ## 快速使用
18
+
19
+ ```ts
20
+ import { createApp } from 'vue';
21
+ import ElementPlus from 'element-plus';
22
+ import 'element-plus/dist/index.css';
23
+ import App from './App.vue';
24
+
25
+ const app = createApp(App);
26
+ app.use(ElementPlus);
27
+ app.mount('#app');
28
+ ```
29
+
30
+ ```vue
31
+ <script setup lang="ts">
32
+ import { ProTable, useTable } from '@qin-ui/element-plus-pro';
33
+
34
+ const table = useTable({
35
+ columns: [
36
+ { prop: 'name', label: '姓名' },
37
+ { prop: 'age', label: '年龄' },
38
+ ],
39
+ });
40
+ </script>
41
+
42
+ <template>
43
+ <ProTable :table="table" />
44
+ </template>
45
+ ```
46
+
47
+ ## ProTable columns
48
+
49
+ `columns` 采用 Element Plus 列协议为主,支持:
50
+
51
+ - 原生列字段:`prop`、`label`、`width`、`fixed`、`align`、`sortable`、`filters`、`filterMethod`、`children`
52
+ - 原生 `formatter`
53
+ - Pro 扩展:`render(scope)`、`hidden`
54
+
55
+ 渲染优先级:`render(scope)` > `formatter`。
56
+
57
+ 数据源语义统一为 `data`,不再对外支持 `dataSource`。
58
+
59
+ ## ProForm 组件键名
60
+
61
+ `component` 使用 Element 风格键名,例如:
62
+
63
+ - `input`(可配合 `type: 'textarea' | 'password'`)
64
+ - `input-number`
65
+ - `autocomplete`
66
+ - `select` / `cascader`
67
+ - `date-picker`(可配合 `type: 'date' | 'dates' | 'daterange' | 'datetimerange'` 等)
68
+ - `time-picker` / `time-select`
69
+ - `checkbox-group` / `radio-group`
70
+ - `switch` / `slider` / `tree-select` / `transfer`
71
+
72
+ 默认 `modelProp` 为 `modelValue`。
73
+
74
+ ## Peer Dependencies
75
+
76
+ - `vue` `^3.5.0`
77
+ - `element-plus` `^2.13.6`
@@ -0,0 +1,220 @@
1
+ import { defineComponent, provide, renderSlot } from "vue";
2
+ import { g as getObject } from "../core/index-VQxpXgLX.js";
3
+ const INJECT_CONFIG = {
4
+ "pro-table": {
5
+ injectionKey: Symbol(""),
6
+ default: {
7
+ pagination: {
8
+ layout: "total, sizes, prev, pager, next, jumper",
9
+ pageSizes: [10, 20, 30, 40, 50, 100],
10
+ background: true
11
+ },
12
+ searchFormConfig: {
13
+ layout: "grid",
14
+ expand: { minExpandRows: 2, expandStatus: false }
15
+ },
16
+ control: true,
17
+ addIndexColumn: true
18
+ }
19
+ },
20
+ "pro-form": {
21
+ injectionKey: Symbol(""),
22
+ default: { grid: { gutter: 24 } }
23
+ },
24
+ "pro-form-item": {
25
+ injectionKey: Symbol(""),
26
+ default: { span: 8 }
27
+ },
28
+ input: {
29
+ injectionKey: Symbol(""),
30
+ default: { maxlength: 100, clearable: true, placeholder: "请输入" }
31
+ },
32
+ "input.textarea": {
33
+ injectionKey: Symbol(""),
34
+ default: {
35
+ maxlength: 200,
36
+ autosize: { minRows: 3, maxRows: 6 },
37
+ showWordLimit: true,
38
+ clearable: true,
39
+ placeholder: "请输入"
40
+ }
41
+ },
42
+ "input.password": {
43
+ injectionKey: Symbol(""),
44
+ default: {
45
+ showPassword: true,
46
+ maxlength: 100,
47
+ clearable: true,
48
+ placeholder: "请输入"
49
+ }
50
+ },
51
+ "input-number": {
52
+ injectionKey: Symbol(""),
53
+ default: {
54
+ max: 10 ** 15 - 1,
55
+ min: -1000000000000001,
56
+ controls: false,
57
+ placeholder: "请输入",
58
+ style: { width: "100%" }
59
+ }
60
+ },
61
+ autocomplete: {
62
+ injectionKey: Symbol(""),
63
+ default: { clearable: true, placeholder: "请输入" }
64
+ },
65
+ select: {
66
+ injectionKey: Symbol(""),
67
+ default: { clearable: true, placeholder: "请选择" }
68
+ },
69
+ cascader: {
70
+ injectionKey: Symbol(""),
71
+ default: { clearable: true, placeholder: "请选择" }
72
+ },
73
+ "date-picker": {
74
+ injectionKey: Symbol(""),
75
+ default: { style: { width: "100%" }, placeholder: "请选择" }
76
+ },
77
+ "date-picker.date": {
78
+ injectionKey: Symbol(""),
79
+ default: { style: { width: "100%" }, placeholder: "请选择" }
80
+ },
81
+ "date-picker.dates": {
82
+ injectionKey: Symbol(""),
83
+ default: { style: { width: "100%" }, placeholder: "请选择" }
84
+ },
85
+ "date-picker.daterange": {
86
+ injectionKey: Symbol(""),
87
+ default: {
88
+ style: { width: "100%" },
89
+ startPlaceholder: "开始日期",
90
+ endPlaceholder: "结束日期"
91
+ }
92
+ },
93
+ "date-picker.week": {
94
+ injectionKey: Symbol(""),
95
+ default: { style: { width: "100%" }, placeholder: "请选择" }
96
+ },
97
+ "date-picker.month": {
98
+ injectionKey: Symbol(""),
99
+ default: { style: { width: "100%" }, placeholder: "请选择" }
100
+ },
101
+ "date-picker.monthrange": {
102
+ injectionKey: Symbol(""),
103
+ default: {
104
+ style: { width: "100%" },
105
+ startPlaceholder: "开始月份",
106
+ endPlaceholder: "结束月份"
107
+ }
108
+ },
109
+ "date-picker.months": {
110
+ injectionKey: Symbol(""),
111
+ default: { style: { width: "100%" }, placeholder: "请选择" }
112
+ },
113
+ "date-picker.yearrange": {
114
+ injectionKey: Symbol(""),
115
+ default: {
116
+ style: { width: "100%" },
117
+ startPlaceholder: "开始年份",
118
+ endPlaceholder: "结束年份"
119
+ }
120
+ },
121
+ "date-picker.year": {
122
+ injectionKey: Symbol(""),
123
+ default: { style: { width: "100%" }, placeholder: "请选择" }
124
+ },
125
+ "date-picker.years": {
126
+ injectionKey: Symbol(""),
127
+ default: { style: { width: "100%" }, placeholder: "请选择" }
128
+ },
129
+ "date-picker.datetime": {
130
+ injectionKey: Symbol(""),
131
+ default: { style: { width: "100%" }, placeholder: "请选择" }
132
+ },
133
+ "date-picker.datetimerange": {
134
+ injectionKey: Symbol(""),
135
+ default: {
136
+ style: { width: "100%" },
137
+ startPlaceholder: "开始时间",
138
+ endPlaceholder: "结束时间"
139
+ }
140
+ },
141
+ "time-picker": {
142
+ injectionKey: Symbol(""),
143
+ default: { style: { width: "100%" }, placeholder: "请选择" }
144
+ },
145
+ "time-select": {
146
+ injectionKey: Symbol(""),
147
+ default: { style: { width: "100%" }, placeholder: "请选择" }
148
+ },
149
+ "checkbox-group": {
150
+ injectionKey: Symbol(""),
151
+ default: {}
152
+ },
153
+ "radio-group": {
154
+ injectionKey: Symbol(""),
155
+ default: {}
156
+ },
157
+ switch: {
158
+ injectionKey: Symbol(""),
159
+ default: { modelProp: "modelValue" }
160
+ },
161
+ slider: {
162
+ injectionKey: Symbol(""),
163
+ default: {}
164
+ },
165
+ "tree-select": {
166
+ injectionKey: Symbol(""),
167
+ default: { clearable: true, placeholder: "请选择" }
168
+ },
169
+ transfer: {
170
+ injectionKey: Symbol(""),
171
+ default: {}
172
+ }
173
+ };
174
+ const INJECT_COMPONENTS = Symbol("INJECT_COMPONENTS");
175
+ const DYNAMIC_INJECT_CONFIG = /* @__PURE__ */ Object.create(null);
176
+ const getInjectConfig = (key) => {
177
+ return INJECT_CONFIG[key] || DYNAMIC_INJECT_CONFIG[key];
178
+ };
179
+ const ensureInjectConfig = (key) => {
180
+ const existing = getInjectConfig(key);
181
+ if (existing) return existing;
182
+ const created = {
183
+ injectionKey: Symbol(`dynamic:${key}`),
184
+ default: {}
185
+ };
186
+ DYNAMIC_INJECT_CONFIG[key] = created;
187
+ return created;
188
+ };
189
+ const _sfc_main = /* @__PURE__ */ defineComponent({
190
+ ...{
191
+ inheritAttrs: false
192
+ },
193
+ __name: "index",
194
+ props: {
195
+ componentVars: {},
196
+ componentMap: {}
197
+ },
198
+ setup(__props) {
199
+ const props = __props;
200
+ if (props.componentVars) {
201
+ Object.entries(props.componentVars).forEach(([key, val]) => {
202
+ const config = ensureInjectConfig(key);
203
+ provide(config.injectionKey, { ...config.default, ...getObject(val) });
204
+ });
205
+ }
206
+ if (props.componentMap) {
207
+ provide(INJECT_COMPONENTS, props.componentMap);
208
+ }
209
+ return (_ctx, _cache) => {
210
+ return renderSlot(_ctx.$slots, "default");
211
+ };
212
+ }
213
+ });
214
+ export {
215
+ INJECT_COMPONENTS,
216
+ INJECT_CONFIG,
217
+ _sfc_main as default,
218
+ ensureInjectConfig,
219
+ getInjectConfig
220
+ };
@@ -0,0 +1,362 @@
1
+ import { inject, camelize, reactive, provide, ref, toValue } from "vue";
2
+ import { i as isPlainObject, s as set, g as get, c as cloneDeep, t as toPath, p as pick } from "../vendor/utils/lodash-es-p6jau26B.js";
3
+ const InjectionFormKey = Symbol("form");
4
+ const InjectionPathKey = Symbol("path");
5
+ function getObject(val) {
6
+ return isPlainObject(val) ? val : {};
7
+ }
8
+ function camelizeProperties(obj) {
9
+ return Object.fromEntries(
10
+ Object.entries(obj).map(([key, value]) => [camelize(key), value])
11
+ );
12
+ }
13
+ const useFields = (initFields) => {
14
+ const fields = ref([]);
15
+ fields.value = initFields || [];
16
+ const _map = /* @__PURE__ */ new Map();
17
+ const cacheMatch = (pathStr, updater) => {
18
+ var _a;
19
+ const fieldPath = _map.get(pathStr) || [];
20
+ if (fieldPath.length === 0) return false;
21
+ const fieldIndex = fieldPath[fieldPath.length - 1];
22
+ const parentField = fieldPath.length === 1 ? fields.value : get(fields.value, fieldPath.slice(0, -2));
23
+ const field = (_a = parentField == null ? void 0 : parentField.fields) == null ? void 0 : _a[fieldIndex];
24
+ const _path = toPath((field == null ? void 0 : field.name) ?? (field == null ? void 0 : field.path)).join(".");
25
+ if (_path === pathStr) {
26
+ updater({ field, fieldIndex, parentField });
27
+ return true;
28
+ }
29
+ return false;
30
+ };
31
+ const updaterMatch = (path, updater, options = {}) => {
32
+ if (!path) return;
33
+ const pathStr = typeof path === "function" ? "" : toPath(path).join(".");
34
+ const { all = typeof path === "function" } = options;
35
+ if (pathStr && _map.has(pathStr) && !all) {
36
+ const bool = cacheMatch(pathStr, updater);
37
+ if (bool) return;
38
+ }
39
+ const queue = fields.value.map((field, i) => ({
40
+ field,
41
+ fieldPath: [i],
42
+ parentField: { fields: fields.value }
43
+ }));
44
+ while (queue.length) {
45
+ const { field, fieldPath, parentField } = queue.shift();
46
+ let matched = false;
47
+ const _path = toPath(field.name ?? field.path).join(".");
48
+ if (_path) _map.set(_path, fieldPath);
49
+ if (typeof path === "function") {
50
+ matched = path(field);
51
+ } else if (_path) {
52
+ matched = pathStr === _path;
53
+ }
54
+ if (matched) {
55
+ const fieldIndex = fieldPath[fieldPath.length - 1];
56
+ updater({ field, fieldIndex, parentField });
57
+ if (!all) return;
58
+ }
59
+ if (Array.isArray(field.fields)) {
60
+ field.fields.forEach((f, i) => {
61
+ queue.push({
62
+ field: f,
63
+ fieldPath: [...fieldPath, "fields", i],
64
+ parentField: field
65
+ });
66
+ });
67
+ }
68
+ }
69
+ };
70
+ function getField(path, options) {
71
+ if (!path) return void 0;
72
+ const res = [];
73
+ updaterMatch(
74
+ path,
75
+ ({ field }) => {
76
+ res.push(field);
77
+ },
78
+ options
79
+ );
80
+ return (options == null ? void 0 : options.all) ? res : res[0];
81
+ }
82
+ function setField(path, field, options) {
83
+ if (!path) return;
84
+ const { updateType = "merge", ...rest } = options || {};
85
+ updaterMatch(
86
+ path,
87
+ ({ field: preField, fieldIndex, parentField }) => {
88
+ let value = field;
89
+ if (typeof field === "function") {
90
+ value = field(preField);
91
+ }
92
+ if (!value) return;
93
+ if (updateType === "rewrite") {
94
+ parentField.fields[fieldIndex] = value;
95
+ } else {
96
+ Object.assign(preField, value);
97
+ }
98
+ },
99
+ rest
100
+ );
101
+ }
102
+ function deleteField(path, options) {
103
+ if (!path) return;
104
+ updaterMatch(
105
+ path,
106
+ ({ fieldIndex, parentField }) => {
107
+ parentField.fields.splice(fieldIndex, 1);
108
+ },
109
+ options
110
+ );
111
+ }
112
+ function addFields(path, newFields, options, placement) {
113
+ if (newFields.length === 0) return;
114
+ if (path) {
115
+ updaterMatch(
116
+ path,
117
+ ({ fieldIndex, parentField }) => {
118
+ const index = placement === "after" ? fieldIndex + 1 : fieldIndex;
119
+ parentField.fields.splice(index, 0, ...newFields);
120
+ },
121
+ options
122
+ );
123
+ } else if (placement === "after") {
124
+ fields.value.push(...newFields);
125
+ } else {
126
+ fields.value.unshift(...newFields);
127
+ }
128
+ }
129
+ function appendField(path, field, options) {
130
+ const newFields = Array.isArray(field) ? field : [field];
131
+ addFields(path, newFields, options, "after");
132
+ }
133
+ function prependField(path, field, options) {
134
+ const newFields = Array.isArray(field) ? field : [field];
135
+ addFields(path, newFields, options, "before");
136
+ }
137
+ function getParentField(path, options) {
138
+ if (!path) return void 0;
139
+ const res = [];
140
+ updaterMatch(
141
+ path,
142
+ ({ parentField }) => {
143
+ res.push(parentField);
144
+ },
145
+ options
146
+ );
147
+ return (options == null ? void 0 : options.all) ? res : res[0];
148
+ }
149
+ return {
150
+ fields,
151
+ getField,
152
+ setField,
153
+ deleteField,
154
+ appendField,
155
+ prependField,
156
+ getParentField
157
+ };
158
+ };
159
+ const InjectionFormDataKey = Symbol("form-data");
160
+ const useFormData = (initFormData) => {
161
+ if (!initFormData) {
162
+ const injectFormDataStore = inject(InjectionFormDataKey, void 0);
163
+ if (injectFormDataStore) return injectFormDataStore;
164
+ }
165
+ const formData = reactive(initFormData ?? {});
166
+ function getFormData(path) {
167
+ if (!path) return void 0;
168
+ return get(formData, path);
169
+ }
170
+ function setFormData(...args) {
171
+ let path;
172
+ let value;
173
+ if (args.length >= 2) {
174
+ [path, value] = args;
175
+ if (!path) return;
176
+ } else {
177
+ [value] = args;
178
+ }
179
+ if (path) {
180
+ if (typeof value === "function") {
181
+ const preValue = getFormData(path);
182
+ value = value(preValue);
183
+ }
184
+ set(formData, path, value);
185
+ } else {
186
+ if (typeof value === "function") {
187
+ const preValue = formData;
188
+ value = value(preValue);
189
+ }
190
+ if (!isPlainObject(value)) return;
191
+ Object.keys(formData).forEach((key) => {
192
+ delete formData[key];
193
+ });
194
+ Object.assign(formData, value);
195
+ }
196
+ }
197
+ const formDataStore = { formData, getFormData, setFormData };
198
+ provide(InjectionFormDataKey, formDataStore);
199
+ return formDataStore;
200
+ };
201
+ const useFormRef = () => {
202
+ const formRef = ref();
203
+ const setFormRef = (inst) => {
204
+ formRef.value = inst;
205
+ };
206
+ return { formRef, setFormRef };
207
+ };
208
+ function useForm(...args) {
209
+ let initFormData = {}, initFields = [], root = true;
210
+ if (args.length === 1) {
211
+ root = args[0];
212
+ } else if (args.length >= 2) {
213
+ initFormData = args[0] ?? {};
214
+ initFields = args[1];
215
+ root = args[2] ?? root;
216
+ }
217
+ if (!root) {
218
+ const injectForm = inject(InjectionFormKey);
219
+ if (injectForm) return injectForm;
220
+ }
221
+ return {
222
+ ...useFormData(initFormData),
223
+ ...useFields(initFields),
224
+ ...useFormRef()
225
+ };
226
+ }
227
+ const getDefaultPageParam = () => ({
228
+ current: 1,
229
+ pageSize: 10,
230
+ total: 0
231
+ });
232
+ const pageParamProperty = ["current", "pageSize", "total"];
233
+ const useTable = (params) => {
234
+ const {
235
+ columns: initColumns = [],
236
+ dataSource: initDataSource = [],
237
+ pageParam: initPageParam = getDefaultPageParam(),
238
+ searchParam: initSearchParam = {},
239
+ searchFields: initSearchFields = []
240
+ } = params;
241
+ const _initSearchParam = cloneDeep(toValue(initSearchParam));
242
+ const columns = ref([]);
243
+ columns.value = initColumns;
244
+ const dataSource = ref(initDataSource);
245
+ const pageParam = reactive(initPageParam);
246
+ const setPageParam = (pa) => {
247
+ let newPageParam = pa;
248
+ if (typeof pa === "function") {
249
+ newPageParam = pa(pageParam);
250
+ }
251
+ Object.assign(pageParam, pick(newPageParam, pageParamProperty));
252
+ };
253
+ const searchForm = useForm(initSearchParam, initSearchFields, true);
254
+ const resetQueryParams = () => {
255
+ Object.assign(pageParam, getDefaultPageParam());
256
+ searchForm.setFormData(cloneDeep(_initSearchParam));
257
+ };
258
+ const updaterMatch = (key, updater, options = {}) => {
259
+ if (!key) return;
260
+ const { all = typeof key === "function" } = options;
261
+ for (let i = 0; i < columns.value.length; i++) {
262
+ const columnItem = columns.value[i];
263
+ let matched = false;
264
+ if (typeof key === "function") {
265
+ matched = key(columnItem);
266
+ } else if (columnItem.key) {
267
+ matched = columnItem.key === key;
268
+ } else if (columnItem.dataIndex) {
269
+ const dataIndexKey = Array.isArray(columnItem.dataIndex) ? columnItem.dataIndex.join(".") : columnItem.dataIndex;
270
+ matched = dataIndexKey === key;
271
+ } else {
272
+ matched = key === i;
273
+ }
274
+ if (matched) {
275
+ updater({
276
+ column: columnItem,
277
+ columnIndex: i,
278
+ group: columns.value
279
+ });
280
+ if (!all) return;
281
+ }
282
+ }
283
+ };
284
+ const setColumn = (key, column, options) => {
285
+ if (!key || !column) return;
286
+ const { updateType = "merge", ...rest } = options || {};
287
+ updaterMatch(
288
+ key,
289
+ ({ column: preColumn, columnIndex, group }) => {
290
+ let value = column;
291
+ if (typeof column === "function") {
292
+ value = column(preColumn);
293
+ }
294
+ if (!value) return;
295
+ if (updateType === "rewrite") {
296
+ group[columnIndex] = value;
297
+ } else {
298
+ Object.assign(preColumn, value);
299
+ }
300
+ },
301
+ rest
302
+ );
303
+ };
304
+ function deleteColumn(path, options) {
305
+ if (!path) return;
306
+ updaterMatch(
307
+ path,
308
+ ({ columnIndex, group }) => {
309
+ group.splice(columnIndex, 1);
310
+ },
311
+ options
312
+ );
313
+ }
314
+ function addColumns(path, newColumns, options, placement) {
315
+ if (newColumns.length === 0) return;
316
+ if (path) {
317
+ updaterMatch(
318
+ path,
319
+ ({ columnIndex, group }) => {
320
+ const index = placement === "after" ? columnIndex + 1 : columnIndex;
321
+ group.splice(index, 0, ...newColumns);
322
+ },
323
+ options
324
+ );
325
+ } else if (placement === "after") {
326
+ columns.value.push(...newColumns);
327
+ } else {
328
+ columns.value.unshift(...newColumns);
329
+ }
330
+ }
331
+ function appendColumn(path, column, options) {
332
+ const newColumns = Array.isArray(column) ? column : [column];
333
+ addColumns(path, newColumns, options, "after");
334
+ }
335
+ function prependColumn(path, column, options) {
336
+ const newColumns = Array.isArray(column) ? column : [column];
337
+ addColumns(path, newColumns, options, "before");
338
+ }
339
+ return {
340
+ columns,
341
+ dataSource,
342
+ pageParam,
343
+ searchForm,
344
+ setColumn,
345
+ deleteColumn,
346
+ appendColumn,
347
+ prependColumn,
348
+ setPageParam,
349
+ resetQueryParams
350
+ };
351
+ };
352
+ export {
353
+ InjectionFormKey as I,
354
+ InjectionPathKey as a,
355
+ useFields as b,
356
+ camelizeProperties as c,
357
+ useFormRef as d,
358
+ useFormData as e,
359
+ useTable as f,
360
+ getObject as g,
361
+ useForm as u
362
+ };