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

Combobox: Remove 'Ingen søketreff' when custom options allowed #2895

Merged
merged 8 commits into from
May 7, 2024
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 && (
larseirikhansen marked this conversation as resolved.
Show resolved Hide resolved
<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
Loading