Skip to content

Commit

Permalink
Add ExpandSectionWithSwitch
Browse files Browse the repository at this point in the history
  • Loading branch information
pcbailey committed Oct 12, 2023
1 parent 9463792 commit 940c603
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.expand-section-with-switch {
&__contents {
padding-left: var(--pf-global--spacer--lg);
}

&__help-text-popover {
.pf-c-popover__content {
width: 400px;
}
}

// Firefox
@-moz-document url-prefix() {
&__help-icon,
.pf-c-switch {
vertical-align: -moz-middle-with-baseline !important;
}
}

// Chrome
@media screen and (-webkit-min-device-pixel-ratio: 0) and (min-resolution: 0.001dpcm) {
&__help-icon,
.pf-c-switch {
vertical-align: -webkit-baseline-middle !important;
}
}

&__help-icon {
color: var(--pf-global--Color--100);
margin-left: calc(-1 * var(--pf-global--spacer--sm));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React, { FC, FormEvent, ReactNode } from 'react';

import HelpTextIcon from '@kubevirt-utils/components/HelpTextIcon/HelpTextIcon';
import { PopoverPosition, Split, SplitItem, Switch } from '@patternfly/react-core';

import ExpandSection from '../../../views/clusteroverview/SettingsTab/ExpandSection/ExpandSection';

import './ExpandSectionWithSwitch.scss';

type ExpandSectionWithSwitchProps = {
children: ReactNode;
helpTextIconContent?: ReactNode;
isDisabled?: boolean;
switchIsOn: boolean;
toggleContent?: ReactNode;
toggleText?: string;
turnOnSwitch: (checked: boolean, event: FormEvent<HTMLInputElement>) => void;
};

const ExpandSectionWithSwitch: FC<ExpandSectionWithSwitchProps> = ({
children,
helpTextIconContent,
isDisabled,
switchIsOn,
toggleContent,
toggleText,
turnOnSwitch,
}) => {
return (
<Split className="expand-section-with-switch">
<SplitItem>
<ExpandSection
isDisabled={isDisabled}
isIndented
toggleContent={toggleContent}
toggleText={toggleText}
>
<div className="expand-section-with-switch__contents">{children}</div>
</ExpandSection>
</SplitItem>
{helpTextIconContent && (
<SplitItem isFilled>
<HelpTextIcon
bodyContent={helpTextIconContent}
className="expand-section-with-switch__help-text-popover"
helpIconClassName="expand-section-with-switch__help-icon"
position={PopoverPosition.right}
/>
</SplitItem>
)}
<SplitItem>
<Switch isChecked={switchIsOn} isDisabled={isDisabled} onChange={turnOnSwitch} />
</SplitItem>
</Split>
);
};

export default ExpandSectionWithSwitch;
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const AutomaticSubscriptionRHELGuests: FC = () => {
const { t } = useKubevirtTranslation();

return (
<ExpandSection toggleText={t('Automatic subscription of new RHEL VirtualMachines')}>
<ExpandSection isIndented toggleText={t('Automatic subscription of new RHEL VirtualMachines')}>
<Stack>
<MutedTextSpan
text={t('Enable automatic subscription for Red Hat Enterprise Linux VirtualMachines.\n')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const EnableLoadBalancerSection: FC = () => {
const { t } = useKubevirtTranslation();

return (
<ExpandSection toggleText={t('LoadBalancer service')}>
<ExpandSection isIndented toggleText={t('LoadBalancer service')}>
<EnableFeatureCheckbox
label={t(
'Enable the creation of LoadBalancer services for SSH connections to VirtualMachines',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const EnablePreviewFeaturesSection: FC = () => {
<PreviewFeaturesPopover />
</>
}
isIndented
>
<Stack hasGutter>
<StackItem isFilled>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const LiveMigrationSection: FC = () => {
const [hyperConverge, , hyperConvergeDataError] = useHyperConvergeConfiguration();

return (
<ExpandSection className="live-migration-tab" toggleText={t('Live migration')}>
<ExpandSection className="live-migration-tab" isIndented toggleText={t('Live migration')}>
<Limits hyperConverge={hyperConverge} />
<Network hyperConverge={hyperConverge} />
{hyperConvergeDataError && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ const TemplatesProjectSection: FC = () => {
};

return (
<ExpandSection className="templates-project-tab__main" toggleText={t('Template project')}>
<ExpandSection
className="templates-project-tab__main"
isIndented
toggleText={t('Template project')}
>
<Text className="templates-project-tab__main--help" component={TextVariants.small}>
<Trans ns="plugin__kubevirt-plugin" t={t}>
Select a project for Red Hat templates. The default project is &apos;openshift&apos;. If
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.expand-section__disabled .pf-c-expandable-section__toggle-icon {
color: var(--pf-global--disabled-color--100) !important;
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
import React, { FC, ReactNode, useState } from 'react';
import React, { FC, ReactNode, useEffect, useState } from 'react';
import classNames from 'classnames';

import { ExpandableSection } from '@patternfly/react-core';

import './ExpandSection.scss';

type ExpandSectionProps = {
className?: string;
isDisabled?: boolean;
isIndented?: boolean;
toggleContent?: ReactNode;
toggleText?: string;
};

const ExpandSection: FC<ExpandSectionProps> = ({
children,
className,
isDisabled = false,
isIndented = false,
toggleContent = null,
toggleText = '',
}) => {
const [isExpanded, setIsExpanded] = useState(false);

useEffect(() => {
if (isDisabled) setIsExpanded(false);
}, [isDisabled, setIsExpanded]);

const handleToggle = (expanded: boolean) => (isDisabled ? null : setIsExpanded(expanded));

return (
<ExpandableSection
className={className}
className={classNames(className, { 'expand-section__disabled': isDisabled })}
isExpanded={isExpanded}
onToggle={setIsExpanded}
isIndented={isIndented}
onToggle={handleToggle}
toggleContent={toggleContent}
toggleText={toggleText}
>
Expand Down

0 comments on commit 940c603

Please sign in to comment.