23
23
THE SOFTWARE.
24
24
*/
25
25
import isEmpty from 'lodash/isEmpty' ;
26
+ import merge from 'lodash/merge' ;
26
27
import React from 'react' ;
27
28
import {
28
29
isBooleanControl ,
29
30
RankedTester ,
30
31
rankWith ,
31
- ControlProps
32
+ ControlProps ,
33
+ isDescriptionHidden
32
34
} from '@jsonforms/core' ;
33
35
import { withJsonFormsControlProps } from '@jsonforms/react' ;
34
- import { FormControlLabel , Hidden } from '@mui/material' ;
36
+ import { FormControlLabel , FormHelperText , Tooltip , Hidden } from '@mui/material' ;
35
37
import { MuiCheckbox } from '../mui-controls/MuiCheckbox' ;
36
38
37
39
export const MaterialBooleanControl = ( {
@@ -46,30 +48,64 @@ export const MaterialBooleanControl = ({
46
48
handleChange,
47
49
errors,
48
50
path,
49
- config
51
+ config,
52
+ description
50
53
} : ControlProps ) => {
54
+
55
+ const isValid = errors . length === 0 ;
56
+ const appliedUiSchemaOptions = merge ( { } , config , uischema . options ) ;
57
+
58
+ const showDescription = ! isDescriptionHidden (
59
+ visible ,
60
+ description ,
61
+ false ,
62
+ appliedUiSchemaOptions . showUnfocusedDescription
63
+ ) ;
64
+
65
+ const showTooltip = ! showDescription && ! isDescriptionHidden (
66
+ visible ,
67
+ description ,
68
+ true ,
69
+ true
70
+ ) ;
71
+
72
+ const firstFormHelperText = showDescription
73
+ ? description
74
+ : ! isValid
75
+ ? errors
76
+ : null ;
77
+ const secondFormHelperText = showDescription && ! isValid ? errors : null ;
78
+
51
79
return (
52
80
< Hidden xsUp = { ! visible } >
53
- < FormControlLabel
54
- label = { label }
55
- id = { id }
56
- control = {
57
- < MuiCheckbox
58
- id = { `${ id } -input` }
59
- isValid = { isEmpty ( errors ) }
60
- data = { data }
61
- enabled = { enabled }
62
- visible = { visible }
63
- path = { path }
64
- uischema = { uischema }
65
- schema = { schema }
66
- rootSchema = { rootSchema }
67
- handleChange = { handleChange }
68
- errors = { errors }
69
- config = { config }
70
- />
71
- }
72
- />
81
+ < Tooltip title = { ( showTooltip ) ? description : '' } >
82
+ < FormControlLabel
83
+ label = { label }
84
+ id = { id }
85
+ control = {
86
+ < MuiCheckbox
87
+ id = { `${ id } -input` }
88
+ isValid = { isEmpty ( errors ) }
89
+ data = { data }
90
+ enabled = { enabled }
91
+ visible = { visible }
92
+ path = { path }
93
+ uischema = { uischema }
94
+ schema = { schema }
95
+ rootSchema = { rootSchema }
96
+ handleChange = { handleChange }
97
+ errors = { errors }
98
+ config = { config }
99
+ />
100
+ }
101
+ />
102
+ </ Tooltip >
103
+ < FormHelperText error = { ! isValid && ! showDescription } >
104
+ { firstFormHelperText }
105
+ </ FormHelperText >
106
+ < FormHelperText error = { ! isValid } >
107
+ { secondFormHelperText }
108
+ </ FormHelperText >
73
109
</ Hidden >
74
110
) ;
75
111
} ;
0 commit comments