Skip to content

Commit

Permalink
Håndter deselection
Browse files Browse the repository at this point in the history
  • Loading branch information
joarau committed Nov 8, 2024
1 parent 7e3cf79 commit ff6db80
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 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,6 +19,8 @@ const Janzz: FunctionComponent<Props> = ({ tittel }) => {
const [input, setInput] = useState<string>(tittel);
const [hasValidSelection, setHasValidSelection] = useState<boolean>(true);

const optionSelectedRef = useRef<boolean>(false);

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

const filteredSuggestions = suggestions
Expand All @@ -30,7 +32,12 @@ const Janzz: FunctionComponent<Props> = ({ tittel }) => {
const onChange = (event: React.ChangeEvent<HTMLInputElement> | null, value?: string) => {
const newValue = event?.target.value || value || '';
setInput(newValue);
setHasValidSelection(false); // Assume invalid until selection is made

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

const onToggleSelected = (option: string, isSelected: boolean, isCustomOption: boolean) => {
Expand All @@ -53,8 +60,10 @@ const Janzz: FunctionComponent<Props> = ({ tittel }) => {
dispatch({ type: SET_JANZZ, kategori });
setInput(capitalizeEmployerName(found.label) || '');
setHasValidSelection(true);
optionSelectedRef.current = true; // Indicate that an option was just selected
}
} else {
// Option was deselected
setHasValidSelection(false);
dispatch({ type: SET_JANZZ, kategori: undefined });
}
Expand Down

0 comments on commit ff6db80

Please sign in to comment.