Skip to content

Commit 636937d

Browse files
author
Peter Hellstrand
committed
fix(ffe-account-selector-react): do not mutate accounts passed in
1 parent e08087c commit 636937d

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from 'react';
2+
import { MicroText } from '@sb1/ffe-core-react';
3+
import classnames from 'classnames';
4+
import { balanceWithCurrency, accountFormatter } from '../format';
5+
import { Account, Locale } from '../types';
6+
7+
interface Props<Item extends Account> {
8+
item: Item;
9+
isHighlighted: boolean;
10+
showBalance: boolean;
11+
locale: Locale;
12+
}
13+
14+
export function AccountActionBody<Item extends Account>({
15+
item,
16+
isHighlighted,
17+
showBalance,
18+
locale,
19+
}: Props<Item>) {
20+
return (
21+
<div
22+
className={classnames('ffe-searchable-dropdown__list-item-body', {
23+
'ffe-searchable-dropdown__list-item-body--highlighted':
24+
isHighlighted,
25+
})}
26+
>
27+
{item.name}
28+
<div className="ffe-searchable-dropdown__list-item-body-details">
29+
<MicroText className="ffe-searchable-dropdown__detail-text">
30+
{accountFormatter(item.accountNumber)}
31+
</MicroText>
32+
{showBalance && (
33+
<MicroText className="ffe-searchable-dropdown__detail-text">
34+
{balanceWithCurrency(
35+
item.balance,
36+
locale,
37+
item.currencyCode,
38+
)}
39+
</MicroText>
40+
)}
41+
</div>
42+
</div>
43+
);
44+
}

packages/ffe-account-selector-react/src/account-selector/AccountSelector.tsx

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { SearchableDropdown } from '@sb1/ffe-searchable-dropdown-react';
1111
import { getAccountsWithCustomAccounts } from './getAccountsWithCustomAccounts';
1212
import { searchMatcherIgnoringAccountNumberFormatting } from '../searchMatcherIgnoringAccountNumberFormatting';
1313
import { texts } from '../texts';
14+
import { AccountActionBody } from './AccountOptionBody';
1415

1516
export interface AccountSelectorProps<T extends Account = Account> {
1617
/**
@@ -131,14 +132,6 @@ export const AccountSelector = <T extends Account = Account>({
131132

132133
const _ariaInvalid = rest['aria-invalid'] ?? ariaInvalid;
133134

134-
const dropdownListAccounts = accounts.map(it => ({
135-
...it,
136-
accountNumber: accountFormatter(it.accountNumber),
137-
balance: OptionBody
138-
? it.balance
139-
: balanceWithCurrency(it.balance, locale, it.currencyCode),
140-
}));
141-
142135
return (
143136
<div
144137
className={classNames(
@@ -165,10 +158,10 @@ export const AccountSelector = <T extends Account = Account>({
165158
allowCustomAccount
166159
? getAccountsWithCustomAccounts({
167160
selectedAccount,
168-
accounts: dropdownListAccounts,
161+
accounts,
169162
inputValue,
170163
})
171-
: dropdownListAccounts
164+
: accounts
172165
}
173166
noMatch={
174167
allowCustomAccount && inputValue.trim() !== ''
@@ -188,7 +181,14 @@ export const AccountSelector = <T extends Account = Account>({
188181
onChange={handleAccountSelected}
189182
searchAttributes={['name', 'accountNumber']}
190183
locale={locale}
191-
optionBody={OptionBody}
184+
optionBody={({ item, isHighlighted }) => (
185+
<AccountActionBody
186+
item={item}
187+
isHighlighted={isHighlighted}
188+
locale={locale}
189+
showBalance={showBalance}
190+
/>
191+
)}
192192
ariaInvalid={_ariaInvalid}
193193
searchMatcher={searchMatcherIgnoringAccountNumberFormatting}
194194
selectedItem={selectedAccount}

0 commit comments

Comments
 (0)