Skip to content

Commit fc10b4c

Browse files
authored
added custom filter to dropdown etc. for better search (#2933)
1 parent fbd0f33 commit fc10b4c

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

src/layout/Dropdown/DropdownComponent.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { ComponentStructureWrapper } from 'src/layout/ComponentStructureWrapper'
1616
import comboboxClasses from 'src/styles/combobox.module.css';
1717
import { useLabel } from 'src/utils/layout/useLabel';
1818
import { useNodeItem } from 'src/utils/layout/useNodeItem';
19+
import { optionSearchFilter } from 'src/utils/options';
1920
import type { PropsFromGenericComponent } from 'src/layout';
2021

2122
export type IDropdownProps = PropsFromGenericComponent<'Dropdown'>;
@@ -84,6 +85,7 @@ export function DropdownComponent({ node, overrideDisplay }: IDropdownProps) {
8485
<ComponentStructureWrapper node={node}>
8586
<Combobox
8687
id={id}
88+
filter={optionSearchFilter}
8789
size='sm'
8890
hideLabel={true}
8991
value={selectedValues}

src/layout/FileUploadWithTag/EditWindowComponent.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { useFileTableRow } from 'src/layout/FileUpload/FileUploadTable/FileTable
2020
import classes from 'src/layout/FileUploadWithTag/EditWindowComponent.module.css';
2121
import comboboxClasses from 'src/styles/combobox.module.css';
2222
import { useNodeItem } from 'src/utils/layout/useNodeItem';
23+
import { optionSearchFilter } from 'src/utils/options';
2324
import type { IAttachment } from 'src/features/attachments';
2425
import type { IOptionInternal } from 'src/features/options/castOptionsToStrings';
2526
import type { PropsFromGenericComponent } from 'src/layout';
@@ -169,6 +170,7 @@ export function EditWindowComponent({
169170
>
170171
<Combobox
171172
id={`attachment-tag-dropdown-${uniqueId}`}
173+
filter={optionSearchFilter}
172174
size='sm'
173175
hideLabel={true}
174176
label={langAsString('general.choose')}

src/layout/MultipleSelect/MultipleSelectComponent.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { ComponentStructureWrapper } from 'src/layout/ComponentStructureWrapper'
1717
import comboboxClasses from 'src/styles/combobox.module.css';
1818
import { useLabel } from 'src/utils/layout/useLabel';
1919
import { useNodeItem } from 'src/utils/layout/useNodeItem';
20+
import { optionSearchFilter } from 'src/utils/options';
2021
import type { PropsFromGenericComponent } from 'src/layout';
2122

2223
export type IMultipleSelectProps = PropsFromGenericComponent<'MultipleSelect'>;
@@ -86,6 +87,7 @@ export function MultipleSelectComponent({ node, overrideDisplay }: IMultipleSele
8687
multiple
8788
hideLabel
8889
id={id}
90+
filter={optionSearchFilter}
8991
size='sm'
9092
value={selectedValues}
9193
readOnly={readOnly}

src/utils/options.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Option } from '@digdir/designsystemet-react/dist/types/components/form/Combobox/useCombobox';
2+
13
import type { IOptionInternal } from 'src/features/options/castOptionsToStrings';
24

35
const emptyArray: IOptionInternal[] = [];
@@ -110,3 +112,11 @@ function logIncludesComma(option: IOptionInternal) {
110112
JSON.stringify(o, null, 2),
111113
);
112114
}
115+
116+
export function optionSearchFilter(inputValue: string, option: Option): boolean {
117+
const search = inputValue.toLowerCase();
118+
const label = option.label.toLowerCase();
119+
const desc = option.description?.toLowerCase();
120+
121+
return label.includes(search) || (!!desc && desc.includes(search));
122+
}

0 commit comments

Comments
 (0)