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: SchemaForm supports Button components

haitaoo095bc79758c5
diff --git a/ui/src/components/SchemaForm/components/Button.tsx b/ui/src/components/SchemaForm/components/Button.tsx
new file mode 100644
index 00000000..f7f83166
--- /dev/null
+++ b/ui/src/components/SchemaForm/components/Button.tsx
@@ -0,0 +1,44 @@
+import React, { FC, useState } from 'react';
+import { Button } from 'react-bootstrap';
+
+import { request } from '@/utils';
+import type * as Type from '@/common/interface';
+import type { UIAction } from '../index.d';
+
+interface Props {
+ fieldName: string;
+ text: string;
+ action: UIAction | undefined;
+ formData: Type.FormDataType;
+ readOnly: boolean;
+}
+const Index: FC<Props> = ({
+ fieldName,
+ action,
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
+ formData,
+ readOnly = false,
+ text = '',
+}) => {
+ const [isLoading, setLoading] = useState(false);
+ const handleAction = async () => {
+ if (!action) {
+ return;
+ }
+ setLoading(true);
+ const method = action.method || 'get';
+ await request[method](action.url);
+ setLoading(false);
+ };
+ const disabled = isLoading || readOnly;
+ return (
+ <div className="d-flex">
+ <Button name={fieldName} onClick={handleAction} disabled={disabled}>
+ {text || fieldName}
+ {isLoading ? '...' : ''}
+ </Button>
+ </div>
+ );
+};
+
+export default Index;
diff --git a/ui/src/components/SchemaForm/components/index.ts b/ui/src/components/SchemaForm/components/index.ts
index 0db3fa35..2c6da07b 100644
--- a/ui/src/components/SchemaForm/components/index.ts
+++ b/ui/src/components/SchemaForm/components/index.ts
@@ -6,5 +6,16 @@ import Timezone from './Timezone';
import Upload from './Upload';
import Textarea from './Textarea';
import Input from './Input';
+import Button from './Button';
-export { Legend, Select, Check, Switch, Timezone, Upload, Textarea, Input };
+export {
+ Legend,
+ Select,
+ Check,
+ Switch,
+ Timezone,
+ Upload,
+ Textarea,
+ Input,
+ Button,
+};
diff --git a/ui/src/components/SchemaForm/index.d.ts b/ui/src/components/SchemaForm/index.d.ts
new file mode 100644
index 00000000..2c525579
--- /dev/null
+++ b/ui/src/components/SchemaForm/index.d.ts
@@ -0,0 +1,6 @@
+export interface UIAction {
+ url: string;
+ method?: 'get' | 'post' | 'put' | 'delete';
+ event?: 'click' | 'change';
+ handler?: ({evt, formData, request}) => Promise<void>
+}
diff --git a/ui/src/components/SchemaForm/index.tsx b/ui/src/components/SchemaForm/index.tsx
index 60136b12..cc45471b 100644
--- a/ui/src/components/SchemaForm/index.tsx
+++ b/ui/src/components/SchemaForm/index.tsx
@@ -11,6 +11,7 @@ import classnames from 'classnames';
import type * as Type from '@/common/interface';
+import type { UIAction } from './index.d';
import {
Legend,
Select,
@@ -20,6 +21,7 @@ import {
Upload,
Textarea,
Input,
+ Button as CtrlButton,
} from './components';
export interface JSONSchema {
@@ -52,6 +54,7 @@ export interface BaseUIOptions {
formData?,
) => Promise<string | true | void> | true | string;
}
+
export interface InputOptions extends BaseUIOptions {
placeholder?: string;
inputType?:
@@ -93,6 +96,12 @@ export interface TextareaOptions extends BaseUIOptions {
rows?: number;
}
+export interface ButtonOptions extends BaseUIOptions {
+ text: string;
+ icon?: string;
+ action?: UIAction;
+}
+
export type UIOptions =
| InputOptions
| SelectOptions
@@ -101,7 +110,8 @@ export type UIOptions =
| TimezoneOptions
| CheckboxOptions
| RadioOptions
- | TextareaOptions;
+ | TextareaOptions
+ | ButtonOptions;
export type UIWidget =
| 'textarea'
@@ -112,7 +122,8 @@ export type UIWidget =
| 'upload'
| 'timezone'
| 'switch'
- | 'legend';
+ | 'legend'
+ | 'button';
export interface UISchema {
[key: string]: {
'ui:widget'?: UIWidget;
@@ -136,6 +147,7 @@ interface IRef {
/**
* TODO:
* - Normalize and document `formData[key].hidden && 'd-none'`
+ * - Normalize and document `hiddenSubmit`
* - `handleXXChange` methods are placed in the concrete component
* - Improving field hints for `formData`
* - Optimise form data updates
@@ -394,7 +406,6 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
groupClassName = uiOpt.fieldClassName;
}
const readOnly = uiOpt?.readOnly || false;
-
return (
<Form.Group
key={title}
@@ -480,6 +491,15 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
readOnly={readOnly}
/>
) : null}
+ {widget === 'button' ? (
+ <CtrlButton
+ fieldName={key}
+ text={uiOpt && 'text' in uiOpt ? uiOpt.text : ''}
+ action={uiOpt && 'action' in uiOpt ? uiOpt.action : undefined}
+ formData={formData}
+ readOnly={readOnly}
+ />
+ ) : null}
{/* Unified handling of `Feedback` and `Text` */}
<Form.Control.Feedback type="invalid">
{fieldState?.errorMsg}