Skip to content

Commit

Permalink
Pass på at onChange ikke resetter validering når den kjøres etter toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
joarau committed Nov 7, 2024
1 parent 03946b9 commit 5de70ba
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/stilling/stilling/edit/om-stillingen/janzz/Janzz.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FunctionComponent, useState } from 'react';
import React, { FunctionComponent, useState, useRef } from 'react';
import css from './Janzz.module.css';
import { SET_EMPLOYMENT_JOBTITLE, SET_JANZZ } from '../../../adDataReducer';
import { useDispatch, useSelector } from 'react-redux';
Expand All @@ -19,11 +19,19 @@ const Janzz: FunctionComponent<Props> = ({ tittel }) => {
const [input, setInput] = useState<string>(tittel);
const [hasValidSelection, setHasValidSelection] = useState<boolean>(true);

const optionSelectedRef = useRef(false);

const { data: suggestions, isLoading, error } = useHentJanzzYrker(input);

const onChange = (event: React.ChangeEvent<HTMLInputElement> | null, value?: string) => {
setInput(event?.target?.value || value || '');
setHasValidSelection(false);
const newValue = event?.target?.value || value || '';
setInput(newValue);

if (optionSelectedRef.current) {
optionSelectedRef.current = false;
} else {
setHasValidSelection(false);
}
};

const onToggleSelected = (option: string, isSelected: boolean) => {
Expand All @@ -46,6 +54,7 @@ const Janzz: FunctionComponent<Props> = ({ tittel }) => {
dispatch({ type: SET_JANZZ, kategori });
setInput(capitalizeEmployerName(found.label) || '');
setHasValidSelection(true);
optionSelectedRef.current = true; // For å unngå resetting av validselection i onChange
}
}
};
Expand Down Expand Up @@ -79,7 +88,7 @@ const Janzz: FunctionComponent<Props> = ({ tittel }) => {
onToggleSelected={onToggleSelected}
onBlur={onBlur}
isLoading={isLoading}
error={yrkestittelError || feilmeldingTilBruker}
error={!hasValidSelection ? yrkestittelError || feilmeldingTilBruker : undefined}
className={css.typeahead}
aria-labelledby="endre-stilling-styrk"
filteredOptions={filteredSuggestions}
Expand Down

0 comments on commit 5de70ba

Please sign in to comment.