-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #750 from balsa-asanovic/wifi-password-show-button
Add PasswordInput component with visibility button
- Loading branch information
Showing
11 changed files
with
200 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) [2023] SUSE LLC | ||
* | ||
* All Rights Reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of version 2 of the GNU General Public License as published | ||
* by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, contact SUSE LLC. | ||
* | ||
* To contact SUSE LLC about this file by physical or electronic mail, you may | ||
* find current contact information at www.suse.com. | ||
*/ | ||
|
||
/** | ||
* Renders a password input field and a toggle button that can be used to reveal | ||
* and hide the password | ||
* @component | ||
* | ||
* @param {string} id - the identifier for the field. | ||
* @param {Object} props - props matching the {@link https://www.patternfly.org/components/forms/text-input PF/TextInput}, | ||
* except `type` that will be ignored. | ||
*/ | ||
import React, { useState } from "react"; | ||
import { | ||
Button, | ||
InputGroup, | ||
TextInput | ||
} from "@patternfly/react-core"; | ||
import { Icon } from "~/components/layout"; | ||
import { _ } from "~/i18n"; | ||
|
||
export default function PasswordInput({ id, ...props }) { | ||
const [showPassword, setShowPassword] = useState(false); | ||
const visibilityIconName = showPassword ? "visibility_off" : "visibility"; | ||
|
||
if (!id) { | ||
const field = props.label || props["aria-label"] || props.name; | ||
console.error(`The PasswordInput component must have an 'id' but it was not given for '${field}'`); | ||
} | ||
|
||
return ( | ||
<InputGroup> | ||
<TextInput | ||
{...props} | ||
id={id} | ||
type={showPassword ? 'text' : 'password'} | ||
/> | ||
<Button | ||
id={`toggle-${id}-visibility`} | ||
className="password-toggler" | ||
aria-label={_("Password visibility button")} | ||
variant="control" | ||
onClick={() => setShowPassword((prev) => !prev)} | ||
icon={<Icon name={visibilityIconName} size="15" />} | ||
isDisabled={props.isDisabled} | ||
/> | ||
</InputGroup> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (c) [2023] SUSE LLC | ||
* | ||
* All Rights Reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of version 2 of the GNU General Public License as published | ||
* by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, contact SUSE LLC. | ||
* | ||
* To contact SUSE LLC about this file by physical or electronic mail, you may | ||
* find current contact information at www.suse.com. | ||
*/ | ||
|
||
import React, { useState } from "react"; | ||
import { screen } from "@testing-library/react"; | ||
import { plainRender } from "~/test-utils"; | ||
import userEvent from "@testing-library/user-event"; | ||
import PasswordInput from "./PasswordInput"; | ||
import { _ } from "~/i18n"; | ||
|
||
describe("PasswordInput Component", () => { | ||
it("renders a password input", () => { | ||
plainRender( | ||
<PasswordInput | ||
id="password" | ||
name="password" | ||
aria-label={_("User password")} | ||
/> | ||
); | ||
|
||
const inputField = screen.getByLabelText("User password"); | ||
expect(inputField).toHaveAttribute("type", "password"); | ||
}); | ||
|
||
it("allows revealing the password", async () => { | ||
plainRender( | ||
<PasswordInput | ||
id="password" | ||
name="password" | ||
aria-label={_("User password")} | ||
/> | ||
); | ||
|
||
const passwordInput = screen.getByLabelText("User password"); | ||
const button = screen.getByRole("button"); | ||
|
||
expect(passwordInput).toHaveAttribute("type", "password"); | ||
await userEvent.click(button); | ||
expect(passwordInput).toHaveAttribute("type", "text"); | ||
}); | ||
|
||
it("applies autoFocus behavior correctly", () => { | ||
plainRender( | ||
<PasswordInput | ||
autoFocus | ||
id="password" | ||
name="password" | ||
aria-label={_("User password")} | ||
/> | ||
); | ||
|
||
const inputField = screen.getByLabelText("User password"); | ||
expect(document.activeElement).toBe(inputField); | ||
}); | ||
|
||
// Using a controlled component for testing the rendered result instead of testing if | ||
// the given onChange callback is called. The former is more aligned with the | ||
// React Testing Library principles, https://testing-library.com/docs/guiding-principles/ | ||
const PasswordInputTest = (props) => { | ||
const [password, setPassword] = useState(null); | ||
|
||
return ( | ||
<> | ||
<PasswordInput {...props} onChange={setPassword} /> | ||
{password && <p>Password value updated!</p>} | ||
</> | ||
); | ||
}; | ||
|
||
it("triggers onChange callback", async () => { | ||
const { user } = plainRender(<PasswordInputTest id="test-password" aria-label="Test password" />); | ||
const passwordInput = screen.getByLabelText("Test password"); | ||
|
||
expect(screen.queryByText("Password value updated!")).toBeNull(); | ||
await user.type(passwordInput, "secret"); | ||
|
||
screen.getByText("Password value updated!"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters