浏览代码

select only one check per row

amenpunk 2 年之前
父节点
当前提交
55d513c6f5
共有 1 个文件被更改,包括 38 次插入26 次删除
  1. 38 26
      src/Components/Test/Cleaver/Question.jsx

+ 38 - 26
src/Components/Test/Cleaver/Question.jsx

@@ -1,15 +1,10 @@
1 1
 import * as React from 'react';
2 2
 
3
-import { 
4
-  Remove as RemoveIcon,
5
-  Add as AddIcon
6
-} from '@mui/icons-material';
7
-
3
+import { Remove as RemoveIcon, Add as AddIcon } from '@mui/icons-material';
8 4
 import {
9 5
   Card,CardContent,Avatar,Checkbox,List,Tooltip,
10 6
   ListItem,ListItemButton,ListItemIcon,ListItemText
11 7
 } from '@mui/material'
12
-
13 8
 import { deepPurple } from '@mui/material/colors';
14 9
 
15 10
 function CheckboxexHeader(prop) {
@@ -33,51 +28,68 @@ function CheckboxexHeader(prop) {
33 28
 
34 29
 function CheckboxesGroup(props) {
35 30
 
36
-  const [checked, setChecked] = React.useState([0]);
31
+  const [checkA, setCheckA] = React.useState(0);
32
+  const [checkB, setCheckB] = React.useState(0);
37 33
 
38
-  const handleToggle = (value) => () => {
39 34
 
40
-    const currentIndex = checked.indexOf(value);
41
-    const newChecked = [...checked];
42
-    if (currentIndex === -1) {
43
-      newChecked.push(value);
44
-    } else {
45
-      newChecked.splice(currentIndex, 1);
35
+  const changeA = (event) => {
36
+    let { id } = event.target
37
+    if(parseInt( checkB ) !== parseInt(id) ){
38
+      setCheckA(parseInt( id ))
39
+    }
40
+  };
41
+  
42
+  const changeB = (event) => {
43
+    let { id } = event.target
44
+    if(parseInt( checkA ) !== parseInt( id )){
45
+      setCheckB(parseInt( id ))
46 46
     }
47
-
48
-    setChecked(newChecked);
49 47
   };
50 48
 
51 49
   return (
52 50
     <List sx={{ width: '100%', bgcolor: 'background.paper' }}>
51
+
53 52
       {props.quiz.map((value) => {
53
+
54 54
         const labelId = `checkbox-list-label-${value.id}`;
55
+
55 56
         return (
56 57
           <ListItem key={value.id}>
57
-            <ListItemButton role={undefined} onClick={handleToggle(value)} dense>
58
+            <ListItemButton dense>
58 59
               <Tooltip title={value.decription} placement="top-start" arrow>
59 60
                 <ListItemText 
60
-                  sx={{ '& .MuiTypography-root' : {
61
+                  sx={{ 
62
+                    '& .MuiTypography-root' : {
61 63
                       fontWeight:'bold'
62
-                  }}} className="checkbox_label_question" id={labelId} primary={value.nombre} />
64
+                  }}} 
65
+                  className="checkbox_label_question" 
66
+                  id={labelId} 
67
+                  primary={value.nombre} 
68
+                 />
63 69
               </Tooltip>
64 70
 
65 71
               <ListItemIcon>
66 72
                 <Checkbox
73
+                  id={value.id.toString()}
74
+                  sx={{ '& .MuiSvgIcon-root': { fontSize: 28 } }}
75
+                  color="error"
67 76
                   edge="start"
68
-                  checked={checked.indexOf(value) !== -1}
69
-                  tabIndex={-1}
70 77
                   disableRipple
71
-                  inputProps={{ 'aria-labelledby': labelId }}
78
+                  inputProps={{ 'aria-labelledby': value.id }}
79
+                  checked={parseInt( checkA ) === parseInt( value.id )}
80
+                  onChange={changeA}
72 81
                 />
73 82
               </ListItemIcon>
74 83
               <ListItemIcon>
75 84
                 <Checkbox
85
+                  id={value.id.toString()}
86
+                  sx={{ '& .MuiSvgIcon-root': { fontSize: 28 } }}
87
+                  color="error"
76 88
                   edge="start"
77
-                  //checked={}
78
-                  tabIndex={-1}
79 89
                   disableRipple
80
-                  inputProps={{ 'aria-labelledby': labelId }}
90
+                  inputProps={{ 'aria-labelledby': value.id }}
91
+                  checked={parseInt( checkB ) === parseInt( value.id )}
92
+                  onChange={changeB}
81 93
                 />
82 94
               </ListItemIcon>
83 95
             </ListItemButton>
@@ -88,7 +100,7 @@ function CheckboxesGroup(props) {
88 100
   );
89 101
 }
90 102
 
91
-export function Question({ quiz}) {
103
+export function Question({quiz}) {
92 104
   let { instrucciondepregunta, respuestas,id } = quiz
93 105
   return (
94 106
     <Card sx={{ minWidth: 275, margin: '30px !important', borderLeft: '5px solid var(--main)'}}>