Skip to content

Commit

Permalink
perf: 优化表单与布局,增加表单校验
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Aug 5, 2022
1 parent f1f63be commit fdd7c6f
Show file tree
Hide file tree
Showing 17 changed files with 221 additions and 94 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
{
"name": "tushan",
"version": "1.0.0",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -44,7 44,7 @@
"lodash": "^4.17.21",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-fastify-form": "^1.0.2",
"react-fastify-form": "^1.0.8",
"react-router": "^6.3.0",
"react-router-dom": "^6.3.0",
"reflect-metadata": "^0.1.13",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions src/Tushan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 47,14 @@ export class Tushan {
return componentId;
}

static getCacheDir(): string {
const cacheDir = findCacheDir({ name: 'tushan' });
if (!cacheDir) {
throw new Error('cannot find package.json file');
}
return cacheDir;
}

datasource: DataSource;

constructor(public options: TushanOptions) {
Expand Down Expand Up @@ -89,18 97,13 @@ export class Tushan {
createViteServer();
}

this.buildComponentEntry();
await this.buildComponentEntry();
}

private async buildComponentEntry() {
const cacheDir = findCacheDir({ name: 'tushan' });
if (!cacheDir) {
throw new Error('cannot find package.json file');
}
const cacheDir = Tushan.getCacheDir();
await fs.ensureDir(cacheDir);

console.log(Tushan.customComponents);

const js = `
${Object.entries(Tushan.customComponents)
.map(([name, url]) => `import ${name} from '${url}';`)
Expand Down
2 changes: 1 addition & 1 deletion src/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 7,7 @@
<title>Tushan</title>
</head>
<body>
<div id="app">
<div id="app" class="h-full">

</div>
<!--SCRIPTS-SLOT-->
Expand Down
109 changes: 64 additions & 45 deletions src/client/src/components/form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 1,12 @@
import React, { useMemo } from 'react';
import React, { useEffect, useMemo } from 'react';
import {
FastifyForm,
regField,
regFormContainer,
FastifyFormContainerProps,
useFastifyFormContext,
FastifyFormInstance,
FastifyFormProps,
} from 'react-fastify-form';
import { Form, Button } from '@arco-design/web-react';
import { FastifyFormText } from './types/Text';
Expand All @@ -22,55 25,71 @@ regField('select', FastifyFormSelect);
regField('checkbox', FastifyFormCheckbox);
regField('custom', FastifyFormCustom);

const FastifyFormContainer: React.FC<FastifyFormContainerProps> = React.memo(
(props) => {
const layout = props.layout;
const hiddenSubmit: boolean = props.extraProps?.hiddenSubmit ?? false;
interface FastifyFormContainerExtraProps {
hiddenSubmit?: boolean;
onFormMount?: (form: FastifyFormInstance) => void;
}

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 FastifyFormContainer: React.FC<
FastifyFormContainerProps<FastifyFormContainerExtraProps>
> = React.memo((props) => {
const layout = props.layout;
const hiddenSubmit: boolean = props.extraProps?.hiddenSubmit ?? false;
const form = useFastifyFormContext();

useEffect(() => {
if (form) {
props.extraProps?.onFormMount?.(form);
}
}, []);

const submitButtonRender = useMemo(() => {
return (
<Form
layout={layout}
labelCol={layout === 'vertical' ? { xs: 24 } : { sm: 24, md: 8 }}
wrapperCol={layout === 'vertical' ? { xs: 24 } : { sm: 24, md: 16 }}
<Form.Item
wrapperCol={
layout === 'vertical'
? { xs: 24 }
: { sm: 24, md: { span: 16, offset: 8 } }
}
>
{props.children}
{!hiddenSubmit && submitButtonRender}
</Form>
<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,
]);

return (
<Form
layout={layout}
labelCol={layout === 'vertical' ? { xs: 24 } : { sm: 24, md: 8 }}
wrapperCol={layout === 'vertical' ? { xs: 24 } : { sm: 24, md: 16 }}
>
{props.children}
{!hiddenSubmit && submitButtonRender}
</Form>
);
});
FastifyFormContainer.displayName = 'FastifyFormContainer';
regFormContainer(FastifyFormContainer);

export const WebFastifyForm = FastifyForm;
WebFastifyForm.displayName = 'WebFastifyForm';
export const WebFastifyForm = FastifyForm as <
T extends Record<string, any> = {}
>(
props: FastifyFormProps<T, FastifyFormContainerExtraProps>
) => React.ReactElement | null;
(WebFastifyForm as any).displayName = 'WebFastifyForm';
3 changes: 2 additions & 1 deletion src/client/src/components/form/types/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 5,14 @@ import { getValidateStatus } from '../utils';

export const FastifyFormCheckbox: FastifyFormFieldComponent = React.memo(
(props) => {
const { name, label, value, onChange, error } = props;
const { name, label, required, value, onChange, error } = props;

return (
<Form.Item
label={label}
validateStatus={getValidateStatus(error)}
help={error}
required={required}
>
<Checkbox
checked={Boolean(value)}
Expand Down
4 changes: 2 additions & 2 deletions src/client/src/components/form/types/Custom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 9,10 @@ import { CustomField } from 'react-fastify-form';
export const FastifyFormCustom: FastifyFormFieldComponent<{
render: (props: FastifyFormFieldProps) => React.ReactNode;
}> = React.memo((props) => {
const { label } = props;
const { label, required } = props;

return (
<Form.Item label={label}>
<Form.Item label={label} required={required}>
<CustomField {...props} />
</Form.Item>
);
Expand Down
13 changes: 11 additions & 2 deletions src/client/src/components/form/types/Number.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 5,23 @@ import { getValidateStatus } from '../utils';

export const FastifyFormNumber: FastifyFormFieldComponent = React.memo(
(props) => {
const { name, label, value, onChange, error, maxLength, placeholder } =
props;
const {
name,
label,
required,
value,
onChange,
error,
maxLength,
placeholder,
} = props;

return (
<Form.Item
label={label}
validateStatus={getValidateStatus(error)}
help={error}
required={required}
>
<InputNumber
name={name}
Expand Down
13 changes: 11 additions & 2 deletions src/client/src/components/form/types/Password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 5,23 @@ import { getValidateStatus } from '../utils';

export const FastifyFormPassword: FastifyFormFieldComponent = React.memo(
(props) => {
const { name, label, value, onChange, error, maxLength, placeholder } =
props;
const {
name,
label,
required,
value,
onChange,
error,
maxLength,
placeholder,
} = props;

return (
<Form.Item
label={label}
validateStatus={getValidateStatus(error)}
help={error}
required={required}
>
<Input.Password
name={name}
Expand Down
4 changes: 2 additions & 2 deletions src/client/src/components/form/types/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 14,7 @@ interface FastifyFormSelectOptionsItem {
export const FastifyFormSelect: FastifyFormFieldComponent<{
options: FastifyFormSelectOptionsItem[];
}> = React.memo((props) => {
const { name, label, value, onChange, options } = props;
const { name, label, required, value, onChange, options } = props;

useEffect(() => {
if (_isNil(value) || value === '') {
Expand All @@ -24,7 24,7 @@ export const FastifyFormSelect: FastifyFormFieldComponent<{
}, []);

return (
<Form.Item label={label}>
<Form.Item label={label} required={required}>
<Select size="large" value={value} onChange={(val) => onChange(val)}>
{options.map((option, i) => (
<Option key={`${option.value}${i}`} value={option.value}>
Expand Down
13 changes: 11 additions & 2 deletions src/client/src/components/form/types/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 5,23 @@ import { getValidateStatus } from '../utils';

export const FastifyFormText: FastifyFormFieldComponent = React.memo(
(props) => {
const { name, label, value, onChange, error, maxLength, placeholder } =
props;
const {
name,
label,
value,
onChange,
error,
required,
maxLength,
placeholder,
} = props;

return (
<Form.Item
label={label}
validateStatus={getValidateStatus(error)}
help={error}
required={required}
>
<Input
name={name}
Expand Down
13 changes: 11 additions & 2 deletions src/client/src/components/form/types/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 5,23 @@ import { getValidateStatus } from '../utils';

export const FastifyFormTextArea: FastifyFormFieldComponent = React.memo(
(props) => {
const { name, label, value, onChange, error, maxLength, placeholder } =
props;
const {
name,
label,
value,
onChange,
error,
required,
maxLength,
placeholder,
} = props;

return (
<Form.Item
label={label}
validateStatus={getValidateStatus(error)}
help={error}
required={required}
>
<Input.TextArea
name={name}
Expand Down
Loading

0 comments on commit fdd7c6f

Please sign in to comment.