general-basic-form 2.0.62 → 2.0.63

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.
Files changed (35) hide show
  1. package/README.assets/image-20210820173738506.png +0 -0
  2. package/README.md +15 -2
  3. package/package.json +8 -17
  4. package/script/link.ts +36 -0
  5. package/script/unlink.ts +45 -0
  6. package/src/Descriptions.vue +107 -0
  7. package/src/GeneralBasicForm.vue +240 -0
  8. package/src/InfiniteScrollList.vue +163 -0
  9. package/src/assets/image-20210820173037871.png +0 -0
  10. package/src/assets/logo.png +0 -0
  11. package/src/components/CustomCom/img-mask/index.vue +79 -0
  12. package/src/components/CustomCom/input-graphic-verification/index.vue +81 -0
  13. package/src/components/CustomCom/input-mobile-verification/index.vue +56 -0
  14. package/src/components/CustomCom/input-mobile-verification/verification-button.vue +82 -0
  15. package/src/components/VABasic/input/index.vue +75 -0
  16. package/src/components/VBasic/cascader/index.vue +32 -0
  17. package/src/components/VBasic/checkbox/index.vue +37 -0
  18. package/src/components/VBasic/date-picker/index.vue +31 -0
  19. package/src/components/VBasic/divider/index.vue +53 -0
  20. package/src/components/VBasic/input/index.vue +67 -0
  21. package/src/components/VBasic/input-number/index.vue +31 -0
  22. package/src/components/VBasic/radio/index.vue +37 -0
  23. package/src/components/VBasic/select/index.vue +37 -0
  24. package/src/components/setting.ts +28 -0
  25. package/src/index.ts +30 -0
  26. package/src/injectKey.ts +2 -0
  27. package/src/types/basicFrom.ts +63 -0
  28. package/src/types/componentType.ts +6 -0
  29. package/src/types/componentsProps.ts +18 -0
  30. package/tsconfig.json +14 -0
  31. package/vite.config.js +123 -0
  32. package/dist/index.js +0 -1028
  33. package/dist/index.umd.cjs +0 -1
  34. package/dist/style.css +0 -1
  35. /package/{dist → public}/index.d.ts +0 -0
package/README.md CHANGED
@@ -118,7 +118,7 @@ getList会传出默认的参数,首次请求时会有页数和分页大小,重
118
118
  const params = VGeneralBasicFormRef.value['queryParams']
119
119
  resolve(params)
120
120
  } else {
121
- resolve(false)
121
+ reject()
122
122
  }
123
123
  }
124
124
  )
@@ -127,17 +127,30 @@ getList会传出默认的参数,首次请求时会有页数和分页大小,重
127
127
 
128
128
  ![image-20211014191532067](https://raw.githubusercontent.com/Alan1034/PicturesServer/main/PicGo_imgs/202110141915657.png)
129
129
 
130
+ parametersType类型介绍
131
+
132
+ | parametersType形式 | 支持页面刷新 | 参数改变引起路由跳转 | 组件间共享数据 | 存储上限 | 浏览器兼容性 |
133
+ | ------------------ | ------------ | -------------------- | -------------- | -------- | ------------ |
134
+ | url | 是 | 是 | 是 | 中 | 高 |
135
+ | data | 否 | 否 | 否 | 高 | 高 |
136
+ | indexDB | 是 | 否 | 是 | 高 | 中 |
137
+
130
138
  数据示例:
131
139
 
132
140
  showSearch: true, // 显示搜索条件
133
141
  getList(); // 请求数据的函数
134
142
  afterReset(); // 在重置按钮点击完后但还没重新请求时触发的的函数
135
143
  formOnly:true // 只展示表单不展示按钮
136
- noUrlParameters:true // 不接受和不改变url的参数
144
+ parametersType:"url" // 见parametersType类型介绍
137
145
  loading:false // 加载动画
138
146
  formData:{} // 注意,因为可能出现的性能问题在组件watch formData的变化时没有使用deep,所以有时候深度的修改会不生效,导致表单数据不完整
139
147
  noInputBlank: true //校验input框不能仅输入空格
140
148
  //例子:formData.value.x=y ✘ | formData.value={...formData.value,x:y} ✔
149
+ currentPageKey:"page", //当前页数key
150
+ pageSizeKey:"limit", //每页显示个数key
151
+ defCurrentPage:1, //默认的页数
152
+ defPageSize:10, //默认的每页显示个数
153
+ queryWhenReady:false,//初始化完成后自动触发查找数据函数,建议用这个this.$refs["VGeneralBasicFormRef"].handleQuery({ defaultPageFirst: false })
141
154
  formItem: [
142
155
  {
143
156
  label: '',
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "general-basic-form",
3
- "version": "2.0.62",
3
+ "version": "2.0.63",
4
4
  "description": "",
5
- "main": "./dist/index.umd.cjs",
5
+ "main": "./src/index.ts",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "build": "cross-env CURRENT_ENV=prod run-p type-check build-only",
@@ -19,7 +19,9 @@
19
19
  "vue",
20
20
  "vue2",
21
21
  "vue3",
22
- "element-plus"
22
+ "element-plus",
23
+ "element-ui",
24
+ "Ant Design Vue"
23
25
  ],
24
26
  "author": "chendeli419287484@qq.com",
25
27
  "license": "ISC",
@@ -46,19 +48,8 @@
46
48
  "vue": ">=3.3.4",
47
49
  "vue-router": ">=4.2.4"
48
50
  },
49
- "typings": "./dist/index.d.ts",
51
+ "typings": "./publish/index.d.ts",
50
52
  "dependencies": {
51
- "network-spanner": "^1.2.8"
52
- },
53
- "module": "./dist/index.js",
54
- "exports": {
55
- ".": {
56
- "import": "./dist/index.js",
57
- "require": "./dist/index.umd.cjs"
58
- },
59
- "./style": "./dist/style.css"
60
- },
61
- "files": [
62
- "/dist"
63
- ]
53
+ "network-spanner": "^1.2.14"
54
+ }
64
55
  }
package/script/link.ts ADDED
@@ -0,0 +1,36 @@
1
+ /*
2
+ * @Author: 陈德立*******419287484@qq.com
3
+ * @Date: 2023-11-10 09:39:59
4
+ * @LastEditTime: 2023-11-15 18:04:19
5
+ * @LastEditors: 陈德立*******419287484@qq.com
6
+ * @Github: https://github.com/Alan1034
7
+ * @Description: link处理流程
8
+ * @FilePath: \GeneralBasicForm\script\link.ts
9
+ *
10
+ */
11
+ import path from "path";
12
+ import fs from 'fs'
13
+ import chalk from 'chalk'
14
+ import { fileURLToPath } from 'url'
15
+ const __filenameNew = fileURLToPath(import.meta.url)
16
+ const __dirnameNew = path.dirname(__filenameNew)
17
+ const updateMain = () => {
18
+ try {
19
+ const packageTxt = fs.readFileSync(path.join(__dirnameNew, '../package.json'), 'utf8');
20
+ const packageJson = JSON.parse(packageTxt);
21
+ const mainDir = "./src/index.ts"
22
+ delete packageJson.files
23
+ delete packageJson.module
24
+ delete packageJson.exports
25
+ packageJson.typings = "./publish/index.d.ts"
26
+ packageJson.main = mainDir
27
+ const versionData = JSON.stringify(packageJson, null, 2);
28
+ fs.writeFileSync(path.join(__dirnameNew, '../package.json'), versionData, 'utf8');
29
+ console.log(chalk.green.bold('修改入口配置成功!当前入口路径为为:' + mainDir));
30
+ } catch (error) {
31
+ console.log(chalk.red.bold('修改入口配置出错:', error.toString()));
32
+ }
33
+
34
+ }
35
+
36
+ updateMain()
@@ -0,0 +1,45 @@
1
+ /*
2
+ * @Author: 陈德立*******419287484@qq.com
3
+ * @Date: 2023-11-10 09:39:59
4
+ * @LastEditTime: 2023-11-16 15:09:53
5
+ * @LastEditors: 陈德立*******419287484@qq.com
6
+ * @Github: https://github.com/Alan1034
7
+ * @Description: unlink处理流程
8
+ * @FilePath: \GeneralBasicForm\script\unlink.ts
9
+ *
10
+ */
11
+ import path from "path";
12
+ import fs from 'fs'
13
+ import chalk from 'chalk'
14
+ import { fileURLToPath } from 'url'
15
+ const __filenameNew = fileURLToPath(import.meta.url)
16
+ const __dirnameNew = path.dirname(__filenameNew)
17
+ const updateMain = () => {
18
+ try {
19
+ const packageTxt = fs.readFileSync(path.join(__dirnameNew, '../package.json'), 'utf8');
20
+ const packageJson = JSON.parse(packageTxt);
21
+ const umdDir = "./dist/index.umd.cjs"
22
+ const esDir = "./dist/index.js"
23
+ packageJson.main = umdDir
24
+ packageJson.module = esDir
25
+ packageJson.exports = {
26
+ ".": {
27
+ "import": esDir,
28
+ "require": umdDir
29
+ },
30
+ "./style": "./dist/style.css"
31
+ }
32
+ packageJson.typings = "./dist/index.d.ts"
33
+ packageJson.files = [
34
+ "/dist"
35
+ ]
36
+ const versionData = JSON.stringify(packageJson, null, 2);
37
+ fs.writeFileSync(path.join(__dirnameNew, '../package.json'), versionData, 'utf8');
38
+ console.log(chalk.green.bold('修改入口配置成功!当前入口路径为为:' + umdDir));
39
+ } catch (error) {
40
+ console.log(chalk.red.bold('修改入口配置出错:', error.toString()));
41
+ }
42
+
43
+ }
44
+
45
+ updateMain()
@@ -0,0 +1,107 @@
1
+ <!--
2
+ * @Author: 陈德立*******419287484@qq.com
3
+ * @Date: 2023-12-08 17:45:01
4
+ * @LastEditTime: 2024-09-25 18:16:46
5
+ * @LastEditors: 陈德立*******419287484@qq.com
6
+ * @Github: https://github.com/Alan1034
7
+ * @Description: 对展示描述列表的封装
8
+ * @FilePath: \GeneralBasicForm\src\Descriptions.vue
9
+ *
10
+ -->
11
+ <template>
12
+ <component :is="descriptions" :column="1" border class="form-width" v-bind="$attrs">
13
+ <component :is="descriptionsItem" v-for="(item, i) in renderFormItem" :key="item.prop" :label="item.label"
14
+ v-bind="item.descriptionsItemProps">
15
+ <RenderComponent v-if="item.render" :i="i" :render="item.render" :formData="formData"></RenderComponent>
16
+ <span v-else>
17
+ {{ formData[item.prop] }}
18
+ </span>
19
+ </component>
20
+ </component>
21
+ </template>
22
+
23
+ <script lang="ts" setup>
24
+ import type { PropType, FunctionalComponent, VNode } from "vue";
25
+ import { shallowRef, watch, ref } from "vue";
26
+ import type { ComponentType } from "./types/componentType"
27
+ import type { ItemType } from "./types/basicFrom";
28
+ const props = defineProps({
29
+ formData: {
30
+ type: Object,
31
+ required: true,
32
+ },
33
+ formItem: {
34
+ type: Array as unknown as PropType<ItemType[]>,
35
+ required: true,
36
+ },
37
+ componentType: {
38
+ type: String as unknown as PropType<ComponentType>,
39
+ default: "Element Plus"
40
+ },
41
+ strict: {
42
+ // 使用strict参数后,如果formData内的某个字段没有值,对应的描述元素将不会展示(包括标签文字),但有render的字段仍然会展示
43
+ type: Boolean,
44
+ default: false
45
+ },
46
+ });
47
+ const renderFormItem = ref<ItemType[]>([])
48
+ watch(
49
+ () => [props.formData, props.formItem],
50
+ ([NewFormData = {}, NewFormItem = <any>[]]) => {
51
+ let PhasedFormItem = NewFormItem
52
+ if (props.strict) {
53
+ // strict严格模式过滤formItem
54
+ // console.log(NewFormData)
55
+ // console.log(PhasedFormItem)
56
+ for (const key in NewFormData) {
57
+ if (Object.prototype.hasOwnProperty.call(NewFormData, key)) {
58
+ const element = NewFormData[key];
59
+ if (!element) {
60
+ PhasedFormItem = PhasedFormItem.filter((item) => {
61
+ return ((item.prop !== key) || (item.render))
62
+ })
63
+ }
64
+ }
65
+ }
66
+
67
+
68
+ }
69
+ // console.log(PhasedFormItem)
70
+ renderFormItem.value = PhasedFormItem
71
+ },
72
+ { immediate: true }
73
+ );
74
+ const descriptions = shallowRef("el-descriptions-item")
75
+ const descriptionsItem = shallowRef("descriptions-item")
76
+ switch (props.componentType) {
77
+ case "Element Plus":
78
+ descriptions.value = "el-descriptions-item"
79
+ descriptionsItem.value = "el-descriptions-item"
80
+ break;
81
+ case "Ant Design Vue":
82
+ descriptions.value = "a-descriptions"
83
+ descriptionsItem.value = "a-descriptions-item"
84
+ break;
85
+ default:
86
+ break;
87
+ }
88
+ type RenderComponentProps = {
89
+ i: any;
90
+ formData: Object;
91
+ render: (item: any) => VNode | String;
92
+ };
93
+ type Events = {};
94
+ // 函数直接返回VNode模板会识别成[object Promise],因此需要转换成函数式组件
95
+ const RenderComponent: FunctionalComponent<RenderComponentProps, Events> = (
96
+ props,
97
+ context
98
+ ) => {
99
+ const { i, render, formData } = props;
100
+ return render({
101
+ row: formData,
102
+ $index: i,
103
+ });
104
+ };
105
+ </script>
106
+
107
+ <style></style>
@@ -0,0 +1,240 @@
1
+ <!--
2
+ * @Author: 陈德立*******419287484@qq.com
3
+ * @Date: 2021-08-20 17:14:53
4
+ * @LastEditTime: 2025-03-10 21:02:58
5
+ * @LastEditors: 陈德立*******419287484@qq.com
6
+ * @Github: https://github.com/Alan1034
7
+ * @Description:
8
+ * @FilePath: \GeneralBasicForm\src\GeneralBasicForm.vue
9
+ *
10
+ -->
11
+ /** 通用基本表单。用在表单页面搜索栏 */
12
+
13
+ <template>
14
+ <el-form :model="queryParams" ref="queryFormRef" v-show="showSearch" inline label-position="left"
15
+ :label-width="labelWidth" v-bind="$attrs">
16
+ <el-form-item v-for="item in formItem" :label="item.label" :prop="item.prop" :key="item.prop"
17
+ :rules="getItemRules(item)">
18
+ <Input v-if="/^input$/i.test(item.type)" :item="item" />
19
+ <Radio v-if="/^radio$/i.test(item.type)" :item="item" />
20
+ <Select v-if="/^select$/i.test(item.type)" :item="item" />
21
+ <Divider v-if="/^divider$/i.test(item.type)" :item="item" />
22
+ <Cascader v-if="/^cascader$/i.test(item.type)" :item="item" />
23
+ <Checkbox v-if="/^checkbox$/i.test(item.type)" :item="item" />
24
+ <DatePicker v-if="/^date-picker$/i.test(item.type)" :item="item" />
25
+ <InputNumber v-if="/^input-number$/i.test(item.type)" :item="item" />
26
+ <slot v-if="/^form-item-slot$/i.test(item.type)" :name="item.name"></slot>
27
+ <InputMobileVerification v-if="/^input-mobile-verification$/i.test(item.type)" :item="item" />
28
+ <InputGraphicVerification v-if="/^input-graphic-verification$/i.test(item.type)" :item="item" :key="item.key" />
29
+ </el-form-item>
30
+ <slot></slot>
31
+ <el-form-item v-if="!formOnly">
32
+ <el-button type="primary" :size="size" @click="handleQuery" v-loading="formLoading">查询</el-button>
33
+ <el-button :size="size" @click="resetQuery">重置</el-button>
34
+ </el-form-item>
35
+ <slot name="behind-the-button" />
36
+ </el-form>
37
+ </template>
38
+
39
+ <script lang="ts">
40
+ import { provide, ref, PropType, defineComponent, computed } from "vue";
41
+ import type { ItemType } from "./types/basicFrom";
42
+ // import { useRoute } from "vue-router";
43
+ import Input from "./components/VBasic/input/index.vue";
44
+ import InputNumber from "./components/VBasic/input-number/index.vue";
45
+ import InputGraphicVerification from "./components/CustomCom/input-graphic-verification/index.vue";
46
+ import InputMobileVerification from "./components/CustomCom/input-mobile-verification/index.vue";
47
+ import Divider from "./components/VBasic/divider/index.vue";
48
+ import Radio from "./components/VBasic/radio/index.vue";
49
+ import Checkbox from "./components/VBasic/checkbox/index.vue";
50
+ import DatePicker from "./components/VBasic/date-picker/index.vue";
51
+ import Select from "./components/VBasic/select/index.vue";
52
+ import Cascader from "./components/VBasic/cascader/index.vue";
53
+ import { formLoadingKey } from "./injectKey";
54
+ import { HandleParamsData } from "network-spanner"
55
+ export default defineComponent({
56
+ name: "GeneralBasicForm",
57
+ components: {
58
+ Input,
59
+ InputNumber,
60
+ InputGraphicVerification,
61
+ InputMobileVerification,
62
+ Divider,
63
+ Radio,
64
+ Checkbox,
65
+ DatePicker,
66
+ Select,
67
+ Cascader,
68
+ },
69
+ props: {
70
+ showSearch: {
71
+ // 是否展示所有元素
72
+ type: Boolean,
73
+ default: true,
74
+ },
75
+ loading: {
76
+ // 加载动画
77
+ type: Boolean,
78
+ default: false,
79
+ },
80
+ formOnly: {
81
+ // 是否只展示表单不展示按钮
82
+ type: Boolean,
83
+ default: false,
84
+ },
85
+ getList: {
86
+ // 查找数据调用的函数
87
+ type: Function,
88
+ default: () => { },
89
+ },
90
+ afterReset: {
91
+ // 在重置按钮点击完后但还没重新请求时触发的的函数
92
+ type: Function,
93
+ default: () => { },
94
+ },
95
+ formItem: {
96
+ // 定义表单的数据
97
+ type: Array as unknown as PropType<ItemType[]>,
98
+ default: () => [],
99
+ },
100
+ size: {
101
+ // 控制按钮大小
102
+ type: String,
103
+ default: "default",
104
+ },
105
+ labelWidth: {
106
+ // 表单文字宽度
107
+ type: String,
108
+ default: "90px",
109
+ },
110
+ parametersType: {
111
+ type: String,
112
+ default: "url",
113
+ },
114
+ DBPrimaryKey: {
115
+ // indexDB的primaryKey,一般配合parametersType==="indexDB"使用
116
+ type: [String, Number],
117
+ required: false,
118
+ },
119
+ formData: {
120
+ // 外部传入的表单数据,用于回填
121
+ type: Object,
122
+ default: () => { },
123
+ },
124
+ noInputBlank: {
125
+ // 用于判断input框是否校验仅空格
126
+ type: Boolean,
127
+ default: () => false,
128
+ },
129
+ currentPageKey: {
130
+ type: String,
131
+ default: "page",
132
+ },
133
+ pageSizeKey: {
134
+ type: String,
135
+ default: "limit",
136
+ },
137
+ defCurrentPage: {
138
+ type: Number,
139
+ default: 1,
140
+ },
141
+ defPageSize: {
142
+ type: Number,
143
+ default: 10,
144
+ },
145
+ queryWhenReady: {
146
+ // 初始化完成后自动触发查找数据函数
147
+ type: Boolean,
148
+ default: () => false,
149
+ }
150
+ },
151
+ data() {
152
+ return {
153
+ formLoading: this.loading || false,
154
+ queryParams: HandleParamsData.initQueryParams({
155
+ vm: this
156
+ }), // form表单数据
157
+ };
158
+ },
159
+ provide() {
160
+ return {
161
+ // 显式提供一个计算属性
162
+ [formLoadingKey]: {
163
+ formLoading: computed(() => this.formLoading),
164
+ updateFormLoading: (val) => {
165
+ this.formLoading = val;
166
+ },
167
+ },
168
+ // 使用computed保持响应性
169
+ queryParams: computed(() => this.queryParams),
170
+ };
171
+ },
172
+ setup(props) {
173
+ const { size, getList } = props;
174
+ provide(/* 注入名 */ "size", /* 值 */ size);
175
+ provide(/* 注入名 */ "getList", /* 值 */ getList);
176
+ },
177
+ watch: {
178
+ formData: {
179
+ handler(val, oldVal) {
180
+ if (JSON.stringify(val) !== JSON.stringify(this.queryParams)) {
181
+ this.queryParams = {
182
+ ...this.queryParams,
183
+ ...val,
184
+ };
185
+ }
186
+ // console.log(this.queryParams);
187
+ },
188
+ // watch 默认是懒执行的:仅当数据源变化时,才会执行回调。但在某些场景中,我们希望在创建侦听器时,立即执行一遍回调。举例来说,我们想请求一些初始数据,然后在相关状态更改时重新请求数据。
189
+ // https://cn.vuejs.org/guide/essentials/watchers.html#deep-watchers
190
+ immediate: true,
191
+ deep: true,
192
+ },
193
+ queryParams: {
194
+ handler(val) {
195
+ this.$emit("update:formData", { ...val });
196
+ },
197
+ deep: true,
198
+ },
199
+ loading(val) {
200
+ // console.log("loading", val);
201
+ if (this.formLoading === val) {
202
+ return;
203
+ }
204
+ this.formLoading = val;
205
+ },
206
+ formLoading(val) {
207
+ // console.log("formLoading", val);
208
+ if (this.loading === val) {
209
+ return;
210
+ }
211
+ this.$emit("update:loading", val);
212
+ },
213
+ },
214
+
215
+ methods: {
216
+ /** 搜索按钮操作 */
217
+ handleQuery(queryParameter = <any>{}) {
218
+ HandleParamsData.handleQuery({
219
+ queryParameter, vm: this
220
+ })
221
+ },
222
+ /** 重置按钮操作 */
223
+ resetQuery() {
224
+ HandleParamsData.resetQuery({
225
+ vm: this
226
+ })
227
+ },
228
+
229
+ getItemRules(item: any) {
230
+ const newRules = HandleParamsData.getItemRules({
231
+ item,
232
+ vm: this
233
+ })
234
+ return newRules;
235
+ },
236
+ },
237
+ });
238
+ </script>
239
+
240
+ <style scoped></style>
@@ -0,0 +1,163 @@
1
+ <!--
2
+ * @Author: 陈德立*******419287484@qq.com
3
+ * @Date: 2023-12-05 15:09:03
4
+ * @LastEditTime: 2024-11-21 19:37:38
5
+ * @LastEditors: 陈德立*******419287484@qq.com
6
+ * @Github: https://github.com/Alan1034
7
+ * @Description: 公共的无限滚动列表
8
+ * @FilePath: \GeneralBasicForm\src\InfiniteScrollList.vue
9
+ *
10
+ -->
11
+ <template>
12
+ <el-checkbox-group v-model="checkedList" v-loading="loading" v-bind="props" v-if="props.checkbox">
13
+ <ul v-infinite-scroll="loadList" class="list" :infinite-scroll-disabled="ifbottom" :style="{ height }">
14
+ <li v-for="i in list" :key="i[id]" class="list-item">
15
+ <el-checkbox :value="i[id]" class="checkbox">{{ i[name] }}
16
+ <ExtraComponent :i="i" v-if="props.extra"></ExtraComponent>
17
+ </el-checkbox>
18
+ </li>
19
+ </ul>
20
+ </el-checkbox-group>
21
+ <ul v-infinite-scroll="loadList" class="list" :infinite-scroll-disabled="ifbottom" :style="{ height }" v-else
22
+ v-bind="props">
23
+ <li v-for="i in list" :key="i[id]" class="list-item">
24
+ <div class="checkbox">
25
+ {{ i[name] }}
26
+ <ExtraComponent :i="i" v-if="props.extra"></ExtraComponent>
27
+ </div>
28
+ </li>
29
+ </ul>
30
+ </template>
31
+
32
+ <script lang="ts" setup>
33
+ import { ref, computed, watch } from "vue";
34
+ import type { PropType, FunctionalComponent, VNode } from "vue";
35
+ type SearchFunction = (page: Number) => Promise<[]>;
36
+ type ExtraFunction = "false" | ((item: any) => VNode | String);
37
+ const props = defineProps({
38
+ search: {
39
+ type: Function as PropType<SearchFunction>,
40
+ required: true,
41
+ },
42
+ checkbox: {
43
+ type: Boolean,
44
+ required: false,
45
+ },
46
+ id: {
47
+ type: String,
48
+ required: true,
49
+ },
50
+ name: {
51
+ type: String,
52
+ required: false,
53
+ },
54
+ extra: {
55
+ type: null as PropType<ExtraFunction>,
56
+ required: false,
57
+ },
58
+ defaultSelection: {
59
+ type: Array,
60
+ required: false,
61
+ },
62
+ height: {
63
+ type: String,
64
+ required: false,
65
+ default: "272px"
66
+ },
67
+ });
68
+ const { search, id, name, extra, height } = props;
69
+ const list = ref<any[]>([]);
70
+ const page = ref(1);
71
+ const ifbottom = ref(false); //判断是否到底部,是的话就不再请求
72
+ const checkedList = ref<any[]>([]);
73
+ const loading = ref(false);
74
+ type ExtraComponentProps = {
75
+ i: any;
76
+ };
77
+ type Events = {};
78
+ // 函数直接返回VNode模板会识别成[object Promise],因此需要转换成函数式组件
79
+ const ExtraComponent: FunctionalComponent<ExtraComponentProps, Events> = (
80
+ props,
81
+ context
82
+ ) => {
83
+ const { i } = props;
84
+ // el-checkbox有固定高度,如果需要配置高度比较高,例如有换行的自定义extra,最好处理一下样式,例子:
85
+ // :deep(.el-checkbox) {
86
+ // padding: 6px 16px !important;
87
+ // display: flex;
88
+ // align-items: baseline;
89
+ // height: auto;
90
+ // }
91
+ return extra && extra !== "false" ? extra(i) : "";
92
+ };
93
+ const updateCheckedList = (newSelection: any[]) => {
94
+ checkedList.value = newSelection.map((item) => {
95
+ return typeof item === "object" ? item[id] : item;
96
+ });
97
+ };
98
+ watch(
99
+ () => props.defaultSelection,
100
+ (NewVal = [], oldVal = []) => {
101
+ updateCheckedList(NewVal);
102
+ },
103
+ { immediate: true }
104
+ );
105
+ const reset = () => {
106
+ lowReset();
107
+ checkedList.value = [];
108
+ };
109
+ const lowReset = () => {
110
+ page.value = 1;
111
+ list.value = [];
112
+ ifbottom.value = false;
113
+ };
114
+ const loadList = async () => {
115
+ if (loading.value) {
116
+ return;
117
+ }
118
+ if (ifbottom.value) {
119
+ return;
120
+ }
121
+ loading.value = true;
122
+ const resList = await search(page.value);
123
+ if (resList && resList.length > 0) {
124
+ list.value = [...list.value, ...resList];
125
+ page.value += 1;
126
+ } else {
127
+ ifbottom.value = true;
128
+ }
129
+ loading.value = false;
130
+ return
131
+ };
132
+ const refreshList = async() => {
133
+ lowReset();
134
+ await loadList();
135
+ return
136
+ };
137
+ const selectInfo =
138
+ computed(() =>
139
+ list.value.filter((item) => checkedList.value.includes(item[id]))
140
+ ) || {};
141
+ defineExpose({
142
+ reset,
143
+ lowReset,
144
+ loadList,
145
+ selectInfo,
146
+ list,
147
+ ifbottom,
148
+ refreshList,
149
+ loading,
150
+ });
151
+ </script>
152
+
153
+ <style lang="less" scoped>
154
+ .list {
155
+ overflow: auto;
156
+ padding: 0;
157
+ margin: 0;
158
+ .checkbox {
159
+ width: calc(100% - 32px);
160
+ padding: 0 16px;
161
+ }
162
+ }
163
+ </style>
Binary file