Skip to content

Commit

Permalink
Make errors on fields appear on blur (#2322)
Browse files Browse the repository at this point in the history
Co-authored-by: Montse Ortega <[email protected]>
  • Loading branch information
2 people authored and rawagner committed Oct 3, 2023
1 parent 42b9177 commit 3617b1c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
9 changes: 4 additions & 5 deletions libs/ui-lib/lib/common/components/ui/formik/InputField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,22 @@ const InputField: React.FC<
idPostfix,
children,
noDefaultOnChange,
inputError,
description,
labelInfo,
showErrorMessage = true,
...props
},
ref: React.Ref<HTMLInputElement>,
) => {
const [field] = useField({
const [field, { error, touched }] = useField({
name: props.name,
validate,
});

const fieldId = getFieldId(props.name, 'input', idPostfix);
const errorMessage = useFieldErrorMsg({ name: props.name, inputError, validate });
const errorMessage = useFieldErrorMsg({ name: props.name, inputError: error, validate });
const fieldHelperText = <HelperText fieldId={fieldId}>{helperText}</HelperText>;
const isValid = !errorMessage;
const isValid = !(touched && error);

return (
<Stack id={`form-control__${fieldId}`}>
Expand Down Expand Up @@ -88,7 +87,7 @@ const InputField: React.FC<
</FormGroup>
</StackItem>
<StackItem>
{showErrorMessage && errorMessage && (
{showErrorMessage && !isValid && (
<HelperText fieldId={fieldId} isError>
{errorMessage}
</HelperText>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ const RichInputField: React.FC<RichInputFieldPropsProps> = React.forwardRef(
validate,
});
const fieldId = getFieldId(props.name, 'input', idPostfix);
const isValid = !touched || !error?.length;
const isValid = !(touched && error?.length);

return (
<FormGroup
id={`form-control__${fieldId}`}
Expand Down Expand Up @@ -133,7 +134,10 @@ const RichInputField: React.FC<RichInputFieldPropsProps> = React.forwardRef(
onChange && onChange(event);
}}
className="rich-input__text"
onBlur={() => setPopoverOpen(false)}
onBlur={() => {
setTouched(true, false);
setPopoverOpen(false);
}}
/>
<Popover
isVisible={popoverOpen}
Expand Down

0 comments on commit 3617b1c

Please sign in to comment.