Skip to content

Commit 19bac3c

Browse files
authored
fix(TokenInput): currency state (#95)
1 parent 4664ef9 commit 19bac3c

4 files changed

Lines changed: 20 additions & 15 deletions

File tree

.changeset/few-books-confess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@interlay/ui": patch
3+
---
4+
5+
fix(TokenInput): currency state

packages/components/src/TokenInput/SelectableTokenInput.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { chain, useId } from '@react-aria/utils';
2-
import { Key, ReactNode, forwardRef, useCallback, useEffect } from 'react';
2+
import { Key, ReactNode, forwardRef, useCallback } from 'react';
33

44
import { HelperText } from '../HelperText';
55

@@ -45,14 +45,6 @@ const SelectableTokenInput = forwardRef<HTMLInputElement, SelectableTokenInputPr
4545
): JSX.Element => {
4646
const selectHelperTextId = useId();
4747

48-
useEffect(() => {
49-
if (selectProps?.value === undefined) return;
50-
51-
const item = (items as TokenData[]).find((item) => item.currency.symbol === selectProps?.value);
52-
53-
onChangeCurrency?.(item?.currency);
54-
}, [selectProps?.value, onChangeCurrency]);
55-
5648
const handleSelectionChange = useCallback(
5749
(ticker: Key) => {
5850
const tokenData = (items as TokenData[]).find((item) => item.currency.symbol === ticker);

packages/components/src/TokenInput/TokenInput.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useDOMRef } from '@interlay/hooks';
22
import { mergeProps, useId } from '@react-aria/utils';
3-
import { ChangeEvent, FocusEvent, forwardRef, useCallback, useEffect, useState } from 'react';
3+
import { ChangeEvent, FocusEvent, forwardRef, useCallback, useEffect, useMemo, useState } from 'react';
44

55
import { trimDecimals } from '../utils';
66

@@ -13,7 +13,9 @@ const getDefaultCurrency = (props: TokenInputProps) => {
1313
case 'fixed':
1414
return (props as FixedTokenInputProps).currency;
1515
case 'selectable':
16-
return (props.items || []).find((item) => item.currency.symbol === props.selectProps?.defaultValue);
16+
return (props.items || []).find(
17+
(item) => item.currency.symbol === (props.selectProps?.value || props.selectProps?.defaultValue)
18+
)?.currency;
1719
}
1820
};
1921

@@ -34,8 +36,10 @@ const TokenInput = forwardRef<HTMLInputElement, TokenInputProps>((props, ref): J
3436

3537
const inputRef = useDOMRef<HTMLInputElement>(ref);
3638

39+
const defaultCurrency = useMemo(() => getDefaultCurrency(props), []);
40+
3741
const [value, setValue] = useState(defaultValue);
38-
const [currency, setCurrency] = useState<any | undefined>(getDefaultCurrency(props));
42+
const [currency, setCurrency] = useState<any | undefined>(defaultCurrency);
3943

4044
const inputId = useId();
4145

@@ -119,9 +123,8 @@ const TokenInput = forwardRef<HTMLInputElement, TokenInputProps>((props, ref): J
119123
if (props.type === 'selectable') {
120124
return (
121125
<SelectableTokenInput
122-
{...mergeProps(otherProps, commonProps)}
126+
{...mergeProps(otherProps, commonProps, { onChangeCurrency: handleChangeCurrency })}
123127
currency={currency}
124-
onChangeCurrency={handleChangeCurrency}
125128
/>
126129
);
127130
}

packages/components/src/TokenInput/__tests__/TokenInput.test.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,9 @@ describe('TokenInput', () => {
329329
expect(screen.getByRole('button', { name: /select token/i })).toHaveTextContent('BTC');
330330
});
331331

332-
it('should control value', async () => {
332+
it('should control value and emit onChangeCurrency', async () => {
333+
const handleChangeCurrency = jest.fn();
334+
333335
const Component = () => {
334336
const [value, setValue] = useState<any | undefined>(currencies[0]);
335337

@@ -342,6 +344,7 @@ describe('TokenInput', () => {
342344
label='label'
343345
selectProps={{ value: value.symbol, onSelectionChange: handleSelectionChange }}
344346
type='selectable'
347+
onChangeCurrency={handleChangeCurrency}
345348
/>
346349
);
347350
};
@@ -365,6 +368,8 @@ describe('TokenInput', () => {
365368
await waitForElementToBeRemoved(screen.getByRole('dialog', { name: /select token/i }));
366369

367370
expect(screen.getByRole('button', { name: /select token/i })).toHaveTextContent('ETH');
371+
expect(handleChangeCurrency).toHaveBeenCalledWith(currencies[1]);
372+
expect(handleChangeCurrency).toHaveBeenCalledTimes(1);
368373
});
369374

370375
it('should apply correct decimals when switching currency', async () => {

0 commit comments

Comments
 (0)