mirror of
https://github.com/fleetdm/fleet
synced 2026-04-21 13:37:30 +00:00
Implements patch policies #31914 - https://github.com/fleetdm/fleet/pull/40816 - https://github.com/fleetdm/fleet/pull/41248 - https://github.com/fleetdm/fleet/pull/41276 - https://github.com/fleetdm/fleet/pull/40948 - https://github.com/fleetdm/fleet/pull/40837 - https://github.com/fleetdm/fleet/pull/40956 - https://github.com/fleetdm/fleet/pull/41168 - https://github.com/fleetdm/fleet/pull/41171 - https://github.com/fleetdm/fleet/pull/40691 - https://github.com/fleetdm/fleet/pull/41524 - https://github.com/fleetdm/fleet/pull/41674 --------- Co-authored-by: Jonathan Katz <44128041+jkatz01@users.noreply.github.com> Co-authored-by: jkatz01 <yehonatankatz@gmail.com> Co-authored-by: RachelElysia <71795832+RachelElysia@users.noreply.github.com> Co-authored-by: Jahziel Villasana-Espinoza <jahziel@fleetdm.com>
110 lines
2.7 KiB
TypeScript
110 lines
2.7 KiB
TypeScript
import React, { useEffect, useRef } from "react";
|
|
import classnames from "classnames";
|
|
import { pick } from "lodash";
|
|
|
|
import FormField from "components/forms/FormField";
|
|
import { IFormFieldProps } from "components/forms/FormField/FormField";
|
|
import TooltipWrapper from "components/TooltipWrapper";
|
|
|
|
interface ISliderProps {
|
|
onChange: () => void;
|
|
value: boolean;
|
|
inactiveText: JSX.Element | string;
|
|
activeText: JSX.Element | string;
|
|
/** Use to display slider label tooltip, compatible with disabled state */
|
|
labelTooltip?: JSX.Element | string;
|
|
className?: string;
|
|
helpText?: JSX.Element | string;
|
|
autoFocus?: boolean;
|
|
disabled?: boolean;
|
|
}
|
|
|
|
const baseClass = "fleet-slider";
|
|
|
|
const Slider = (props: ISliderProps): JSX.Element => {
|
|
const {
|
|
onChange,
|
|
value,
|
|
inactiveText,
|
|
activeText,
|
|
labelTooltip,
|
|
autoFocus,
|
|
disabled,
|
|
} = props;
|
|
|
|
const sliderRef = useRef<HTMLButtonElement>(null);
|
|
|
|
useEffect(() => {
|
|
if (autoFocus && sliderRef.current) {
|
|
sliderRef.current.focus();
|
|
}
|
|
}, [autoFocus]);
|
|
|
|
const sliderBtnClass = classnames(baseClass, {
|
|
[`${baseClass}--active`]: value,
|
|
});
|
|
|
|
const sliderDotClass = classnames(`${baseClass}__dot`, {
|
|
[`${baseClass}__dot--active`]: value,
|
|
});
|
|
|
|
const handleClick = (evt: React.MouseEvent) => {
|
|
evt.preventDefault();
|
|
if (disabled) return;
|
|
|
|
onChange();
|
|
};
|
|
|
|
const formFieldProps = pick(props, [
|
|
"helpText",
|
|
"label",
|
|
"error",
|
|
"name",
|
|
"className",
|
|
"disabled",
|
|
]) as IFormFieldProps;
|
|
|
|
const wrapperClassNames = classnames(`${baseClass}__wrapper`, {
|
|
[`${baseClass}__wrapper--disabled`]: disabled,
|
|
});
|
|
|
|
const text = value ? activeText : inactiveText;
|
|
return (
|
|
<FormField {...formFieldProps} type="slider">
|
|
<div className={wrapperClassNames}>
|
|
<button
|
|
role="switch"
|
|
aria-checked={value}
|
|
className={`button button--unstyled ${sliderBtnClass}`}
|
|
onClick={handleClick}
|
|
disabled={disabled}
|
|
ref={sliderRef}
|
|
>
|
|
<div className={sliderDotClass} />
|
|
</button>
|
|
{labelTooltip ? (
|
|
<TooltipWrapper tipContent={labelTooltip}>
|
|
{" "}
|
|
<span
|
|
className={`${baseClass}__label ${baseClass}__label--${
|
|
value ? "active" : "inactive"
|
|
}`}
|
|
>
|
|
{text}
|
|
</span>
|
|
</TooltipWrapper>
|
|
) : (
|
|
<span
|
|
className={`${baseClass}__label ${baseClass}__label--${
|
|
value ? "active" : "inactive"
|
|
}`}
|
|
>
|
|
{text}
|
|
</span>
|
|
)}
|
|
</div>
|
|
</FormField>
|
|
);
|
|
};
|
|
|
|
export default Slider;
|