@@ -2,47 +2,82 @@ import React, { CSSProperties, FunctionComponent, useContext, useMemo } from 're
2
2
import { ButtonProps } from './types' ;
3
3
import Loading from '../loading' ;
4
4
import ConfigProviderContext from '../config-provider/config-provider-context' ;
5
+ import ButtonContext from './button-context' ;
5
6
import { useNamespace } from '../../hooks' ;
6
7
import { joinTrim } from '../../utils' ;
7
8
8
9
export const Button : FunctionComponent < Partial < ButtonProps > > = props => {
9
10
const {
10
11
color,
11
12
shape = 'default' ,
12
- plain = false ,
13
+ iconPosition = 'left' ,
13
14
hairline = false ,
14
15
loading = false ,
15
- disabled = false ,
16
- type = 'default' ,
17
- size = 'normal' ,
18
- block = false ,
19
16
children,
20
- iconPosition = 'left' ,
21
17
icon,
22
18
className,
23
19
style,
24
20
loadingText,
21
+ block,
22
+ plain,
25
23
...rest
26
24
} = props ;
27
25
26
+ const { parent } = useContext ( ButtonContext ) ;
27
+
28
+ const currentSize = useMemo ( ( ) => props . size || parent ?. size || 'normal' , [
29
+ props . size ,
30
+ parent ?. size ,
31
+ ] ) ;
32
+
33
+ const currentType = useMemo ( ( ) => props . type || parent ?. type || 'default' , [
34
+ props . type ,
35
+ parent ?. type ,
36
+ ] ) ;
37
+
38
+ const currentPlain = useMemo ( ( ) => ! ! plain || ! ! parent ?. plain , [ plain , parent ?. plain ] ) ;
39
+
40
+ const currentBlock = useMemo ( ( ) => ! ! block || ! ! parent ?. block , [ parent ?. block , block ] ) ;
41
+
42
+ const currentIconPosition = useMemo ( ( ) => parent ?. iconPosition || iconPosition , [
43
+ parent ?. iconPosition ,
44
+ iconPosition ,
45
+ ] ) ;
46
+
47
+ const currentDisabled = React . useMemo ( ( ) => props . disabled ?? parent ?. disabled , [
48
+ parent ?. disabled ,
49
+ props . disabled ,
50
+ ] ) ;
51
+
28
52
const { prefix } = useContext ( ConfigProviderContext ) ;
29
53
const ns = useNamespace ( 'button' , prefix ) ;
30
54
31
55
const varClasses = useMemo ( ( ) => {
32
56
return joinTrim ( [
33
57
ns . b ( ) ,
34
- type ? ns . m ( type ) : '' ,
35
- size ? ns . m ( size ) : '' ,
58
+ ns . m ( currentType ) ,
59
+ ns . m ( currentSize ) ,
36
60
shape ? ns . em ( 'shape' , shape ) : '' ,
37
- plain ? ns . m ( 'plain' ) : '' ,
38
- block ? ns . m ( 'block' ) : '' ,
39
- disabled ? ns . m ( 'disabled' ) : '' ,
61
+ currentPlain ? ns . m ( 'plain' ) : '' ,
62
+ currentBlock ? ns . m ( 'block' ) : '' ,
63
+ currentDisabled ? ns . m ( 'disabled' ) : '' ,
40
64
hairline ? ns . m ( 'hairline' ) : '' ,
41
65
icon ? ns . e ( 'icon' ) : '' ,
42
66
loading ? ns . e ( 'loading' ) : '' ,
43
67
className ,
44
68
] ) ;
45
- } , [ type , size , shape , plain , block , disabled , hairline , icon , loading , className ] ) ;
69
+ } , [
70
+ currentBlock ,
71
+ currentPlain ,
72
+ currentDisabled ,
73
+ currentType ,
74
+ currentSize ,
75
+ shape ,
76
+ hairline ,
77
+ icon ,
78
+ loading ,
79
+ className ,
80
+ ] ) ;
46
81
47
82
const varStyles = useMemo ( ( ) => {
48
83
const styles : CSSProperties = { } ;
@@ -59,7 +94,7 @@ export const Button: FunctionComponent<Partial<ButtonProps>> = props => {
59
94
} , [ plain , color , style ] ) ;
60
95
61
96
const handleClick = ( event : any ) => {
62
- if ( ! loading && ! disabled && props . onClick ) {
97
+ if ( ! loading && ! currentDisabled && props . onClick ) {
63
98
props . onClick ( event ) ;
64
99
}
65
100
} ;
@@ -85,7 +120,7 @@ export const Button: FunctionComponent<Partial<ButtonProps>> = props => {
85
120
< Loading
86
121
size = { loadingSize }
87
122
type = { loadingType }
88
- color = { type === 'default' ? undefined : '' }
123
+ color = { currentType === 'default' ? undefined : '' }
89
124
className = { ns . em ( 'icon' , position ) }
90
125
/>
91
126
) ;
@@ -108,9 +143,9 @@ export const Button: FunctionComponent<Partial<ButtonProps>> = props => {
108
143
109
144
return (
110
145
< div className = { varClasses } style = { { ...varStyles } } { ...rest } onClick = { handleClick } >
111
- { iconPosition === 'left' && renderIcon ( 'left' ) }
146
+ { currentIconPosition === 'left' && renderIcon ( 'left' ) }
112
147
{ renderText ( ) }
113
- { iconPosition === 'right' && renderIcon ( 'right' ) }
148
+ { currentIconPosition === 'right' && renderIcon ( 'right' ) }
114
149
</ div >
115
150
) ;
116
151
} ;
0 commit comments