Skip to content

Commit

Permalink
Fix dropdown crashing if option is missing label (#2166)
Browse files Browse the repository at this point in the history
* fix dropdown crashing if option is missing label

* remove empty options instead
  • Loading branch information
bjosttveit authored Jun 18, 2024
1 parent 6d5d91a commit e2af5e7
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 50 deletions.
8 changes: 7 additions & 1 deletion src/features/options/useGetOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useLanguage } from 'src/features/language/useLanguage';
import { castOptionsToStrings } from 'src/features/options/castOptionsToStrings';
import { useGetOptionsQuery } from 'src/features/options/useGetOptionsQuery';
import { useSourceOptions } from 'src/hooks/useSourceOptions';
import { filterDuplicateOptions } from 'src/utils/options';
import { filterDuplicateOptions, filterEmptyOptions } from 'src/utils/options';
import type { IUseLanguage } from 'src/features/language/useLanguage';
import type { IOptionInternal } from 'src/features/options/castOptionsToStrings';
import type {
Expand Down Expand Up @@ -38,6 +38,7 @@ interface Props {
// Generic props
node: LayoutNode;
removeDuplicates?: boolean;
removeEmpty?: boolean;
preselectedOptionIndex?: number;

dataModelBindings?: IDataModelBindingsOptionsSimple | IDataModelBindingsSimple;
Expand Down Expand Up @@ -107,6 +108,7 @@ export function useGetOptions(props: Props): OptionsResult {
optionsId,
secure,
removeDuplicates,
removeEmpty,
source,
mapping,
queryParameters,
Expand All @@ -133,6 +135,9 @@ export function useGetOptions(props: Props): OptionsResult {
if (draft && removeDuplicates) {
draft = filterDuplicateOptions(draft);
}
if (draft && removeEmpty) {
draft = filterEmptyOptions(draft);
}
if (draft && sortOrder) {
draft = [...draft].sort(compareOptionAlphabetically(langAsString, sortOrder, selectedLanguage));
}
Expand All @@ -143,6 +148,7 @@ export function useGetOptions(props: Props): OptionsResult {
langAsString,
preselectedOptionIndex,
removeDuplicates,
removeEmpty,
selectedLanguage,
sortOrder,
sourceOptions,
Expand Down
46 changes: 0 additions & 46 deletions src/hooks/useFormattedOptions.tsx

This file was deleted.

3 changes: 2 additions & 1 deletion src/layout/Dropdown/DropdownComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function DropdownComponent({ node, isValid, overrideDisplay }: IDropdownP
valueType: 'single',
node,
removeDuplicates: true,
removeEmpty: true,
});

const changeMessageGenerator = useCallback(
Expand Down Expand Up @@ -87,7 +88,7 @@ export function DropdownComponent({ node, isValid, overrideDisplay }: IDropdownP
<Combobox.Option
key={option.value}
value={option.value}
description={langAsString(option.description)}
description={option.description ? langAsString(option.description) : undefined}
displayValue={langAsString(option.label)}
>
<Lang
Expand Down
2 changes: 2 additions & 0 deletions src/layout/FileUpload/FileUploadComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export function FileUploadComponent({ node }: IFileUploadWithTagProps): React.JS
...node.item,
valueType: 'single',
node,
removeDuplicates: true,
removeEmpty: true,
dataModelBindings: undefined,
});

Expand Down
2 changes: 1 addition & 1 deletion src/layout/FileUploadWithTag/EditWindowComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export function EditWindowComponent({ attachment, mobileView, node, options }: E
<Combobox.Option
key={option.value}
value={option.value}
description={langAsString(option.description)}
description={option.description ? langAsString(option.description) : undefined}
displayValue={langAsString(option.label)}
>
<Lang
Expand Down
3 changes: 2 additions & 1 deletion src/layout/MultipleSelect/MultipleSelectComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function MultipleSelectComponent({ node, isValid, overrideDisplay }: IMul
valueType: 'multi',
node,
removeDuplicates: true,
removeEmpty: true,
});
const { langAsString, lang } = useLanguage();

Expand Down Expand Up @@ -86,7 +87,7 @@ export function MultipleSelectComponent({ node, isValid, overrideDisplay }: IMul
<Combobox.Option
key={option.value}
value={option.value}
description={langAsString(option.description)}
description={option.description ? langAsString(option.description) : undefined}
displayValue={langAsString(option.label)}
>
<Lang
Expand Down
15 changes: 15 additions & 0 deletions src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,18 @@ export function filterDuplicateOptions(options: IOptionInternal[]): IOptionInter
}
return out;
}

/**
* Fast method for filtering options with empty value or label
*/
export function filterEmptyOptions(options: IOptionInternal[]): IOptionInternal[] {
const out: IOptionInternal[] = [];
let j = 0;
for (let i = 0; i < options.length; i++) {
if (!options[i].value?.length || !options[i].label?.length) {
continue;
}
out[j++] = options[i];
}
return out;
}

0 comments on commit e2af5e7

Please sign in to comment.