Ver código fonte

fix: demo validator usage & types import

allen 2 meses atrás
pai
commit
12a81a7a7d

+ 5 - 4
apps/web-naive/src/views/demos/form/basic.vue

@@ -30,17 +30,18 @@ const layouts = [
 const layout = ref(layouts[0].value);
 
 function getNumberValidator(key: string, limit?: [number, number]) {
-  const validator = z.number({
+  let validator = z.number({
     required_error: `${key} 值不能为空`,
     invalid_type_error: `${key} 值只能为数字`,
   });
 
   if (limit) {
-    validator.min(limit[0], { message: `${key} 值不在区间范围内` });
-    validator.max(limit[1], { message: `${key} 值不在区间范围内` });
+    validator = validator
+      .min(limit[0], { message: `${key} 值不在区间范围内` })
+      .max(limit[1], { message: `${key} 值不在区间范围内` });
   }
 
-  return validator;
+  return validator.default(null);
 }
 
 const paramsSchema = [

+ 2 - 1
packages/@core/ui-kit/shadcn-ui/src/components/collapsible/collapsible-params-item.vue

@@ -15,12 +15,13 @@ const modelValue = defineModel('value');
 const finalOption = computed(() => {
   const { type, ...otherOption } = props.data.option;
 
-  if (type === 'number') {
+  if (type === 'number' || type === 'exponential') {
     return {
       step: props.data.option.step ?? 1,
       min: props.data.option.min,
       max: props.data.option.max,
       precision: props.data.option.precision ?? 0,
+      ...otherOption,
     };
   }
 

+ 4 - 0
packages/@core/ui-kit/shadcn-ui/src/components/collapsible/collapsible-params.vue

@@ -55,6 +55,10 @@ const bodyStyle = computed(() => {
 });
 
 function init() {
+  if (!modelValue.value) {
+    modelValue.value = {};
+  }
+
   for (const param of props.params) {
     modelValue.value[param.key] = param.defaultValue ?? null;
   }

+ 2 - 0
packages/@core/ui-kit/shadcn-ui/src/components/collapsible/collapsible.vue

@@ -1,6 +1,8 @@
 <script setup lang="ts">
 import type { CollapsibleRootEmits, CollapsibleRootProps } from 'reka-ui';
 
+import type { ClassType } from '@vben-core/typings';
+
 import { computed } from 'vue';
 
 import { ChevronsDown } from 'lucide-vue-next';

+ 2 - 2
packages/@core/ui-kit/shadcn-ui/src/components/collapsible/type.ts

@@ -4,11 +4,11 @@ export interface CollapsibleParamOption {
   min?: number;
   precision?: number;
   step?: number;
-  type: 'exponential' | 'number' | 'select' | 'string';
+  type?: 'exponential' | 'number' | 'select' | 'string';
 }
 
 export interface CollapsibleParamSchema {
-  defaultValue: number | number[] | string | string[];
+  defaultValue?: number | number[] | string | string[];
   description: string;
   key: string;
   option: CollapsibleParamOption;

+ 8 - 5
playground/src/views/examples/form/collapsible.vue

@@ -1,4 +1,6 @@
 <script lang="ts" setup>
+import type { CollapsibleParamSchema } from '@vben-core/shadcn-ui';
+
 import { ref } from 'vue';
 
 import { Page } from '@vben/common-ui';
@@ -18,20 +20,21 @@ const layouts = [
 const layout = ref(layouts[0].value);
 
 function getNumberValidator(key: string, limit?: [number, number]) {
-  const validator = z.number({
+  let validator = z.number({
     required_error: `${key} 值不能为空`,
     invalid_type_error: `${key} 值只能为数字`,
   });
 
   if (limit) {
-    validator.min(limit[0], { message: `${key} 值不在区间范围内` });
-    validator.max(limit[1], { message: `${key} 值不在区间范围内` });
+    validator = validator
+      .min(limit[0], { message: `${key} 值不在区间范围内` })
+      .max(limit[1], { message: `${key} 值不在区间范围内` });
   }
 
   return validator.default(null);
 }
 
-const paramsSchema = [
+const paramsSchema: CollapsibleParamSchema[] = [
   {
     key: 'micro_batch_size',
     description: `批次大小,代表模型训练过程中,模型更新模型参数的数据步长,可理解为模型每看多少数据即更新一次模型参数,
@@ -95,7 +98,7 @@ const paramsSchema = [
       max: 2_147_483_647,
     },
   },
-] as CollapsibleParamSchema[];
+];
 
 const paramsValidator = z.object({
   micro_batch_size: getNumberValidator('micro_batch_size', [8, 1024]),