Skip to content

Commit

Permalink
Show description or tooltip on react material boolean controls (#2072)
Browse files Browse the repository at this point in the history
* Show description or tooltip for checkboxes
* Add description to boolean example
* Document hardcoded parameters in boolean control
* Give toggle booleans the same description treatment as checkboxes
  • Loading branch information
brockfanning authored Jan 9, 2023
1 parent 3b32c9b commit 36e3732
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 47 deletions.
3 changes: 2 additions & 1 deletion packages/examples/src/examples/control-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export const schema = {
type: 'string'
},
boolean: {
type: 'boolean'
type: 'boolean',
description: 'Boolean description as a tooltip'
},
number: {
type: 'number'
Expand Down
89 changes: 66 additions & 23 deletions packages/material-renderers/src/controls/MaterialBooleanControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
THE SOFTWARE.
*/
import isEmpty from 'lodash/isEmpty';
import merge from 'lodash/merge';
import React from 'react';
import {
isBooleanControl,
RankedTester,
rankWith,
ControlProps
ControlProps,
isDescriptionHidden
} from '@jsonforms/core';
import { withJsonFormsControlProps } from '@jsonforms/react';
import { FormControlLabel, Hidden } from '@mui/material';
import { FormControlLabel, FormHelperText, Tooltip, Hidden } from '@mui/material';
import { MuiCheckbox } from '../mui-controls/MuiCheckbox';

export const MaterialBooleanControl = ({
Expand All @@ -46,30 +48,71 @@ export const MaterialBooleanControl = ({
handleChange,
errors,
path,
config
config,
description
}: ControlProps) => {

const isValid = errors.length === 0;
const appliedUiSchemaOptions = merge({}, config, uischema.options);

const showDescription = !isDescriptionHidden(
visible,
description,
// Checkboxes do not receive focus until they are used, so
// we cannot rely on focus as criteria for showing descriptions.
// So we pass "false" to treat it as unfocused.
false,
appliedUiSchemaOptions.showUnfocusedDescription
);

const showTooltip = !showDescription && !isDescriptionHidden(
visible,
description,
// Tooltips have their own focus handlers, so we do not need to rely
// on focus state here. So we pass 'true' to treat it as focused.
true,
// We also pass true here for showUnfocusedDescription since it should
// render regardless of that setting.
true
);

const firstFormHelperText = showDescription
? description
: !isValid
? errors
: null;
const secondFormHelperText = showDescription && !isValid ? errors : null;

return (
<Hidden xsUp={!visible}>
<FormControlLabel
label={label}
id={id}
control={
<MuiCheckbox
id={`${id}-input`}
isValid={isEmpty(errors)}
data={data}
enabled={enabled}
visible={visible}
path={path}
uischema={uischema}
schema={schema}
rootSchema={rootSchema}
handleChange={handleChange}
errors={errors}
config={config}
/>
}
/>
<Tooltip title={(showTooltip) ? description : ''}>
<FormControlLabel
label={label}
id={id}
control={
<MuiCheckbox
id={`${id}-input`}
isValid={isEmpty(errors)}
data={data}
enabled={enabled}
visible={visible}
path={path}
uischema={uischema}
schema={schema}
rootSchema={rootSchema}
handleChange={handleChange}
errors={errors}
config={config}
/>
}
/>
</Tooltip>
<FormHelperText error={!isValid && !showDescription}>
{firstFormHelperText}
</FormHelperText>
<FormHelperText error={!isValid}>
{secondFormHelperText}
</FormHelperText>
</Hidden>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@
THE SOFTWARE.
*/
import isEmpty from 'lodash/isEmpty';
import merge from 'lodash/merge';
import React from 'react';
import {
isBooleanControl,
RankedTester,
rankWith,
ControlProps,
optionIs,
and
and,
isDescriptionHidden
} from '@jsonforms/core';
import { withJsonFormsControlProps } from '@jsonforms/react';
import { FormControlLabel, Hidden } from '@mui/material';
import { FormControlLabel, FormHelperText, Tooltip, Hidden } from '@mui/material';
import { MuiToggle } from '../mui-controls/MuiToggle';

export const MaterialBooleanToggleControl = ({
Expand All @@ -48,30 +50,71 @@ export const MaterialBooleanToggleControl = ({
handleChange,
errors,
path,
config
config,
description
}: ControlProps) => {

const isValid = errors.length === 0;
const appliedUiSchemaOptions = merge({}, config, uischema.options);

const showDescription = !isDescriptionHidden(
visible,
description,
// Checkboxes do not receive focus until they are used, so
// we cannot rely on focus as criteria for showing descriptions.
// So we pass "false" to treat it as unfocused.
false,
appliedUiSchemaOptions.showUnfocusedDescription
);

const showTooltip = !showDescription && !isDescriptionHidden(
visible,
description,
// Tooltips have their own focus handlers, so we do not need to rely
// on focus state here. So we pass 'true' to treat it as focused.
true,
// We also pass true here for showUnfocusedDescription since it should
// render regardless of that setting.
true
);

const firstFormHelperText = showDescription
? description
: !isValid
? errors
: null;
const secondFormHelperText = showDescription && !isValid ? errors : null;

return (
<Hidden xsUp={!visible}>
<FormControlLabel
label={label}
id={id}
control={
<MuiToggle
id={`${id}-input`}
isValid={isEmpty(errors)}
data={data}
enabled={enabled}
visible={visible}
path={path}
uischema={uischema}
schema={schema}
rootSchema={rootSchema}
handleChange={handleChange}
errors={errors}
config={config}
/>
}
/>
<Tooltip title={(showTooltip) ? description : ''}>
<FormControlLabel
label={label}
id={id}
control={
<MuiToggle
id={`${id}-input`}
isValid={isEmpty(errors)}
data={data}
enabled={enabled}
visible={visible}
path={path}
uischema={uischema}
schema={schema}
rootSchema={rootSchema}
handleChange={handleChange}
errors={errors}
config={config}
/>
}
/>
</Tooltip>
<FormHelperText error={!isValid && !showDescription}>
{firstFormHelperText}
</FormHelperText>
<FormHelperText error={!isValid}>
{secondFormHelperText}
</FormHelperText>
</Hidden>
);
};
Expand Down

0 comments on commit 36e3732

Please sign in to comment.