Skip to content

Commit b4f165e

Browse files
committed
Self-review of SelectFormField and TextAreaFormField
1 parent 77344d4 commit b4f165e

2 files changed

Lines changed: 39 additions & 17 deletions

File tree

src/molecules/form-fields/select-form-field.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import React from "react";
22
import uuid from "uuid";
33
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
413

514
// -----------------------------------------------------------------------------------------
615
// #region Interfaces
@@ -40,23 +49,25 @@ const SelectFormField: React.FC<SelectFormFieldProps> = (
4049
values,
4150
} = props;
4251

52+
const cssIsValid = isValid ? "" : "-invalid";
4353
const fieldId = props.fieldId ?? uuid.v4();
54+
const hasErrorMessage = StringUtils.hasValue(errorMessage);
55+
const hasErrors = CollectionUtils.hasValues(errorMessages);
4456

4557
return (
46-
<div className={`c-form-field ${isValid ? "" : "-invalid"}`}>
58+
<div className={`${COMPONENT_CLASS} ${cssIsValid}`}>
4759
<label htmlFor={fieldId}>
4860
{label}
49-
{required ? "*" : ""}
61+
{// if
62+
required && "*"}
5063
</label>
5164
<Select options={values} id={id} onChange={onChange} name={name} />
52-
<div className="c-form-field__errors">
65+
<div className={`${COMPONENT_CLASS}__errors`}>
5366
{// if
54-
errorMessage != null && errorMessage.length > 0 && (
55-
<label>{errorMessage}</label>
56-
)}
67+
hasErrorMessage && <label>{errorMessage}</label>}
5768
{// if
58-
errorMessages != null &&
59-
errorMessages.map((s: string) => (
69+
hasErrors &&
70+
errorMessages?.map((s: string) => (
6071
<label key={s}>{s}</label>
6172
))}
6273
</div>

src/molecules/form-fields/text-area-form-field.tsx

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import React from "react";
22
import uuid from "uuid";
33
import { TextArea } from "../../atoms/forms/text-area";
4+
import { StringUtils, CollectionUtils } from "andculturecode-javascript-core";
5+
6+
// -----------------------------------------------------------------------------------------
7+
// #region Constants
8+
// -----------------------------------------------------------------------------------------
9+
10+
const COMPONENT_CLASS = "c-form-field";
11+
12+
// #endregion Constants
413

514
// -----------------------------------------------------------------------------------------
615
// #region Interfaces
@@ -51,33 +60,35 @@ const TextAreaFormField: React.FC<TextAreaFormFieldProps> = (
5160
value,
5261
} = props;
5362

63+
const cssIsValid = isValid ? "" : "-invalid";
5464
const fieldId = props.fieldId ?? uuid.v4();
65+
const hasErrorMessage = StringUtils.hasValue(errorMessage);
66+
const hasErrors = CollectionUtils.hasValues(errorMessages);
5567

5668
return (
57-
<div className={`c-form-field ${isValid ? "" : "-invalid"}`}>
69+
<div className={`${COMPONENT_CLASS} ${cssIsValid}`}>
5870
<label htmlFor={fieldId}>
5971
{label}
60-
{required ? "*" : ""}
72+
{// if
73+
required ?? "*"}
6174
</label>
6275
<TextArea
6376
disabled={disabled}
6477
id={fieldId}
65-
testId={inputTestId}
6678
maxLength={maxLength}
6779
name={name}
6880
onChange={onChange}
6981
placeholder={placeholder}
7082
rows={rows}
83+
testId={inputTestId}
7184
value={value}
7285
/>
73-
<div className="c-form-field__errors">
86+
<div className={`${COMPONENT_CLASS}__errors`}>
7487
{// if
75-
errorMessage != null && errorMessage.length > 0 && (
76-
<label>{errorMessage}</label>
77-
)}
88+
hasErrorMessage && <label>{errorMessage}</label>}
7889
{// if
79-
errorMessages != null &&
80-
errorMessages.map((s: string) => (
90+
hasErrors &&
91+
errorMessages?.map((s: string) => (
8192
<label key={s}>{s}</label>
8293
))}
8394
</div>

0 commit comments

Comments
 (0)