Skip to content
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

Feature/combobox grouped options #3317

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/nasty-tigers-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@navikt/ds-react": minor
"@navikt/ds-css": minor
---

Combobox: Group options with a heading for when several types of content is used within one Combobox
11 changes: 11 additions & 0 deletions @navikt/core/css/form/combobox.css
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,17 @@
cursor: default;
}

/* Group / category */
.navds-combobox__list__group {
width: 100%;
}

.navds-combobox__list__group__heading {
background-color: var(--a-surface-subtle);
padding-block: var(--a-spacing-05);
padding-inline: var(--a-spacing-3);
}

/* ul-list and selectable li-items */

.navds-combobox__list-options {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import cl from "clsx";
import React from "react";
import { useInputContext } from "../Input/Input.context";
import { useSelectedOptionsContext } from "../SelectedOptions/selectedOptionsContext";
import { ComboboxOption } from "../types";
import AddNewOption from "./AddNewOption";
import FilteredOptionsGroup from "./FilteredOptionsGroup";
import FilteredOptionsItem from "./FilteredOptionsItem";
import LoadingMessage from "./LoadingMessage";
import MaxSelectedMessage from "./MaxSelectedMessage";
Expand Down Expand Up @@ -34,6 +36,16 @@ const FilteredOptions = () => {
(allowNewValues && isValueNew && !maxSelected?.isLimitReached) || // Render add new option
filteredOptions.length > 0; // Render filtered options

const groups = filteredOptions.reduce(
(_groups: string[], option: ComboboxOption): string[] => {
if (option.group && !_groups.includes(option.group)) {
return [..._groups, option.group];
}
return _groups;
},
[],
);

return (
<div
className={cl("navds-combobox__list", {
Expand Down Expand Up @@ -63,9 +75,20 @@ const FilteredOptions = () => {
{isValueNew && !maxSelected?.isLimitReached && allowNewValues && (
<AddNewOption />
)}
{filteredOptions.map((option) => (
<FilteredOptionsItem key={option.value} option={option} />
))}
{groups.length > 0 &&
groups.map((group) => (
<FilteredOptionsGroup
key={group}
group={group}
options={filteredOptions.filter(
(option) => option.group === group,
)}
/>
))}
{groups.length === 0 &&
filteredOptions.map((option) => (
<FilteredOptionsItem key={option.value} option={option} />
))}
</ul>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from "react";
import { Detail } from "../../../typography";
import { ComboboxOption } from "../types";
import FilteredOptionsItem from "./FilteredOptionsItem";

const FilteredOptionsGroup = ({ group, options }) => (
<div
role="group"
className="navds-combobox__list__group"
key={group}
aria-labelledby={`group-${group}`}
>
<Detail
id={`group-${group}`}
className="navds-combobox__list__group__heading"
data-no-focus="true"
weight="semibold"
>
{group}
</Detail>
{options.map((option: ComboboxOption) => (
<FilteredOptionsItem key={option.value} option={option} />
))}
</div>
);

export default FilteredOptionsGroup;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const isPartOfText = (value: string, text: string) =>
normalizeText(text).includes(normalizeText(value ?? ""));

const getMatchingValuesFromList = (value: string, list: ComboboxOption[]) =>
list.filter((listItem) => isPartOfText(value, listItem.label));
list.filter(
(listItem) =>
isPartOfText(value, listItem.label) ||
(listItem.group && isPartOfText(value, listItem.group)),
);

const getFirstValueStartingWith = (text: string, list: ComboboxOption[]) => {
return list.find((listItem) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ const useVirtualFocus = (
);

const getListOfAllChildren = (): HTMLElement[] =>
Array.from(containerRef?.children ?? []) as HTMLElement[];
(Array.from(containerRef?.children ?? []) as HTMLElement[]).reduce(
(acc: HTMLElement[], el) => {
if (el.role === "group") {
return [...acc, ...(Array.from(el.children) as HTMLElement[])];
}
return [...acc, el];
},
[],
);
const getElementsAbleToReceiveFocus = () =>
getListOfAllChildren().filter(
(child) => child.getAttribute("data-no-focus") !== "true",
Expand Down
11 changes: 8 additions & 3 deletions @navikt/core/react/src/form/combobox/combobox-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ const toComboboxOption = (value: string): ComboboxOption => ({
});

const mapToComboboxOptionArray = (options?: string[] | ComboboxOption[]) => {
return options?.map((option: string | ComboboxOption) =>
typeof option === "string" ? toComboboxOption(option) : option,
);
return options
?.map((option: string | ComboboxOption) =>
typeof option === "string" ? toComboboxOption(option) : option,
)
.map((option: ComboboxOption) => ({
...option,
group: option.group,
}));
};

export { isInList, mapToComboboxOptionArray, toComboboxOption };
40 changes: 40 additions & 0 deletions @navikt/core/react/src/form/combobox/combobox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,43 @@ Chromatic.parameters = {
disable: false,
},
};

export const GroupedOptions: StoryFn<ComboboxProps> = ({ ...rest }) => (
<UNSAFE_Combobox
{...rest}
id="combobox"
isMultiSelect
options={[
{
label: "Apple",
value: "apple",
group: "Fruit",
},
{
label: "Banana",
value: "banana",
group: "Fruit",
},
{
label: "Orange",
value: "orange",
group: "Fruit",
},
{
label: "Carrot",
value: "carrot",
group: "Vegetable",
},
{
label: "Cucumber",
value: "cucumber",
group: "Vegetable",
},
{
label: "Tomato",
value: "tomato",
group: "Vegetable",
},
]}
/>
);
5 changes: 5 additions & 0 deletions @navikt/core/react/src/form/combobox/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export type ComboboxOption = {
* The programmatic value of the option, for use internally. Will be returned from onToggleSelected.
*/
value: string;
/**
* Group options under a "heading" by adding this prop.
* Can also be searched for.
*/
group?: string;
};

export interface ComboboxProps
Expand Down
Loading