Skip to content

Commit 3b3426e

Browse files
committed
Make sure html strings are sorted by their textcontent
1 parent e8cc8a9 commit 3b3426e

File tree

1 file changed

+12
-2
lines changed
  • packages/shared-components/src/util/list

1 file changed

+12
-2
lines changed

packages/shared-components/src/util/list/sort.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import { SortState } from '@navikt/ds-react';
2+
import htmlUtils from '../html/htmlUtils';
23

34
type SortDirection = SortState['direction'];
45

6+
const normalizeString = (value: string | undefined) => {
7+
const textValue = `${value ?? ''}`;
8+
9+
if (htmlUtils.isHtmlString(textValue)) {
10+
return htmlUtils.extractTextContent(textValue).trim();
11+
}
12+
return textValue.trim();
13+
};
14+
515
const getLocaleComparator =
616
(property?: string, direction?: SortDirection, reverseDefault: boolean = false) =>
717
(a, b) => {
818
if (!property || !direction) {
919
return reverseDefault ? -1 : 1;
1020
}
11-
const valueA = `${a[property] ?? ''}`.trim();
12-
const valueB = `${b[property] ?? ''}`.trim();
21+
const valueA = normalizeString(a[property]);
22+
const valueB = normalizeString(b[property]);
1323

1424
if (direction === 'ascending') {
1525
return valueA.localeCompare(valueB, 'nb-NO');

0 commit comments

Comments
 (0)