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: Maintain `SchemaForm` all type definitions centrally

haitaoo53a2a104a7dc
diff --git a/ui/src/components/SchemaForm/README.md b/ui/src/components/SchemaForm/README.md
deleted file mode 100644
index 74fb514e..00000000
--- a/ui/src/components/SchemaForm/README.md
+++ /dev/null
@@ -1,233 +0,0 @@
----
-sidebar_position: 0
----
-# Schema Form
-
-## Introduction
-
-A React component capable of building HTML forms out of a [JSON schema](https://json-schema.org/understanding-json-schema/index.html).
-
-## Usage
-
-```tsx
-import React, { useState } from 'react';
-
-import { SchemaForm, initFormData, JSONSchema, UISchema } from '@/components';
-
-const schema: JSONSchema = {
- title: 'General',
- properties: {
- name: {
- type: 'string',
- title: 'Name',
- },
- age: {
- type: 'number',
- title: 'Age',
- },
- sex: {
- type: 'string',
- title: 'sex',
- enum: [1, 2],
- enumNames: ['male', 'female'],
- },
- },
-};
-
-const uiSchema: UISchema = {
- name: {
- 'ui:widget': 'input',
- },
- age: {
- 'ui:widget': 'input',
- 'ui:options': {
- type: 'number',
- },
- },
- sex: {
- 'ui:widget': 'radio',
- },
-};
-
-const Form = () => {
- const [formData, setFormData] = useState(initFormData(schema));
-
- const handleChange = (data) => {
- setFormData(data);
- };
-
- return (
- <SchemaForm
- schema={schema}
- uiSchema={uiSchema}
- formData={formData}
- onChange={handleChange}
- />
- );
-};
-
-export default Form;
-
-```
-
-## Props
-
-| Property | Description | Type | Default |
-| -------- | ---------------------------------------- | ------------------------------------- | ------- |
-| schema | Describe the form structure with schema | [JSONSchema](#json-schema) | - |
-| uiSchema | Describe the properties of the field | [UISchema](#uischema) | - |
-| formData | Describe form data | [FormData](#formdata) | - |
-| onChange | Callback function when form data changes | (data: [FormData](#formdata)) => void | - |
-| onSubmit | Callback function when form is submitted | (data: React.FormEvent) => void | - |
-
-## Types Definition
-### JSONSchema
-
-```ts
-export interface JSONSchema {
- title: string;
- description?: string;
- required?: string[];
- properties: {
- [key: string]: {
- type: 'string' | 'boolean' | 'number';
- title: string;
- label?: string;
- description?: string;
- enum?: Array<string | boolean | number>;
- enumNames?: string[];
- default?: string | boolean | number;
- };
- };
-}
-```
-
-### UIOptions
-
-```ts
-export interface UIOptions {
- empty?: string;
- className?: string | string[];
- validator?: (
- value,
- formData?,
- ) => Promise<string | true | void> | true | string;
-}
-```
-### InputOptions
-
-```ts
-export interface InputOptions extends UIOptions {
- placeholder?: string;
- type?:
- | 'color'
- | 'date'
- | 'datetime-local'
- | 'email'
- | 'month'
- | 'number'
- | 'password'
- | 'range'
- | 'search'
- | 'tel'
- | 'text'
- | 'time'
- | 'url'
- | 'week';
-}
-```
-### SelectOptions
-
-```ts
-export interface SelectOptions extends UIOptions {}
-```
-### UploadOptions
-
-```ts
-export interface UploadOptions extends UIOptions {
- acceptType?: string;
- imageType?: 'post' | 'avatar' | 'branding';
-}
-```
-### SwitchOptions
-
-```ts
-export interface SwitchOptions extends UIOptions {}
-```
-### TimezoneOptions
-
-```ts
-export interface TimezoneOptions extends UIOptions {
- placeholder?: string;
-}
-```
-### CheckboxOptions
-
-```ts
-export interface CheckboxOptions extends UIOptions {}
-```
-### RadioOptions
-
-```ts
-export interface RadioOptions extends UIOptions {}
-```
-### TextareaOptions
-
-```ts
-export interface TextareaOptions extends UIOptions {
- placeholder?: string;
- rows?: number;
-}
-```
-### UIWidget
-
-```ts
-export type UIWidget =
- | 'textarea'
- | 'input'
- | 'checkbox'
- | 'radio'
- | 'select'
- | 'upload'
- | 'timezone'
- | 'switch';
-```
-
-### UISchema
-
-```ts
-export interface UISchema {
- [key: string]: {
- 'ui:widget'?: UIWidget;
- 'ui:options'?:
- | InputOptions
- | SelectOptions
- | UploadOptions
- | SwitchOptions
- | TimezoneOptions
- | CheckboxOptions
- | RadioOptions
- | TextareaOptions;
- };
-}
-```
-
-### FormData
-```ts
-export interface FormValue<T = any> {
- value: T;
- isInvalid: boolean;
- errorMsg: string;
- [prop: string]: any;
-}
-
-export interface FormDataType {
- [prop: string]: FormValue;
-}
-```
-
-## reference
-
-- [json schema](https://json-schema.org/understanding-json-schema/index.html)
-- [react-jsonschema-form](https://github.com/rjsf-team/react-jsonschema-form)
-- [vue-json-schema-form](https://github.com/lljj-x/vue-json-schema-form/)
diff --git a/ui/src/components/SchemaForm/index.tsx b/ui/src/components/SchemaForm/index.tsx
index 2fec88e0..5810590f 100644
--- a/ui/src/components/SchemaForm/index.tsx
+++ b/ui/src/components/SchemaForm/index.tsx
@@ -12,7 +12,13 @@ import classnames from 'classnames';
import type * as Type from '@/common/interface';
-import type { JSONSchema, UISchema, BaseUIOptions, FormKit } from './types';
+import type {
+ JSONSchema,
+ FormProps,
+ FormRef,
+ BaseUIOptions,
+ FormKit,
+} from './types';
import {
Legend,
Select,
@@ -27,20 +33,6 @@ import {
export * from './types';
-interface IProps {
- schema: JSONSchema | null;
- formData: Type.FormDataType | null;
- uiSchema?: UISchema;
- refreshConfig?: FormKit['refreshConfig'];
- hiddenSubmit?: boolean;
- onChange?: (data: Type.FormDataType) => void;
- onSubmit?: (e: React.FormEvent) => void;
-}
-
-interface IRef {
- validator: () => Promise<boolean>;
-}
-
/**
* TODO:
* - [!] Standardised `Admin/Plugins/Config/index.tsx` method for generating dynamic form configurations.
@@ -60,7 +52,7 @@ interface IRef {
* @param onChange change event
* @param onSubmit submit event
*/
-const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
+const SchemaForm: ForwardRefRenderFunction<FormRef, FormProps> = (
{
schema,
uiSchema = {},
diff --git a/ui/src/components/SchemaForm/types.ts b/ui/src/components/SchemaForm/types.ts
index a6eed6ef..3d8cba4a 100644
--- a/ui/src/components/SchemaForm/types.ts
+++ b/ui/src/components/SchemaForm/types.ts
@@ -1,9 +1,24 @@
import { ButtonProps } from 'react-bootstrap';
+import React from 'react';
import classnames from 'classnames';
import * as Type from '@/common/interface';
+export interface FormProps {
+ schema: JSONSchema | null;
+ formData: Type.FormDataType | null;
+ uiSchema?: UISchema;
+ refreshConfig?: FormKit['refreshConfig'];
+ hiddenSubmit?: boolean;
+ onChange?: (data: Type.FormDataType) => void;
+ onSubmit?: (e: React.FormEvent) => void;
+}
+
+export interface FormRef {
+ validator: () => Promise<boolean>;
+}
+
export interface JSONSchema {
title: string;
description?: string;