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 ' ;
33
4- import styled , { css } from 'styled-components' ;
54import { createFlatBoxStyles } from '../common' ;
65import { StyledCutout } from '../Cutout/Cutout' ;
76import { StyledListItem } from '../ListItem/ListItem' ;
8-
97import {
8+ LabelText ,
109 size ,
1110 StyledInput ,
12- StyledLabel ,
13- LabelText
11+ StyledLabel
1412} 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 ;
1529
1630const sharedCheckboxStyles = css `
1731 width : ${ size } px;
@@ -22,11 +36,15 @@ const sharedCheckboxStyles = css`
2236 justify-content : space-around;
2337 margin-right : 0.5rem ;
2438` ;
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 > `
2745 ${ sharedCheckboxStyles }
28- background: ${ ( { theme , isDisabled } ) =>
29- isDisabled ? theme . material : theme . canvas } ;
46+ background: ${ ( { $disabled , theme } ) =>
47+ $disabled ? theme . material : theme . canvas } ;
3048
3149 &:before {
3250 content: '';
@@ -39,12 +57,12 @@ const StyledCheckbox = styled(StyledCutout)`
3957 box-shadow: none;
4058 }
4159` ;
42- const StyledFlatCheckbox = styled . div `
60+ const StyledFlatCheckbox = styled . div < StyledCheckboxProps > `
4361 ${ createFlatBoxStyles ( ) }
4462 ${ sharedCheckboxStyles }
4563 outline: none;
46- background : ${ ( { theme , isDisabled } ) =>
47- isDisabled ? theme . flatLight : theme . canvas } ;
64+ background: ${ ( { $disabled , theme } ) =>
65+ $disabled ? theme . flatLight : theme . canvas } ;
4866 &:before {
4967 content: '';
5068 display: inline-block;
@@ -66,9 +84,16 @@ const StyledMenuCheckbox = styled.div`
6684 outline : none;
6785 background : none;
6886` ;
87+
88+ type IconProps = {
89+ 'data-testid' : 'checkmarkIcon' ;
90+ $disabled : boolean ;
91+ variant : RadioVariant ;
92+ } ;
93+
6994const Icon = styled . span . attrs ( ( ) => ( {
7095 'data-testid' : 'checkmarkIcon'
71- } ) ) `
96+ } ) ) < IconProps > `
7297 position: absolute;
7398 content: '';
7499 display: inline-block;
@@ -78,23 +103,23 @@ const Icon = styled.span.attrs(() => ({
78103 height: 6px;
79104 transform: translate(-50%, -50%);
80105 border-radius: 50%;
81- ${ ( { variant , theme, isDisabled } ) =>
106+ ${ ( { $disabled , theme, variant } ) =>
82107 variant === 'menu'
83108 ? css `
84- background : ${ isDisabled
109+ background : ${ $disabled
85110 ? theme . materialTextDisabled
86111 : theme . materialText } ;
87112 filter : drop-shadow (
88113 1px 1px 0px
89- ${ isDisabled ? theme . materialTextDisabledShadow : 'transparent' }
114+ ${ $disabled ? theme . materialTextDisabledShadow : 'transparent' }
90115 );
91116 `
92117 : css `
93- background : ${ isDisabled ? theme . checkmarkDisabled : theme . checkmark } ;
118+ background : ${ $disabled ? theme . checkmarkDisabled : theme . checkmark } ;
94119 ` }
95120 ${ StyledListItem } :hover & {
96- ${ ( { theme , isDisabled , variant } ) =>
97- ! isDisabled &&
121+ ${ ( { $disabled , theme , variant } ) =>
122+ ! $disabled &&
98123 variant === 'menu' &&
99124 css `
100125 background : ${ theme . materialTextInvert } ;
@@ -108,28 +133,25 @@ const CheckboxComponents = {
108133 menu : StyledMenuCheckbox
109134} ;
110135
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+ {
117138 checked,
118- className,
119- style,
139+ className = '' ,
140+ disabled = false ,
141+ label = '' ,
142+ onChange,
143+ style = { } ,
144+ variant = 'default' ,
120145 ...otherProps
121- } = props ;
122-
146+ } ,
147+ ref
148+ ) {
123149 const CheckboxComponent = CheckboxComponents [ variant ] ;
124150
125151 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 } /> }
133155 </ CheckboxComponent >
134156 < StyledInput
135157 disabled = { disabled }
@@ -145,33 +167,4 @@ const Radio = React.forwardRef(function Radio(props, ref) {
145167 ) ;
146168} ) ;
147169
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