@skyfox2000/webui 1.4.16 → 1.4.17

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skyfox2000/webui",
3
- "version": "1.4.16",
3
+ "version": "1.4.17",
4
4
  "description": "后台前端通用组件定义",
5
5
  "type": "module",
6
6
  "keywords": [],
@@ -1,9 +1,10 @@
1
1
  <script setup lang="ts">
2
- import { EditorControl, ProviderKeys, useFormItemFactory, ValidateRuleItem } from '@/index';
2
+ import { EditorControl, ProviderKeys, useFormItemFactory } from '@/index';
3
3
  import { FormItem, message } from 'ant-design-vue';
4
4
  import { Helper } from '../../common';
5
5
  import { computed, inject, ref, useAttrs } from 'vue';
6
6
  import { AnyData } from '@skyfox2000/fapi';
7
+ import { getRule } from '@/utils/form-validate';
7
8
 
8
9
  const props = defineProps<{
9
10
  /**
@@ -48,21 +49,6 @@ setTimeout(() => {
48
49
  visible.value = true;
49
50
  }, 30);
50
51
 
51
- /**
52
- * 递归获取规则
53
- * - async-validator的语法规范
54
- */
55
- const getRule = (rule: Array<string>, ruleObj: Record<string, any> | undefined): ValidateRuleItem | undefined => {
56
- if (!ruleObj) {
57
- return undefined;
58
- }
59
- const [key, ...rest] = rule;
60
- if (rule.length === 1) {
61
- return ruleObj[key];
62
- }
63
- if (!ruleObj[key]) return undefined;
64
- return getRule(rest, ruleObj[key].fields as Record<string, any>);
65
- };
66
52
  /**
67
53
  * 是否必填
68
54
  * - 如果rule为空,则不显示必填标记
@@ -1,8 +1,10 @@
1
1
  <script setup lang="ts">
2
- import { inject, useAttrs } from 'vue';
2
+ import { computed, inject, useAttrs } from 'vue';
3
3
  import { ProviderKeys, EditorControl, useFormItemFactory } from '@/index';
4
4
  import { FormItem } from 'ant-design-vue';
5
5
  import { AnyData } from '@skyfox2000/fapi';
6
+ import { getRule } from '@/utils/form-validate';
7
+ import message from 'vue-m-message';
6
8
 
7
9
  const props = defineProps<{
8
10
  /**
@@ -26,16 +28,38 @@ const attrs = useAttrs(); // 手动获取 $attrs
26
28
 
27
29
  const editorCtrl = inject(ProviderKeys.EditorControl, undefined) as EditorControl<AnyData> | undefined;
28
30
  const errInfo = useFormItemFactory(props, editorCtrl);
31
+
32
+ /**
33
+ * 是否必填
34
+ * - 如果rule为空,则不显示必填标记
35
+ * - 如果rule不为空,则根据formRules中的required属性判断是否必填
36
+ */
37
+ const required = computed(() => {
38
+ if (!props.rule) {
39
+ // 如果rule为空,则不显示必填标记
40
+ return false;
41
+ }
42
+ // 如果rule包含.,则表示是对象属性
43
+ const rule = getRule(props.rule.split('.'), editorCtrl?.formRules?.value);
44
+ if (!rule) {
45
+ message.error(`"${props.label}" 的验证规则 \`${props.rule}\` 不存在`);
46
+ errInfo.value.errClass = 'text-[#ff4d4f]';
47
+ errInfo.value.msg = `规则 \`${props.rule}\` 不存在,请检查代码!`;
48
+ return true;
49
+ }
50
+ if (!rule.required) {
51
+ // 如果rule.required为false,则表示该字段是可选字段,不需要验证
52
+ errInfo.value.errClass = '';
53
+ errInfo.value.msg = '';
54
+ }
55
+ return rule.required ?? true;
56
+ });
57
+
29
58
  </script>
30
59
  <template>
31
60
  <div class="w-1/3 relative mb-1">
32
- <FormItem
33
- :required="rule !== undefined"
34
- class="w-[90%] relative"
35
- v-bind="attrs"
36
- :class="[rule ? '' : 'mb-3', width]"
37
- :labelCol="{ span: 6 }"
38
- >
61
+ <FormItem :required="required" class="w-[90%] relative" v-bind="attrs" :class="[rule ? '' : 'mb-3', width]"
62
+ :labelCol="{ span: 6 }">
39
63
  <template #label v-if="label">
40
64
  <span :class="[errInfo.errClass ? 'text-[#ff4d4f]' : '', 'w-full']"> {{ label }}</span>
41
65
  </template>
@@ -15,7 +15,7 @@ export interface TableColumn {
15
15
  key?: string;
16
16
  visible?: boolean; // 是否显示列
17
17
  export?: boolean; // 是否导出列
18
- customRender?: (value: any, record: Record<string, any>) => any;
18
+ customRender?: (options: { text: any; record: Record<string, any>; index: number; column: TableColumn }) => any;
19
19
  }
20
20
 
21
21
  /**
@@ -45,7 +45,7 @@ export const exportSelectedRows = async <T extends Record<string, any>>(
45
45
  }));
46
46
 
47
47
  // 4. 处理数据行
48
- const processedData = selectRows.map((row) => {
48
+ const processedData = selectRows.map((row, index) => {
49
49
  const newRow: Record<string, any> = { ...row };
50
50
 
51
51
  exportColumns.forEach((col) => {
@@ -54,7 +54,12 @@ export const exportSelectedRows = async <T extends Record<string, any>>(
54
54
 
55
55
  // 应用自定义渲染
56
56
  if (col.customRender) {
57
- newRow[field] = col.customRender(row[field], row);
57
+ newRow[field] = col.customRender({
58
+ text: row[field],
59
+ record: row,
60
+ index: index,
61
+ column: col,
62
+ });
58
63
  }
59
64
  });
60
65
  return newRow;
@@ -119,7 +124,7 @@ export const exportResults = async <T extends Record<string, any>>(
119
124
  if (result.data) {
120
125
  // 5. 处理数据行
121
126
  let results = result.data as unknown as T[];
122
- const processedData = results.map((row) => {
127
+ const processedData = results.map((row, index) => {
123
128
  const newRow: Record<string, any> = { ...row };
124
129
 
125
130
  exportColumns.forEach((col) => {
@@ -128,7 +133,12 @@ export const exportResults = async <T extends Record<string, any>>(
128
133
 
129
134
  // 应用自定义渲染
130
135
  if (col.customRender) {
131
- newRow[field] = col.customRender(row[field], row);
136
+ newRow[field] = col.customRender({
137
+ text: row[field],
138
+ record: row,
139
+ index: index,
140
+ column: col,
141
+ });
132
142
  }
133
143
  });
134
144
  return newRow;
@@ -1,6 +1,6 @@
1
1
  import { inject, toRaw, ref, provide, watch, Ref } from 'vue';
2
2
  import Validator from 'async-validator';
3
- import { EditorControl, InputFactoryItems, ValidateError, ValidateRule } from '@/typings/form.d';
3
+ import { EditorControl, InputFactoryItems, ValidateError, ValidateRule, ValidateRuleItem } from '@/typings/form.d';
4
4
  import { ProviderKeys } from '@/typings/page.d';
5
5
  import { AnyData } from '@skyfox2000/fapi';
6
6
  import { isEmpty } from './isEmpty';
@@ -383,3 +383,22 @@ export const useFormItemFactory = (options: RuleFactoryOptions, editorCtrl?: Edi
383
383
  provide(ProviderKeys.ErrInfo, errInfo);
384
384
  return errInfo;
385
385
  };
386
+
387
+ /**
388
+ * 递归获取规则
389
+ * - async-validator的语法规范
390
+ */
391
+ export const getRule = (
392
+ rule: Array<string>,
393
+ ruleObj: Record<string, any> | undefined,
394
+ ): ValidateRuleItem | undefined => {
395
+ if (!ruleObj) {
396
+ return undefined;
397
+ }
398
+ const [key, ...rest] = rule;
399
+ if (rule.length === 1) {
400
+ return ruleObj[key];
401
+ }
402
+ if (!ruleObj[key]) return undefined;
403
+ return getRule(rest, ruleObj[key].fields as Record<string, any>);
404
+ };