diff --git a/src/swap/components/Swap.tsx b/src/swap/components/Swap.tsx index fafc0b6881..f64e5f028b 100644 --- a/src/swap/components/Swap.tsx +++ b/src/swap/components/Swap.tsx @@ -6,6 +6,7 @@ import { SwapAmountInput } from './SwapAmountInput'; import { SwapButton } from './SwapButton'; import { SwapMessage } from './SwapMessage'; import { SwapProvider } from './SwapProvider'; +import { SwapSettings } from './SwapSettings'; import { SwapToggleButton } from './SwapToggleButton'; export function Swap({ @@ -15,15 +16,17 @@ export function Swap({ title = 'Swap', className, }: SwapReact) { - const { inputs, toggleButton, swapButton, swapMessage } = useMemo(() => { - const childrenArray = Children.toArray(children); - return { - inputs: childrenArray.filter(findComponent(SwapAmountInput)), - toggleButton: childrenArray.find(findComponent(SwapToggleButton)), - swapButton: childrenArray.find(findComponent(SwapButton)), - swapMessage: childrenArray.find(findComponent(SwapMessage)), - }; - }, [children]); + const { inputs, toggleButton, swapButton, swapMessage, swapSettings } = + useMemo(() => { + const childrenArray = Children.toArray(children); + return { + inputs: childrenArray.filter(findComponent(SwapAmountInput)), + toggleButton: childrenArray.find(findComponent(SwapToggleButton)), + swapButton: childrenArray.find(findComponent(SwapButton)), + swapMessage: childrenArray.find(findComponent(SwapMessage)), + swapSettings: childrenArray.find(findComponent(SwapSettings)), + }; + }, [children]); return ( @@ -35,10 +38,11 @@ export function Swap({ )} data-testid="ockSwap_Container" > -
+

{title}

+
{swapSettings}
{inputs[0]}
{toggleButton}
diff --git a/src/swap/components/SwapSettings.test.tsx b/src/swap/components/SwapSettings.test.tsx new file mode 100644 index 0000000000..ee850d3c1b --- /dev/null +++ b/src/swap/components/SwapSettings.test.tsx @@ -0,0 +1,35 @@ +import { render, screen } from '@testing-library/react'; +import { describe, expect, it } from 'vitest'; +import { SwapSettings } from './SwapSettings'; + +describe('SwapSettings', () => { + it('renders with default title', () => { + render(); + expect(screen.getByText('Auto')).toBeInTheDocument(); + }); + + it('renders with custom title', () => { + render(); + expect(screen.getByText('Custom')).toBeInTheDocument(); + }); + + it('renders custom icon when provided', () => { + const CustomIcon = () => ( + + ); + render(} />); + expect(screen.getByTestId('custom-icon')).toBeInTheDocument(); + }); + + it('applies correct classes to the button', () => { + render(); + const button = screen.getByRole('button', { + name: /toggle swap settings/i, + }); + expect(button).toHaveClass( + 'rounded-full p-2 opacity-50 transition-opacity hover:opacity-100', + ); + }); +}); diff --git a/src/swap/components/SwapSettings.tsx b/src/swap/components/SwapSettings.tsx new file mode 100644 index 0000000000..135b84a5c1 --- /dev/null +++ b/src/swap/components/SwapSettings.tsx @@ -0,0 +1,32 @@ +import { isValidElement, useMemo } from 'react'; +import { SwapSettingsSvg } from '../../internal/svg/swapSettings'; +import type { SwapSettingsReact } from '../types'; + +export function SwapSettings({ icon, title = 'Auto' }: SwapSettingsReact) { + const iconSvg = useMemo(() => { + if (icon === undefined) { + return SwapSettingsSvg; + } + if (isValidElement(icon)) { + return icon; + } + }, [icon]); + + return ( +
+
+

{title}

+ +
+
+ ); +} diff --git a/src/swap/index.ts b/src/swap/index.ts index ef89155aea..d2a515a9a3 100644 --- a/src/swap/index.ts +++ b/src/swap/index.ts @@ -3,6 +3,8 @@ export { Swap } from './components/Swap'; export { SwapAmountInput } from './components/SwapAmountInput'; export { SwapButton } from './components/SwapButton'; export { SwapMessage } from './components/SwapMessage'; +export { SwapSettings } from './components/SwapSettings'; +export { SwapSettingsContainer } from './components/SwapSettingsContainer'; export { SwapToggleButton } from './components/SwapToggleButton'; export { buildSwapTransaction } from './utils/buildSwapTransaction'; export { getSwapQuote } from './utils/getSwapQuote'; diff --git a/src/swap/types.ts b/src/swap/types.ts index d8d94da87b..4dbd2af281 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -219,6 +219,15 @@ export type SwapReact = { title?: string; // Title for the Swap component. (default: "Swap") }; +export type SwapSettingsReact = { + icon: ReactNode; + title: string; +}; + +export type SwapSettingsContainerReact = { + className?: string; // Optional className override for top div element. +}; + /** * Note: exported as public Type */