Skip to content

Commit

Permalink
🐛 fix: Combobox , SingleSelect: allow custom options (#2180)
Browse files Browse the repository at this point in the history
Co-authored-by: Ken <[email protected]>
  • Loading branch information
larseirikhansen and KenAJoh authored Aug 17, 2023
1 parent 7486ba1 commit e7cb375
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-buckets-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@navikt/ds-react": patch
---

:bug: Combobox, SingleSelect: fix adding custom options
5 changes: 5 additions & 0 deletions @navikt/core/css/form/combobox.css
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@
padding-left: calc(var(--a-spacing-3) - 4px);
}

.navds-form-field--small .navds-combobox__list-item--focus,
.navds-form-field--small .navds-combobox__list-item:hover {
padding-left: calc(var(--a-spacing-2) - 4px);
}

.navds-combobox__list-item--selected {
background-color: var(--ac-combobox-list-item-selected-bg, var(--a-surface-selected));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ export const SelectedOptionsProvider = ({
>;
}) => {
const { clearInput, focusInput } = useInputContext();
const { customOptions, removeCustomOption, addCustomOption } =
useCustomOptionsContext();
const {
customOptions,
removeCustomOption,
addCustomOption,
setCustomOptions,
} = useCustomOptionsContext();
const {
allowNewValues,
isMultiSelect,
Expand All @@ -65,17 +69,26 @@ export const SelectedOptionsProvider = ({
.includes(option?.toLowerCase?.());
if (isCustomOption) {
allowNewValues && addCustomOption(option);
!isMultiSelect && setSelectedOptions([]);
} else if (isMultiSelect) {
setSelectedOptions((prevSelectedOptions) => [
...prevSelectedOptions,
option,
]);
} else {
setSelectedOptions([option]);
setCustomOptions([]);
}
onToggleSelected?.(option, true, isCustomOption);
},
[addCustomOption, allowNewValues, isMultiSelect, onToggleSelected, options]
[
addCustomOption,
allowNewValues,
isMultiSelect,
onToggleSelected,
options,
setCustomOptions,
]
);

const removeSelectedOption = useCallback(
Expand Down
12 changes: 10 additions & 2 deletions @navikt/core/react/src/form/combobox/customOptionsContext.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React, { useState, useCallback, createContext, useContext } from "react";
import { useInputContext } from "./Input/inputContext";
import { useSelectedOptionsContext } from "./SelectedOptions/selectedOptionsContext";

type CustomOptionsContextType = {
customOptions: string[];
removeCustomOption: (option: string) => void;
addCustomOption: (option: string) => void;
setCustomOptions: React.Dispatch<React.SetStateAction<string[]>>;
};

const CustomOptionsContext = createContext<CustomOptionsContextType>(
Expand All @@ -14,6 +16,7 @@ const CustomOptionsContext = createContext<CustomOptionsContextType>(
export const CustomOptionsProvider = ({ children }) => {
const [customOptions, setCustomOptions] = useState<string[]>([]);
const { focusInput } = useInputContext();
const { isMultiSelect } = useSelectedOptionsContext();

const removeCustomOption = useCallback(
(option) => {
Expand All @@ -27,16 +30,21 @@ export const CustomOptionsProvider = ({ children }) => {

const addCustomOption = useCallback(
(option) => {
setCustomOptions((prevOptions) => [...prevOptions, option]);
if (isMultiSelect) {
setCustomOptions((prevOptions) => [...prevOptions, option]);
} else {
setCustomOptions([option]);
}
focusInput();
},
[focusInput, setCustomOptions]
[focusInput, isMultiSelect, setCustomOptions]
);

const customOptionsState = {
customOptions,
removeCustomOption,
addCustomOption,
setCustomOptions,
};

return (
Expand Down

0 comments on commit e7cb375

Please sign in to comment.