1
- import React from 'react' ;
2
- import propTypes from 'prop-types ' ;
1
+ import React , { forwardRef } from 'react' ;
2
+ import styled , { css , CSSProperties } from 'styled-components ' ;
3
3
4
- import styled , { css } from 'styled-components' ;
5
4
import { createFlatBoxStyles } from '../common' ;
6
5
import { StyledCutout } from '../Cutout/Cutout' ;
7
6
import { StyledListItem } from '../ListItem/ListItem' ;
8
-
9
7
import {
8
+ LabelText ,
10
9
size ,
11
10
StyledInput ,
12
- StyledLabel ,
13
- LabelText
11
+ StyledLabel
14
12
} from '../SwitchBase/SwitchBase' ;
13
+ import { CommonStyledProps } from '../types' ;
14
+
15
+ type RadioVariant = 'default' | 'flat' | 'menu' ;
16
+
17
+ type RadioProps = {
18
+ checked ?: boolean ;
19
+ className ?: string ;
20
+ disabled ?: boolean ;
21
+ label ?: string | number ;
22
+ name ?: string ;
23
+ onChange ?: React . ChangeEventHandler < HTMLInputElement > ;
24
+ style ?: CSSProperties ;
25
+ value ?: string | number | boolean ;
26
+ variant ?: RadioVariant ;
27
+ } & React . InputHTMLAttributes < HTMLInputElement > &
28
+ CommonStyledProps ;
15
29
16
30
const sharedCheckboxStyles = css `
17
31
width : ${ size } px;
@@ -22,11 +36,15 @@ const sharedCheckboxStyles = css`
22
36
justify-content : space-around;
23
37
margin-right : 0.5rem ;
24
38
` ;
25
- // had to overwrite box-shadow for StyledCheckbox since the default made checkbox too dark
26
- const StyledCheckbox = styled ( StyledCutout ) `
39
+
40
+ type StyledCheckboxProps = {
41
+ $disabled : boolean ;
42
+ } ;
43
+
44
+ const StyledCheckbox = styled ( StyledCutout ) < StyledCheckboxProps > `
27
45
${ sharedCheckboxStyles }
28
- background: ${ ( { theme , isDisabled } ) =>
29
- isDisabled ? theme . material : theme . canvas } ;
46
+ background: ${ ( { $disabled , theme } ) =>
47
+ $disabled ? theme . material : theme . canvas } ;
30
48
31
49
&:before {
32
50
content: '';
@@ -39,12 +57,12 @@ const StyledCheckbox = styled(StyledCutout)`
39
57
box-shadow: none;
40
58
}
41
59
` ;
42
- const StyledFlatCheckbox = styled . div `
60
+ const StyledFlatCheckbox = styled . div < StyledCheckboxProps > `
43
61
${ createFlatBoxStyles ( ) }
44
62
${ sharedCheckboxStyles }
45
63
outline: none;
46
- background : ${ ( { theme , isDisabled } ) =>
47
- isDisabled ? theme . flatLight : theme . canvas } ;
64
+ background: ${ ( { $disabled , theme } ) =>
65
+ $disabled ? theme . flatLight : theme . canvas } ;
48
66
&:before {
49
67
content: '';
50
68
display: inline-block;
@@ -66,9 +84,16 @@ const StyledMenuCheckbox = styled.div`
66
84
outline : none;
67
85
background : none;
68
86
` ;
87
+
88
+ type IconProps = {
89
+ 'data-testid' : 'checkmarkIcon' ;
90
+ $disabled : boolean ;
91
+ variant : RadioVariant ;
92
+ } ;
93
+
69
94
const Icon = styled . span . attrs ( ( ) => ( {
70
95
'data-testid' : 'checkmarkIcon'
71
- } ) ) `
96
+ } ) ) < IconProps > `
72
97
position: absolute;
73
98
content: '';
74
99
display: inline-block;
@@ -78,23 +103,23 @@ const Icon = styled.span.attrs(() => ({
78
103
height: 6px;
79
104
transform: translate(-50%, -50%);
80
105
border-radius: 50%;
81
- ${ ( { variant , theme, isDisabled } ) =>
106
+ ${ ( { $disabled , theme, variant } ) =>
82
107
variant === 'menu'
83
108
? css `
84
- background : ${ isDisabled
109
+ background : ${ $disabled
85
110
? theme . materialTextDisabled
86
111
: theme . materialText } ;
87
112
filter : drop-shadow (
88
113
1px 1px 0px
89
- ${ isDisabled ? theme . materialTextDisabledShadow : 'transparent' }
114
+ ${ $disabled ? theme . materialTextDisabledShadow : 'transparent' }
90
115
);
91
116
`
92
117
: css `
93
- background : ${ isDisabled ? theme . checkmarkDisabled : theme . checkmark } ;
118
+ background : ${ $disabled ? theme . checkmarkDisabled : theme . checkmark } ;
94
119
` }
95
120
${ StyledListItem } :hover & {
96
- ${ ( { theme , isDisabled , variant } ) =>
97
- ! isDisabled &&
121
+ ${ ( { $disabled , theme , variant } ) =>
122
+ ! $disabled &&
98
123
variant === 'menu' &&
99
124
css `
100
125
background : ${ theme . materialTextInvert } ;
@@ -108,28 +133,25 @@ const CheckboxComponents = {
108
133
menu : StyledMenuCheckbox
109
134
} ;
110
135
111
- const Radio = React . forwardRef ( function Radio ( props , ref ) {
112
- const {
113
- onChange,
114
- label,
115
- disabled,
116
- variant,
136
+ const Radio = forwardRef < HTMLInputElement , RadioProps > ( function Radio (
137
+ {
117
138
checked,
118
- className,
119
- style,
139
+ className = '' ,
140
+ disabled = false ,
141
+ label = '' ,
142
+ onChange,
143
+ style = { } ,
144
+ variant = 'default' ,
120
145
...otherProps
121
- } = props ;
122
-
146
+ } ,
147
+ ref
148
+ ) {
123
149
const CheckboxComponent = CheckboxComponents [ variant ] ;
124
150
125
151
return (
126
- < StyledLabel isDisabled = { disabled } className = { className } style = { style } >
127
- < CheckboxComponent
128
- checked = { checked }
129
- isDisabled = { disabled }
130
- role = 'presentation'
131
- >
132
- { checked && < Icon isDisabled = { disabled } variant = { variant } /> }
152
+ < StyledLabel $disabled = { disabled } className = { className } style = { style } >
153
+ < CheckboxComponent $disabled = { disabled } role = 'presentation' >
154
+ { checked && < Icon $disabled = { disabled } variant = { variant } /> }
133
155
</ CheckboxComponent >
134
156
< StyledInput
135
157
disabled = { disabled }
@@ -145,33 +167,4 @@ const Radio = React.forwardRef(function Radio(props, ref) {
145
167
) ;
146
168
} ) ;
147
169
148
- Radio . defaultProps = {
149
- onChange : undefined ,
150
- name : null ,
151
- value : undefined ,
152
- checked : undefined ,
153
- label : '' ,
154
- disabled : false ,
155
- variant : 'default' ,
156
- className : '' ,
157
- style : { }
158
- } ;
159
-
160
- Radio . propTypes = {
161
- onChange : propTypes . func ,
162
- name : propTypes . string ,
163
- value : propTypes . oneOfType ( [
164
- propTypes . string ,
165
- propTypes . number ,
166
- propTypes . bool
167
- ] ) ,
168
- label : propTypes . oneOfType ( [ propTypes . string , propTypes . number ] ) ,
169
- checked : propTypes . bool ,
170
- disabled : propTypes . bool ,
171
- variant : propTypes . oneOf ( [ 'default' , 'flat' , 'menu' ] ) ,
172
- // eslint-disable-next-line react/forbid-prop-types
173
- style : propTypes . any ,
174
- className : propTypes . string
175
- } ;
176
-
177
- export default Radio ;
170
+ export { Radio , RadioProps } ;
0 commit comments