Skip to content

Commit

Permalink
Search: i18n + remove role
Browse files Browse the repository at this point in the history
  • Loading branch information
HalvorHaugan committed Oct 24, 2024
1 parent f27698a commit 6b6b740
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-planets-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@navikt/ds-react": patch
---

Search: Remove redundant role attribute
56 changes: 20 additions & 36 deletions @navikt/core/react/src/form/search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import cl from "clsx";
import React, {
InputHTMLAttributes,
forwardRef,
useCallback,
useRef,
useState,
} from "react";
import { MagnifyingGlassIcon, XMarkIcon } from "@navikt/aksel-icons";
import { BodyShort, ErrorMessage, Label } from "../../typography";
import { omit } from "../../util";
import { useMergeRefs } from "../../util/hooks/useMergeRefs";
import { useI18n } from "../../util/i18n/i18n.context";
import { FormFieldProps, useFormField } from "../useFormField";
import SearchButton, { SearchButtonType } from "./SearchButton";
import { SearchContext } from "./context";
Expand Down Expand Up @@ -68,13 +68,9 @@ export interface SearchProps
*/
variant?: "primary" | "secondary" | "simple";
/**
* Exposes the HTML size attribute. Specifies the width of the element, in characters.
* HTML size attribute. Specifies the width of the input, in characters.
*/
htmlSize?: number | string;
/*
* Exposes role attribute.
*/
role?: string;
}

interface SearchComponent
Expand Down Expand Up @@ -123,31 +119,24 @@ export const Search = forwardRef<HTMLInputElement, SearchProps>(
onChange,
onSearchClick,
htmlSize,
role,
...rest
} = props;

const searchRef = useRef<HTMLInputElement | null>(null);
const mergedRef = useMergeRefs(searchRef, ref);

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

const handleChange = useCallback(
(v: string) => {
value === undefined && setInternalValue(v);
onChange?.(v);
},
[onChange, value],
);
const handleChange = (newValue: string) => {
value === undefined && setInternalValue(newValue);
onChange?.(newValue);
};

const handleClear = useCallback(
(event: SearchClearEvent) => {
onClear?.(event);
handleChange("");
searchRef.current?.focus?.();
},
[handleChange, onClear],
);
const handleClear = (clearEvent: SearchClearEvent) => {
onClear?.(clearEvent);
handleChange("");
searchRef.current?.focus?.();
};

const handleClick = () => {
onSearchClick?.(`${value ?? internalValue}`);
Expand All @@ -156,26 +145,22 @@ export const Search = forwardRef<HTMLInputElement, SearchProps>(
return (
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
<div
onKeyDown={(e) => {
if (e.key !== "Escape") {
onKeyDown={(event) => {
if (event.key !== "Escape") {
return;
}
searchRef.current?.value &&
searchRef.current?.value !== "" &&
e.preventDefault();

handleClear({ trigger: "Escape", event: e });
searchRef.current?.value && event.preventDefault();
handleClear({ trigger: "Escape", event });
}}
className={cl(
className,
"navds-form-field",
`navds-form-field--${size}`,
"navds-search",

{
"navds-search--error": hasError,
"navds-search--disabled": !!inputProps.disabled,
"navds-search--with-size": !!htmlSize,
"navds-search--disabled": inputProps.disabled,
"navds-search--with-size": htmlSize,
},
)}
>
Expand Down Expand Up @@ -215,7 +200,6 @@ export const Search = forwardRef<HTMLInputElement, SearchProps>(
value={value ?? internalValue}
onChange={(e) => handleChange(e.target.value)}
type="search"
role={role ?? "searchbox"}
className={cl(
className,
"navds-search__input",
Expand All @@ -229,11 +213,11 @@ export const Search = forwardRef<HTMLInputElement, SearchProps>(
{(value ?? internalValue) && clearButton && (
<button
type="button"
onClick={(e) => handleClear({ trigger: "Click", event: e })}
onClick={(event) => handleClear({ trigger: "Click", event })}
className="navds-search__button-clear"
>
<span className="navds-sr-only">
{clearButtonLabel ? clearButtonLabel : "Tøm"}
{clearButtonLabel || translate("clear")}
</span>
<XMarkIcon aria-hidden />
</button>
Expand Down
6 changes: 5 additions & 1 deletion @navikt/core/react/src/form/search/SearchButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { forwardRef, useContext } from "react";
import { MagnifyingGlassIcon } from "@navikt/aksel-icons";
import { Button, ButtonProps } from "../../button";
import { composeEventHandlers } from "../../util/composeEventHandlers";
import { useI18n } from "../../util/i18n/i18n.context";
import { SearchContext } from "./context";

export interface SearchButtonProps
Expand All @@ -19,6 +20,7 @@ export type SearchButtonType = React.ForwardRefExoticComponent<

const SearchButton: SearchButtonType = forwardRef(
({ className, children, disabled, onClick, ...rest }, ref) => {
const translate = useI18n("Search");
const context = useContext(SearchContext);

if (context === null) {
Expand All @@ -40,7 +42,9 @@ const SearchButton: SearchButtonType = forwardRef(
onClick={composeEventHandlers(onClick, handleClick)}
icon={
<MagnifyingGlassIcon
{...(children ? { "aria-hidden": true } : { title: "Søk" })}
{...(children
? { "aria-hidden": true }
: { title: translate("Button.search") })}
/>
}
>
Expand Down
6 changes: 6 additions & 0 deletions @navikt/core/react/src/util/i18n/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ export default {
labelSuffix: "delete",
},
},
Search: {
clear: "Clear",
Button: {
search: "Search",
},
},
} satisfies Translations;
12 changes: 7 additions & 5 deletions @navikt/core/react/src/util/i18n/locales/nb.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
interface TranslationMap {
[component: string]:
| Record<string, string>
| {
[subComponent: string]: Record<string, string>;
};
[component: string]: Record<string, string | Record<string, string>>;
}

export default {
Expand Down Expand Up @@ -44,4 +40,10 @@ export default {
labelSuffix: "slett",
},
},
Search: {
clear: "Tøm",
Button: {
search: "Søk",
},
},
} satisfies TranslationMap;
6 changes: 6 additions & 0 deletions @navikt/core/react/src/util/i18n/locales/nn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ export default {
labelSuffix: "slett",
},
},
Search: {
clear: "Tøm",
Button: {
search: "Søk",
},
},
} satisfies Translations;

0 comments on commit 6b6b740

Please sign in to comment.