Skip to content

Commit

Permalink
Forsøk comboboks janzz uten å bruke ref
Browse files Browse the repository at this point in the history
  • Loading branch information
joarau committed Nov 8, 2024
1 parent 5de70ba commit 7e3cf79
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 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, useRef } from 'react';
import React, { FunctionComponent, useState } 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,24 +19,23 @@ 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 filteredSuggestions = suggestions
? suggestions
.filter((f) => f.styrk08 && f.styrk08.trim() !== '' && f.styrk08 !== '9999')
.map((f) => f.label)
: [];

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

if (optionSelectedRef.current) {
optionSelectedRef.current = false;
} else {
setHasValidSelection(false);
}
setHasValidSelection(false); // Assume invalid until selection is made
};

const onToggleSelected = (option: string, isSelected: boolean) => {
if (isSelected && suggestions) {
const found = suggestions.find(
const onToggleSelected = (option: string, isSelected: boolean, isCustomOption: boolean) => {
if (isSelected) {
const found = suggestions?.find(
(forslag) => forslag.label.toLowerCase() === option.toLowerCase()
);
if (found) {
Expand All @@ -54,8 +53,10 @@ 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
}
} else {
setHasValidSelection(false);
dispatch({ type: SET_JANZZ, kategori: undefined });
}
};

Expand All @@ -67,12 +68,6 @@ const Janzz: FunctionComponent<Props> = ({ tittel }) => {

const feilmeldingTilBruker = error ? error.message : undefined;

const filteredSuggestions = suggestions
? suggestions
.filter((f) => f.styrk08 && f.styrk08.trim() !== '' && f.styrk08 !== '9999')
.map((f) => f.label)
: [];

return (
<div>
<UnsafeCombobox
Expand Down

0 comments on commit 7e3cf79

Please sign in to comment.