fleet/frontend/components/forms/fields/Slider/Slider.tsx
Gabriel Hernandez a5c69a60a4
Fix bug for not being to reenable end user migration in UI (#30106)
Fixes #30063

This fixes an issue added in the
[PR](https://github.com/fleetdm/fleet/pull/29968) where the user was not
able to reenable the end user migration form.

I've also added improved a11y attributes to the slider component,
ensured we are functionally disabling the form controls during gitops
mode and not just visually, and updated/added tests for the
EndUserMigrationSection component.


- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
- [x] Added/updated automated tests
- [x] Manual QA for all new/changed functionality
2025-06-20 17:04:30 +01:00

91 lines
2.1 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";
interface ISliderProps {
onChange: () => void;
value: boolean;
inactiveText: JSX.Element | string;
activeText: 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,
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,
});
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>
<span
className={`${baseClass}__label ${baseClass}__label--${
value ? "active" : "inactive"
}`}
>
{value ? activeText : inactiveText}
</span>
</div>
</FormField>
);
};
export default Slider;