|
| 1 | +import React from "react"; |
| 2 | +import uuid from "uuid"; |
| 3 | +import { Select, SelectOption } from "../../atoms/forms/select"; |
| 4 | + |
| 5 | +// ----------------------------------------------------------------------------------------- |
| 6 | +// #region Interfaces |
| 7 | +// ----------------------------------------------------------------------------------------- |
| 8 | + |
| 9 | +export interface SelectFormFieldProps { |
| 10 | + errorMessage?: string; |
| 11 | + errorMessages?: string[]; |
| 12 | + fieldId?: string; |
| 13 | + id: string; |
| 14 | + isValid?: boolean; |
| 15 | + name?: string; |
| 16 | + label: string; |
| 17 | + onChange: (e: SelectOption<any> | undefined) => void; |
| 18 | + required?: boolean; |
| 19 | + values: SelectOption[]; |
| 20 | +} |
| 21 | + |
| 22 | +// #endregion Interfaces |
| 23 | + |
| 24 | +// ----------------------------------------------------------------------------------------- |
| 25 | +// #region Component |
| 26 | +// ----------------------------------------------------------------------------------------- |
| 27 | + |
| 28 | +const SelectFormField: React.FC<SelectFormFieldProps> = ( |
| 29 | + props: SelectFormFieldProps |
| 30 | +) => { |
| 31 | + const { |
| 32 | + errorMessage, |
| 33 | + errorMessages, |
| 34 | + id, |
| 35 | + isValid, |
| 36 | + name, |
| 37 | + label, |
| 38 | + onChange, |
| 39 | + required, |
| 40 | + values, |
| 41 | + } = props; |
| 42 | + |
| 43 | + const fieldId = props.fieldId ?? uuid.v4(); |
| 44 | + |
| 45 | + return ( |
| 46 | + <div className={`c-form-field ${isValid ? "" : "-invalid"}`}> |
| 47 | + <label htmlFor={fieldId}> |
| 48 | + {label} |
| 49 | + {required ? "*" : ""} |
| 50 | + </label> |
| 51 | + <Select options={values} id={id} onChange={onChange} name={name} /> |
| 52 | + <div className="c-form-field__errors"> |
| 53 | + {// if |
| 54 | + errorMessage != null && errorMessage.length > 0 && ( |
| 55 | + <label>{errorMessage}</label> |
| 56 | + )} |
| 57 | + {// if |
| 58 | + errorMessages != null && |
| 59 | + errorMessages.map((s: string) => ( |
| 60 | + <label key={s}>{s}</label> |
| 61 | + ))} |
| 62 | + </div> |
| 63 | + </div> |
| 64 | + ); |
| 65 | +}; |
| 66 | + |
| 67 | +// #endregion Component |
| 68 | + |
| 69 | +// ----------------------------------------------------------------------------------------- |
| 70 | +// #region Exports |
| 71 | +// ----------------------------------------------------------------------------------------- |
| 72 | + |
| 73 | +export { SelectFormField }; |
| 74 | + |
| 75 | +// #endregion Exports |
0 commit comments