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(SchemaForm): Making form components more independent

haitaoofd2c5f9946ab
diff --git a/ui/src/components/SchemaForm/components/Check.tsx b/ui/src/components/SchemaForm/components/Check.tsx
index 9936e5de..5a62872c 100644
--- a/ui/src/components/SchemaForm/components/Check.tsx
+++ b/ui/src/components/SchemaForm/components/Check.tsx
@@ -6,7 +6,7 @@ import type * as Type from '@/common/interface';
interface Props {
type: 'radio' | 'checkbox';
fieldName: string;
- onChange: (evt: React.ChangeEvent<HTMLInputElement>, ...rest) => void;
+ onChange?: (fd: Type.FormDataType) => void;
enumValues: (string | boolean | number)[];
enumNames: string[];
formData: Type.FormDataType;
@@ -20,6 +20,24 @@ const Index: FC<Props> = ({
formData,
}) => {
const fieldObject = formData[fieldName];
+ const handleCheck = (
+ evt: React.ChangeEvent<HTMLInputElement>,
+ index: number,
+ ) => {
+ const { name, checked } = evt.currentTarget;
+ const freshVal = checked ? enumValues?.[index] : '';
+ const state = {
+ ...formData,
+ [name]: {
+ ...formData[name],
+ value: freshVal,
+ isInvalid: false,
+ },
+ };
+ if (typeof onChange === 'function') {
+ onChange(state);
+ }
+ };
return (
<Stack direction="horizontal">
{enumValues?.map((item, index) => {
@@ -36,7 +54,7 @@ const Index: FC<Props> = ({
feedback={fieldObject?.errorMsg}
feedbackType="invalid"
isInvalid={fieldObject?.isInvalid}
- onChange={(evt) => onChange(evt, index)}
+ onChange={(evt) => handleCheck(evt, index)}
/>
);
})}
diff --git a/ui/src/components/SchemaForm/components/Input.tsx b/ui/src/components/SchemaForm/components/Input.tsx
index ed3e8d2f..02f20ce9 100644
--- a/ui/src/components/SchemaForm/components/Input.tsx
+++ b/ui/src/components/SchemaForm/components/Input.tsx
@@ -7,7 +7,7 @@ interface Props {
type: string | undefined;
placeholder: string | undefined;
fieldName: string;
- onChange: (evt: React.ChangeEvent<HTMLInputElement>, ...rest) => void;
+ onChange?: (fd: Type.FormDataType) => void;
formData: Type.FormDataType;
readOnly: boolean;
}
@@ -20,13 +20,28 @@ const Index: FC<Props> = ({
readOnly = false,
}) => {
const fieldObject = formData[fieldName];
+ const handleChange = (evt: React.ChangeEvent<HTMLInputElement>) => {
+ const { name, value } = evt.currentTarget;
+ const state = {
+ ...formData,
+ [name]: {
+ ...formData[name],
+ value,
+ isInvalid: false,
+ },
+ };
+ if (typeof onChange === 'function') {
+ onChange(state);
+ }
+ };
+
return (
<Form.Control
name={fieldName}
placeholder={placeholder}
type={type}
value={fieldObject?.value || ''}
- onChange={onChange}
+ onChange={handleChange}
readOnly={readOnly}
isInvalid={fieldObject?.isInvalid}
style={type === 'color' ? { width: '6rem' } : {}}
diff --git a/ui/src/components/SchemaForm/components/Select.tsx b/ui/src/components/SchemaForm/components/Select.tsx
index d2f44b26..77c0f87a 100644
--- a/ui/src/components/SchemaForm/components/Select.tsx
+++ b/ui/src/components/SchemaForm/components/Select.tsx
@@ -6,7 +6,7 @@ import type * as Type from '@/common/interface';
interface Props {
desc: string | undefined;
fieldName: string;
- onChange: (evt: React.ChangeEvent<HTMLSelectElement>) => void;
+ onChange?: (fd: Type.FormDataType) => void;
enumValues: (string | boolean | number)[];
enumNames: string[];
formData: Type.FormDataType;
@@ -20,12 +20,26 @@ const Index: FC<Props> = ({
formData,
}) => {
const fieldObject = formData[fieldName];
+ const handleChange = (evt: React.ChangeEvent<HTMLSelectElement>) => {
+ const { name, value } = evt.currentTarget;
+ const state = {
+ ...formData,
+ [name]: {
+ ...formData[name],
+ value,
+ isInvalid: false,
+ },
+ };
+ if (typeof onChange === 'function') {
+ onChange(state);
+ }
+ };
return (
<Form.Select
aria-label={desc}
name={fieldName}
value={fieldObject?.value || ''}
- onChange={onChange}
+ onChange={handleChange}
isInvalid={fieldObject?.isInvalid}>
{enumValues?.map((item, index) => {
return (
diff --git a/ui/src/components/SchemaForm/components/Switch.tsx b/ui/src/components/SchemaForm/components/Switch.tsx
index 1513f29b..7de87d06 100644
--- a/ui/src/components/SchemaForm/components/Switch.tsx
+++ b/ui/src/components/SchemaForm/components/Switch.tsx
@@ -7,11 +7,25 @@ interface Props {
title: string;
label: string | undefined;
fieldName: string;
- onChange: (evt: React.ChangeEvent<HTMLInputElement>, ...rest) => void;
+ onChange?: (fd: Type.FormDataType) => void;
formData: Type.FormDataType;
}
const Index: FC<Props> = ({ title, fieldName, onChange, label, formData }) => {
const fieldObject = formData[fieldName];
+ const handleChange = (evt: React.ChangeEvent<HTMLInputElement>) => {
+ const { name, checked } = evt.currentTarget;
+ const state = {
+ ...formData,
+ [name]: {
+ ...formData[name],
+ value: checked,
+ isInvalid: false,
+ },
+ };
+ if (typeof onChange === 'function') {
+ onChange(state);
+ }
+ };
return (
<Form.Check
required
@@ -23,7 +37,7 @@ const Index: FC<Props> = ({ title, fieldName, onChange, label, formData }) => {
feedback={fieldObject?.errorMsg}
feedbackType="invalid"
isInvalid={fieldObject.isInvalid}
- onChange={onChange}
+ onChange={handleChange}
/>
);
};
diff --git a/ui/src/components/SchemaForm/components/Textarea.tsx b/ui/src/components/SchemaForm/components/Textarea.tsx
index 6d7c16ed..ed504102 100644
--- a/ui/src/components/SchemaForm/components/Textarea.tsx
+++ b/ui/src/components/SchemaForm/components/Textarea.tsx
@@ -10,7 +10,7 @@ interface Props {
rows: number | undefined;
className: classnames.Argument;
fieldName: string;
- onChange: (evt: React.ChangeEvent<HTMLInputElement>, ...rest) => void;
+ onChange?: (fd: Type.FormDataType) => void;
formData: Type.FormDataType;
}
const Index: FC<Props> = ({
@@ -22,13 +22,28 @@ const Index: FC<Props> = ({
formData,
}) => {
const fieldObject = formData[fieldName];
+ const handleChange = (evt: React.ChangeEvent<HTMLInputElement>) => {
+ const { name, value } = evt.currentTarget;
+ const state = {
+ ...formData,
+ [name]: {
+ ...formData[name],
+ value,
+ isInvalid: false,
+ },
+ };
+ if (typeof onChange === 'function') {
+ onChange(state);
+ }
+ };
+
return (
<Form.Control
as="textarea"
name={fieldName}
placeholder={placeholder}
value={fieldObject?.value || ''}
- onChange={onChange}
+ onChange={handleChange}
isInvalid={fieldObject?.isInvalid}
rows={rows}
className={classnames(className)}
diff --git a/ui/src/components/SchemaForm/components/Timezone.tsx b/ui/src/components/SchemaForm/components/Timezone.tsx
index aca3c7cf..f99bc4c4 100644
--- a/ui/src/components/SchemaForm/components/Timezone.tsx
+++ b/ui/src/components/SchemaForm/components/Timezone.tsx
@@ -5,17 +5,31 @@ import TimeZonePicker from '@/components/TimeZonePicker';
interface Props {
fieldName: string;
- onChange: (evt: React.ChangeEvent<HTMLSelectElement>, ...rest) => void;
+ onChange?: (fd: Type.FormDataType) => void;
formData: Type.FormDataType;
}
const Index: FC<Props> = ({ fieldName, onChange, formData }) => {
const fieldObject = formData[fieldName];
+ const handleChange = (evt: React.ChangeEvent<HTMLSelectElement>) => {
+ const { name, value } = evt.currentTarget;
+ const state = {
+ ...formData,
+ [name]: {
+ ...formData[name],
+ value,
+ isInvalid: false,
+ },
+ };
+ if (typeof onChange === 'function') {
+ onChange(state);
+ }
+ };
return (
<TimeZonePicker
value={fieldObject?.value || ''}
isInvalid={fieldObject?.isInvalid}
name={fieldName}
- onChange={onChange}
+ onChange={handleChange}
/>
);
};
diff --git a/ui/src/components/SchemaForm/components/Upload.tsx b/ui/src/components/SchemaForm/components/Upload.tsx
index f979a368..1e0c5745 100644
--- a/ui/src/components/SchemaForm/components/Upload.tsx
+++ b/ui/src/components/SchemaForm/components/Upload.tsx
@@ -8,7 +8,7 @@ interface Props {
type: Type.UploadType | undefined;
acceptType: string | undefined;
fieldName: string;
- onChange: (key, val) => void;
+ onChange?: (fd: Type.FormDataType) => void;
formData: Type.FormDataType;
}
const Index: FC<Props> = ({
@@ -19,13 +19,26 @@ const Index: FC<Props> = ({
formData,
}) => {
const fieldObject = formData[fieldName];
+ const handleChange = (name: string, value: string) => {
+ console.log('upload: ', name, value);
+ const state = {
+ ...formData,
+ [name]: {
+ ...formData[name],
+ value,
+ },
+ };
+ if (typeof onChange === 'function') {
+ onChange(state);
+ }
+ };
return (
<>
<BrandUpload
type={type}
acceptType={acceptType}
value={fieldObject?.value}
- onChange={(value) => onChange(fieldName, value)}
+ onChange={(value) => handleChange(fieldName, value)}
/>
<Form.Control
name={fieldName}
diff --git a/ui/src/components/SchemaForm/index.tsx b/ui/src/components/SchemaForm/index.tsx
index cc45471b..6fc12dd2 100644
--- a/ui/src/components/SchemaForm/index.tsx
+++ b/ui/src/components/SchemaForm/index.tsx
@@ -148,7 +148,6 @@ 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
* * Automatic field type conversion
@@ -210,38 +209,6 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
useEffect(() => {
setDefaultValueAsDomBehaviour();
}, [formData]);
- const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
- const { name, value } = e.target;
- const data = {
- ...formData,
- [name]: { ...formData[name], value, isInvalid: false },
- };
- if (onChange instanceof Function) {
- onChange(data);
- }
- };
-
- const handleSelectChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
- const { name, value } = e.target;
- const data = {
- ...formData,
- [name]: { ...formData[name], value, isInvalid: false },
- };
- if (onChange instanceof Function) {
- onChange(data);
- }
- };
-
- const handleSwitchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
- const { name, checked } = e.target;
- const data = {
- ...formData,
- [name]: { ...formData[name], value: checked, isInvalid: false },
- };
- if (onChange instanceof Function) {
- onChange(data);
- }
- };
const requiredValidator = () => {
const errors: string[] = [];
@@ -351,32 +318,6 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
}
};
- const handleUploadChange = (name: string, value: string) => {
- const data = { ...formData, [name]: { ...formData[name], value } };
- if (onChange instanceof Function) {
- onChange(data);
- }
- };
-
- const handleInputCheck = (
- e: React.ChangeEvent<HTMLInputElement>,
- index: number,
- ) => {
- const { name, checked } = e.currentTarget;
- const freshVal = checked ? schema.properties[name]?.enum?.[index] : '';
- const data = {
- ...formData,
- [name]: {
- ...formData[name],
- value: freshVal,
- isInvalid: false,
- },
- };
- if (onChange instanceof Function) {
- onChange(data);
- }
- };
-
useImperativeHandle(ref, () => ({
validator,
}));
@@ -422,7 +363,7 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
<Select
desc={description}
fieldName={key}
- onChange={handleSelectChange}
+ onChange={onChange}
enumValues={enumValues}
enumNames={enumNames}
formData={formData}
@@ -432,7 +373,7 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
<Check
type={widget}
fieldName={key}
- onChange={handleInputCheck}
+ onChange={onChange}
enumValues={enumValues}
enumNames={enumNames}
formData={formData}
@@ -443,14 +384,14 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
title={title}
label={uiOpt && 'label' in uiOpt ? uiOpt.label : ''}
fieldName={key}
- onChange={handleSwitchChange}
+ onChange={onChange}
formData={formData}
/>
) : null}
{widget === 'timezone' ? (
<Timezone
fieldName={key}
- onChange={handleSelectChange}
+ onChange={onChange}
formData={formData}
/>
) : null}
@@ -463,7 +404,7 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
uiOpt && 'acceptType' in uiOpt ? uiOpt.acceptType : ''
}
fieldName={key}
- onChange={handleUploadChange}
+ onChange={onChange}
formData={formData}
/>
) : null}
@@ -475,7 +416,7 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
rows={uiOpt && 'rows' in uiOpt ? uiOpt.rows : 3}
className={uiOpt && 'className' in uiOpt ? uiOpt.className : ''}
fieldName={key}
- onChange={handleInputChange}
+ onChange={onChange}
formData={formData}
/>
) : null}
@@ -486,7 +427,7 @@ const SchemaForm: ForwardRefRenderFunction<IRef, IProps> = (
uiOpt && 'placeholder' in uiOpt ? uiOpt.placeholder : ''
}
fieldName={key}
- onChange={handleInputChange}
+ onChange={onChange}
formData={formData}
readOnly={readOnly}
/>