Skip to content

Commit

Permalink
move max button functionality inside SwapAmountInput component
Browse files Browse the repository at this point in the history
  • Loading branch information
abcrane123 committed Jun 11, 2024
1 parent 2bd86a7 commit af68798
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 18 deletions.
7 changes: 1 addition & 6 deletions site/docs/components/SwapAmountInputContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ type SwapAmountInputContainer = {
setAmount: (a: string) => void,
amount: string,
tokenBalance: string,
onMaxButtonClick: () => void,
) => ReactElement;
};

Expand All @@ -24,9 +23,5 @@ export default function SwapAmountInputContainer({ children }: SwapAmountInputCo

const tokenBalance = TOKEN_BALANCE_MAP[token?.symbol];

const onMaxButtonClick = () => {
setAmount(tokenBalance);
};

return children(token, setToken, setAmount, amount, tokenBalance, onMaxButtonClick);
return children(token, setToken, setAmount, amount, tokenBalance);
}
4 changes: 1 addition & 3 deletions site/docs/pages/swap/swap-amount-input.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ The `SwapAmountInput` component is a stylized input field designed for users to
tokenBalance={tokenBalance}
onAmountChange={setAmount}
onTokenSelectorClick={setToken}
onMaxButtonClick={onMaxButtonClick}
/>
```

Expand Down Expand Up @@ -74,7 +73,7 @@ The `SwapAmountInput` component is a stylized input field designed for users to

<App>
<SwapAmountInputContainer>
{(token, setToken, setAmount, amount, tokenBalance, onMaxButtonClick) => (
{(token, setToken, setAmount, amount, tokenBalance) => (
<SwapAmountInput
token={token}
swappableTokens={[
Expand Down Expand Up @@ -110,7 +109,6 @@ The `SwapAmountInput` component is a stylized input field designed for users to
tokenBalance={tokenBalance}
onAmountChange={setAmount}
onTokenSelectorClick={setToken}
onMaxButtonClick={onMaxButtonClick}
/>
)}
</SwapAmountInputContainer>
Expand Down
1 change: 0 additions & 1 deletion site/docs/pages/swap/types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type SwapAmountInputReact = {
token?: Token; // Selected token
swappableTokens?: Token[]; // Tokens available for swap
tokenBalance?: string; // Amount of selected token user owns
onMaxButtonClick: () => void; // Callback function when max button is clicked
onAmountChange: (amount: string) => void; // Callback function when the amount changes
onTokenSelectorClick: () => void; // Callback function when the token selector is clicked
disabled?: boolean; // Whether the input is disabled
Expand Down
7 changes: 2 additions & 5 deletions src/swap/components/SwapAmountInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { Token } from '../../token';

const setAmountMock = jest.fn();
const selectTokenClickMock = jest.fn();
const onMaxButtonClickMock = jest.fn();

const token = {
address: '0x123' as Address,
Expand Down Expand Up @@ -109,7 +108,7 @@ describe('SwapAmountInput Component', () => {
expect(input.value).toBe('1');
});

it('should call onMaxButtonClick when the max button is clicked', async () => {
it('should call onAmountChange with tokenBalance when the max button is clicked', async () => {
render(
<SwapAmountInput
token={token}
Expand All @@ -118,14 +117,13 @@ describe('SwapAmountInput Component', () => {
amount="1"
onAmountChange={setAmountMock}
onTokenSelectorClick={selectTokenClickMock}
onMaxButtonClick={onMaxButtonClickMock}
tokenBalance="100"
/>,
);

const maxButton = screen.getByTestId('ockSwapAmountInput_MaxButton');
fireEvent.click(maxButton);
expect(onMaxButtonClickMock).toHaveBeenCalled();
expect(setAmountMock).toHaveBeenCalledWith('100');
});

it('should disable the input when disabled prop is true', async () => {
Expand All @@ -137,7 +135,6 @@ describe('SwapAmountInput Component', () => {
amount="1"
onAmountChange={setAmountMock}
onTokenSelectorClick={selectTokenClickMock}
onMaxButtonClick={onMaxButtonClickMock}
disabled
/>,
);
Expand Down
10 changes: 8 additions & 2 deletions src/swap/components/SwapAmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export function SwapAmountInput({
tokenBalance,
onAmountChange,
onTokenSelectorClick,
onMaxButtonClick,
disabled = false,
}: SwapAmountInputReact) {
const handleAmountChange = useCallback(
Expand All @@ -24,6 +23,12 @@ export function SwapAmountInput({
[onAmountChange],
);

const handleMaxButtonClick = useCallback(() => {
if (tokenBalance && isValidAmount(tokenBalance)) {
onAmountChange(tokenBalance);
}
}, [tokenBalance, onAmountChange]);

return (
<div data-testid="ockSwapAmountInput_Container" className="ock-swapamountinput-container">
<div className="ock-swapamountinput-row">
Expand All @@ -39,7 +44,8 @@ export function SwapAmountInput({
<button
data-testid="ockSwapAmountInput_MaxButton"
className="ock-swapamountinput-maxbutton"
onClick={onMaxButtonClick}
onClick={handleMaxButtonClick}
disabled={tokenBalance === undefined}
>
Max
</button>
Expand Down
1 change: 0 additions & 1 deletion src/swap/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export type SwapAmountInputReact = {
token?: Token;
swappableTokens: Token[];
tokenBalance?: string;
onMaxButtonClick?: () => void;
onAmountChange: (amount: string) => void;
onTokenSelectorClick: () => void;
disabled?: boolean;
Expand Down

0 comments on commit af68798

Please sign in to comment.