11import CheckIcon from '@mui/icons-material/Check' ;
22import CloseIcon from '@mui/icons-material/Close' ;
3- import { Button , Typography } from '@mui/material' ;
3+ import { Button , Checkbox , Typography } from '@mui/material' ;
44
55import { buildMultipleChoicesButtonCy } from '../../../config/selectors' ;
66import theme from '../../../layout/theme' ;
@@ -14,41 +14,107 @@ const DEFAULT_COLOR = 'primary';
1414const CORRECT_COLOR = 'success' ;
1515const INCORRECT_COLOR = 'error' ;
1616const DEFAULT_DISABLED = 'whitesmoke' ;
17+ const SUCCESS_COLOR = theme . palette . success . main ;
18+ const ERROR_COLOR = theme . palette . error . main ;
19+ const PRIMARY_COLOR = theme . palette . primary . main ;
1720
1821const styleButton = ( {
1922 color,
20- isSelected,
2123 dataCy,
2224 endIcon,
2325} : {
2426 color : StatusColor ;
25- isSelected : boolean ;
2627 dataCy : string ;
2728 endIcon ?: JSX . Element ;
2829} ) =>
2930 ( {
3031 color,
31- variant : isSelected ? 'contained' : 'outlined' ,
3232 endIcon,
33+ variant : 'outlined' ,
3334 'data-cy' : dataCy ,
3435 } as const ) ;
3536
3637const computeDisabledSx = ( choiceState : ChoiceState | undefined ) => {
37- const successColor = theme . palette . success . main ;
38- const errorColor = theme . palette . error . main ;
39-
4038 switch ( choiceState ) {
4139 case ChoiceState . CORRECT :
42- return { backgroundColor : successColor , color : DEFAULT_DISABLED } ;
43- case ChoiceState . INCORRECT :
44- return { backgroundColor : errorColor , color : DEFAULT_DISABLED } ;
4540 case ChoiceState . MISSING :
46- return { color : successColor , borderColor : successColor } ;
41+ return { borderColor : SUCCESS_COLOR , color : SUCCESS_COLOR } ;
42+ case ChoiceState . INCORRECT :
43+ return { borderColor : ERROR_COLOR , color : ERROR_COLOR } ;
4744 default :
4845 return { } ;
4946 }
5047} ;
5148
49+ const computeStyles = ( {
50+ isSelected,
51+ idx,
52+ showState,
53+ choiceState,
54+ } : {
55+ isSelected : boolean ;
56+ idx : number ;
57+ showState : boolean ;
58+ choiceState : number ;
59+ } ) => {
60+ const btn = {
61+ color : DEFAULT_COLOR ,
62+ isSelected : isSelected ,
63+ dataCy : buildMultipleChoicesButtonCy ( idx , isSelected ) ,
64+ } as const ; // const is needed to allow color strings
65+
66+ if ( showState ) {
67+ switch ( choiceState ) {
68+ case ChoiceState . CORRECT :
69+ case ChoiceState . MISSING :
70+ return styleButton ( {
71+ ...btn ,
72+ color : CORRECT_COLOR ,
73+ endIcon : < CheckIcon /> ,
74+ } ) ;
75+ case ChoiceState . INCORRECT :
76+ return styleButton ( {
77+ ...btn ,
78+ color : INCORRECT_COLOR ,
79+ endIcon : < CloseIcon /> ,
80+ } ) ;
81+ }
82+ }
83+
84+ return styleButton ( btn ) ;
85+ } ;
86+
87+ const computeCheckboxSx = ( {
88+ showState,
89+ choiceState,
90+ } : {
91+ showState : boolean ;
92+ choiceState : number ;
93+ } ) => {
94+ let borderColor = PRIMARY_COLOR ;
95+
96+ if ( showState ) {
97+ switch ( choiceState ) {
98+ case ChoiceState . CORRECT :
99+ case ChoiceState . MISSING :
100+ borderColor = SUCCESS_COLOR ;
101+ break ;
102+ case ChoiceState . INCORRECT :
103+ borderColor = ERROR_COLOR ;
104+ break ;
105+ default :
106+ borderColor = DEFAULT_DISABLED ;
107+ }
108+ }
109+
110+ return {
111+ '&.Mui-checked' : {
112+ color : borderColor ,
113+ } ,
114+ color : borderColor ,
115+ } ;
116+ } ;
117+
52118type Props = {
53119 choice : MultipleChoicesChoice ;
54120 choiceState : ChoiceState ;
@@ -68,34 +134,6 @@ export const ChoiceButton = ({
68134 showState,
69135 onClick,
70136} : Props ) => {
71- const computeStyles = ( ) => {
72- const btn = {
73- color : DEFAULT_COLOR ,
74- isSelected : isSelected ,
75- dataCy : buildMultipleChoicesButtonCy ( idx , isSelected ) ,
76- } as const ; // const is needed to allow color strings
77-
78- if ( showState ) {
79- switch ( choiceState ) {
80- case ChoiceState . CORRECT :
81- case ChoiceState . MISSING :
82- return styleButton ( {
83- ...btn ,
84- color : CORRECT_COLOR ,
85- endIcon : < CheckIcon /> ,
86- } ) ;
87- case ChoiceState . INCORRECT :
88- return styleButton ( {
89- ...btn ,
90- color : INCORRECT_COLOR ,
91- endIcon : < CloseIcon /> ,
92- } ) ;
93- }
94- }
95-
96- return styleButton ( btn ) ;
97- } ;
98-
99137 const handleClick = ( ) => {
100138 if ( isReadonly ) {
101139 return ;
@@ -106,14 +144,20 @@ export const ChoiceButton = ({
106144 return (
107145 < Button
108146 key = { choice . value }
147+ startIcon = {
148+ < Checkbox
149+ checked = { isSelected }
150+ sx = { computeCheckboxSx ( { showState, choiceState } ) }
151+ />
152+ }
109153 onClick = { handleClick }
110154 fullWidth
111155 sx = { {
112156 '&.MuiButton-root' : {
113157 '&.Mui-disabled' : computeDisabledSx ( choiceState ) ,
114158 } ,
115159 } }
116- { ...computeStyles ( ) }
160+ { ...computeStyles ( { isSelected , idx , choiceState , showState } ) }
117161 disabled = { isReadonly }
118162 >
119163 < Typography variant = "body1" sx = { { fontWeight : 500 } } >
0 commit comments