From 8367ae85ded779e0dfef1c70862b38b8a6db2252 Mon Sep 17 00:00:00 2001 From: twwu Date: Sun, 27 Apr 2025 10:16:16 +0800 Subject: [PATCH] feat: Replace BaseVarType with BaseFieldType for consistent field type usage across components --- .../base/form/form-scenarios/base/field.tsx | 10 +++++----- .../base/form/form-scenarios/base/types.ts | 4 ++-- .../base/form/form-scenarios/base/utils.ts | 18 +++++++++--------- web/app/dev-preview/page.tsx | 12 ++++++------ 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/web/app/components/base/form/form-scenarios/base/field.tsx b/web/app/components/base/form/form-scenarios/base/field.tsx index 2adae3316a..7076535414 100644 --- a/web/app/components/base/form/form-scenarios/base/field.tsx +++ b/web/app/components/base/form/form-scenarios/base/field.tsx @@ -1,5 +1,5 @@ import React, { useMemo } from 'react' -import { type BaseConfiguration, BaseVarType } from './types' +import { type BaseConfiguration, BaseFieldType } from './types' import { withForm } from '../..' import { useStore } from '@tanstack/react-form' @@ -36,7 +36,7 @@ const BaseField = ({ if (!isAllConditionsMet) return <> - if (type === BaseVarType.textInput) { + if (type === BaseFieldType.textInput) { return ( ({ ) } - if (type === BaseVarType.numberInput) { + if (type === BaseFieldType.numberInput) { return ( ({ ) } - if (type === BaseVarType.checkbox) { + if (type === BaseFieldType.checkbox) { return ( ({ ) } - if (type === BaseVarType.select) { + if (type === BaseFieldType.select) { return ( = { required: boolean showOptional?: boolean // show optional label showConditions: ShowCondition[] // Show this field only when all conditions are met - type: BaseVarType + type: BaseFieldType tooltip?: string // Tooltip for this field } & NumberConfiguration & SelectConfiguration diff --git a/web/app/components/base/form/form-scenarios/base/utils.ts b/web/app/components/base/form/form-scenarios/base/utils.ts index e332dd5392..d23e6bf015 100644 --- a/web/app/components/base/form/form-scenarios/base/utils.ts +++ b/web/app/components/base/form/form-scenarios/base/utils.ts @@ -1,6 +1,6 @@ import type { ZodSchema, ZodString } from 'zod' import { z } from 'zod' -import { type BaseConfiguration, BaseVarType } from './types' +import { type BaseConfiguration, BaseFieldType } from './types' export const generateZodSchema = (fields: BaseConfiguration[]) => { const shape: Record = {} @@ -9,16 +9,16 @@ export const generateZodSchema = (fields: BaseConfiguration[]) => { let zodType switch (field.type) { - case BaseVarType.textInput: + case BaseFieldType.textInput: zodType = z.string() break - case BaseVarType.numberInput: + case BaseFieldType.numberInput: zodType = z.number() break - case BaseVarType.checkbox: + case BaseFieldType.checkbox: zodType = z.boolean() break - case BaseVarType.select: + case BaseFieldType.select: zodType = z.string() break default: @@ -27,7 +27,7 @@ export const generateZodSchema = (fields: BaseConfiguration[]) => { } if (field.required) { - if ([BaseVarType.textInput].includes(field.type)) + if ([BaseFieldType.textInput].includes(field.type)) zodType = (zodType as ZodString).nonempty(`${field.label} is required`) } else { @@ -35,17 +35,17 @@ export const generateZodSchema = (fields: BaseConfiguration[]) => { } if (field.maxLength) { - if ([BaseVarType.textInput].includes(field.type)) + if ([BaseFieldType.textInput].includes(field.type)) zodType = (zodType as ZodString).max(field.maxLength, `${field.label} exceeds max length of ${field.maxLength}`) } if (field.min) { - if ([BaseVarType.numberInput].includes(field.type)) + if ([BaseFieldType.numberInput].includes(field.type)) zodType = (zodType as ZodString).min(field.min, `${field.label} must be at least ${field.min}`) } if (field.max) { - if ([BaseVarType.numberInput].includes(field.type)) + if ([BaseFieldType.numberInput].includes(field.type)) zodType = (zodType as ZodString).max(field.max, `${field.label} exceeds max value of ${field.max}`) } diff --git a/web/app/dev-preview/page.tsx b/web/app/dev-preview/page.tsx index e0c4a8c300..e06f10d795 100644 --- a/web/app/dev-preview/page.tsx +++ b/web/app/dev-preview/page.tsx @@ -1,6 +1,6 @@ 'use client' import BaseForm from '../components/base/form/form-scenarios/base' -import { BaseVarType } from '../components/base/form/form-scenarios/base/types' +import { BaseFieldType } from '../components/base/form/form-scenarios/base/types' export default function Page() { return ( @@ -16,21 +16,21 @@ export default function Page() { }} configurations={[ { - type: BaseVarType.textInput, + type: BaseFieldType.textInput, variable: 'variable', label: 'Variable', required: true, showConditions: [], }, { - type: BaseVarType.textInput, + type: BaseFieldType.textInput, variable: 'label', label: 'Label', required: true, showConditions: [], }, { - type: BaseVarType.numberInput, + type: BaseFieldType.numberInput, variable: 'maxLength', label: 'Max Length', required: true, @@ -39,14 +39,14 @@ export default function Page() { min: 1, }, { - type: BaseVarType.checkbox, + type: BaseFieldType.checkbox, variable: 'required', label: 'Required', required: true, showConditions: [], }, { - type: BaseVarType.select, + type: BaseFieldType.select, variable: 'type', label: 'Type', required: true,