-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsection-form.tsx
55 lines (50 loc) · 1.54 KB
/
section-form.tsx
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
import * as React from 'react';
import { createElement, cloneElement } from 'react';
import { Form, Card, Button } from '@alifd/next';
import type { FormProps } from '@alifd/next/types/form';
import type { Operations } from '../form/form';
import ProCard from '../pro-card';
export interface SectionFormProps extends FormProps {
operations: Operations[];
isPreview: boolean;
color: string;
}
const SectionForm: React.ForwardRefRenderFunction<React.Component, SectionFormProps> = (
props,
ref,
) => {
const { children, isPreview, operations = [], color, ...otherProps } = props;
return (
<Form {...otherProps} ref={ref}>
{children ? formatChildForm(children, { color }) : null}
{isPreview ? null : (
<div style={{ textAlign: 'center' }}>
<Button.Group>
{operations.map(({ id, content, type, onClick }) => {
return (
<Button key={id} onClick={onClick} type={type}>
{content}
</Button>
);
})}
</Button.Group>
</div>
)}
</Form>
);
};
const SectionFormRef = React.forwardRef(SectionForm);
export default SectionFormRef;
const formatChildForm = (children, globalProps) => {
const { color } = globalProps;
return React.Children.map(children, (child) => {
const { props } = child;
const { cardProps = {} } = props;
const { primaryKey, ...restProps } = cardProps;
return (
<ProCard key={primaryKey} {...restProps} color={color}>
{child}
</ProCard>
);
});
};