forked from alibaba/formily
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathButton.tsx
93 lines (87 loc) · 2.3 KB
/
Button.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
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
import React from 'react'
import { FormSpy, LifeCycleTypes, createVirtualBox } from '@uform/react-schema-renderer'
import { Button } from 'antd'
import { ButtonProps } from 'antd/lib/button'
import { ISubmitProps, IResetProps } from '../types'
export const TextButton: React.FC<ButtonProps> = props => (
<Button type="link" {...props} />
)
export const CircleButton: React.FC<ButtonProps> = props => {
const hasText = String(props.className || '').indexOf('has-text') > -1
return (
<Button
type={hasText ? 'link' : undefined}
shape={hasText ? undefined : 'circle'}
{...props}
/>
)
}
export const Submit = ({ showLoading, onSubmit, ...props }: ISubmitProps) => {
return (
<FormSpy
selector={[
LifeCycleTypes.ON_FORM_SUBMIT_START,
LifeCycleTypes.ON_FORM_SUBMIT_END
]}
reducer={(state, action) => {
switch (action.type) {
case LifeCycleTypes.ON_FORM_SUBMIT_START:
return {
...state,
submitting: true
}
case LifeCycleTypes.ON_FORM_SUBMIT_END:
return {
...state,
submitting: false
}
default:
return state
}
}}
>
{({ state, form }) => {
return (
<Button
type="primary"
htmlType="submit"
onClick={() => form.submit(onSubmit)}
disabled={showLoading ? state.submitting : undefined}
{...props}
loading={showLoading ? state.submitting : undefined}
>
{props.children || '提交'}
</Button>
)
}}
</FormSpy>
)
}
Submit.defaultProps = {
showLoading: true
}
export const Reset: React.FC<IResetProps> = ({
children,
forceClear,
validate,
...props
}) => {
return (
<FormSpy selector={[]}>
{({ form }) => {
return (
<Button
{...props}
onClick={() => form.reset({ forceClear, validate })}
>
{children || '重置'}
</Button>
)
}}
</FormSpy>
)
}
createVirtualBox<IResetProps>('reset', Reset)
createVirtualBox<ButtonProps>('text-button', TextButton)
createVirtualBox<ISubmitProps>('submit', Submit)
createVirtualBox<ButtonProps>('circle-button', CircleButton)