Skip to content

Commit bd0fe05

Browse files
committed
feat: add forwardRef support to WalletContainer
1 parent eae53b3 commit bd0fe05

2 files changed

Lines changed: 32 additions & 11 deletions

File tree

packages/ui/src/2_molecules/WalletContainer/WalletContainer.test.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { render } from '@testing-library/react';
1+
import { render, waitFor } from '@testing-library/react';
2+
import userEvent from '@testing-library/user-event';
23

34
import React from 'react';
45

@@ -17,4 +18,27 @@ describe('WalletContainer', () => {
1718
);
1819
expect(findByRole('svg[data-icon="info"]')).toBeDefined();
1920
});
21+
22+
it('eventHandler called on click', () => {
23+
const handleClick = jest.fn();
24+
const { getByText } = render(
25+
<WalletContainer
26+
name="Ledger"
27+
icon={<Icon icon="info" />}
28+
onClick={handleClick}
29+
/>,
30+
);
31+
userEvent.click(getByText('Ledger'));
32+
expect(handleClick).toHaveBeenCalledTimes(1);
33+
});
34+
35+
it('can be focused when using refs (button)', () => {
36+
const ref = React.createRef<HTMLButtonElement>();
37+
render(
38+
<WalletContainer name="Ledger" icon={<Icon icon="info" />} ref={ref} />,
39+
);
40+
waitFor(() => ref.current);
41+
ref.current?.focus();
42+
expect(document.activeElement).toEqual(ref.current);
43+
});
2044
});

packages/ui/src/2_molecules/WalletContainer/WalletContainer.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { FC, ReactNode } from 'react';
1+
import React, { ReactNode, forwardRef, LegacyRef } from 'react';
22

33
import classNames from 'classnames';
44

@@ -13,16 +13,13 @@ type WalletContainerProps = {
1313
dataLayoutId?: string;
1414
} & React.ButtonHTMLAttributes<HTMLButtonElement>;
1515

16-
export const WalletContainer: FC<WalletContainerProps> = ({
17-
name,
18-
icon,
19-
tooltip,
20-
className,
21-
dataLayoutId,
22-
...buttonProps
23-
}) => {
16+
export const WalletContainer = forwardRef<
17+
HTMLButtonElement,
18+
WalletContainerProps
19+
>(({ name, icon, tooltip, className, dataLayoutId, ...buttonProps }, ref) => {
2420
return (
2521
<button
22+
ref={ref as LegacyRef<HTMLButtonElement>}
2623
data-layout-id={dataLayoutId}
2724
className={classNames(className, styles.walletContainer)}
2825
{...buttonProps}
@@ -34,4 +31,4 @@ export const WalletContainer: FC<WalletContainerProps> = ({
3431
{icon && <div className={styles.icon}>{icon}</div>}
3532
</button>
3633
);
37-
};
34+
});

0 commit comments

Comments
 (0)