-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Search: i18n + remove role #3280
Open
HalvorHaugan
wants to merge
1
commit into
main
Choose a base branch
from
search-i18n
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+49
−42
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@navikt/ds-react": patch | ||
--- | ||
|
||
Search: Remove redundant role attribute |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
@@ -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 | ||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. useCallback had no effect since only these two functions had it. |
||
(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}`); | ||
|
@@ -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, | ||
}, | ||
)} | ||
> | ||
|
@@ -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", | ||
|
@@ -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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,4 +37,10 @@ export default { | |
labelSuffix: "slett", | ||
}, | ||
}, | ||
Search: { | ||
clear: "Tøm", | ||
Button: { | ||
search: "Søk", | ||
}, | ||
}, | ||
Comment on lines
+40
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could discuss if it's necessary to have |
||
} satisfies Translations; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SearchProps
extendsInputHTMLAttributes
which already includesrole