|
| 1 | +import React, { forwardRef, Ref, RefObject } from "react"; |
| 2 | +import uuid from "uuid"; |
| 3 | +import { CollectionUtils, StringUtils } from "andculturecode-javascript-core"; |
| 4 | +import { InputCharacterCount } from "../../atoms/forms/input-character-count"; |
| 5 | +import { InputTypes } from "../../atoms/constants/input-types"; |
| 6 | +import { InputProperties } from "../../atoms/interfaces/input-properties"; |
| 7 | + |
| 8 | +// ----------------------------------------------------------------------------------------- |
| 9 | +// #region Constants |
| 10 | +// ----------------------------------------------------------------------------------------- |
| 11 | + |
| 12 | +const COMPONENT_CLASS = "c-form-field"; |
| 13 | + |
| 14 | +// #endregion Constants |
| 15 | + |
| 16 | +// ----------------------------------------------------------------------------------------- |
| 17 | +// #region Interfaces |
| 18 | +// ----------------------------------------------------------------------------------------- |
| 19 | + |
| 20 | +export interface InputFormFieldProps extends InputProperties { |
| 21 | + errorMessage?: string; |
| 22 | + errorMessages?: string[]; |
| 23 | + |
| 24 | + /** |
| 25 | + * Unique identifier used select the underlying <input> for functional/e2e testing |
| 26 | + */ |
| 27 | + inputTestId?: string; |
| 28 | + |
| 29 | + fieldId?: string; |
| 30 | + label: string; |
| 31 | + maxLength?: number; |
| 32 | + name?: string; |
| 33 | + ref?: RefObject<HTMLInputElement>; |
| 34 | + required?: boolean; |
| 35 | + showCharacterCount?: boolean; |
| 36 | + showLabelForScreenReadersOnly?: boolean; |
| 37 | +} |
| 38 | + |
| 39 | +// #endregion Interfaces |
| 40 | + |
| 41 | +// ----------------------------------------------------------------------------------------- |
| 42 | +// #region Component |
| 43 | +// ----------------------------------------------------------------------------------------- |
| 44 | + |
| 45 | +const InputFormField: React.RefForwardingComponent< |
| 46 | + HTMLInputElement, |
| 47 | + InputFormFieldProps |
| 48 | +> = forwardRef((props: InputFormFieldProps, ref: Ref<HTMLInputElement>) => { |
| 49 | + const { |
| 50 | + disabled, |
| 51 | + errorMessage, |
| 52 | + errorMessages, |
| 53 | + inputTestId, |
| 54 | + isValid, |
| 55 | + label, |
| 56 | + maxLength, |
| 57 | + name, |
| 58 | + onChange, |
| 59 | + placeholder, |
| 60 | + required, |
| 61 | + showCharacterCount, |
| 62 | + showLabelForScreenReadersOnly, |
| 63 | + type, |
| 64 | + value, |
| 65 | + } = props; |
| 66 | + |
| 67 | + const fieldId = props.fieldId ?? uuid.v4(); |
| 68 | + |
| 69 | + return ( |
| 70 | + <div className={`${COMPONENT_CLASS} ${isValid ? "" : "-invalid"}`}> |
| 71 | + <label htmlFor={fieldId}> |
| 72 | + {showLabelForScreenReadersOnly ? ( |
| 73 | + <span className="sr-only">{label}</span> |
| 74 | + ) : ( |
| 75 | + <React.Fragment>{label}</React.Fragment> |
| 76 | + )} |
| 77 | + {required && ( |
| 78 | + <span className={`${COMPONENT_CLASS}__required`}> |
| 79 | + {" *"} |
| 80 | + </span> |
| 81 | + )} |
| 82 | + </label> |
| 83 | + <input |
| 84 | + data-test-id={inputTestId} |
| 85 | + disabled={disabled} |
| 86 | + id={fieldId} |
| 87 | + placeholder={placeholder} |
| 88 | + maxLength={maxLength ?? 20} |
| 89 | + name={name} |
| 90 | + onChange={onChange} |
| 91 | + ref={ref} |
| 92 | + type={type ?? InputTypes.Text} |
| 93 | + value={value} |
| 94 | + /> |
| 95 | + <div className={`${COMPONENT_CLASS}__bottom`}> |
| 96 | + <div className={`${COMPONENT_CLASS}__bottom__errors`}> |
| 97 | + {// if |
| 98 | + StringUtils.hasValue(errorMessage) && ( |
| 99 | + <label>{errorMessage}</label> |
| 100 | + )} |
| 101 | + {// if |
| 102 | + CollectionUtils.hasValues(errorMessages) && |
| 103 | + errorMessages?.map((s: string) => ( |
| 104 | + <label key={s}>{s}</label> |
| 105 | + ))} |
| 106 | + </div> |
| 107 | + {// if |
| 108 | + showCharacterCount !== false && ( |
| 109 | + <InputCharacterCount |
| 110 | + currentLength={(value ?? "").length} |
| 111 | + maxLength={maxLength ?? 20} |
| 112 | + /> |
| 113 | + )} |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + ); |
| 117 | +}); |
| 118 | + |
| 119 | +// #endregion Component |
| 120 | + |
| 121 | +// ----------------------------------------------------------------------------------------- |
| 122 | +// #region Exports |
| 123 | +// ----------------------------------------------------------------------------------------- |
| 124 | + |
| 125 | +export { InputFormField }; |
| 126 | + |
| 127 | +// #endregion Exports |
0 commit comments