A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
fix(SchemaForm): Make `setDefaultValueAsDomBehaviour` compatible with empty string values
haitaoo9a6f7700621e
diff --git a/ui/src/components/SchemaForm/index.tsx b/ui/src/components/SchemaForm/index.tsxindex 6c245d24..ec768298 100644--- a/ui/src/components/SchemaForm/index.tsx+++ b/ui/src/components/SchemaForm/index.tsx@@ -2,6 +2,7 @@ import {ForwardRefRenderFunction,forwardRef,useImperativeHandle,+ useEffect,} from 'react';import { Form, Button, Stack } from 'react-bootstrap';import { useTranslation } from 'react-i18next';@@ -155,16 +156,10 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (*/const setDefaultValueAsDomBehaviour = () => {keys.forEach((k) => {- const formVal = formData[k]?.value;+ const fieldVal = formData[k]?.value;const metaProp = properties[k];const uiCtrl = uiSchema[k]?.['ui:widget'];- if (- !metaProp ||- !uiCtrl ||- formVal ||- formVal === 0 ||- formVal === false- ) {+ if (!metaProp || !uiCtrl || fieldVal !== undefined) {return;}if (uiCtrl === 'select' && metaProp.enum?.[0] !== undefined) {@@ -176,7 +171,9 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (}});};- setDefaultValueAsDomBehaviour();+ useEffect(() => {+ setDefaultValueAsDomBehaviour();+ }, [formData]);const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {const { name, value } = e.target;@@ -348,7 +345,6 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (useImperativeHandle(ref, () => ({validator,}));- console.log('uiSchema: ', uiSchema);return (<Form noValidate onSubmit={handleSubmit}>{keys.map((key) => {@@ -577,7 +573,6 @@ export const initFormData = (schema: JSONSchema): Type.FormDataType => {Object.keys(schema.properties).forEach((key) => {const prop = schema.properties[key];const defaultVal = prop?.default;-formData[key] = {value: defaultVal,isInvalid: false,diff --git a/ui/src/pages/Admin/Smtp/index.tsx b/ui/src/pages/Admin/Smtp/index.tsxindex 3a625157..9750fa8f 100644--- a/ui/src/pages/Admin/Smtp/index.tsx+++ b/ui/src/pages/Admin/Smtp/index.tsx@@ -33,7 +33,7 @@ const Smtp: FC = () => {description: t('smtp_host.text'),},encryption: {- type: 'boolean',+ type: 'string',title: t('encryption.label'),description: t('encryption.text'),enum: ['SSL', ''],