|
| 1 | +import React, { useState } from "react"; |
| 2 | +import uuid from "uuid"; |
| 3 | +import { InputProperties } from "../../atoms/interfaces/input-properties"; |
| 4 | +import { PasswordInput } from "../../atoms/forms/password-input"; |
| 5 | + |
| 6 | +// ----------------------------------------------------------------------------------------- |
| 7 | +// #region Interfaces |
| 8 | +// ----------------------------------------------------------------------------------------- |
| 9 | + |
| 10 | +export interface PasswordFormFields extends InputProperties { |
| 11 | + errorMessage?: string; |
| 12 | + |
| 13 | + /** |
| 14 | + * Unique identifier used select the underlying <input> for functional/e2e testing |
| 15 | + */ |
| 16 | + inputTestId?: string; |
| 17 | + |
| 18 | + label: string; |
| 19 | + required?: boolean; |
| 20 | +} |
| 21 | + |
| 22 | +// #endregion Interfaces |
| 23 | + |
| 24 | +// ----------------------------------------------------------------------------------------- |
| 25 | +// #region Component |
| 26 | +// ----------------------------------------------------------------------------------------- |
| 27 | + |
| 28 | +const PasswordFormField: React.FC<PasswordFormFields> = ( |
| 29 | + props: PasswordFormFields |
| 30 | +) => { |
| 31 | + const { |
| 32 | + disabled, |
| 33 | + errorMessage, |
| 34 | + inputTestId, |
| 35 | + isValid, |
| 36 | + label, |
| 37 | + placeholder, |
| 38 | + onChange, |
| 39 | + required, |
| 40 | + value, |
| 41 | + } = props; |
| 42 | + |
| 43 | + const fieldId = uuid.v4(); |
| 44 | + const [isVisible, setIsVisible] = useState<boolean>(false); |
| 45 | + const disableShowHide = value == null || value === "" || disabled; |
| 46 | + const passwordShowHideLabel = isVisible ? "Hide" : "Show"; |
| 47 | + |
| 48 | + const onChangeIsVisible = ( |
| 49 | + event: React.MouseEvent<Element, MouseEvent> |
| 50 | + ) => { |
| 51 | + event.preventDefault(); |
| 52 | + |
| 53 | + setIsVisible(!isVisible); |
| 54 | + }; |
| 55 | + |
| 56 | + if (disabled && isVisible === true) { |
| 57 | + setIsVisible(false); |
| 58 | + } |
| 59 | + |
| 60 | + return ( |
| 61 | + <div className={`c-form-field -password ${isValid ? "" : "-invalid"}`}> |
| 62 | + <label htmlFor={fieldId}> |
| 63 | + {label} |
| 64 | + {required && ( |
| 65 | + <span className="c-form-field__required">{" *"}</span> |
| 66 | + )} |
| 67 | + </label> |
| 68 | + {// if |
| 69 | + !disableShowHide && ( |
| 70 | + <button |
| 71 | + type="button" |
| 72 | + aria-label={`${passwordShowHideLabel} Password`} |
| 73 | + onClick={(event) => onChangeIsVisible(event)}> |
| 74 | + {passwordShowHideLabel} |
| 75 | + </button> |
| 76 | + )} |
| 77 | + <PasswordInput |
| 78 | + disabled={disabled} |
| 79 | + id={fieldId} |
| 80 | + isValid={isValid} |
| 81 | + isVisible={isVisible} |
| 82 | + onChange={onChange} |
| 83 | + placeholder={placeholder} |
| 84 | + testId={inputTestId} |
| 85 | + value={value} |
| 86 | + /> |
| 87 | + <div className="c-form-field__bottom"> |
| 88 | + <div className="c-form-field__bottom__errors"> |
| 89 | + <label>{errorMessage}</label> |
| 90 | + </div> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +}; |
| 95 | + |
| 96 | +// #endregion Component |
| 97 | + |
| 98 | +// ----------------------------------------------------------------------------------------- |
| 99 | +// #region Exports |
| 100 | +// ----------------------------------------------------------------------------------------- |
| 101 | + |
| 102 | +export { PasswordFormField }; |
| 103 | + |
| 104 | +// #endregion Exports |
0 commit comments