feat: 自行开发设计 ui样式优化 超管端-控制台 相关的页面
This commit is contained in:
+54
-11
@@ -29,6 +29,8 @@ import React, {
|
||||
} from 'react';
|
||||
import { Checkbox, Form as AntForm } from 'antd';
|
||||
import * as Controls from './controls';
|
||||
import { useConsolePage } from '../console/ConsoleContext';
|
||||
import { ConsoleForm } from '../console/ConsolePrimitives';
|
||||
|
||||
const FormContext = createContext(null);
|
||||
const fieldPath = (field) =>
|
||||
@@ -36,6 +38,26 @@ const fieldPath = (field) =>
|
||||
? field.replace(/\[(\d+)\]/g, '.$1').split('.')
|
||||
: field;
|
||||
|
||||
function FormRenderContent({ form, api, children }) {
|
||||
const values = form.getFieldsValue(true);
|
||||
return children({ formState: { values }, formApi: api, values });
|
||||
}
|
||||
|
||||
function fieldUpdate(form, path, value) {
|
||||
let update = value;
|
||||
for (let index = path.length - 1; index > 0; index--) {
|
||||
const previous = form.getFieldValue(path.slice(0, index));
|
||||
const parent = Array.isArray(previous)
|
||||
? [...previous]
|
||||
: previous == null && typeof path[index] === 'number'
|
||||
? []
|
||||
: { ...previous };
|
||||
parent[path[index]] = update;
|
||||
update = parent;
|
||||
}
|
||||
return { [path[0]]: update };
|
||||
}
|
||||
|
||||
export const Form = forwardRef(function Form(
|
||||
{
|
||||
initValues,
|
||||
@@ -56,6 +78,8 @@ export const Form = forwardRef(function Form(
|
||||
},
|
||||
ref,
|
||||
) {
|
||||
const consolePage = useConsolePage();
|
||||
const FormComponent = consolePage ? ConsoleForm : AntForm;
|
||||
const [form] = AntForm.useForm();
|
||||
const [, renderValues] = useReducer((value) => value + 1, 0);
|
||||
const callbacks = useRef({ getFormApi, onValueChange });
|
||||
@@ -65,7 +89,8 @@ export const Form = forwardRef(function Form(
|
||||
getValues: () => form.getFieldsValue(true),
|
||||
getValue: (field) => form.getFieldValue(fieldPath(field)),
|
||||
setValue: (field, value) => {
|
||||
form.setFieldValue(fieldPath(field), value);
|
||||
// External value updates avoid setFieldValue's redundant validation-meta reset.
|
||||
form.setFieldsValue(fieldUpdate(form, fieldPath(field), value));
|
||||
callbacks.current.onValueChange?.(form.getFieldsValue(true), {
|
||||
[field]: value,
|
||||
});
|
||||
@@ -83,6 +108,12 @@ export const Form = forwardRef(function Form(
|
||||
},
|
||||
validate: (fields) => form.validateFields(fields?.map?.(fieldPath)),
|
||||
submitForm: () => form.submit(),
|
||||
scrollToError: () => {
|
||||
const first = form
|
||||
.getFieldsError()
|
||||
.find((field) => field.errors.length);
|
||||
if (first) form.scrollToField(first.name, { block: 'nearest' });
|
||||
},
|
||||
getFieldExist: (field) =>
|
||||
form.getFieldInstance(fieldPath(field)) !== undefined,
|
||||
}),
|
||||
@@ -98,7 +129,7 @@ export const Form = forwardRef(function Form(
|
||||
}, [form, values]);
|
||||
return (
|
||||
<FormContext.Provider value={form}>
|
||||
<AntForm
|
||||
<FormComponent
|
||||
{...props}
|
||||
form={form}
|
||||
initialValues={initValues}
|
||||
@@ -122,20 +153,32 @@ export const Form = forwardRef(function Form(
|
||||
}
|
||||
labelAlign={labelAlign}
|
||||
onFinish={onSubmit}
|
||||
onFinishFailed={onSubmitFail}
|
||||
onFinishFailed={
|
||||
consolePage && onSubmitFail
|
||||
? ({ errorFields }) =>
|
||||
onSubmitFail(
|
||||
Object.fromEntries(
|
||||
errorFields.map(({ name, errors }) => [
|
||||
name.join('.'),
|
||||
errors,
|
||||
]),
|
||||
),
|
||||
)
|
||||
: onSubmitFail
|
||||
}
|
||||
onValuesChange={(_, all) => {
|
||||
callbacks.current.onValueChange?.(all);
|
||||
renderValues();
|
||||
}}
|
||||
>
|
||||
{typeof children === 'function'
|
||||
? children({
|
||||
formState: { values: form.getFieldsValue(true) },
|
||||
formApi: api,
|
||||
values: form.getFieldsValue(true),
|
||||
})
|
||||
: children}
|
||||
</AntForm>
|
||||
{typeof children === 'function' ? (
|
||||
<FormRenderContent form={form} api={api}>
|
||||
{children}
|
||||
</FormRenderContent>
|
||||
) : (
|
||||
children
|
||||
)}
|
||||
</FormComponent>
|
||||
</FormContext.Provider>
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user