mirror of https://github.com/grafana/grafana
Password Field Improvements (#36160)
* Password: added show password functionality added autcomplete props created password component Fixes #28721 * addressed review changes and added unit tests * wrapped passwordField component in forwardRef * fix validation and testspull/36702/head
parent
dc8874cd2e
commit
c26bd6c52f
@ -0,0 +1,22 @@ |
||||
import React from 'react'; |
||||
import { fireEvent, render, screen } from '@testing-library/react'; |
||||
import { PasswordField } from './PasswordField'; |
||||
|
||||
describe('PasswordField', () => { |
||||
const props = { |
||||
id: 'password', |
||||
placeholder: 'enter password', |
||||
'data-testid': 'password-field', |
||||
}; |
||||
it('should renders correctly', () => { |
||||
render(<PasswordField {...props} />); |
||||
expect(screen.getByTestId('password-field')).toBeInTheDocument(); |
||||
expect(screen.getByRole('button')).toBeInTheDocument(); |
||||
}); |
||||
it('should able to show password value if clicked on password-reveal icon', () => { |
||||
render(<PasswordField {...props} />); |
||||
expect(screen.getByTestId('password-field')).toHaveProperty('type', 'password'); |
||||
fireEvent.click(screen.getByRole('button')); |
||||
expect(screen.getByTestId('password-field')).toHaveProperty('type', 'text'); |
||||
}); |
||||
}); |
@ -0,0 +1,44 @@ |
||||
import React, { FC, useState } from 'react'; |
||||
import { selectors } from '@grafana/e2e-selectors'; |
||||
|
||||
import { Input, IconButton } from '@grafana/ui'; |
||||
|
||||
export interface Props { |
||||
autoFocus?: boolean; |
||||
autoComplete?: string; |
||||
id?: string; |
||||
passwordHint?: string; |
||||
} |
||||
|
||||
export const PasswordField: FC<Props> = React.forwardRef<HTMLInputElement, Props>( |
||||
({ autoComplete, autoFocus, id, passwordHint, ...props }, ref) => { |
||||
const [showPassword, setShowPassword] = useState(false); |
||||
|
||||
return ( |
||||
<Input |
||||
id={id} |
||||
autoFocus={autoFocus} |
||||
autoComplete={autoComplete} |
||||
{...props} |
||||
type={showPassword ? 'text' : 'password'} |
||||
placeholder={passwordHint} |
||||
aria-label={selectors.pages.Login.password} |
||||
ref={ref} |
||||
suffix={ |
||||
<IconButton |
||||
name={showPassword ? 'eye-slash' : 'eye'} |
||||
surface="header" |
||||
aria-controls={id} |
||||
aria-expanded={showPassword} |
||||
onClick={(e) => { |
||||
e.preventDefault(); |
||||
setShowPassword(!showPassword); |
||||
}} |
||||
/> |
||||
} |
||||
/> |
||||
); |
||||
} |
||||
); |
||||
|
||||
PasswordField.displayName = 'PasswordField'; |
Loading…
Reference in new issue