Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amount Preset inputs #1806

Merged
merged 143 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
143 commits
Select commit Hold shift + click to select a range
9fe2fde
Onramp - event subscriptions migration
rustam-cb Nov 14, 2024
5c95ff9
Fund form
rustam-cb Nov 15, 2024
7c3fba9
Merge branch 'main' into fund-form
rustam-cb Nov 19, 2024
a0486e4
Initial utils
rustam-cb Nov 20, 2024
bffb90f
Update
rustam-cb Dec 5, 2024
a56f488
Fix lint issues
rustam-cb Dec 5, 2024
34916c7
Update
rustam-cb Dec 5, 2024
455e4bd
Merge branch 'main' into onramp-api-utils
rustam-cb Dec 5, 2024
d40eca0
Merge branch 'main' into fund-form
rustam-cb Dec 5, 2024
fc9db1f
Update
rustam-cb Dec 6, 2024
595f54e
Update
rustam-cb Dec 6, 2024
486d618
Update
rustam-cb Dec 6, 2024
6ed6c16
Update
rustam-cb Dec 6, 2024
b16da4a
Update
rustam-cb Dec 6, 2024
b7b2cd2
Update
rustam-cb Dec 6, 2024
bb348df
Update
rustam-cb Dec 6, 2024
e92e933
Update
rustam-cb Dec 6, 2024
2651a5f
Update
rustam-cb Dec 6, 2024
e91f671
Update
rustam-cb Dec 6, 2024
e5b0cae
Merge branch 'onramp-api-utils' into fund-form
rustam-cb Dec 6, 2024
a27489b
Update
rustam-cb Dec 9, 2024
51d7041
Merge branch 'main' into onramp-api-utils
rustam-cb Dec 9, 2024
eefc65d
Use Api key from the config
rustam-cb Dec 9, 2024
dbb6b80
Merge branch 'main' into fund-form
rustam-cb Dec 9, 2024
eb92aa5
Merge branch 'onramp-api-utils' into fund-form
rustam-cb Dec 9, 2024
edd3766
Update
rustam-cb Dec 10, 2024
839a045
Update
rustam-cb Dec 10, 2024
c7521d7
Update
rustam-cb Dec 10, 2024
45e7a77
Merge branch 'main' into fund-form
rustam-cb Dec 11, 2024
c14a5a0
Intermediate changes
rustam-cb Dec 16, 2024
9583b11
Merge branch 'main' into fund-form
rustam-cb Dec 16, 2024
6ee7143
Fund card implementation
rustam-cb Dec 17, 2024
cd499ba
Merge branch 'main' into fund-form
rustam-cb Dec 17, 2024
535b2f3
Fund form
rustam-cb Dec 18, 2024
6b9c6ae
Organize code
rustam-cb Dec 18, 2024
8669f40
Merge branch 'main' into fund-form
rustam-cb Dec 18, 2024
4dc16e1
Update
rustam-cb Dec 18, 2024
8991cdb
Update
rustam-cb Dec 18, 2024
eb9a993
Update
rustam-cb Dec 18, 2024
4ffe559
Update
rustam-cb Dec 18, 2024
1b3f1fc
Update
rustam-cb Dec 18, 2024
423843a
Update
rustam-cb Dec 18, 2024
2fabd85
Update
rustam-cb Dec 18, 2024
9032c5e
Update
rustam-cb Dec 18, 2024
b14bc3c
Format
rustam-cb Dec 18, 2024
c143dcb
Update naming
rustam-cb Dec 18, 2024
302806e
Format
rustam-cb Dec 18, 2024
282bc3c
Amount input snippets
rustam-cb Dec 18, 2024
c99194f
Address comments
rustam-cb Dec 18, 2024
950504e
Address comments
rustam-cb Dec 18, 2024
ba15289
Address comments
rustam-cb Dec 19, 2024
cd67c2e
Address comments
rustam-cb Dec 19, 2024
ac2dd6a
Merge branch 'main' into amount-input-snippets
rustam-cb Dec 19, 2024
0d3e6db
Merge branch 'main' into fund-form
rustam-cb Dec 19, 2024
4669aeb
Update
rustam-cb Dec 19, 2024
ee55dc8
Update
rustam-cb Dec 19, 2024
72fcbb2
Fix lint issue
rustam-cb Dec 19, 2024
ef17061
Address comments
rustam-cb Dec 19, 2024
1b06d8d
Address comments
rustam-cb Dec 19, 2024
af2203e
Update
rustam-cb Dec 19, 2024
64533c9
Merge branch 'fund-form' into amount-input-snippets
rustam-cb Dec 20, 2024
edb13c6
Amount snippet
rustam-cb Dec 20, 2024
af24c30
Address comments
rustam-cb Dec 20, 2024
f6f1845
Format
rustam-cb Dec 20, 2024
98c055d
Format
rustam-cb Dec 20, 2024
461e136
Address comments
rustam-cb Jan 6, 2025
8f529c4
Organize import statements
rustam-cb Jan 6, 2025
9204930
Fix typo
rustam-cb Jan 6, 2025
54b46d2
Merge branch 'fund-form' into amount-input-snippets
rustam-cb Jan 6, 2025
0b1e28d
Add type annotations
rustam-cb Jan 6, 2025
46ee540
Upadte className for FundCardHeader
rustam-cb Jan 6, 2025
d11e78a
Update css class
rustam-cb Jan 6, 2025
5272942
Address comments
rustam-cb Jan 6, 2025
c028137
Remove pressable.error
rustam-cb Jan 6, 2025
cc61b22
Merge branch 'fund-form' into amount-input-snippets
rustam-cb Jan 6, 2025
d41374b
FundCardAmountInputSnippet tests
rustam-cb Jan 6, 2025
492b911
Address comments
rustam-cb Jan 7, 2025
a417a96
Address comments...
rustam-cb Jan 8, 2025
3969c11
Address comments
rustam-cb Jan 8, 2025
849a18a
Address comments
rustam-cb Jan 8, 2025
a1f3b32
Merge branch 'main' into fund-form
rustam-cb Jan 8, 2025
0c95bb0
Address comments
rustam-cb Jan 8, 2025
eb784a5
Address comments
rustam-cb Jan 9, 2025
0309e65
Show connect wallet when there is no address only
rustam-cb Jan 10, 2025
1e0d236
Make children default prop
rustam-cb Jan 10, 2025
3a61814
Create FundCardSubmitButton component
rustam-cb Jan 10, 2025
fdd28fb
Update
rustam-cb Jan 10, 2025
d8f2561
Update
rustam-cb Jan 10, 2025
1200f79
Remove debounce
rustam-cb Jan 11, 2025
2df11f6
Add Lifecycle methods
rustam-cb Jan 11, 2025
5a3552e
Default payment methods
rustam-cb Jan 13, 2025
394c34e
Rename fundProvider to fundCardProvider
rustam-cb Jan 13, 2025
4439b28
Fix tests
rustam-cb Jan 13, 2025
226d5ca
Fix test
rustam-cb Jan 13, 2025
4ceb0ac
Fix tests
rustam-cb Jan 13, 2025
85623ec
Merge branch 'fund-form' into amount-input-snippets
rustam-cb Jan 13, 2025
693f978
Update
rustam-cb Jan 13, 2025
5611550
Update
rustam-cb Jan 13, 2025
928cfa6
Update testIds
rustam-cb Jan 13, 2025
c9e057d
Update
rustam-cb Jan 13, 2025
9d47368
Address comments
rustam-cb Jan 13, 2025
9d90ea5
Merge branch 'fund-form' into amount-input-snippets
rustam-cb Jan 13, 2025
95876dd
Update
rustam-cb Jan 14, 2025
1852142
Update
rustam-cb Jan 14, 2025
e47fac5
Address more comments
rustam-cb Jan 14, 2025
84820ee
Fix tests
rustam-cb Jan 14, 2025
bf65ed2
Merge branch 'main' into fund-form
rustam-cb Jan 14, 2025
88ed65a
Merge branch 'fund-form' into amount-input-snippets
rustam-cb Jan 14, 2025
eb538ca
Merge branch 'main' into fund-form
rustam-cb Jan 14, 2025
f4269a8
Fix tests
rustam-cb Jan 14, 2025
63098de
Merge branch 'fund-form' into amount-input-snippets
rustam-cb Jan 14, 2025
e152292
Update width of amount type swith
rustam-cb Jan 14, 2025
84a281b
Fix flaky tests
rustam-cb Jan 14, 2025
cf442e3
Merge branch 'fund-form' into amount-input-snippets
rustam-cb Jan 14, 2025
871cf5d
Add lifecycle hooks to demo
rustam-cb Jan 15, 2025
9a842d1
Address comments
rustam-cb Jan 15, 2025
3383b14
Address comments
rustam-cb Jan 16, 2025
05f2610
Format
rustam-cb Jan 16, 2025
7850ac4
Fix tests
rustam-cb Jan 16, 2025
3d9f499
Merge branch 'fund-form' into amount-input-snippets
rustam-cb Jan 16, 2025
b4b1a5f
Update
rustam-cb Jan 16, 2025
65b5e60
Update
rustam-cb Jan 16, 2025
50d6c00
Update
rustam-cb Jan 16, 2025
cd3ccf0
Fix test
rustam-cb Jan 16, 2025
9e41f7f
Upadte
rustam-cb Jan 16, 2025
751446b
Merge branch 'main' into amount-input-snippets
rustam-cb Jan 16, 2025
f94fc8b
Update
rustam-cb Jan 16, 2025
95e3910
Fix tests
rustam-cb Jan 16, 2025
44dacc0
Update
rustam-cb Jan 16, 2025
e78c229
Flaky tests
rustam-cb Jan 16, 2025
084bf97
Format
rustam-cb Jan 16, 2025
158fa84
Update
rustam-cb Jan 16, 2025
9ef1a6f
Address comments
rustam-cb Jan 16, 2025
0c47dc1
Address comments
rustam-cb Jan 16, 2025
a074028
Update key
rustam-cb Jan 16, 2025
8d06497
Remove unneeded expoer
rustam-cb Jan 16, 2025
d145bc0
Update
rustam-cb Jan 17, 2025
70f0f1f
Merge branch 'main' into amount-input-snippets
rustam-cb Jan 17, 2025
bfe2c28
Update
rustam-cb Jan 17, 2025
159cb3e
Revemo console.log
rustam-cb Jan 17, 2025
8a95bee
Fix tests
rustam-cb Jan 17, 2025
85ef1e4
Fix test
rustam-cb Jan 17, 2025
732acc5
Update
rustam-cb Jan 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions playground/nextjs-app-router/components/demo/FundCard.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { FundCard } from '@coinbase/onchainkit/fund';
import type { PresetAmountInputs } from '../../onchainkit/esm/fund/types';

export default function FundCardDemo() {
const presetAmountInputs: PresetAmountInputs = ['10', '20', '100'];

return (
<div className="mx-auto grid w-[500px] gap-8">
<FundCard
assetSymbol="ETH"
country="US"
presetAmountInputs={presetAmountInputs}
onError={(error) => {
console.log('FundCard onError', error);
}}
Expand Down
2 changes: 1 addition & 1 deletion playground/nextjs-app-router/onchainkit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coinbase/onchainkit",
"version": "0.36.5",
"version": "0.36.7",
"type": "module",
"repository": "https://github.com/coinbase/onchainkit.git",
"license": "MIT",
Expand Down
33 changes: 30 additions & 3 deletions src/fund/components/FundCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest';
import { useAccount } from 'wagmi';
import { useFundCardFundingUrl } from '../hooks/useFundCardFundingUrl';
import type { PresetAmountInputs } from '../types';
import { fetchOnrampQuote } from '../utils/fetchOnrampQuote';
import { getFundingPopupSize } from '../utils/getFundingPopupSize';
import { FundCard } from './FundCard';
Expand Down Expand Up @@ -110,10 +111,18 @@ const TestComponent = () => {
);
};

const renderComponent = () =>
const renderComponent = (presetAmountInputs?: PresetAmountInputs) =>
render(
<FundCardProvider asset="BTC" country="US">
<FundCard assetSymbol="BTC" country="US" />
<FundCardProvider
asset="BTC"
country="US"
presetAmountInputs={presetAmountInputs}
>
<FundCard
assetSymbol="BTC"
country="US"
presetAmountInputs={presetAmountInputs}
/>
<TestComponent />
</FundCardProvider>,
);
Expand Down Expand Up @@ -293,4 +302,22 @@ describe('FundCard', () => {
expect(screen.getByTestId('custom-child')).toBeInTheDocument();
expect(screen.queryByTestId('ockFundCardHeader')).not.toBeInTheDocument();
});

it('handles preset amount input click correctly', async () => {
const presetAmountInputs: PresetAmountInputs = ['12345', '20', '30'];

renderComponent(presetAmountInputs);

await waitFor(() => {
expect(screen.getByTestId('loading-state').textContent).toBe(
'not-loading',
);

// Click the preset amount input
const presetAmountInput = screen.getByText('12345 USD');

// Verify the input value was updated
expect(presetAmountInput).toBeInTheDocument();
});
});
});
4 changes: 4 additions & 0 deletions src/fund/components/FundCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import FundCardAmountInput from './FundCardAmountInput';
import FundCardAmountInputTypeSwitch from './FundCardAmountInputTypeSwitch';
import { FundCardHeader } from './FundCardHeader';
import { FundCardPaymentMethodDropdown } from './FundCardPaymentMethodDropdown';
import { FundCardPresetAmountInputList } from './FundCardPresetAmountInputList';
import { FundCardProvider } from './FundCardProvider';
import { FundCardSubmitButton } from './FundCardSubmitButton';

Expand All @@ -17,6 +18,7 @@ export function FundCard({
headerText,
country = 'US',
subdivision,
presetAmountInputs,
children = <DefaultFundCardContent />,
className,
onError,
Expand All @@ -36,6 +38,7 @@ export function FundCard({
onError={onError}
onStatus={onStatus}
onSuccess={onSuccess}
presetAmountInputs={presetAmountInputs}
>
<div
className={cn(
Expand Down Expand Up @@ -71,6 +74,7 @@ function DefaultFundCardContent() {
<FundCardHeader />
<FundCardAmountInput />
<FundCardAmountInputTypeSwitch />
<FundCardPresetAmountInputList />
<FundCardPaymentMethodDropdown />
<FundCardSubmitButton />
</>
Expand Down
1 change: 1 addition & 0 deletions src/fund/components/FundCardAmountInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ describe('FundCardAmountInput', () => {
expect(valueFiat.textContent).toBe('0');
});
});

it('handles non zero values in fiat mode', async () => {
act(() => {
render(
Expand Down
78 changes: 18 additions & 60 deletions src/fund/components/FundCardAmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,20 @@ import { isValidAmount } from '@/core/utils/isValidAmount';
import { TextInput } from '@/internal/components/TextInput';
import { useCallback, useEffect, useRef } from 'react';
import { cn, text } from '../../styles/theme';
import { useAmountInput } from '../hooks/useAmountInput';
import { useInputResize } from '../hooks/useInputResize';
import type { FundCardAmountInputPropsReact } from '../types';
import { truncateDecimalPlaces } from '../utils/truncateDecimalPlaces';
import { FundCardCurrencyLabel } from './FundCardCurrencyLabel';
import { useFundContext } from './FundCardProvider';

export const FundCardAmountInput = ({
className,
}: FundCardAmountInputPropsReact) => {
// TODO: Get currency label from country (This is coming in the follow up PRs)
const currencyLabel = 'USD';
const { fundAmountFiat, fundAmountCrypto, asset, selectedInputType } =
useFundContext();

const {
fundAmountFiat,
setFundAmountFiat,
fundAmountCrypto,
setFundAmountCrypto,
asset,
selectedInputType,
exchangeRate,
} = useFundContext();
// Next PR will include a support for any currency
const currencyOrAsset = selectedInputType === 'fiat' ? 'USD' : asset;

const containerRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLInputElement>(null);
Expand All @@ -39,50 +32,20 @@ export const FundCardAmountInput = ({
currencySpanRef,
);

const handleFiatChange = useCallback(
(value: string) => {
const fiatValue = truncateDecimalPlaces(value, 2);
setFundAmountFiat(fiatValue);

const calculatedCryptoValue = String(
Number(fiatValue) * Number(exchangeRate),
);
const resultCryptoValue = truncateDecimalPlaces(calculatedCryptoValue, 8);
setFundAmountCrypto(
calculatedCryptoValue === '0' ? '' : resultCryptoValue,
);
},
[exchangeRate, setFundAmountFiat, setFundAmountCrypto],
);
const { handleChange } = useAmountInput();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved code into useAmountInput hook to reuse it from outside of the amount input component


const handleCryptoChange = useCallback(
const handleAmountChange = useCallback(
(value: string) => {
const truncatedValue = truncateDecimalPlaces(value, 8);
setFundAmountCrypto(truncatedValue);

const calculatedFiatValue = String(
Number(truncatedValue) / Number(exchangeRate),
);

const resultFiatValue = truncateDecimalPlaces(calculatedFiatValue, 2);
setFundAmountFiat(resultFiatValue === '0' ? '' : resultFiatValue);
handleChange(value, () => {
if (inputRef.current) {
inputRef.current.focus();
}
});
},
[exchangeRate, setFundAmountFiat, setFundAmountCrypto],
[handleChange],
);

const handleChange = useCallback(
(value: string) => {
if (selectedInputType === 'fiat') {
handleFiatChange(value);
} else {
handleCryptoChange(value);
}
},
[handleFiatChange, handleCryptoChange, selectedInputType],
);

// Update width when value changes
// biome-ignore lint/correctness/useExhaustiveDependencies: We want to update the input width when the value changes
// biome-ignore lint/correctness/useExhaustiveDependencies: When value changes, we want to update the input width
useEffect(() => {
updateInputWidth();
}, [value, updateInputWidth]);
Expand All @@ -103,11 +66,9 @@ export const FundCardAmountInput = ({
<div
ref={containerRef}
data-testid="ockFundCardAmountInputContainer"
className={cn('flex cursor-text py-6', className)}
onClick={handleFocusInput}
onKeyUp={handleFocusInput}
className={cn('flex cursor-text pt-6 pb-4', className)}
>
<div className="flex h-20">
<div className="flex h-14">
<TextInput
className={cn(
text.body,
Expand All @@ -118,17 +79,14 @@ export const FundCardAmountInput = ({
'[&::-webkit-outer-spin-button]:m-0 [&::-webkit-outer-spin-button]:appearance-none',
)}
value={value}
onChange={handleChange}
onChange={handleAmountChange}
inputValidator={isValidAmount}
ref={inputRef}
inputMode="decimal"
placeholder="0"
/>

<FundCardCurrencyLabel
ref={currencySpanRef}
label={selectedInputType === 'crypto' ? asset : currencyLabel}
/>
<FundCardCurrencyLabel ref={currencySpanRef} label={currencyOrAsset} />
</div>

{/* Hidden span for measuring text width
Expand Down
6 changes: 3 additions & 3 deletions src/fund/components/FundCardPaymentMethodDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { background, border, cn } from '../../styles/theme';
import type {
FundCardPaymentMethodDropdownPropsReact,
PaymentMethodReact,
PaymentMethod,
} from '../types';
import { FundCardPaymentMethodSelectRow } from './FundCardPaymentMethodSelectRow';
import { FundCardPaymentMethodSelectorToggle } from './FundCardPaymentMethodSelectorToggle';
Expand All @@ -31,7 +31,7 @@ export function FundCardPaymentMethodDropdown({
}, [paymentMethods]);

const isPaymentMethodDisabled = useCallback(
(method: PaymentMethodReact) => {
(method: PaymentMethod) => {
const amount = Number(fundAmountFiat);
return (
(method.id === 'APPLE_PAY' || method.id === 'ACH_BANK_ACCOUNT') &&
Expand Down Expand Up @@ -60,7 +60,7 @@ export function FundCardPaymentMethodDropdown({
]);

const handlePaymentMethodSelect = useCallback(
(paymentMethod: PaymentMethodReact) => {
(paymentMethod: PaymentMethod) => {
if (!isPaymentMethodDisabled(paymentMethod)) {
setSelectedPaymentMethod(paymentMethod);
setIsOpen(false);
Expand Down
4 changes: 2 additions & 2 deletions src/fund/components/FundCardPaymentMethodSelectRow.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import '@testing-library/jest-dom';
import { fireEvent, render, screen } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import type { PaymentMethodReact } from '../types';
import type { PaymentMethod } from '../types';
import { FundCardPaymentMethodSelectRow } from './FundCardPaymentMethodSelectRow';

describe('FundCardPaymentMethodSelectRow', () => {
const mockPaymentMethod: PaymentMethodReact = {
const mockPaymentMethod: PaymentMethod = {
id: 'APPLE_PAY',
name: 'Apple Pay',
description: 'Up to $500/week',
Expand Down
Loading
Loading