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

feat: qrcode canvas supports style configuration #48194

Merged
merged 13 commits into from Apr 30, 2024

Conversation

thinkasany
Copy link
Contributor

[中文版模板 / Chinese template]

🤔 This is a ...

  • New feature
  • Bug fix
  • Site / documentation update
  • Demo update
  • Component style update
  • TypeScript definition update
  • Bundle size optimization
  • Performance optimization
  • Enhancement feature
  • Internationalization
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Workflow
  • Other (about what?)

🔗 Related issue link

关联修改 #45815 #48053

💡 Background and solution

qrcode修改需要支持style的配置才能实现‘100%’,之前的pr只修复了外层的div的宽度,实际上我们需要让canvas也生效,用style的宽度来支持修改是react.qrcode 作者推荐的写法,他不建议使用size来做‘100%’,所以需要佬们看一下,怎么支持一下。

目前我的修改代码实际上只有在配置了witdh才会覆盖,如果没配置witdth应该值还是原来的,不会影响到原来的代码逻辑。

image image

📝 Changelog

Language Changelog
🇺🇸 English feat: qrcode canvas supports style configuration
🇨🇳 Chinese feat: qrcode 的画布支持style配置

☑️ Self-Check before Merge

⚠️ Please check all items below before requesting a reviewing. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

Copy link

stackblitz bot commented Mar 31, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link
Contributor

github-actions bot commented Mar 31, 2024

👁 Visual Regression Report for PR #48194 Passed ✅

🎯 Target branch: feature (6f6868f)
📖 View Full Report ↗︎

🎊 Congrats! No visual-regression diff found.

Copy link
Contributor

github-actions bot commented Mar 31, 2024

Preview is ready

Copy link

codesandbox-ci bot commented Mar 31, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link

codecov bot commented Mar 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (6f6868f) to head (e2f8df5).

Additional details and impacted files
@@            Coverage Diff            @@
##           feature    #48194   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          745       745           
  Lines        12987     12989    +2     
  Branches      3406      3408    +2     
=========================================
+ Hits         12987     12989    +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@li-jia-nan
Copy link
Member

@afc163
Copy link
Member

afc163 commented Mar 31, 2024

原始问题是啥?

@thinkasany
Copy link
Contributor Author

thinkasany commented Mar 31, 2024

原始问题是啥?

原始问题是:我们有时候需要配置 width:'100%', 但是现在只能用size来配置固定px。
react.qrcode是可以通过style来配置的,但是antd的封装不行,就只能用size来了
ant-design/ant-design-web3#679

@afc163
Copy link
Member

afc163 commented Mar 31, 2024

给个重现?

@thinkasany
Copy link
Contributor Author

thinkasany commented Mar 31, 2024

给个重现?

https://codepen.io/thinkasany/pen/JjVOYyq?editors=0010

外部div witdth 200px了,但是内部svg还是会保留默认的size

image

我们是为了解决溢出的问题
image

现在的代码用了‘100%’ 会出现下面这种情况,classname为antd-qrcode的div撑开来了, 但是内部svg还是size的尺寸,size这边要求字段为number 这个也是受react.qrcode 那边影响,去提过pr了,他们不建议让这个字段兼容string
image

@afc163
Copy link
Member

afc163 commented Apr 1, 2024

这样不行的,QRCode 的默认大小就是 160,如果没有覆盖 size,大小不应该变化。

另外 style.width + style.height 设置的实际上是 QRCode 外框的大小,而非二维码本身的大小,这两个存在分开配置的可能性。

如果需要二维码大小跟踪外框走,建议 size="100%" 来满足。

@afc163 afc163 closed this Apr 1, 2024
@thinkasany
Copy link
Contributor Author

thinkasany commented Apr 1, 2024

这样不行的,QRCode 的默认大小就是 160,如果没有覆盖 size,大小不应该变化。

另外 style.width + style.height 设置的实际上是 QRCode 外框的大小,而非二维码本身的大小,这两个存在分开配置的可能性。

如果需要二维码大小跟踪外框走,建议 size="100%" 来满足。

@afc163 size="100% 的写法不是很受推荐, 当时有去这边讨论过这种写法
zpao/qrcode.react#338
image

@thinkasany
Copy link
Contributor Author

这样不行的,QRCode 的默认大小就是 160,如果没有覆盖 size,大小不应该变化。

另外 style.width + style.height 设置的实际上是 QRCode 外框的大小,而非二维码本身的大小,这两个存在分开配置的可能性。

如果需要二维码大小跟踪外框走,建议 size="100%" 来满足。

还有就是 size 目前有约束 只能是number类型,当时试过pr number | stirng, 但是遇到了底层库的报错,所以才去作者那边问的

@afc163
Copy link
Member

afc163 commented Apr 1, 2024

我感觉可以加一个 max-width: 100%;

@thinkasany
Copy link
Contributor Author

我感觉可以加一个 max-width: 100%;

现在就是只有修改size 才能影响到内部的svg的尺寸,现在给的所有style都是给了svg外部的div, 所以内部的svg有溢出风险,感觉如果用 size: '100%' as unknown as string 去做不是很友好

@afc163
Copy link
Member

afc163 commented Apr 1, 2024

还有就是 size 目前有约束 只能是number类型

可以在 antd 这层放开类型限制。

@thinkasany
Copy link
Contributor Author

还有就是 size 目前有约束 只能是number类型

可以在 antd 这层放开类型限制。

我曾经尝试过 #48019 ,ci 过不去

https://github.com/ant-design/ant-design/actions/runs/8386601484/job/22967339294

image

@li-jia-nan
Copy link
Member

as 一下?

@li-jia-nan
Copy link
Member

顺便加个 max-width: 100% 的属性

@li-jia-nan
Copy link
Member

 '> canvas': {
  alignSelf: 'stretch',
  flex: 'auto',
  minWidth: 0,
  minHeight: 0,
  maxWidth: '100%',
  maxHeight: '100%',
},

@thinkasany
Copy link
Contributor Author

zpao/qrcode.react#338

@afc163 老板辛苦你再看一眼 🙏, 希望没有耽误你太多时间精力,如果还是不能解决的话,我再想想其他方式去解决这个问题好了,我又重新整理了一下组织语言,最终汇集成以下两点:

  1. qrcode.react 并没有做size的string的兼容,'100%' 虽然生效, 但是作者不建议这么使用, 所以出现了上面的ci报错,在原仓库中string是无法通过ci测试的,作者是建议使用style去做宽度。

image

  1. 我现在遇到的场景是: 我想给定一个div宽高,然后想让svg(qrcode)跟着这个div自适应宽度,但是针对你上面说的那种用户可能想要分开配置两个宽高,我觉得我会设置 border: false, , 然后自己去实现这个border,不然的话,感觉做size的宽度同步挺麻烦的,因为按照作者库要求只能是number类型。(max-width: 100%; 虽然可以宽度不溢出,但是最后宽高不是同等长度的话其实也不是很好看 )。然后我想表达的其实就是,qrcode外框的大小想调整还是很容易的,但是想要让内部的svg size={'100%'} , 不太方便,毕竟不受库本身的支持。
    image
    image

@afc163 afc163 reopened this Apr 1, 2024
@yoyo837
Copy link
Contributor

yoyo837 commented Apr 1, 2024

bugfix or feature?

@thinkasany
Copy link
Contributor Author

thinkasany commented Apr 1, 2024

bugfix or feature?

感觉也可以算是feature,因为原来的预期是只用配置border宽度就够了(二维码用size控制),现在多了一个style支持调整qrcode内部二维码的宽高

@afc163
Copy link
Member

afc163 commented Apr 1, 2024

max-width: 100% 不能解决原始问题么?

@thinkasany
Copy link
Contributor Author

@afc163 ,辛苦老板再帮忙看看,上面提到的都调整好了。

@afc163 afc163 closed this Apr 17, 2024
@afc163 afc163 reopened this Apr 17, 2024
@afc163
Copy link
Member

afc163 commented Apr 30, 2024

需要加个 bordered 属性。

@thinkasany
Copy link
Contributor Author

需要加个 bordered 属性。

这个属性之前就有啦
image

@afc163 afc163 merged commit 3fa9d45 into ant-design:feature Apr 30, 2024
60 checks passed
expect(container.querySelector<HTMLDivElement>('.ant-qrcode')).toHaveStyle(
'width: 100px; height: 80px',
it('correct style order for canvas', () => {
const { container } = render(<QRCode value="test" size={80} style={{ width: '100%' }} />);
Copy link
Member

Choose a reason for hiding this comment

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

加了新特性的话,需要新增 test case,不要动原来用例

Copy link
Member

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants