Skip to content
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

*feat PasswordField hideToggle property #508

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/ui/__stories__/PasswordField.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,8 @@ export const WrappedInFormik = () => {

return <TestForm />
}

export const WithoutToggle = Template.bind({})
WithoutToggle.args = {
hideToggle: true,
}
20 changes: 20 additions & 0 deletions packages/ui/__tests__/PasswordField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,24 @@ describe('PasswordField', () => {

expect(wrapper.find(IconButton).prop('disabled')).toBe(true)
})

it('hides toggle button', async () => {
const onBlur = jest.fn()
const onChange = jest.fn()

const wrapper = await mountAndCheckA11Y(
<PasswordField
name="name"
value="password"
placeholder="Enter the name"
label="Wisest jedi"
onBlur={onBlur}
onChange={onChange}
hideToggle
disabled
/>,
)

expect(wrapper.findDataTestFirst('password-field-toggle').exists()).toBeFalsy()
})
})
6 changes: 6 additions & 0 deletions packages/ui/src/PasswordField.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ $inputMargin: c.$iconSizeSmall + $buttonMargin * 2;
top: 50%;
transform: translateY(-50%);
}

&.withoutToggle {
> input {
margin-right: 0;
}
}
}
11 changes: 9 additions & 2 deletions packages/ui/src/PasswordField.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DataTestProp } from '@hazelcast/helpers'
import React, { FC, FocusEvent, ChangeEvent, InputHTMLAttributes, useMemo, useState } from 'react'
import { Eye, EyeOff, Lock } from 'react-feather'
import cls from 'classnames'

import { TextField, TextFieldSize, TextFieldVariant } from './TextField'
import { IconButton, IconButtonDisabledProps, IconButtonNotDisabledProps } from './IconButton'
Expand All @@ -14,6 +15,7 @@ type PasswordFieldCoreProps = {
onBlur?: (e: FocusEvent<HTMLInputElement>) => void
onChange: (e: ChangeEvent<HTMLInputElement>) => void
error?: string
hideToggle?: boolean
}
export type PasswordFieldExtraProps = {
showIconLabel?: string
Expand All @@ -39,11 +41,16 @@ export const PasswordField: FC<PasswordFieldProps> = ({
withIcon,
disabled,
initiallyVisible = false,
hideToggle = false,
...props
}) => {
const [visible, setVisible] = useState(initiallyVisible)

const overlay = useMemo(() => {
if (hideToggle) {
return undefined
}

let disabledProps: IconButtonDisabledProps | IconButtonNotDisabledProps = {}
if (disabled) {
// The IconButton is disabled only when the entire input is disabled. No need for a tooltip.
Expand All @@ -67,14 +74,14 @@ export const PasswordField: FC<PasswordFieldProps> = ({
{...disabledProps}
/>
)
}, [visible, hideIconLabel, showIconLabel, disabled])
}, [visible, hideToggle, hideIconLabel, showIconLabel, disabled])

return (
<TextField
{...props}
type={visible ? 'text' : 'password'}
inputContainerChild={overlay}
inputContainerClassName={styles.inputContainer}
inputContainerClassName={cls(styles.inputContainer, { [styles.withoutToggle]: hideToggle })}
inputClassName={inputClassName}
inputIcon={withIcon ? Lock : undefined}
disabled={disabled}
Expand Down
Loading