1
1
import PropTypes from 'prop-types' ;
2
- import React from 'react' ;
2
+ import React , { useContext } from 'react' ;
3
3
import getRootSizeClassName from '../../../helpers/getRootSizeClassName' ;
4
4
import getRootColorClassName from '../../../helpers/getRootColorClassName' ;
5
+ import { resolveContextOrProp } from '../../../helpers/resolveContextOrProp' ;
5
6
import { withProviderContext } from '../../../provider' ;
6
7
import transferProps from '../../../utils/transferProps' ;
7
8
import withForwardedRef from '../withForwardedRef' ;
9
+ import { ButtonGroupContext } from '../ButtonGroup' ;
8
10
import getRootLabelVisibilityClassName from './helpers/getRootLabelVisibilityClassName' ;
9
11
import getRootPriorityClassName from './helpers/getRootPriorityClassName' ;
10
12
import styles from './Button.scss' ;
@@ -17,7 +19,6 @@ export const Button = ({
17
19
disabled,
18
20
endCorner,
19
21
forwardedRef,
20
- grouped,
21
22
id,
22
23
label,
23
24
labelVisibility,
@@ -28,68 +29,78 @@ export const Button = ({
28
29
type,
29
30
color,
30
31
...restProps
31
- } ) => (
32
- /* No worries, `type` is always assigned correctly through props. */
33
- /* eslint-disable react/button-has-type */
34
- < button
35
- { ...transferProps ( restProps ) }
36
- className = {
37
- priority === 'link'
38
- ? [
39
- styles . root ,
40
- getRootPriorityClassName ( priority , styles ) ,
41
- ] . join ( ' ' )
42
- : [
43
- styles . root ,
44
- getRootPriorityClassName ( priority , styles ) ,
45
- getRootColorClassName ( color , styles ) ,
46
- getRootSizeClassName ( size , styles ) ,
47
- getRootLabelVisibilityClassName ( labelVisibility , styles ) ,
48
- block ? styles . rootBlock : '' ,
49
- grouped ? styles . rootGrouped : '' ,
50
- loadingIcon ? styles . isRootLoading : '' ,
51
- ] . join ( ' ' )
52
- }
53
- disabled = { disabled || ! ! loadingIcon }
54
- id = { id }
55
- onClick = { clickHandler }
56
- ref = { forwardedRef }
57
- type = { type }
58
- >
59
- { priority !== 'link' && startCorner && (
60
- < span className = { styles . startCorner } >
61
- { startCorner }
62
- </ span >
63
- ) }
64
- { beforeLabel && (
65
- < span className = { styles . beforeLabel } >
66
- { beforeLabel }
67
- </ span >
68
- ) }
69
- < span
70
- className = { styles . label }
71
- { ...( id && { id : `${ id } __labelText` } ) }
32
+ } ) => {
33
+ const context = useContext ( ButtonGroupContext ) ;
34
+
35
+ return (
36
+ /* No worries, `type` is always assigned correctly through props. */
37
+ /* eslint-disable react/button-has-type */
38
+ < button
39
+ { ...transferProps ( restProps ) }
40
+ className = {
41
+ priority === 'link'
42
+ ? [
43
+ styles . root ,
44
+ getRootPriorityClassName ( priority , styles ) ,
45
+ ] . join ( ' ' )
46
+ : [
47
+ styles . root ,
48
+ getRootPriorityClassName (
49
+ resolveContextOrProp ( context ?. priority , priority ) ,
50
+ styles ,
51
+ ) ,
52
+ getRootColorClassName ( color , styles ) ,
53
+ getRootSizeClassName (
54
+ resolveContextOrProp ( context ?. size , size ) ,
55
+ styles ,
56
+ ) ,
57
+ getRootLabelVisibilityClassName ( labelVisibility , styles ) ,
58
+ resolveContextOrProp ( context ?. block , block ) ? styles . rootBlock : '' ,
59
+ context ? styles . rootGrouped : '' ,
60
+ loadingIcon ? styles . isRootLoading : '' ,
61
+ ] . join ( ' ' )
62
+ }
63
+ disabled = { resolveContextOrProp ( context ?. disabled , disabled ) || ! ! loadingIcon }
64
+ id = { id }
65
+ onClick = { clickHandler }
66
+ ref = { forwardedRef }
67
+ type = { type }
72
68
>
73
- { label }
74
- </ span >
75
- { afterLabel && (
76
- < span className = { styles . afterLabel } >
77
- { afterLabel }
69
+ { priority !== 'link' && startCorner && (
70
+ < span className = { styles . startCorner } >
71
+ { startCorner }
72
+ </ span >
73
+ ) }
74
+ { beforeLabel && (
75
+ < span className = { styles . beforeLabel } >
76
+ { beforeLabel }
77
+ </ span >
78
+ ) }
79
+ < span
80
+ className = { styles . label }
81
+ { ...( id && { id : `${ id } __labelText` } ) }
82
+ >
83
+ { label }
78
84
</ span >
79
- ) }
80
- { priority !== 'link' && endCorner && (
81
- < span className = { styles . endCorner } >
82
- { endCorner }
83
- </ span >
84
- ) }
85
- { priority !== 'link' && loadingIcon && (
86
- < span className = { styles . loadingIcon } >
87
- { loadingIcon }
88
- </ span >
89
- ) }
90
- </ button >
91
- /* eslint-enable react/button-has-type */
92
- ) ;
85
+ { afterLabel && (
86
+ < span className = { styles . afterLabel } >
87
+ { afterLabel }
88
+ </ span >
89
+ ) }
90
+ { priority !== 'link' && endCorner && (
91
+ < span className = { styles . endCorner } >
92
+ { endCorner }
93
+ </ span >
94
+ ) }
95
+ { priority !== 'link' && loadingIcon && (
96
+ < span className = { styles . loadingIcon } >
97
+ { loadingIcon }
98
+ </ span >
99
+ ) }
100
+ </ button >
101
+ /* eslint-enable react/button-has-type */
102
+ ) ;
103
+ } ;
93
104
94
105
Button . defaultProps = {
95
106
afterLabel : null ,
@@ -100,7 +111,6 @@ Button.defaultProps = {
100
111
disabled : false ,
101
112
endCorner : null ,
102
113
forwardedRef : undefined ,
103
- grouped : false ,
104
114
id : undefined ,
105
115
labelVisibility : 'all' ,
106
116
loadingIcon : null ,
@@ -121,6 +131,9 @@ Button.propTypes = {
121
131
beforeLabel : PropTypes . node ,
122
132
/**
123
133
* If `true`, the button will span the full width of its parent. Only available if `priority` != `link`.
134
+ *
135
+ * Ignored if the component is rendered within `ButtonGroup` component
136
+ * as the value is inherited in such case.
124
137
*/
125
138
block : PropTypes . bool ,
126
139
/**
@@ -133,6 +146,9 @@ Button.propTypes = {
133
146
color : PropTypes . oneOf ( [ 'primary' , 'secondary' , 'success' , 'warning' , 'danger' , 'help' , 'info' , 'note' , 'light' , 'dark' ] ) ,
134
147
/**
135
148
* If `true`, the button will be disabled.
149
+ *
150
+ * Ignored if the component is rendered within `ButtonGroup` component
151
+ * as the value is inherited in such case.
136
152
*/
137
153
disabled : PropTypes . bool ,
138
154
/**
@@ -147,10 +163,6 @@ Button.propTypes = {
147
163
// eslint-disable-next-line react/forbid-prop-types
148
164
PropTypes . shape ( { current : PropTypes . any } ) ,
149
165
] ) ,
150
- /**
151
- * Treat button differently when it's inside `ButtonGroup`. Do not set manually!
152
- */
153
- grouped : PropTypes . bool ,
154
166
/**
155
167
* ID of the root HTML element.
156
168
*
@@ -173,10 +185,16 @@ Button.propTypes = {
173
185
loadingIcon : PropTypes . node ,
174
186
/**
175
187
* Visual priority to highlight or suppress the button.
188
+ *
189
+ * Ignored if the component is rendered within `ButtonGroup` component
190
+ * as the value is inherited in such case.
176
191
*/
177
192
priority : PropTypes . oneOf ( [ 'filled' , 'outline' , 'flat' , 'link' ] ) ,
178
193
/**
179
194
* Size of the button. Only available if `priority` != `link`.
195
+ *
196
+ * Ignored if the component is rendered within `ButtonGroup` component
197
+ * as the value is inherited in such case.
180
198
*/
181
199
size : PropTypes . oneOf ( [ 'small' , 'medium' , 'large' ] ) ,
182
200
/**
0 commit comments