|
| 1 | +import React, { useState } from 'react'; |
| 2 | + |
| 3 | +import * as Form from '@radix-ui/react-form'; |
| 4 | +import cn from 'classnames'; |
| 5 | + |
| 6 | +import { Flex } from '../flex'; |
| 7 | +import { Text } from '../text'; |
| 8 | + |
| 9 | +import { PasswordInputButton } from './password-box-button.component'; |
| 10 | +import * as cx from './password-box-input.css'; |
| 11 | + |
| 12 | +export type Password = { |
| 13 | + input: HTMLInputElement; |
| 14 | + // TODO: convert this to UInt8Array |
| 15 | + value: string; |
| 16 | +}; |
| 17 | + |
| 18 | +export type OnPasswordChange = (event: Readonly<Password>) => void; |
| 19 | + |
| 20 | +export interface UncontrolledPasswordInputProps |
| 21 | + extends Omit<Form.FormControlProps, 'value' | 'onChange'> { |
| 22 | + required?: boolean; |
| 23 | + disabled?: boolean; |
| 24 | + id?: string; |
| 25 | + label: string; |
| 26 | + name?: string; |
| 27 | + errorMessage?: string; |
| 28 | + onChange: OnPasswordChange; |
| 29 | + defaultIsPasswordVisible?: boolean; |
| 30 | + containerClassName?: string; |
| 31 | + containerStyle?: React.CSSProperties; |
| 32 | + testId?: string; |
| 33 | +} |
| 34 | + |
| 35 | +export const UncontrolledPasswordInput = ({ |
| 36 | + required = false, |
| 37 | + disabled = false, |
| 38 | + id, |
| 39 | + label, |
| 40 | + name, |
| 41 | + errorMessage = '', |
| 42 | + containerClassName = '', |
| 43 | + onChange, |
| 44 | + defaultIsPasswordVisible = false, |
| 45 | + containerStyle, |
| 46 | + testId, |
| 47 | +}: Readonly<UncontrolledPasswordInputProps>): JSX.Element => { |
| 48 | + const [isPasswordVisible, setIsPasswordVisible] = useState( |
| 49 | + defaultIsPasswordVisible, |
| 50 | + ); |
| 51 | + |
| 52 | + const onChangeHandler: React.ChangeEventHandler<HTMLInputElement> = event => |
| 53 | + onChange({ input: event.target, value: event.target.value }); |
| 54 | + |
| 55 | + return ( |
| 56 | + <div className={cx.root}> |
| 57 | + <Form.Field |
| 58 | + name="field" |
| 59 | + className={cn(cx.container, { |
| 60 | + [cx.disabledContainer]: disabled, |
| 61 | + [containerClassName]: containerClassName, |
| 62 | + })} |
| 63 | + style={containerStyle} |
| 64 | + > |
| 65 | + <Flex justifyContent="space-between" alignItems="center"> |
| 66 | + <Form.Control asChild> |
| 67 | + <input |
| 68 | + type={isPasswordVisible ? 'text' : 'password'} |
| 69 | + required={required} |
| 70 | + placeholder="" |
| 71 | + className={cn(cx.input, { [cx.largeDots]: !isPasswordVisible })} |
| 72 | + disabled={disabled} |
| 73 | + name={name} |
| 74 | + onChange={onChangeHandler} |
| 75 | + id={id} |
| 76 | + data-testid={testId} |
| 77 | + /> |
| 78 | + </Form.Control> |
| 79 | + <Form.Label |
| 80 | + className={cn(cx.label, { [cx.disabledLabel]: disabled })} |
| 81 | + > |
| 82 | + {label} |
| 83 | + </Form.Label> |
| 84 | + <PasswordInputButton |
| 85 | + testId={testId && `${testId}-toggle`} |
| 86 | + onClick={(event): void => { |
| 87 | + event.preventDefault(); |
| 88 | + setIsPasswordVisible(!isPasswordVisible); |
| 89 | + }} |
| 90 | + disabled={disabled} |
| 91 | + isPasswordVisible={isPasswordVisible} |
| 92 | + /> |
| 93 | + </Flex> |
| 94 | + </Form.Field> |
| 95 | + {errorMessage && ( |
| 96 | + <Text.Label color="error" className={cx.errorMessage}> |
| 97 | + {errorMessage} |
| 98 | + </Text.Label> |
| 99 | + )} |
| 100 | + </div> |
| 101 | + ); |
| 102 | +}; |
0 commit comments