Skip to content

Commit b75f08e

Browse files
RSS1102tdesign-bot
andauthored
fix(QRCode): fix SVG QRCode not reactive to value prop changes (#5864)
* fix(QrCode): fix SVG QRCode not reactive to value prop changes * test(QRCode): add tests for value prop changes in canvas and svg modes * chore: stash changelog [ci skip] * chore: stash changelog [ci skip] * chore: stash changelog [ci skip] --------- Co-authored-by: tdesign-bot <[email protected]>
1 parent b74d456 commit b75f08e

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

packages/components/qrcode/__tests__/qrcode.test.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,41 @@ describe('QRCode', () => {
9292
await wrapper.setProps({ status: 'expired', statusRender });
9393
expect(statusRender).toBeCalled();
9494
});
95+
96+
it(':value[string] changes - canvas mode', async () => {
97+
await wrapper.setProps({ type: 'canvas' });
98+
const initialValue = 'https://tdesign.tencent.com/';
99+
const newValue = 'https://github.com/Tencent/tdesign-vue-next';
100+
expect(wrapper.vm.value).eq(initialValue);
101+
102+
await wrapper.setProps({ value: newValue });
103+
104+
expect(wrapper.vm.value).eq(newValue);
105+
expect(wrapper.find('.t-qrcode').find('canvas').exists()).eq(true);
106+
});
107+
108+
it(':value[string] changes - svg mode', async () => {
109+
await wrapper.setProps({ type: 'svg' });
110+
111+
const initialValue = 'https://tdesign.tencent.com/';
112+
const newValue = 'https://github.com/Tencent/tdesign-vue-next';
113+
114+
expect(wrapper.vm.value).eq(initialValue);
115+
116+
const initialSvg = wrapper.find('.t-qrcode').find('svg');
117+
expect(initialSvg.exists()).eq(true);
118+
const initialPaths = initialSvg.findAll('path');
119+
const initialFgPath = initialPaths[1].attributes('d');
120+
await wrapper.setProps({ value: newValue });
121+
122+
expect(wrapper.vm.value).eq(newValue);
123+
const updatedSvg = wrapper.find('.t-qrcode').find('svg');
124+
expect(updatedSvg.exists()).eq(true);
125+
const updatedPaths = updatedSvg.findAll('path');
126+
const updatedFgPath = updatedPaths[1].attributes('d');
127+
128+
// 验证前景路径已改变(不同的 value 应该生成不同的二维码图案)
129+
expect(updatedFgPath).not.eq(initialFgPath);
130+
});
95131
});
96132
});

packages/components/qrcode/components/qrcode-svg.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,28 @@ export default defineComponent({
1212
name: 'QRCodeSVG',
1313
props: QRCodeSubComponentProps,
1414
setup(props) {
15-
const { margin, cells, numCells, calculatedImageSettings } = useQRCode({
16-
value: props.value,
17-
level: props.level,
18-
minVersion: DEFAULT_MINVERSION,
19-
includeMargin: DEFAULT_NEED_MARGIN,
20-
marginSize: props.marginSize,
21-
imageSettings: props.imageSettings,
22-
size: props.size,
23-
});
15+
const qrCodeData = computed(() =>
16+
useQRCode({
17+
value: props.value,
18+
level: props.level,
19+
minVersion: DEFAULT_MINVERSION,
20+
includeMargin: DEFAULT_NEED_MARGIN,
21+
marginSize: props.marginSize,
22+
imageSettings: props.imageSettings,
23+
size: props.size,
24+
}),
25+
);
2426

2527
const cellsToDraw = computed(() => {
28+
const { cells, calculatedImageSettings } = qrCodeData.value;
2629
if (props.imageSettings && calculatedImageSettings.value?.excavation != null) {
2730
return excavateModules(cells.value, calculatedImageSettings.value.excavation);
2831
}
2932
return cells.value;
3033
});
3134

3235
const imageNode = computed(() => {
36+
const { calculatedImageSettings, margin } = qrCodeData.value;
3337
if (!props.imageSettings || !calculatedImageSettings.value) return null;
3438

3539
return (
@@ -45,6 +49,7 @@ export default defineComponent({
4549
});
4650

4751
return () => {
52+
const { margin, numCells } = qrCodeData.value;
4853
const fgPath = generatePath(cellsToDraw.value, margin.value);
4954
return (
5055
<svg
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
pr_number: 5864
3+
contributor: RSS1102
4+
---
5+
6+
- fix(QRCode): 修复 `type='svg'``value` 值变化而二维码未刷新的问题 @RSS1102 ([#5864](https://github.com/Tencent/tdesign-vue-next/pull/5864))

0 commit comments

Comments
 (0)