-
Notifications
You must be signed in to change notification settings - Fork 74
Created PasswordInput component with visibility button (eye icon) #750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dgdavid
merged 49 commits into
agama-project:master
from
balsa-asanovic:wifi-password-show-button
Sep 20, 2023
Merged
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
6ffd9f7
added show button to the WiFi password input
balsa-asanovic c297a64
added description to changes file
balsa-asanovic 7c3dd11
added icon-size-15 class to utilities.scss
balsa-asanovic e4e1cf4
imported visibility icons to Icon component
balsa-asanovic 772a34e
switched from text to icon inside the button
balsa-asanovic 948782b
updated changes file
balsa-asanovic 30ccb0e
updated changes file
balsa-asanovic 2aacf8f
added desc to changes file
balsa-asanovic 1424e61
updated changes file to resolve conflict
balsa-asanovic 05ee9f1
Merge branch 'openSUSE:master' into wifi-password-show-button
balsa-asanovic 4266493
added desc to changes file
balsa-asanovic 61e0764
moved visibility lines above warning in Icon comp
balsa-asanovic 569c823
removed label attribute from TextInput
balsa-asanovic e57a463
removed label property from Button
balsa-asanovic 580d410
added variable to track type of visibility icon
balsa-asanovic 1d7491d
centering eye icon of the visibility button
balsa-asanovic 09a58a7
added newline at the end of file
balsa-asanovic 2213fbf
created PasswordInput component
balsa-asanovic d7c5e6a
added export for PasswordInput in index.js
balsa-asanovic 36da61f
switched in WifiConnectionForm to use PasswordInput
balsa-asanovic 468b3fa
Merge branch 'openSUSE:master' into wifi-password-show-button
balsa-asanovic 88e7e67
Merge branch 'wifi-password-show-button' of https://github.com/balsa-…
balsa-asanovic 0317fc9
added validated and isDisabled props
balsa-asanovic a51464b
switched to using PasswordInput component
balsa-asanovic 3dfac77
added autoFocus prop to PasswordInput
balsa-asanovic 1c007f4
switched to using PasswordInput component
balsa-asanovic 2c282e0
added onBlur prop to PasswordInput
balsa-asanovic bff2400
switched to using PasswordInput component
balsa-asanovic e375121
added FormGroup node to PasswordInput comp
balsa-asanovic f4bd01a
removed FormGroup from password input part
balsa-asanovic 5af26c3
added unit tests for PasswordInput component
balsa-asanovic 97f55fa
updated changes file
balsa-asanovic 7aba261
adjusted description in changes file
balsa-asanovic a4a356d
updated changelog
balsa-asanovic 35cfb05
Merge branch 'openSUSE:master' into wifi-password-show-button
balsa-asanovic 328f83c
removed FormGroup, simplifed props
balsa-asanovic 2d74e8b
removed split prop and div wrapper for it
balsa-asanovic b17730f
reverted to using FormGroup
balsa-asanovic 107e20f
adjusted test considering simplifed component
balsa-asanovic 7c401ff
adjusted description of css comment
balsa-asanovic 17217c7
changelog
balsa-asanovic 233226a
Merge branch 'wifi-password-show-button' of https://github.com/balsa-…
balsa-asanovic 370d52e
missing semicolon
balsa-asanovic 0346c08
added className to visibility button
balsa-asanovic 2341ca5
changed CSS override to target class instead of id
balsa-asanovic 06e6197
year typo correction
balsa-asanovic 578be45
added unit test for onChange callback
balsa-asanovic 7e97287
corrected eslinit issues in test file
balsa-asanovic 6f12cf0
added documention data to component
balsa-asanovic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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} | ||
|
dgdavid marked this conversation as resolved.
|
||
| 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 hidden or 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"); | ||
| }); | ||
|
|
||
|
balsa-asanovic marked this conversation as resolved.
|
||
| 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.