index.tsx
4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import type { CSSProperties, ReactNode } from 'react';
import type { FormInstance, FormProps } from 'antd';
import { UCascader, UDescriptions, UForm, UFormItem, UInput, USelect, USwitch } from '@uj/ui';
import type { FieldSchema } from '../formSchema';
export interface CombiFormProps<T extends Record<string, unknown>> {
mode: 'form' | 'detail';
fields: FieldSchema<T>[];
form?: FormInstance<T>;
initialValues?: Partial<T>;
onSubmit?: (values: T) => void;
layout?: FormProps['layout'];
formStyle?: CSSProperties;
sectionContentStyle?: CSSProperties;
renderSectionTitle?: (title: string) => ReactNode;
renderControl?: (
field: FieldSchema<T>,
form: FormInstance<T>,
values: T,
) => ReactNode | undefined;
}
export function CombiForm<T extends Record<string, unknown>>({
mode,
fields,
form: externalForm,
initialValues,
onSubmit,
layout,
formStyle,
sectionContentStyle,
renderSectionTitle,
renderControl,
}: CombiFormProps<T>) {
const [innerForm] = UForm.useForm<T>();
const form = externalForm ?? innerForm;
const values = UForm.useWatch([], form) as T | undefined;
const data = (values ?? initialValues ?? {}) as T;
const visible = fields.filter(
(field) => !(typeof field.hidden === 'function' ? field.hidden(data) : field.hidden),
);
if (mode === 'detail')
return (
<UDescriptions
column={1}
items={visible.map((field) => ({
key: field.name,
label: field.label,
children: field.render
? field.render(data[field.name], data)
: String(data[field.name] ?? '—'),
}))}
/>
);
const sections = visible.reduce<{ title?: string; fields: FieldSchema<T>[] }[]>(
(groups, field) => {
const last = groups.at(-1);
if (last && last.title === field.section) last.fields.push(field);
else groups.push({ title: field.section, fields: [field] });
return groups;
},
[],
);
const defaultControl = (field: FieldSchema<T>) => {
const disabled = typeof field.disabled === 'function' ? field.disabled(data) : field.disabled;
if (field.type === 'cascader')
return (
<UCascader
options={field.options}
disabled={disabled}
style={{ width: '100%' }}
{...field.cascaderProps}
/>
);
if (field.type === 'select' || field.type === 'selectMultiple')
return (
<USelect
options={field.options}
mode={field.type === 'selectMultiple' ? 'multiple' : undefined}
placeholder={
field.selectProps?.placeholder ??
'请选择' + (typeof field.label === 'string' ? field.label : '')
}
disabled={disabled}
style={{ width: '100%' }}
{...field.selectProps}
/>
);
if (field.type === 'switch') return <USwitch disabled={disabled} />;
if (field.type === 'textarea')
return (
<UInput.TextArea
disabled={disabled}
placeholder={inputPlaceholder(field)}
{...field.textareaProps}
/>
);
return (
<UInput disabled={disabled} placeholder={inputPlaceholder(field)} {...field.inputProps} />
);
};
return (
<UForm
form={form}
initialValues={initialValues}
layout={layout}
style={formStyle}
onFinish={(next) => onSubmit?.(next as T)}
>
{sections.map((section, sectionIndex) => (
<section key={`${section.title ?? 'default'}-${sectionIndex}`}>
{section.title ? renderSectionTitle?.(section.title) : null}
<div style={sectionContentStyle}>
{section.fields.map((field) => (
<UFormItem
key={field.name}
name={field.name as string}
label={field.label}
rules={field.rules}
style={field.itemStyle}
normalize={field.normalize}
>
{renderControl?.(field, form, data) ?? defaultControl(field)}
</UFormItem>
))}
</div>
</section>
))}
</UForm>
);
}
function inputPlaceholder<T extends Record<string, unknown>>(field: FieldSchema<T>): string {
if (field.placeholder) return field.placeholder;
return `请输入${typeof field.label === 'string' ? field.label : '内容'}`;
}