Skip to content

Commit

Permalink
Merge pull request #22 from JohnThomson/lockedBooleans
Browse files Browse the repository at this point in the history
Allow booleans to be 'locked' (disabled but text not greyed) (#22)
  • Loading branch information
andrew-polk authored Apr 9, 2024
2 parents fa2ac3a + a1b7036 commit c4d3afd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/ContentPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,10 @@ export const ConfigrBoolean: React.FunctionComponent<
// When immediateEffect is true, overrideValue will
// misbehave: the control will seem to work but not actually save the setting.
immediateEffect?: boolean;
// When true, the control will be disabled, but the label will not be greyed out, only the checkbox.
// This is useful when the checkbox is one of a set and is disabled because it is the only one that
// is turned on; in that case, it should not look less prominent than the others.
locked?: boolean;
}
>
> = (props) => {
Expand All @@ -870,12 +874,12 @@ export const ConfigrBoolean: React.FunctionComponent<
window.setTimeout(() => helpers.setValue(false), 0);
}
const control = props.immediateEffect ? (
<Field component={Switch} type="checkbox" name={props.path} label={props.label} />
<Field component={Switch} type="checkbox" name={props.path} label={props.label} disabled={props.disabled || props.locked}/>
) : (
<Field
component={Checkbox}
type="checkbox"
disabled={props.disabled}
disabled={props.disabled || props.locked}
name={props.path}
label={props.label}
/>
Expand All @@ -885,10 +889,19 @@ export const ConfigrBoolean: React.FunctionComponent<
<ConfigrRowTwoColumns
// clicking the row is the same as clicking the toggle control
onClick={() => {
// if locked, we can't change the value, but we didn't tell the component it is disabled
// so we still get the click.
if (props.locked) return;
helpers.setValue(!field.value);
}}
control={control}
{...props}
// If it is locked, we want it to BE disabled but not LOOK disabld (the checkbox is
// the control above, and will LOOK disabled for either disabled or locked or both).
// Note that the label will still BE disabled since we ignore clicks (above) if locked.
// Here, we want disabled to be false if locked, even if disabled is also true,
// so we get the undimmed label.
disabled = {props.disabled && !props.locked}
/>
);
};
Expand Down
16 changes: 16 additions & 0 deletions src/stories/bloom.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ const BloomCollectionInner: React.FunctionComponent<{
overrideValue={false}
overrideDescription="This is locked by Kyrgyzstan xmatter"
/>
<ConfigrBoolean
label="This script requires taller lines (locked)"
path={`${prefix}.script.tallerLines`}
locked={true}
/>
<ConfigrBoolean
label="This script requires taller lines"
path={`${prefix}.script.tallerLines`}
Expand Down Expand Up @@ -307,7 +312,18 @@ const BloomCollectionInner: React.FunctionComponent<{
label="Show Experimental Book Sources"
path="feature.experimentalBookSources"
immediateEffect={true}
/>
<ConfigrBoolean
label="Show Experimental Book Sources (disabled)"
path="feature.experimentalBookSources"
immediateEffect={true}
disabled={true}
/>
<ConfigrBoolean
label="Show Experimental Book Sources (locked)"
path="feature.experimentalBookSources"
immediateEffect={true}
locked={true}
/>
<ConfigrBoolean
label="Team Collections"
Expand Down

0 comments on commit c4d3afd

Please sign in to comment.