Skip to content

Commit

Permalink
♻️ Oppdatert Search escape-håndtering
Browse files Browse the repository at this point in the history
  • Loading branch information
KenAJoh committed Sep 6, 2023
1 parent 85a7521 commit f91af70
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-falcons-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@navikt/ds-react": patch
---

Search: Kjører nå bare preventDefault ved Escape når `input` inneholder tekst
35 changes: 11 additions & 24 deletions @navikt/core/react/src/form/search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ import React, {
useRef,
useState,
} from "react";
import {
BodyShort,
ErrorMessage,
Label,
mergeRefs,
omit,
useEventListener,
} from "../..";
import { BodyShort, ErrorMessage, Label, mergeRefs, omit } from "../..";
import { FormFieldProps, useFormField } from "../useFormField";
import SearchButton, { SearchButtonType } from "./SearchButton";

Expand Down Expand Up @@ -135,7 +128,6 @@ export const Search = forwardRef<HTMLInputElement, SearchProps>(

const searchRef = useRef<HTMLInputElement | null>(null);
const mergedRef = useMemo(() => mergeRefs([searchRef, ref]), [ref]);
const [wrapperRef, setWrapperRef] = useState<HTMLDivElement | null>(null);

const [internalValue, setInternalValue] = useState(defaultValue ?? "");

Expand All @@ -156,27 +148,22 @@ export const Search = forwardRef<HTMLInputElement, SearchProps>(
[handleChange, onClear]
);

useEventListener(
"keydown",
useCallback(
(e) => {
if (e.key === "Escape") {
e.preventDefault();
handleClear({ trigger: "Escape", event: e });
}
},
[handleClear]
),
wrapperRef
);

const handleClick = () => {
onSearchClick?.(`${value ?? internalValue}`);
};

return (
<div
ref={setWrapperRef}
onKeyDown={(e) => {
if (e.key !== "Escape") {
return;
}
searchRef.current?.value &&
searchRef.current?.value !== "" &&
e.preventDefault();

handleClear({ trigger: "Escape", event: e });
}}
className={cl(
className,
"navds-form-field",
Expand Down

0 comments on commit f91af70

Please sign in to comment.