Skip to content

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.

refactor: add defaultValue to the form

robind3a803d8a6e1
diff --git a/ui/src/components/SchemaForm/index.tsx b/ui/src/components/SchemaForm/index.tsx
index a004deb6..899fbd2a 100644
--- a/ui/src/components/SchemaForm/index.tsx
+++ b/ui/src/components/SchemaForm/index.tsx
@@ -291,7 +291,12 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
return (
<Form noValidate onSubmit={handleSubmit}>
{keys.map((key) => {
- const { title, description, label } = properties[key];
+ const {
+ title,
+ description,
+ label,
+ default: defaultValue,
+ } = properties[key];
const { 'ui:widget': widget = 'input', 'ui:options': options = {} } =
uiSchema[key] || {};
if (widget === 'select') {
@@ -304,7 +309,7 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
<Form.Select
aria-label={description}
name={key}
- value={formData[key]?.value}
+ value={formData[key]?.value || defaultValue}
onChange={handleSelectChange}
isInvalid={formData[key].isInvalid}>
{properties[key].enum?.map((item, index) => {
@@ -342,7 +347,7 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
name={key}
id={`form-${String(item)}`}
label={properties[key].enumNames?.[index]}
- checked={formData[key]?.value === item}
+ checked={(formData[key]?.value || defaultValue) === item}
feedback={formData[key]?.errorMsg}
feedbackType="invalid"
isInvalid={formData[key].isInvalid}
@@ -374,7 +379,7 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
name={key}
type="switch"
label={label}
- checked={formData[key]?.value}
+ checked={formData[key]?.value || defaultValue}
feedback={formData[key]?.errorMsg}
feedbackType="invalid"
isInvalid={formData[key].isInvalid}
@@ -397,7 +402,7 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
controlId={key}>
<Form.Label>{title}</Form.Label>
<TimeZonePicker
- value={formData[key]?.value}
+ value={formData[key]?.value || defaultValue}
name={key}
onChange={handleSelectChange}
/>
@@ -456,7 +461,7 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
name={key}
placeholder={options?.placeholder || ''}
type={options?.type || 'text'}
- value={formData[key]?.value}
+ value={formData[key]?.value || defaultValue}
onChange={handleInputChange}
isInvalid={formData[key].isInvalid}
rows={options?.rows || 3}
@@ -482,7 +487,7 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
name={key}
placeholder={options?.placeholder || ''}
type={options?.type || 'text'}
- value={formData[key]?.value}
+ value={formData[key]?.value || defaultValue}
onChange={handleInputChange}
style={options?.type === 'color' ? { width: '6rem' } : {}}
isInvalid={formData[key].isInvalid}
diff --git a/ui/src/pages/Admin/Interface/index.tsx b/ui/src/pages/Admin/Interface/index.tsx
index f710b94f..aad84117 100644
--- a/ui/src/pages/Admin/Interface/index.tsx
+++ b/ui/src/pages/Admin/Interface/index.tsx
@@ -8,7 +8,7 @@ import {
AdminSettingsInterface,
} from '@/common/interface';
import { interfaceStore } from '@/stores';
-import { JSONSchema, SchemaForm, UISchema } from '@/components';
+import { JSONSchema, SchemaForm, UISchema, initFormData } from '@/components';
import { DEFAULT_TIMEZONE } from '@/common/constants';
import { updateInterfaceSetting, useInterfaceSetting } from '@/services';
import {
@@ -36,27 +36,18 @@ const Interface: FC = () => {
description: t('language.text'),
enum: langs?.map((lang) => lang.value),
enumNames: langs?.map((lang) => lang.label),
+ default: setting?.language || storeInterface.language,
},
time_zone: {
type: 'string',
title: t('time_zone.label'),
description: t('time_zone.text'),
+ default: setting?.time_zone || DEFAULT_TIMEZONE,
},
},
};
- const [formData, setFormData] = useState<FormDataType>({
- language: {
- value: setting?.language || storeInterface.language,
- isInvalid: false,
- errorMsg: '',
- },
- time_zone: {
- value: setting?.time_zone || DEFAULT_TIMEZONE,
- isInvalid: false,
- errorMsg: '',
- },
- });
+ const [formData, setFormData] = useState<FormDataType>(initFormData(schema));
const uiSchema: UISchema = {
language: {