|
1 | 1 | import React from "react"; |
2 | 2 | import uuid from "uuid"; |
3 | 3 | import { Select, SelectOption } from "../../atoms/forms/select"; |
| 4 | +import { StringUtils, CollectionUtils } from "andculturecode-javascript-core"; |
| 5 | + |
| 6 | +// ----------------------------------------------------------------------------------------- |
| 7 | +// #region Component |
| 8 | +// ----------------------------------------------------------------------------------------- |
| 9 | + |
| 10 | +const COMPONENT_CLASS = "c-form-field"; |
| 11 | + |
| 12 | +// #endregion Component |
4 | 13 |
|
5 | 14 | // ----------------------------------------------------------------------------------------- |
6 | 15 | // #region Interfaces |
@@ -40,23 +49,25 @@ const SelectFormField: React.FC<SelectFormFieldProps> = ( |
40 | 49 | values, |
41 | 50 | } = props; |
42 | 51 |
|
| 52 | + const cssIsValid = isValid ? "" : "-invalid"; |
43 | 53 | const fieldId = props.fieldId ?? uuid.v4(); |
| 54 | + const hasErrorMessage = StringUtils.hasValue(errorMessage); |
| 55 | + const hasErrors = CollectionUtils.hasValues(errorMessages); |
44 | 56 |
|
45 | 57 | return ( |
46 | | - <div className={`c-form-field ${isValid ? "" : "-invalid"}`}> |
| 58 | + <div className={`${COMPONENT_CLASS} ${cssIsValid}`}> |
47 | 59 | <label htmlFor={fieldId}> |
48 | 60 | {label} |
49 | | - {required ? "*" : ""} |
| 61 | + {// if |
| 62 | + required && "*"} |
50 | 63 | </label> |
51 | 64 | <Select options={values} id={id} onChange={onChange} name={name} /> |
52 | | - <div className="c-form-field__errors"> |
| 65 | + <div className={`${COMPONENT_CLASS}__errors`}> |
53 | 66 | {// if |
54 | | - errorMessage != null && errorMessage.length > 0 && ( |
55 | | - <label>{errorMessage}</label> |
56 | | - )} |
| 67 | + hasErrorMessage && <label>{errorMessage}</label>} |
57 | 68 | {// if |
58 | | - errorMessages != null && |
59 | | - errorMessages.map((s: string) => ( |
| 69 | + hasErrors && |
| 70 | + errorMessages?.map((s: string) => ( |
60 | 71 | <label key={s}>{s}</label> |
61 | 72 | ))} |
62 | 73 | </div> |
|
0 commit comments