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.

feat(ui): add json schema form

robin26f606e285a0
diff --git a/ui/src/components/SchemaForm/index.tsx b/ui/src/components/SchemaForm/index.tsx
new file mode 100644
index 00000000..4c7a01d0
--- /dev/null
+++ b/ui/src/components/SchemaForm/index.tsx
@@ -0,0 +1,265 @@
+import { FC } from 'react';
+import { Form, Button, Stack } from 'react-bootstrap';
+import { useTranslation } from 'react-i18next';
+
+import type * as Type from '@/common/interface';
+
+export interface JSONSchema {
+ title: string;
+ description?: string;
+ required?: string[];
+ properties: {
+ [key: string]: {
+ type: 'string' | 'boolean';
+ title: string;
+ description?: string;
+ enum?: Array<string | boolean>;
+ enumNames?: string[];
+ default?: string | boolean;
+ };
+ };
+}
+export interface UISchema {
+ [key: string]: {
+ 'ui:widget'?:
+ | 'textarea'
+ | 'text'
+ | 'checkbox'
+ | 'radio'
+ | 'select'
+ | 'switch';
+ 'ui:options'?: {
+ rows?: number;
+ placeholder?: string;
+ type?:
+ | 'color'
+ | 'date'
+ | 'datetime-local'
+ | 'email'
+ | 'month'
+ | 'number'
+ | 'password'
+ | 'range'
+ | 'search'
+ | 'tel'
+ | 'text'
+ | 'time'
+ | 'url'
+ | 'week';
+ empty?: string;
+ invalid?: string;
+ validator?: (value) => boolean;
+ };
+ };
+}
+
+interface IProps {
+ schema: JSONSchema;
+ uiSchema?: UISchema;
+ formData?: Type.FormDataType;
+ onChange?: (data: Type.FormDataType) => void;
+ onSubmit: (e: React.FormEvent) => void;
+}
+
+const SchemaForm: FC<IProps> = ({
+ schema,
+ uiSchema = {},
+ formData = {},
+ onChange,
+ onSubmit,
+}) => {
+ const { t } = useTranslation('translation', {
+ keyPrefix: 'form',
+ });
+ const { properties } = schema;
+ const keys = Object.keys(properties);
+
+ const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
+ const { name, value } = e.target;
+ const data = { ...formData, [name]: { ...formData[name], value } };
+ if (onChange instanceof Function) {
+ onChange(data);
+ }
+ };
+ const requiredValidator = () => {
+ const required = schema.required || [];
+ const errors: string[] = [];
+ required.forEach((key) => {
+ if (!formData[key] || !formData[key].value) {
+ errors.push(key);
+ }
+ });
+ return errors;
+ };
+ const syncValidator = () => {
+ const errors: string[] = [];
+ keys.forEach((key) => {
+ const { validator } = uiSchema[key]?.['ui:options'] || {};
+ if (validator instanceof Function) {
+ const value = formData[key]?.value;
+ if (!validator(value)) {
+ errors.push(key);
+ }
+ }
+ });
+ return errors;
+ };
+ const handleSubmit = (e: React.FormEvent) => {
+ e.preventDefault();
+ const errors = requiredValidator();
+ if (errors.length > 0) {
+ formData = errors.reduce((acc, cur) => {
+ acc[cur] = {
+ ...formData[cur],
+ isInvalid: true,
+ errorMsg:
+ uiSchema[cur]['ui:options']?.empty ||
+ `${schema.properties[cur].title} ${t('form.empty')}`,
+ };
+ return acc;
+ }, formData);
+ if (onChange instanceof Function) {
+ onChange(formData);
+ }
+ return;
+ }
+ const syncErrors = syncValidator();
+ if (syncErrors.length > 0) {
+ formData = syncErrors.reduce((acc, cur) => {
+ acc[cur] = {
+ ...formData[cur],
+ isInvalid: true,
+ errorMsg:
+ uiSchema[cur]['ui:options']?.invalid ||
+ `${schema.properties[cur].title} ${t('form.invalid')}`,
+ };
+ return acc;
+ }, formData);
+ if (onChange instanceof Function) {
+ onChange(formData);
+ }
+ return;
+ }
+ Object.keys(formData).forEach((key) => {
+ formData[key].isInvalid = false;
+ formData[key].errorMsg = '';
+ });
+ if (onChange instanceof Function) {
+ onChange(formData);
+ }
+ onSubmit(e);
+ };
+ return (
+ <Form noValidate onSubmit={handleSubmit}>
+ {keys.map((key) => {
+ const { title, description } = properties[key];
+ const { 'ui:widget': widget = 'input', 'ui:options': options = {} } =
+ uiSchema[key] || {};
+ if (widget === 'select') {
+ return (
+ <Form.Group key={title} controlId={key} className="mb-3">
+ <Form.Label>{title}</Form.Label>
+ <Form.Select
+ aria-label={description}
+ isInvalid={formData[key].isInvalid}>
+ {properties[key].enum?.map((item, index) => {
+ return (
+ <option value={String(item)} key={String(item)}>
+ {properties[key].enumNames?.[index]}
+ </option>
+ );
+ })}
+ </Form.Select>
+ <Form.Control.Feedback type="invalid">
+ {formData[key]?.errorMsg}
+ </Form.Control.Feedback>
+ <Form.Text className="text-muted">{description}</Form.Text>
+ </Form.Group>
+ );
+ }
+ if (widget === 'checkbox' || widget === 'radio') {
+ return (
+ <Form.Group key={title} className="mb-3" controlId={key}>
+ <Form.Label>{title}</Form.Label>
+ <Stack direction="horizontal">
+ {properties[key].enum?.map((item, index) => {
+ return (
+ <Form.Check
+ key={String(item)}
+ inline
+ required
+ type={widget}
+ name={title}
+ id={String(item)}
+ label={properties[key].enumNames?.[index]}
+ checked={formData[key]?.value === item}
+ feedback={formData[key]?.errorMsg}
+ feedbackType="invalid"
+ isInvalid={formData[key].isInvalid}
+ />
+ );
+ })}
+ </Stack>
+ <Form.Text className="text-muted">{description}</Form.Text>
+ </Form.Group>
+ );
+ }
+
+ if (widget === 'switch') {
+ return (
+ <Form.Group key={title} className="mb-3" controlId={key}>
+ <Form.Label>{title}</Form.Label>
+ <Form.Check
+ required
+ id={title}
+ type="switch"
+ label={title}
+ feedback={formData[key]?.errorMsg}
+ feedbackType="invalid"
+ isInvalid={formData[key].isInvalid}
+ />
+ <Form.Text className="text-muted">{description}</Form.Text>
+ </Form.Group>
+ );
+ }
+
+ const as = widget === 'textarea' ? 'textarea' : 'input';
+ return (
+ <Form.Group controlId={key} key={key} className="mb-3">
+ <Form.Label>{title}</Form.Label>
+ <Form.Control
+ as={as}
+ name={key}
+ placeholder={options?.placeholder || ''}
+ type={options?.type || 'text'}
+ value={formData[key]?.value}
+ onChange={handleInputChange}
+ isInvalid={formData[key].isInvalid}
+ />
+ <Form.Control.Feedback type="invalid">
+ {formData[key]?.errorMsg}
+ </Form.Control.Feedback>
+
+ <Form.Text className="text-muted">{description}</Form.Text>
+ </Form.Group>
+ );
+ })}
+ <Button variant="primary" type="submit">
+ {t('btn_submit')}
+ </Button>
+ </Form>
+ );
+};
+export const initFormData = (schema: JSONSchema): Type.FormDataType => {
+ const formData: Type.FormDataType = {};
+ Object.keys(schema.properties).forEach((key) => {
+ formData[key] = {
+ value: '',
+ isInvalid: false,
+ errorMsg: '',
+ };
+ });
+ return formData;
+};
+
+export default SchemaForm;
diff --git a/ui/src/components/index.ts b/ui/src/components/index.ts
index 25bdefa7..4ff5e71c 100644
--- a/ui/src/components/index.ts
+++ b/ui/src/components/index.ts
@@ -25,6 +25,7 @@ import Empty from './Empty';
import BaseUserCard from './BaseUserCard';
import FollowingTags from './FollowingTags';
import QueryGroup from './QueryGroup';
+import SchemaForm, { JSONSchema, UISchema, initFormData } from './SchemaForm';
export {
Avatar,
@@ -55,5 +56,7 @@ export {
FollowingTags,
htmlRender,
QueryGroup,
+ SchemaForm,
+ initFormData,
};
-export type { EditorRef };
+export type { EditorRef, JSONSchema, UISchema };