-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
218 additions
and
121 deletions.
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 | ||
--- | ||
|
||
Refactored Combobox FilteredOptions |
63 changes: 63 additions & 0 deletions
63
@navikt/core/react/src/form/combobox/FilteredOptions/AddNewOption.tsx
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,63 @@ | ||
import cl from "clsx"; | ||
import React from "react"; | ||
import { PlusIcon } from "@navikt/aksel-icons"; | ||
import { BodyShort, Label } from "../../../typography"; | ||
import { useInputContext } from "../Input/Input.context"; | ||
import { useSelectedOptionsContext } from "../SelectedOptions/selectedOptionsContext"; | ||
import { isInList, toComboboxOption } from "../combobox-utils"; | ||
import filteredOptionsUtil from "./filtered-options-util"; | ||
import { useFilteredOptionsContext } from "./filteredOptionsContext"; | ||
|
||
const AddNewOption = () => { | ||
const { | ||
inputProps: { id }, | ||
size, | ||
value, | ||
} = useInputContext(); | ||
const { | ||
setIsMouseLastUsedInputDevice, | ||
toggleIsListOpen, | ||
activeDecendantId, | ||
virtualFocus, | ||
} = useFilteredOptionsContext(); | ||
const { isMultiSelect, selectedOptions, toggleOption } = | ||
useSelectedOptionsContext(); | ||
return ( | ||
<li | ||
tabIndex={-1} | ||
onMouseMove={() => { | ||
if (activeDecendantId !== filteredOptionsUtil.getAddNewOptionId(id)) { | ||
virtualFocus.moveFocusToElement( | ||
filteredOptionsUtil.getAddNewOptionId(id), | ||
); | ||
setIsMouseLastUsedInputDevice(true); | ||
} | ||
}} | ||
onPointerUp={(event) => { | ||
toggleOption(toComboboxOption(value), event); | ||
if (!isMultiSelect && !isInList(value, selectedOptions)) | ||
toggleIsListOpen(false); | ||
}} | ||
id={filteredOptionsUtil.getAddNewOptionId(id)} | ||
className={cl( | ||
"navds-combobox__list-item navds-combobox__list-item--new-option", | ||
{ | ||
"navds-combobox__list-item--new-option--focus": | ||
activeDecendantId === filteredOptionsUtil.getAddNewOptionId(id), | ||
}, | ||
)} | ||
role="option" | ||
aria-selected={false} | ||
> | ||
<PlusIcon aria-hidden /> | ||
<BodyShort size={size}> | ||
Legg til{" "} | ||
<Label as="span" size={size}> | ||
“{value}” | ||
</Label> | ||
</BodyShort> | ||
</li> | ||
); | ||
}; | ||
|
||
export default AddNewOption; |
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
73 changes: 73 additions & 0 deletions
73
@navikt/core/react/src/form/combobox/FilteredOptions/FilteredOptionsItem.tsx
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,73 @@ | ||
import cl from "clsx"; | ||
import React from "react"; | ||
import { CheckmarkIcon } from "@navikt/aksel-icons"; | ||
import { BodyShort } from "../../../typography"; | ||
import { useInputContext } from "../Input/Input.context"; | ||
import { useSelectedOptionsContext } from "../SelectedOptions/selectedOptionsContext"; | ||
import { isInList } from "../combobox-utils"; | ||
import { ComboboxOption } from "../types"; | ||
import filteredOptionsUtil from "./filtered-options-util"; | ||
import { useFilteredOptionsContext } from "./filteredOptionsContext"; | ||
|
||
const FilteredOptionsItem = ({ option }: { option: ComboboxOption }) => { | ||
const { | ||
inputProps: { id }, | ||
size, | ||
} = useInputContext(); | ||
const { | ||
setIsMouseLastUsedInputDevice, | ||
toggleIsListOpen, | ||
activeDecendantId, | ||
virtualFocus, | ||
} = useFilteredOptionsContext(); | ||
const { isMultiSelect, maxSelected, selectedOptions, toggleOption } = | ||
useSelectedOptionsContext(); | ||
|
||
const isDisabled = (_option: ComboboxOption) => | ||
maxSelected?.isLimitReached && !isInList(_option.value, selectedOptions); | ||
return ( | ||
<li | ||
className={cl("navds-combobox__list-item", { | ||
"navds-combobox__list-item--focus": | ||
activeDecendantId === | ||
filteredOptionsUtil.getOptionId(id, option.label), | ||
"navds-combobox__list-item--selected": isInList( | ||
option.value, | ||
selectedOptions, | ||
), | ||
})} | ||
data-no-focus={isDisabled(option) || undefined} | ||
id={filteredOptionsUtil.getOptionId(id, option.label)} | ||
key={option.label} | ||
tabIndex={-1} | ||
onMouseMove={() => { | ||
if ( | ||
activeDecendantId !== | ||
filteredOptionsUtil.getOptionId(id, option.label) | ||
) { | ||
virtualFocus.moveFocusToElement( | ||
filteredOptionsUtil.getOptionId(id, option.label), | ||
); | ||
setIsMouseLastUsedInputDevice(true); | ||
} | ||
}} | ||
onPointerUp={(event) => { | ||
if (isDisabled(option)) { | ||
return; | ||
} | ||
toggleOption(option, event); | ||
if (!isMultiSelect && !isInList(option.value, selectedOptions)) { | ||
toggleIsListOpen(false); | ||
} | ||
}} | ||
role="option" | ||
aria-selected={isInList(option.value, selectedOptions)} | ||
aria-disabled={isDisabled(option) || undefined} | ||
> | ||
<BodyShort size={size}>{option.label}</BodyShort> | ||
{isInList(option.value, selectedOptions) && <CheckmarkIcon />} | ||
</li> | ||
); | ||
}; | ||
|
||
export default FilteredOptionsItem; |
20 changes: 20 additions & 0 deletions
20
@navikt/core/react/src/form/combobox/FilteredOptions/LoadingMessage.tsx
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,20 @@ | ||
import React from "react"; | ||
import { Loader } from "../../../loader"; | ||
import { useInputContext } from "../Input/Input.context"; | ||
import filteredOptionsUtil from "./filtered-options-util"; | ||
|
||
const LoadingMessage = () => { | ||
const { | ||
inputProps: { id }, | ||
} = useInputContext(); | ||
return ( | ||
<div | ||
className="navds-combobox__list-item--loading" | ||
id={filteredOptionsUtil.getIsLoadingId(id)} | ||
> | ||
<Loader title="Søker..." /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default LoadingMessage; |
27 changes: 27 additions & 0 deletions
27
@navikt/core/react/src/form/combobox/FilteredOptions/MaxSelectedMessage.tsx
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,27 @@ | ||
import React from "react"; | ||
import { useInputContext } from "../Input/Input.context"; | ||
import { useSelectedOptionsContext } from "../SelectedOptions/selectedOptionsContext"; | ||
import filteredOptionsUtil from "./filtered-options-util"; | ||
|
||
const MaxSelectedMessage = () => { | ||
const { | ||
inputProps: { id }, | ||
} = useInputContext(); | ||
const { maxSelected, selectedOptions } = useSelectedOptionsContext(); | ||
|
||
if (!maxSelected) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div | ||
className="navds-combobox__list-item--max-selected" | ||
id={filteredOptionsUtil.getMaxSelectedOptionsId(id)} | ||
> | ||
{maxSelected.message ?? | ||
`${selectedOptions.length} av ${maxSelected.limit} er valgt.`} | ||
</div> | ||
); | ||
}; | ||
|
||
export default MaxSelectedMessage; |
19 changes: 19 additions & 0 deletions
19
@navikt/core/react/src/form/combobox/FilteredOptions/NoSearchHitsMessage.tsx
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,19 @@ | ||
import React from "react"; | ||
import { useInputContext } from "../Input/Input.context"; | ||
import filteredOptionsUtil from "./filtered-options-util"; | ||
|
||
const NoSearchHitsMessage = () => { | ||
const { | ||
inputProps: { id }, | ||
} = useInputContext(); | ||
return ( | ||
<div | ||
className="navds-combobox__list-item--no-options" | ||
id={filteredOptionsUtil.getNoHitsId(id)} | ||
> | ||
Ingen søketreff | ||
</div> | ||
); | ||
}; | ||
|
||
export default NoSearchHitsMessage; |