Skip to content

Commit 6ff90d0

Browse files
committed
Show description or tooltip for checkboxes
1 parent 3b32c9b commit 6ff90d0

File tree

1 file changed

+59
-23
lines changed

1 file changed

+59
-23
lines changed

packages/material-renderers/src/controls/MaterialBooleanControl.tsx

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@
2323
THE SOFTWARE.
2424
*/
2525
import isEmpty from 'lodash/isEmpty';
26+
import merge from 'lodash/merge';
2627
import React from 'react';
2728
import {
2829
isBooleanControl,
2930
RankedTester,
3031
rankWith,
31-
ControlProps
32+
ControlProps,
33+
isDescriptionHidden
3234
} from '@jsonforms/core';
3335
import { withJsonFormsControlProps } from '@jsonforms/react';
34-
import { FormControlLabel, Hidden } from '@mui/material';
36+
import { FormControlLabel, FormHelperText, Tooltip, Hidden } from '@mui/material';
3537
import { MuiCheckbox } from '../mui-controls/MuiCheckbox';
3638

3739
export const MaterialBooleanControl = ({
@@ -46,30 +48,64 @@ export const MaterialBooleanControl = ({
4648
handleChange,
4749
errors,
4850
path,
49-
config
51+
config,
52+
description
5053
}: 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+
5179
return (
5280
<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>
73109
</Hidden>
74110
);
75111
};

0 commit comments

Comments
 (0)