mirror of https://github.com/msgbyte/tailchat
refactor: 使用react-fastify-form替代meta-form
parent
b7ff2a9875
commit
1371b080dd
@ -1,71 +1,73 @@
|
||||
import React, { useMemo } from 'react';
|
||||
import {
|
||||
MetaForm,
|
||||
FastifyForm,
|
||||
regField,
|
||||
MetaFormContainerComponent,
|
||||
FastifyFormContainerComponent,
|
||||
regFormContainer,
|
||||
} from 'meta-form';
|
||||
} from 'react-fastify-form';
|
||||
import { Form, Button } from 'antd';
|
||||
|
||||
import { MetaFormText } from './types/Text';
|
||||
import { MetaFormTextArea } from './types/TextArea';
|
||||
import { MetaFormPassword } from './types/Password';
|
||||
import { MetaFormSelect } from './types/Select';
|
||||
import { MetaFormCheckbox } from './types/Checkbox';
|
||||
import { MetaFormCustom } from './types/Custom';
|
||||
import { FastifyFormText } from './types/Text';
|
||||
import { FastifyFormTextArea } from './types/TextArea';
|
||||
import { FastifyFormPassword } from './types/Password';
|
||||
import { FastifyFormSelect } from './types/Select';
|
||||
import { FastifyFormCheckbox } from './types/Checkbox';
|
||||
import { FastifyFormCustom } from './types/Custom';
|
||||
|
||||
regField('text', MetaFormText);
|
||||
regField('textarea', MetaFormTextArea);
|
||||
regField('password', MetaFormPassword);
|
||||
regField('select', MetaFormSelect);
|
||||
regField('checkbox', MetaFormCheckbox);
|
||||
regField('custom', MetaFormCustom);
|
||||
regField('text', FastifyFormText);
|
||||
regField('textarea', FastifyFormTextArea);
|
||||
regField('password', FastifyFormPassword);
|
||||
regField('select', FastifyFormSelect);
|
||||
regField('checkbox', FastifyFormCheckbox);
|
||||
regField('custom', FastifyFormCustom);
|
||||
|
||||
const WebFastifyFormContainer: FastifyFormContainerComponent = React.memo(
|
||||
(props) => {
|
||||
const layout = props.layout;
|
||||
const submitButtonRender = useMemo(() => {
|
||||
return (
|
||||
<Form.Item
|
||||
wrapperCol={
|
||||
layout === 'vertical'
|
||||
? { xs: 24 }
|
||||
: { sm: 24, md: { span: 16, offset: 8 } }
|
||||
}
|
||||
>
|
||||
<Button
|
||||
loading={props.loading}
|
||||
type="primary"
|
||||
size="large"
|
||||
htmlType="button"
|
||||
style={{ width: '100%' }}
|
||||
onClick={() => props.handleSubmit()}
|
||||
disabled={props.canSubmit === false}
|
||||
>
|
||||
{props.submitLabel ?? '提交'}
|
||||
</Button>
|
||||
</Form.Item>
|
||||
);
|
||||
}, [
|
||||
props.loading,
|
||||
props.handleSubmit,
|
||||
props.canSubmit,
|
||||
props.submitLabel,
|
||||
layout,
|
||||
]);
|
||||
|
||||
const WebMetaFormContainer: MetaFormContainerComponent = React.memo((props) => {
|
||||
const layout = props.layout;
|
||||
const submitButtonRender = useMemo(() => {
|
||||
return (
|
||||
<Form.Item
|
||||
wrapperCol={
|
||||
layout === 'vertical'
|
||||
? { xs: 24 }
|
||||
: { sm: 24, md: { span: 16, offset: 8 } }
|
||||
}
|
||||
<Form
|
||||
layout={layout}
|
||||
labelCol={layout === 'vertical' ? { xs: 24 } : { sm: 24, md: 8 }}
|
||||
wrapperCol={layout === 'vertical' ? { xs: 24 } : { sm: 24, md: 16 }}
|
||||
>
|
||||
<Button
|
||||
loading={props.loading}
|
||||
type="primary"
|
||||
size="large"
|
||||
htmlType="button"
|
||||
style={{ width: '100%' }}
|
||||
onClick={() => props.handleSubmit()}
|
||||
disabled={props.canSubmit === false}
|
||||
>
|
||||
{props.submitLabel ?? '提交'}
|
||||
</Button>
|
||||
</Form.Item>
|
||||
{props.children}
|
||||
{submitButtonRender}
|
||||
</Form>
|
||||
);
|
||||
}, [
|
||||
props.loading,
|
||||
props.handleSubmit,
|
||||
props.canSubmit,
|
||||
props.submitLabel,
|
||||
layout,
|
||||
]);
|
||||
|
||||
return (
|
||||
<Form
|
||||
layout={layout}
|
||||
labelCol={layout === 'vertical' ? { xs: 24 } : { sm: 24, md: 8 }}
|
||||
wrapperCol={layout === 'vertical' ? { xs: 24 } : { sm: 24, md: 16 }}
|
||||
>
|
||||
{props.children}
|
||||
{submitButtonRender}
|
||||
</Form>
|
||||
);
|
||||
});
|
||||
WebMetaFormContainer.displayName = 'WebMetaFormContainer';
|
||||
regFormContainer(WebMetaFormContainer);
|
||||
}
|
||||
);
|
||||
WebFastifyFormContainer.displayName = 'WebFastifyFormContainer';
|
||||
regFormContainer(WebFastifyFormContainer);
|
||||
|
||||
export const WebMetaForm = MetaForm;
|
||||
WebMetaForm.displayName = 'WebMetaForm';
|
||||
export const WebMetaForm = FastifyForm;
|
||||
(WebMetaForm as any).displayName = 'WebMetaForm';
|
||||
|
@ -1,25 +1,27 @@
|
||||
import React from 'react';
|
||||
import { Form, Checkbox } from 'antd';
|
||||
import type { MetaFormFieldComponent } from 'meta-form';
|
||||
import type { FastifyFormFieldComponent } from 'react-fastify-form';
|
||||
import { getValidateStatus } from '../utils';
|
||||
|
||||
export const MetaFormCheckbox: MetaFormFieldComponent = React.memo((props) => {
|
||||
const { name, label, value, onChange, error } = props;
|
||||
export const FastifyFormCheckbox: FastifyFormFieldComponent = React.memo(
|
||||
(props) => {
|
||||
const { name, label, value, onChange, error } = props;
|
||||
|
||||
return (
|
||||
<Form.Item
|
||||
label={label}
|
||||
validateStatus={getValidateStatus(error)}
|
||||
help={error}
|
||||
>
|
||||
<Checkbox
|
||||
name={name}
|
||||
checked={Boolean(value)}
|
||||
onChange={(e) => onChange(e.target.checked)}
|
||||
return (
|
||||
<Form.Item
|
||||
label={label}
|
||||
validateStatus={getValidateStatus(error)}
|
||||
help={error}
|
||||
>
|
||||
{label}
|
||||
</Checkbox>
|
||||
</Form.Item>
|
||||
);
|
||||
});
|
||||
MetaFormCheckbox.displayName = 'MetaFormCheckbox';
|
||||
<Checkbox
|
||||
name={name}
|
||||
checked={Boolean(value)}
|
||||
onChange={(e) => onChange(e.target.checked)}
|
||||
>
|
||||
{label}
|
||||
</Checkbox>
|
||||
</Form.Item>
|
||||
);
|
||||
}
|
||||
);
|
||||
FastifyFormCheckbox.displayName = 'FastifyFormCheckbox';
|
||||
|
@ -1,27 +1,30 @@
|
||||
import React from 'react';
|
||||
import { Input, Form } from 'antd';
|
||||
import type { MetaFormFieldComponent } from 'meta-form';
|
||||
import type { FastifyFormFieldComponent } from 'react-fastify-form';
|
||||
import { getValidateStatus } from '../utils';
|
||||
|
||||
export const MetaFormPassword: MetaFormFieldComponent = React.memo((props) => {
|
||||
const { name, label, value, onChange, error, maxLength, placeholder } = props;
|
||||
export const FastifyFormPassword: FastifyFormFieldComponent = React.memo(
|
||||
(props) => {
|
||||
const { name, label, value, onChange, error, maxLength, placeholder } =
|
||||
props;
|
||||
|
||||
return (
|
||||
<Form.Item
|
||||
label={label}
|
||||
validateStatus={getValidateStatus(error)}
|
||||
help={error}
|
||||
>
|
||||
<Input.Password
|
||||
name={name}
|
||||
type="password"
|
||||
size="large"
|
||||
maxLength={maxLength}
|
||||
placeholder={placeholder}
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
/>
|
||||
</Form.Item>
|
||||
);
|
||||
});
|
||||
MetaFormPassword.displayName = 'MetaFormPassword';
|
||||
return (
|
||||
<Form.Item
|
||||
label={label}
|
||||
validateStatus={getValidateStatus(error)}
|
||||
help={error}
|
||||
>
|
||||
<Input.Password
|
||||
name={name}
|
||||
type="password"
|
||||
size="large"
|
||||
maxLength={maxLength}
|
||||
placeholder={placeholder}
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
/>
|
||||
</Form.Item>
|
||||
);
|
||||
}
|
||||
);
|
||||
FastifyFormPassword.displayName = 'FastifyFormPassword';
|
||||
|
@ -1,26 +1,29 @@
|
||||
import React from 'react';
|
||||
import { Input, Form } from 'antd';
|
||||
import type { MetaFormFieldComponent } from 'meta-form';
|
||||
import type { FastifyFormFieldComponent } from 'react-fastify-form';
|
||||
import { getValidateStatus } from '../utils';
|
||||
|
||||
export const MetaFormText: MetaFormFieldComponent = React.memo((props) => {
|
||||
const { name, label, value, onChange, error, maxLength, placeholder } = props;
|
||||
export const FastifyFormText: FastifyFormFieldComponent = React.memo(
|
||||
(props) => {
|
||||
const { name, label, value, onChange, error, maxLength, placeholder } =
|
||||
props;
|
||||
|
||||
return (
|
||||
<Form.Item
|
||||
label={label}
|
||||
validateStatus={getValidateStatus(error)}
|
||||
help={error}
|
||||
>
|
||||
<Input
|
||||
name={name}
|
||||
size="large"
|
||||
maxLength={maxLength}
|
||||
placeholder={placeholder}
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
/>
|
||||
</Form.Item>
|
||||
);
|
||||
});
|
||||
MetaFormText.displayName = 'MetaFormText';
|
||||
return (
|
||||
<Form.Item
|
||||
label={label}
|
||||
validateStatus={getValidateStatus(error)}
|
||||
help={error}
|
||||
>
|
||||
<Input
|
||||
name={name}
|
||||
size="large"
|
||||
maxLength={maxLength}
|
||||
placeholder={placeholder}
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
/>
|
||||
</Form.Item>
|
||||
);
|
||||
}
|
||||
);
|
||||
FastifyFormText.displayName = 'FastifyFormText';
|
||||
|
@ -1,26 +1,29 @@
|
||||
import React from 'react';
|
||||
import { Input, Form } from 'antd';
|
||||
import type { MetaFormFieldComponent } from 'meta-form';
|
||||
import type { FastifyFormFieldComponent } from 'react-fastify-form';
|
||||
import { getValidateStatus } from '../utils';
|
||||
|
||||
export const MetaFormTextArea: MetaFormFieldComponent = React.memo((props) => {
|
||||
const { name, label, value, onChange, error, maxLength, placeholder } = props;
|
||||
export const FastifyFormTextArea: FastifyFormFieldComponent = React.memo(
|
||||
(props) => {
|
||||
const { name, label, value, onChange, error, maxLength, placeholder } =
|
||||
props;
|
||||
|
||||
return (
|
||||
<Form.Item
|
||||
label={label}
|
||||
validateStatus={getValidateStatus(error)}
|
||||
help={error}
|
||||
>
|
||||
<Input.TextArea
|
||||
name={name}
|
||||
rows={4}
|
||||
maxLength={maxLength}
|
||||
placeholder={placeholder}
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
/>
|
||||
</Form.Item>
|
||||
);
|
||||
});
|
||||
MetaFormTextArea.displayName = 'MetaFormTextArea';
|
||||
return (
|
||||
<Form.Item
|
||||
label={label}
|
||||
validateStatus={getValidateStatus(error)}
|
||||
help={error}
|
||||
>
|
||||
<Input.TextArea
|
||||
name={name}
|
||||
rows={4}
|
||||
maxLength={maxLength}
|
||||
placeholder={placeholder}
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
/>
|
||||
</Form.Item>
|
||||
);
|
||||
}
|
||||
);
|
||||
FastifyFormTextArea.displayName = 'FastifyFormTextArea';
|
||||
|
Loading…
Reference in New Issue