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(ui): Improve the schema form type declaration

robin7e3adc979557
diff --git a/ui/src/common/interface.ts b/ui/src/common/interface.ts
index 29aa8a19..e640d91b 100644
--- a/ui/src/common/interface.ts
+++ b/ui/src/common/interface.ts
@@ -552,7 +552,7 @@ export interface PluginItem {
title: string;
description: string;
ui_options?: UIOptions;
- option?: PluginOption[];
+ options?: PluginOption[];
value?: string;
required?: boolean;
}
diff --git a/ui/src/components/SchemaForm/index.tsx b/ui/src/components/SchemaForm/index.tsx
index 3f4c2b7e..1b4c4e3d 100644
--- a/ui/src/components/SchemaForm/index.tsx
+++ b/ui/src/components/SchemaForm/index.tsx
@@ -30,7 +30,14 @@ export interface JSONSchema {
}
export interface UIOptions {
- rows?: number;
+ empty?: string;
+ className?: string | string[];
+ validator?: (
+ value,
+ formData?,
+ ) => Promise<string | true | void> | true | string;
+}
+export interface InputOptions extends UIOptions {
placeholder?: string;
type?:
| 'color'
@@ -47,19 +54,31 @@ export interface UIOptions {
| 'time'
| 'url'
| 'week';
- empty?: string;
- className?: string | string[];
- validator?: (
- value,
- formData?,
- ) => Promise<string | true | void> | true | string;
- textRender?: () => React.ReactElement;
- imageType?: Type.UploadType;
+}
+export interface SelectOptions extends UIOptions {}
+export interface UploadOptions extends UIOptions {
acceptType?: string;
+ imageType?: Type.UploadType;
+}
+
+export interface SwitchOptions extends UIOptions {}
+
+export interface TimezoneOptions extends UIOptions {
+ placeholder?: string;
}
+
+export interface CheckboxOptions extends UIOptions {}
+
+export interface RadioOptions extends UIOptions {}
+
+export interface TextareaOptions extends UIOptions {
+ placeholder?: string;
+ rows?: number;
+}
+
export type UIWidget =
| 'textarea'
- | 'text'
+ | 'input'
| 'checkbox'
| 'radio'
| 'select'
@@ -69,7 +88,15 @@ export type UIWidget =
export interface UISchema {
[key: string]: {
'ui:widget'?: UIWidget;
- 'ui:options'?: UIOptions;
+ 'ui:options'?:
+ | InputOptions
+ | SelectOptions
+ | UploadOptions
+ | SwitchOptions
+ | TimezoneOptions
+ | CheckboxOptions
+ | RadioOptions
+ | TextareaOptions;
};
}
@@ -295,8 +322,7 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
<Form noValidate onSubmit={handleSubmit}>
{keys.map((key) => {
const { title, description, label } = properties[key];
- const { 'ui:widget': widget = 'input', 'ui:options': options = {} } =
- uiSchema[key] || {};
+ const { 'ui:widget': widget = 'input' } = uiSchema[key] || {};
if (widget === 'select') {
return (
<Form.Group
@@ -420,6 +446,7 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
}
if (widget === 'upload') {
+ const options: UploadOptions = uiSchema[key]?.['ui:options'] || {};
return (
<Form.Group
key={title}
@@ -448,6 +475,8 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
}
if (widget === 'textarea') {
+ const options: TextareaOptions = uiSchema[key]?.['ui:options'] || {};
+
return (
<Form.Group
controlId={`form-${key}`}
@@ -458,7 +487,6 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
as="textarea"
name={key}
placeholder={options?.placeholder || ''}
- type={options?.type || 'text'}
value={formData[key]?.value || ''}
onChange={handleInputChange}
isInvalid={formData[key].isInvalid}
@@ -475,6 +503,9 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
</Form.Group>
);
}
+
+ const options: InputOptions = uiSchema[key]?.['ui:options'] || {};
+
return (
<Form.Group
controlId={key}
diff --git a/ui/src/pages/Admin/Plugins/Config/index.tsx b/ui/src/pages/Admin/Plugins/Config/index.tsx
index 040b6b50..ff4ff3cd 100644
--- a/ui/src/pages/Admin/Plugins/Config/index.tsx
+++ b/ui/src/pages/Admin/Plugins/Config/index.tsx
@@ -36,6 +36,13 @@ const Config = () => {
default: item.value,
};
+ if (item.options instanceof Array) {
+ properties[item.name].enum = item.options.map((option) => option.value);
+ properties[item.name].enumNames = item.options.map(
+ (option) => option.label,
+ );
+ }
+
if (item.ui_options) {
uiSchema[item.name] = {
'ui:options': item.ui_options,