Skip to content

Commit 5dbecd5

Browse files
fix: NFTCard action button not work (#344)
* test: add NFTCard like test case * fix: action click bug * chore: add change log
1 parent 2bdb1d9 commit 5dbecd5

File tree

8 files changed

+75
-38
lines changed

8 files changed

+75
-38
lines changed

.changeset/yellow-files-beg.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ant-design/web3': patch
3+
---
4+
5+
fix: NFTCard action button click not work

packages/web3/src/nft-card/NFTCard.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ interface NFTCardProps {
3939
style?: React.CSSProperties;
4040
showAction?: boolean;
4141
type?: 'default' | 'pithy';
42-
onActionChange?: () => void;
42+
onActionClick?: () => void;
4343
}
4444

4545
const CardSkeleton: React.FC<PropsWithChildren<{ loading: boolean; prefixCls: string }>> = ({
@@ -73,6 +73,7 @@ const NFTCard: React.FC<NFTCardProps> = ({
7373
showAction,
7474
actionText = 'Buy Now',
7575
footer,
76+
onActionClick,
7677
getNFTMetadata,
7778
...metadataProps
7879
}) => {
@@ -188,13 +189,7 @@ const NFTCard: React.FC<NFTCardProps> = ({
188189
</div>
189190
{showAction ? (
190191
<div className={`${prefixCls}-action`}>
191-
<Button
192-
style={{
193-
width: '100%',
194-
}}
195-
>
196-
{actionText}
197-
</Button>
192+
<Button onClick={onActionClick}>{actionText}</Button>
198193
</div>
199194
) : null}
200195
{footer ? (
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { NFTCard } from '@ant-design/web3';
2+
import { fireEvent, render } from '@testing-library/react';
3+
import { describe, it, expect, vi } from 'vitest';
4+
5+
describe('NFTCard actions', () => {
6+
it('render action and onActionChange work', () => {
7+
const actionFn = vi.fn();
8+
const address = '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B';
9+
const tokenId = 123;
10+
11+
const { baseElement } = render(
12+
<NFTCard
13+
address={address}
14+
tokenId={tokenId}
15+
actionText="ActionTest"
16+
showAction
17+
onActionClick={actionFn}
18+
price={{
19+
value: 139999n,
20+
}}
21+
/>,
22+
);
23+
// Ensure the like and price elements are rendered
24+
expect(baseElement.querySelector('.ant-nft-card-action')?.textContent).toBe('ActionTest');
25+
fireEvent.click(baseElement.querySelector('.ant-nft-card-action .ant-btn')!);
26+
expect(actionFn).toHaveBeenCalled();
27+
});
28+
});

packages/web3/src/nft-card/__tests__/basic.test.tsx

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,6 @@ describe('NFTCard', () => {
4141
});
4242
});
4343

44-
it('renders correctly with likeConfig and price', () => {
45-
const address = '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B';
46-
const tokenId = 123;
47-
const likeConfig = {
48-
liked: true,
49-
totalLikes: 42,
50-
};
51-
52-
const { baseElement } = render(
53-
<NFTCard
54-
address={address}
55-
tokenId={tokenId}
56-
like={likeConfig}
57-
price={{
58-
value: 139999n,
59-
}}
60-
/>,
61-
);
62-
63-
// Ensure the like and price elements are rendered
64-
expect(baseElement.querySelector('.ant-nft-card-like-value')).toBeTruthy();
65-
expect(baseElement.querySelector('.ant-web3-crypto-price-balance')).toBeTruthy();
66-
});
67-
6844
it('renders correctly without likeConfig and price', () => {
6945
const address = '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B';
7046
const tokenId = 123;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { NFTCard } from '@ant-design/web3';
2+
import { fireEvent, render } from '@testing-library/react';
3+
import { describe, it, expect, vi } from 'vitest';
4+
5+
describe('NFTCard like', () => {
6+
it('renders correctly with likeConfig and onLikeChange work', () => {
7+
const likeFn = vi.fn();
8+
const address = '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B';
9+
const tokenId = 123;
10+
const likeConfig = {
11+
liked: true,
12+
totalLikes: 42,
13+
onLikeChange: likeFn,
14+
};
15+
16+
const { baseElement } = render(
17+
<NFTCard
18+
address={address}
19+
tokenId={tokenId}
20+
like={likeConfig}
21+
price={{
22+
value: 139999n,
23+
}}
24+
/>,
25+
);
26+
// Ensure the like and price elements are rendered
27+
expect(baseElement.querySelector('.ant-nft-card-like-value')).toBeTruthy();
28+
expect(baseElement.querySelector('.ant-web3-crypto-price-balance')).toBeTruthy();
29+
fireEvent.click(baseElement.querySelector('.ant-nft-card-like-icon')!);
30+
expect(likeFn).toHaveBeenCalled();
31+
});
32+
});

packages/web3/src/nft-card/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ Components used to display NFTCard.
3636
| footer | The footer of the card | `React.ReactNode` | - | - |
3737
| name | The name of the card | `string` | - | - |
3838
| style | The style of the card | `React.CSSProperties` | - | - |
39-
| showAction | Whether to show the main button of the card | `boolean` | `true` | - |
39+
| showAction | Whether to show the action button of the card | `boolean` | `true` | - |
4040
| type | The type of the card | `'default' \| 'pithy'` | `'default'` | - |
41-
| onActionChange | The callback when the main button of the card is clicked | `() => void` | - | - |
41+
| onActionClick | The callback when the action button of the card is clicked | `() => void` | - | - |
4242

4343
The definition of `NFTMetadata` refers to the Ethereum ERC721 standard, see [NFTMetadata document](../types/index.md#nftmetadata) for details.
4444

packages/web3/src/nft-card/index.zh-CN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ group: 展示
3636
| footer | 卡片的底部内容 | `React.ReactNode` | - | - |
3737
| name | 卡片的名称 | `string` | - | - |
3838
| style | 卡片的样式 | `React.CSSProperties` | - | - |
39-
| showAction | 是否显示卡片的主要按钮 | `boolean` | `true` | - |
39+
| showAction | 是否显示卡片的操作按钮 | `boolean` | `true` | - |
4040
| type | 卡片的类型 | `'default' \| 'pithy'` | `'default'` | - |
41-
| onActionChange | 点击卡片的主要按钮时的回调 | `() => void` | - | - |
41+
| onActionClick | 点击卡片的操作按钮时的回调 | `() => void` | - | - |
4242

4343
`NFTMetadata` 的定义参考以太坊 ERC721 的标准,具体见 [NFTMetadata 文档](../types/index.zh-CN.md#nftmetadata)
4444

packages/web3/src/nft-card/style/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,20 @@ const genNFTCardStyle: GenerateStyle<NFTCardToken> = (token) => {
134134
},
135135
[`${componentCls}-action`]: {
136136
marginTop: marginLG,
137-
'.ant-btn': {
137+
[`${antCls}-btn`]: {
138138
backgroundColor: colorFillSecondary,
139139
height: controlHeightLG,
140140
borderRadius: borderRadiusLG,
141141
border: 'none',
142142
color: colorText,
143143
fontWeight: 500,
144+
width: '100%',
144145
},
145146
},
146147
[`${componentCls}-footer`]: {
147148
color: colorTextSecondary,
148149
fontSize,
149-
'.ant-divider': {
150+
[`${antCls}-divider`]: {
150151
borderColor: colorTextQuaternary,
151152
marginTop: margin,
152153
marginBottom: marginXS,

0 commit comments

Comments
 (0)