AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend): make password reveal toggle keyboard-accessible (#12846)
djpjronline-netizene7bd686d414c
diff --git a/autogpt_platform/frontend/src/components/atoms/Input/Input.tsx b/autogpt_platform/frontend/src/components/atoms/Input/Input.tsxindex a83e4e82e3..a3f9f27dc0 100644--- a/autogpt_platform/frontend/src/components/atoms/Input/Input.tsx+++ b/autogpt_platform/frontend/src/components/atoms/Input/Input.tsx@@ -84,20 +84,19 @@ export const Input = forwardRef<InputElement, TextFieldProps>(function Input(decimalCount,});const [showPassword, setShowPassword] = useState(false);+ const inputId = props.id;const isPasswordType = props.type === "password";- const inputType = showPassword ? "text" : props.type;+ const inputType = isPasswordType && showPassword ? "text" : props.type;- function handleMouseDown() {- setShowPassword(true);+ function handleTogglePassword() {+ setShowPassword((prev) => !prev);}- function handleMouseUp() {- setShowPassword(false);- }-- function handleMouseLeave() {- setShowPassword(false);+ function handleWrapperBlur(e: React.FocusEvent<HTMLDivElement>) {+ if (!e.currentTarget.contains(e.relatedTarget)) {+ setShowPassword(false);+ }}const baseStyles = cn(@@ -139,7 +138,7 @@ export const Input = forwardRef<InputElement, TextFieldProps>(function Input(onKeyDown={guardedOnKeyDown}rows={props.rows || 3}{...(hideLabel ? { "aria-label": label } : {})}- id={props.id}+ id={inputId}disabled={props.disabled}value={props.value}maxLength={props.maxLength}@@ -168,7 +167,7 @@ export const Input = forwardRef<InputElement, TextFieldProps>(function Input(// CurrencyInput gives unformatted numeric string in value paramonValueChange={handleAmountValueChange}value={props.value as string | number | undefined}- id={props.id}+ id={inputId}name={props.name}disabled={props.disabled}inputMode="decimal"@@ -212,6 +211,7 @@ export const Input = forwardRef<InputElement, TextFieldProps>(function Input(onChange={handleInputChange}{...(hideLabel ? { "aria-label": label } : {})}{...props}+ id={inputId}onKeyDown={guardedOnKeyDown}type={inputType}/>@@ -219,16 +219,20 @@ export const Input = forwardRef<InputElement, TextFieldProps>(function Input(};const input = (- <div className={cn("relative w-full", wrapperClassName)}>+ <div+ onBlur={isPasswordType ? handleWrapperBlur : undefined}+ className={cn("relative w-full", wrapperClassName)}+ >{renderInput()}{isPasswordType && (<buttontype="button"- onMouseDown={handleMouseDown}- onMouseUp={handleMouseUp}- onMouseLeave={handleMouseLeave}- className="absolute right-4 top-1/2 -translate-y-1/2 text-zinc-400 transition-colors hover:text-zinc-600"- aria-label="Press and hold to show password"+ onClick={handleTogglePassword}+ disabled={props.disabled}+ className="absolute right-4 top-1/2 -translate-y-1/2 text-zinc-400 transition-colors hover:text-zinc-600 disabled:cursor-not-allowed disabled:opacity-50"+ aria-label={showPassword ? "Hide password" : "Show password"}+ aria-pressed={showPassword}+ aria-controls={inputId}>{showPassword ? (<Icon icon={EyeIcon} size={16} />@@ -260,16 +264,18 @@ export const Input = forwardRef<InputElement, TextFieldProps>(function Input(return hideLabel ? (inputWithError) : (- <label htmlFor={props.id} className="flex w-full flex-col gap-2">+ <div className="flex w-full flex-col gap-2"><div className="flex items-center justify-between gap-2"><div className="flex items-center gap-1">- <Text- variant={labelVariant}- as="span"- className={cn("text-black", labelClassName)}- >- {label}- </Text>+ <label htmlFor={inputId}>+ <Text+ variant={labelVariant}+ as="span"+ className={cn("text-black", labelClassName)}+ >+ {label}+ </Text>+ </label>{labelTooltip ? (<InformationTooltip description={labelTooltip} iconSize={20} />) : null}@@ -281,6 +287,6 @@ export const Input = forwardRef<InputElement, TextFieldProps>(function Input() : null}</div>{inputWithError}- </label>+ </div>);});diff --git a/autogpt_platform/frontend/src/components/atoms/Input/PasswordReveal.test.tsx b/autogpt_platform/frontend/src/components/atoms/Input/PasswordReveal.test.tsxnew file mode 100644index 0000000000..57ec7e9e5f--- /dev/null+++ b/autogpt_platform/frontend/src/components/atoms/Input/PasswordReveal.test.tsx@@ -0,0 +1,129 @@+import { act, fireEvent, render, screen } from "@testing-library/react";+import userEvent from "@testing-library/user-event";+import { describe, expect, it, vi } from "vitest";+import { Input } from "./Input";++describe("password reveal", () => {+ it("toggles its input type, accessible name and pressed state on click", async () => {+ const user = userEvent.setup();+ render(+ <Input+ id="password"+ label="Password"+ type="password"+ defaultValue="secret"+ />,+ );+ const input = screen.getByLabelText("Password");+ const toggle = screen.getByRole("button", { name: "Show password" });+ expect(input.getAttribute("type")).toBe("password");+ expect(toggle.getAttribute("aria-pressed")).toBe("false");+ await user.click(toggle);+ expect(input.getAttribute("type")).toBe("text");+ expect(+ screen+ .getByRole("button", { name: "Hide password" })+ .getAttribute("aria-pressed"),+ ).toBe("true");+ await user.click(toggle);+ expect(input.getAttribute("type")).toBe("password");+ expect(toggle.getAttribute("aria-label")).toBe("Show password");+ });++ it.each([" ", "{Enter}"])(+ "supports keyboard activation with %j without submitting",+ async (key) => {+ const user = userEvent.setup();+ const submit = vi.fn((event) => event.preventDefault());+ render(+ <form onSubmit={submit}>+ <Input id="password" label="Password" type="password" />+ <button type="submit">Submit</button>+ </form>,+ );+ await user.tab();+ await user.tab();+ const toggle = screen.getByRole("button", { name: "Show password" });+ expect(document.activeElement).toBe(toggle);+ await user.keyboard(key);+ expect(screen.getByLabelText("Password").getAttribute("type")).toBe(+ "text",+ );+ expect(toggle.getAttribute("aria-pressed")).toBe("true");+ expect(submit).not.toHaveBeenCalled();+ },+ );++ it("stays revealed within the input group and masks when focus leaves", async () => {+ const user = userEvent.setup();+ const onBlur = vi.fn();+ render(+ <>+ <Input id="password" label="Password" type="password" onBlur={onBlur} />+ <button>Next</button>+ </>,+ );+ const input = screen.getByLabelText("Password");+ await user.click(screen.getByRole("button", { name: "Show password" }));+ await user.click(input);+ expect(input.getAttribute("type")).toBe("text");+ await user.tab();+ expect(input.getAttribute("type")).toBe("text");+ expect(onBlur).toHaveBeenCalledOnce();+ await user.tab();+ expect(document.activeElement).toBe(+ screen.getByRole("button", { name: "Next" }),+ );+ expect(input.getAttribute("type")).toBe("password");+ });++ it("masks when focus has no related target", async () => {+ const user = userEvent.setup();+ render(<Input id="password" label="Password" type="password" />);+ const toggle = screen.getByRole("button", { name: "Show password" });+ await user.click(toggle);+ act(() => {+ fireEvent.blur(toggle, { relatedTarget: null });+ });+ expect(screen.getByLabelText("Password").getAttribute("type")).toBe(+ "password",+ );+ });++ it("does not reveal a disabled password", async () => {+ const user = userEvent.setup();+ render(<Input id="password" label="Password" type="password" disabled />);+ const toggle = screen.getByRole("button", { name: "Show password" });+ expect(toggle.hasAttribute("disabled")).toBe(true);+ await user.click(toggle);+ expect(screen.getByLabelText("Password").getAttribute("type")).toBe(+ "password",+ );+ });++ it.each(["text", "email", "textarea", "amount", "number"] as const)(+ "does not add a reveal button for %s",+ (type) => {+ render(<Input id="field" label="Field" type={type} />);+ expect(screen.queryByRole("button", { name: /password/i })).toBeNull();+ },+ );+});++it("associates each password label and toggle with its own input ID", () => {+ render(+ <>+ <Input id="first-password" label="First password" type="password" />+ <Input id="second-password" label="Second password" type="password" />+ </>,+ );+ const first = screen.getByLabelText("First password");+ const second = screen.getByLabelText("Second password");+ expect(first.id).toBeTruthy();+ expect(first.id).not.toBe(second.id);+ expect(+ screen+ .getAllByRole("button", { name: "Show password" })+ .map((button) => button.getAttribute("aria-controls")),+ ).toEqual([first.id, second.id]);+});