Skip to content

Commit 0296174

Browse files
mgold1234regexowl
authored andcommitted
Wizard: Refactor hostname and blueprint name inputs
This commit Implement refactor of HookValidatedInput for hostname and blueprint name fields, addressing the following bugs: 1) Fixes a bug where the validation symbol persisted after a user deleted the value in the hostname field. 2) Fixes a bug where the validation symbol persisted after a user deleted the value in the blueprint name field. These changes improve code maintainability and provide a more consistent user experience.
1 parent 1d39a57 commit 0296174

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

src/Components/CreateImageWizard/ValidatedInput.tsx

+10-3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type ValidationInputProp = TextInputProps &
5050
event: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>,
5151
value: string
5252
) => void;
53+
isRequired?: boolean;
5354
};
5455

5556
type ErrorMessageProps = {
@@ -117,12 +118,13 @@ export const ValidatedInputAndTextArea = ({
117118
onChange,
118119
ariaLabel,
119120
inputType = 'textInput',
121+
isRequired,
120122
}: ValidationInputProp) => {
121123
const errorMessage = stepValidation.errors[fieldName];
122124
const hasError = errorMessage !== '';
123125

124126
const [isPristine, setIsPristine] = useState(!value);
125-
const validated = getValidationState(isPristine, errorMessage);
127+
const validated = getValidationState(isPristine, errorMessage, isRequired);
126128

127129
const handleBlur = () => {
128130
if (value) {
@@ -164,9 +166,14 @@ export const ValidatedInputAndTextArea = ({
164166

165167
const getValidationState = (
166168
isPristine: boolean,
167-
errorMessage: string
169+
errorMessage: string,
170+
isRequired: boolean | undefined
168171
): ValidationResult => {
169-
const validated = isPristine ? 'default' : errorMessage ? 'error' : 'success';
172+
const validated = isPristine
173+
? 'default'
174+
: (isRequired && errorMessage) || errorMessage
175+
? 'error'
176+
: 'success';
170177

171178
return validated;
172179
};

src/Components/CreateImageWizard/steps/Details/index.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import {
1919
} from '../../../../store/wizardSlice';
2020
import { useGenerateDefaultName } from '../../utilities/useGenerateDefaultName';
2121
import { useDetailsValidation } from '../../utilities/useValidation';
22-
import { HookValidatedInput } from '../../ValidatedInput';
22+
import {
23+
HookValidatedInput,
24+
ValidatedInputAndTextArea,
25+
} from '../../ValidatedInput';
2326

2427
const DetailsStep = () => {
2528
const dispatch = useAppDispatch();
@@ -55,15 +58,15 @@ const DetailsStep = () => {
5558
blueprint.
5659
</Text>
5760
<FormGroup isRequired label="Blueprint name" fieldId="blueprint-name">
58-
<HookValidatedInput
61+
<ValidatedInputAndTextArea
5962
ariaLabel="blueprint name"
60-
dataTestId="blueprint"
6163
value={blueprintName}
6264
isDisabled={false}
6365
onChange={handleNameChange}
6466
placeholder="Add blueprint name"
6567
stepValidation={stepValidation}
6668
fieldName="name"
69+
isRequired={true}
6770
/>
6871
<FormHelperText>
6972
<HelperText>

src/Components/CreateImageWizard/steps/Hostname/components/HostnameInput.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
selectHostname,
99
} from '../../../../../store/wizardSlice';
1010
import { useHostnameValidation } from '../../../utilities/useValidation';
11-
import { HookValidatedInput } from '../../../ValidatedInput';
11+
import { ValidatedInputAndTextArea } from '../../../ValidatedInput';
1212

1313
const HostnameInput = () => {
1414
const dispatch = useAppDispatch();
@@ -22,7 +22,7 @@ const HostnameInput = () => {
2222

2323
return (
2424
<FormGroup label="Hostname">
25-
<HookValidatedInput
25+
<ValidatedInputAndTextArea
2626
ariaLabel="hostname input"
2727
value={hostname}
2828
onChange={handleChange}

src/Components/CreateImageWizard/utilities/useValidation.tsx

+11-9
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,14 @@ export function useHostnameValidation(): StepValidation {
250250
const errorMessage =
251251
'Invalid hostname. The hostname should be composed of up to 64 7-bit ASCII lower-case alphanumeric characters or hyphens forming a valid DNS domain name. It is recommended that this name contains only a single label, i.e. without any dots.';
252252

253-
if (!isHostnameValid(hostname)) {
254-
return {
255-
errors: {
256-
hostname: errorMessage,
257-
},
258-
disabledNext: true,
259-
};
260-
}
261-
return { errors: {}, disabledNext: false };
253+
const hostnameError = !isHostnameValid(hostname) ? errorMessage : '';
254+
255+
return {
256+
errors: {
257+
hostname: hostnameError,
258+
},
259+
disabledNext: !!hostnameError,
260+
};
262261
}
263262

264263
export function useKernelValidation(): StepValidation {
@@ -479,6 +478,9 @@ export function useDetailsValidation(): StepValidation {
479478
}, [blueprintId, name, setUniqueName, trigger, nameValid]);
480479

481480
let nameError = '';
481+
if (!name) {
482+
nameError = 'Blueprint name is required';
483+
}
482484
if (name && !nameValid) {
483485
nameError = 'Invalid blueprint name';
484486
} else if (uniqueName === false) {

0 commit comments

Comments
 (0)