-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (67 loc) · 2.13 KB
/
Copy pathindex.js
File metadata and controls
73 lines (67 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import React, { useState, useCallback, useEffect } from 'react'
import { Box, RadioGroup, Radio, FormControlLabel } from '@mui/material'
const BooleanInput = ({ value, onChange, alignCenter = false }) => {
const [isYesChecked, setYesChecked] = useState(false)
const [isNoChecked, setNoChecked] = useState(false)
const strBoolToBoolean = (string, stateChanged = false) => {
switch (string) {
case 'true':
setYesChecked(true)
setNoChecked(false)
if (stateChanged) onChange(true)
break
case 'false':
setNoChecked(true)
setYesChecked(false)
if (stateChanged) onChange(false)
break
default:
console.log('error')
}
}
useEffect(() => {
if (typeof value !== 'undefined') {
if (typeof value === 'string') {
strBoolToBoolean(value)
return
}
setYesChecked(value)
setNoChecked(!value)
}
}, [value])
const handleChange = useCallback(
e => {
strBoolToBoolean(e.target.value, true)
},
[onChange],
)
return (
<Box>
<RadioGroup
row
sx={{ justifyContent: alignCenter ? 'center' : 'start' }}
aria-label="yes-no"
value={value}
onChange={handleChange}
>
<FormControlLabel
key={'yes'}
control={<Radio color="primary" />}
label={'Yes'}
labelPlacement="start"
value={true}
checked={isYesChecked}
/>
<FormControlLabel
key={'no'}
control={<Radio color="primary" />}
label={'No'}
labelPlacement="start"
value={false}
checked={isNoChecked}
/>
</RadioGroup>
</Box>
)
}
export default BooleanInput