Skip to content

Commit 3f9b782

Browse files
authored
Merge pull request #2435 from SpareBank1/develop_multi-account-selector
fix(ffe-account-selector-react): do not mutate accounts passed in mul…
2 parents 20f8ff1 + 4ce701f commit 3f9b782

File tree

2 files changed

+82
-14
lines changed

2 files changed

+82
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React from 'react';
2+
import classnames from 'classnames';
3+
import { SmallText } from '@sb1/ffe-core-react';
4+
import { Icon } from '@sb1/ffe-icons-react';
5+
import { Account, Locale } from '../types';
6+
import { accountFormatter, balanceWithCurrency } from '../format';
7+
8+
interface MultiselectOptionBodyProps<Item extends Account> {
9+
item: Item;
10+
isHighlighted: boolean;
11+
showBalance: boolean;
12+
locale: Locale;
13+
}
14+
15+
const checkIcon =
16+
'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgLTk2MCA5NjAgOTYwIiB3aWR0aD0iMjQiPgogICAgPHBhdGgKICAgICAgICBkPSJtMzgyLTM2Mi4xMzEgMzM0LjY5Ni0zMzQuNjk1UTczMC4zNy03MTAuNSA3NDguNzYxLTcxMC41dDMyLjA2NSAxMy42NzRRNzk0LjUtNjgzLjE1MiA3OTQuNS02NjQuMzhxMCAxOC43NzEtMTMuNjc0IDMyLjQ0NUw0MTQuMDY1LTI2NC40MTNRNDAwLjM5MS0yNTAuNzM5IDM4Mi0yNTAuNzM5dC0zMi4wNjUtMTMuNjc0TDE3OC40MTMtNDM1LjkzNXEtMTMuNjc0LTEzLjY3NC0xMy4yOTQtMzIuNDQ1LjM4MS0xOC43NzIgMTQuMDU1LTMyLjQ0NlQyMTEuNjItNTE0LjVxMTguNzcxIDAgMzIuNDQ1IDEzLjY3NEwzODItMzYyLjEzMVoiIC8+Cjwvc3ZnPg==';
17+
18+
export function AccountMultiselectOptionBody<Item extends Account>({
19+
item,
20+
isHighlighted,
21+
showBalance,
22+
locale,
23+
}: MultiselectOptionBodyProps<Item>) {
24+
return (
25+
<div
26+
className={classnames(
27+
'ffe-searchable-dropdown__list-item-body',
28+
'ffe-searchable-dropdown__list-item-body--condensed',
29+
{
30+
'ffe-searchable-dropdown__list-item-body--highlighted':
31+
isHighlighted,
32+
},
33+
)}
34+
>
35+
<div className="ffe-searchable-dropdown__list-item-body-content">
36+
{item.name}
37+
<div className="ffe-searchable-dropdown__list-item-body-details">
38+
<SmallText className="ffe-searchable-dropdown__detail-text">
39+
{accountFormatter(item.accountNumber)}
40+
</SmallText>
41+
{showBalance && (
42+
<SmallText className="ffe-searchable-dropdown__detail-text">
43+
{balanceWithCurrency(
44+
item.balance,
45+
locale,
46+
item.currencyCode,
47+
)}
48+
</SmallText>
49+
)}
50+
</div>
51+
</div>
52+
<Icon
53+
fileUrl={checkIcon}
54+
size="md"
55+
className="ffe-searchable-dropdown__selected-icon"
56+
/>
57+
</div>
58+
);
59+
}

packages/ffe-account-selector-react/src/account-selector-multi/AccountSelectorMulti.tsx

+23-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React, { AriaAttributes } from 'react';
22
import { Account, Locale } from '../types';
33
import { SearchableDropdownMultiSelect } from '@sb1/ffe-searchable-dropdown-react';
4-
import { balanceWithCurrency, formatIncompleteAccountNumber } from '../format';
4+
import { formatIncompleteAccountNumber } from '../format';
55
import { searchMatcherIgnoringAccountNumberFormatting } from '../searchMatcherIgnoringAccountNumberFormatting';
66
import { texts } from '../texts';
7+
import { AccountMultiselectOptionBody } from './AccountMultiselectOptionBody';
78

89
export interface AccountSelectorMultiProps<T extends Account = Account> {
910
/**
@@ -98,24 +99,32 @@ export const AccountSelectorMulti = <T extends Account = Account>({
9899
: ['name', 'accountNumber']
99100
}
100101
postListElement={postListElement}
101-
dropdownList={
102-
OptionBody
103-
? accounts
104-
: accounts.map(it => ({
105-
...it,
106-
balance: balanceWithCurrency(
107-
it.balance,
108-
locale,
109-
it.currencyCode,
110-
),
111-
}))
112-
}
102+
dropdownList={accounts}
113103
noMatch={noMatches ?? { text: texts[locale].noMatch }}
114104
formatter={formatter}
115105
onChange={onChange}
116106
searchAttributes={['name', 'accountNumber']}
117107
locale={locale}
118-
optionBody={OptionBody}
108+
optionBody={({ item, isHighlighted, ...restOptionBody }) => {
109+
if (OptionBody) {
110+
return (
111+
<OptionBody
112+
item={item}
113+
isHighlighted={isHighlighted}
114+
{...restOptionBody}
115+
/>
116+
);
117+
}
118+
119+
return (
120+
<AccountMultiselectOptionBody
121+
item={item}
122+
isHighlighted={isHighlighted}
123+
locale={locale}
124+
showBalance={showBalance}
125+
/>
126+
);
127+
}}
119128
ariaInvalid={rest['aria-invalid'] ?? ariaInvalid}
120129
searchMatcher={searchMatcherIgnoringAccountNumberFormatting}
121130
selectedItems={selectedAccounts}

0 commit comments

Comments
 (0)