Skip to content

Commit

Permalink
Merge aad64ed into 7fd62f3
Browse files Browse the repository at this point in the history
  • Loading branch information
larseirikhansen authored May 6, 2024
2 parents 7fd62f3 + aad64ed commit c60edfb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-dots-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@navikt/ds-react": patch
---

Combobox: Remove 'Ingen søketreff' when custom options allowed
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const FilteredOptions = () => {
const shouldRenderNonSelectables =
maxSelected?.isLimitReached || // Render maxSelected message
isLoading || // Render loading message
(!isLoading && filteredOptions.length === 0); // Render no hits message
(!isLoading && filteredOptions.length === 0 && !allowNewValues); // Render no hits message

const shouldRenderFilteredOptionsList =
(allowNewValues && isValueNew && !maxSelected?.isLimitReached) || // Render add new option
Expand Down Expand Up @@ -72,7 +72,7 @@ const FilteredOptions = () => {
<Loader title="Søker..." />
</div>
)}
{!isLoading && filteredOptions.length === 0 && (
{!isLoading && filteredOptions.length === 0 && !allowNewValues && (
<div
className="navds-combobox__list-item--no-options"
id={filteredOptionsUtil.getNoHitsId(id)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ export const FilteredOptionsProvider = ({

const ariaDescribedBy = useMemo(() => {
let activeOption;
if (!isLoading && filteredOptions.length === 0) {
if (!isLoading && filteredOptions.length === 0 && !allowNewValues) {
activeOption = filteredOptionsUtils.getNoHitsId(id);
} else if ((value && value !== "") || isLoading) {
} else if (value || isLoading) {
if (shouldAutocomplete && filteredOptions[0]) {
activeOption = filteredOptionsUtils.getOptionId(
id,
Expand All @@ -180,6 +180,7 @@ export const FilteredOptionsProvider = ({
shouldAutocomplete,
filteredOptions,
id,
allowNewValues,
]);

const currentOption = useMemo(
Expand Down
33 changes: 31 additions & 2 deletions @navikt/core/react/src/form/combobox/combobox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -464,13 +464,13 @@ export const RemoveSelectedMultiSelectTest: StoryObject = {
},
};

export const AddWhenAddNewDisabledTest: StoryObject = {
export const AllowNewValuesTest: StoryObject = {
render: () => {
return (
<UNSAFE_Combobox
options={options}
label="Hva er dine favorittfrukter?"
isMultiSelect
allowNewValues
/>
);
},
Expand All @@ -497,6 +497,35 @@ export const AddWhenAddNewDisabledTest: StoryObject = {
},
};

export const DisallowNewValuesTest: StoryObject = {
render: () => {
return (
<UNSAFE_Combobox options={options} label="Hva er dine favorittfrukter?" />
);
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

const input = canvas.getByLabelText("Hva er dine favorittfrukter?");

userEvent.click(input);
await userEvent.type(input, "aaa", { delay: 200 });
await sleep(250);

userEvent.keyboard("{ArrowDown}");
await sleep(250);
userEvent.keyboard("{ArrowDown}");
await sleep(250);
userEvent.keyboard("{Enter}");
await sleep(250);
userEvent.keyboard("{Escape}");
await sleep(250);

const invalidSelect = canvas.queryByLabelText("aaa slett");
expect(invalidSelect).not.toBeInTheDocument();
},
};

export const TestThatCallbacksOnlyFireWhenExpected: StoryObj<{
onChange: ReturnType<typeof fn>;
onClear: ReturnType<typeof fn>;
Expand Down

0 comments on commit c60edfb

Please sign in to comment.