feat(ui): add timezone,upload type

This commit is contained in:
robin 2022-11-10 16:06:56 +08:00
parent 860f39664e
commit 297186d1ea
1 changed files with 51 additions and 0 deletions

View File

@ -2,6 +2,8 @@ import { FC } from 'react';
import { Form, Button, Stack } from 'react-bootstrap'; import { Form, Button, Stack } from 'react-bootstrap';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import BrandUpload from '../BrandUpload';
import TimeZonePicker from '../TimeZonePicker';
import type * as Type from '@/common/interface'; import type * as Type from '@/common/interface';
export interface JSONSchema { export interface JSONSchema {
@ -27,6 +29,8 @@ export interface UISchema {
| 'checkbox' | 'checkbox'
| 'radio' | 'radio'
| 'select' | 'select'
| 'upload'
| 'timezone'
| 'switch'; | 'switch';
'ui:options'?: { 'ui:options'?: {
rows?: number; rows?: number;
@ -49,6 +53,8 @@ export interface UISchema {
empty?: string; empty?: string;
invalid?: string; invalid?: string;
validator?: (value) => boolean; validator?: (value) => boolean;
textRender?: () => React.ReactElement;
imageType?: 'avatar' | 'logo';
}; };
}; };
} }
@ -61,6 +67,14 @@ interface IProps {
onSubmit: (e: React.FormEvent) => void; onSubmit: (e: React.FormEvent) => void;
} }
/**
* json schema form
* @param schema json schema
* @param uiSchema ui schema
* @param formData form data
* @param onChange change event
* @param onSubmit submit event
*/
const SchemaForm: FC<IProps> = ({ const SchemaForm: FC<IProps> = ({
schema, schema,
uiSchema = {}, uiSchema = {},
@ -81,6 +95,7 @@ const SchemaForm: FC<IProps> = ({
onChange(data); onChange(data);
} }
}; };
const requiredValidator = () => { const requiredValidator = () => {
const required = schema.required || []; const required = schema.required || [];
const errors: string[] = []; const errors: string[] = [];
@ -91,6 +106,7 @@ const SchemaForm: FC<IProps> = ({
}); });
return errors; return errors;
}; };
const syncValidator = () => { const syncValidator = () => {
const errors: string[] = []; const errors: string[] = [];
keys.forEach((key) => { keys.forEach((key) => {
@ -104,6 +120,7 @@ const SchemaForm: FC<IProps> = ({
}); });
return errors; return errors;
}; };
const handleSubmit = (e: React.FormEvent) => { const handleSubmit = (e: React.FormEvent) => {
e.preventDefault(); e.preventDefault();
const errors = requiredValidator(); const errors = requiredValidator();
@ -149,6 +166,14 @@ const SchemaForm: FC<IProps> = ({
} }
onSubmit(e); onSubmit(e);
}; };
const handleUploadChange = (name: string, value: string) => {
const data = { ...formData, [name]: { ...formData[name], value } };
if (onChange instanceof Function) {
onChange(data);
}
};
return ( return (
<Form noValidate onSubmit={handleSubmit}> <Form noValidate onSubmit={handleSubmit}>
{keys.map((key) => { {keys.map((key) => {
@ -222,6 +247,32 @@ const SchemaForm: FC<IProps> = ({
</Form.Group> </Form.Group>
); );
} }
if (widget === 'timezone') {
return (
<Form.Group key={title} className="mb-3" controlId={key}>
<Form.Label>{title}</Form.Label>
<TimeZonePicker
value={formData[key]?.value}
onChange={handleInputChange}
/>
<Form.Text className="text-muted">{description}</Form.Text>
</Form.Group>
);
}
if (widget === 'upload') {
return (
<Form.Group key={title} className="mb-3" controlId={key}>
<Form.Label>{title}</Form.Label>
<BrandUpload
type={options.imageType || 'avatar'}
value={formData[key]?.value}
onChange={(value) => handleUploadChange(key, value)}
/>
<Form.Text className="text-muted">{description}</Form.Text>
</Form.Group>
);
}
const as = widget === 'textarea' ? 'textarea' : 'input'; const as = widget === 'textarea' ? 'textarea' : 'input';
return ( return (