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

fix(custom-elements): cast numbers with Number rules #4393

Closed
wants to merge 5 commits into from
Closed

Conversation

posva
Copy link
Member

@posva posva commented Aug 19, 2021

Fix #4370

By using Number(), we handle Infinity, NaN, and all other number syntaxes. I think this makes the most sense for the custom elements since it's also "the platform" and Infinity and NaN can be conveniently casted to a string too (String(Number('Infinity')) === 'Infinity')).

We could also take a look at the type of the prop being a Number and only apply the cast in that case. Let me know if that makes more sense.

@github-actions
Copy link

github-actions bot commented Aug 19, 2021

Size report

Path Size
vue.global.prod.js 45.81 KB (+0.08% 🔺)
runtime-dom.global.prod.js 30.25 KB (+0.11% 🔺)
size-check.global.prod.js 17.41 KB (0%)

@zhangenming
Copy link
Contributor

did we have two toNumber at the same time?

@posva
Copy link
Member Author

posva commented Aug 20, 2021

It should definitely be renamed to something else

@@ -0,0 +1,36 @@
import { parseNumber } from '../src/apiCustomElement'
Copy link
Member

Choose a reason for hiding this comment

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

The test file customElement.spec.ts already exists 😂

Copy link
Member Author

Choose a reason for hiding this comment

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

Cmd + P betrayed me! 😆

alexcs-bsft added a commit to blueshift-labs/ng-interop that referenced this pull request Aug 26, 2021
Internally Vue was using `parseFloat` for this,
  and `parseFloat('1abc') === 1` rather than `NaN` like it is for
  non-numeric strings

The fix was lifted from vuejs/core#4393,
  so I'll double check whether that's been merged by the time
  the rest of this is ready
@yyx990803
Copy link
Member

While this is a good improvement, I took another look at #4370 and feels there's a bit more to the original issue. Specifically for a prop declared as type: String, we probably should not attempt to parse it into a number at all. For example, if the original usage was something like <my-el phone="123456789">, it should be retained as a string.

@posva
Copy link
Member Author

posva commented Sep 2, 2021

Yeah, I thought about it too. I have two questions then:

  • Should I just look if the props is an object and if it exists and it's either not Number or not { type: Number }, skip the parseNumber() call.
  • Should I still call parseNumber() for non existant props?

@matt-filion
Copy link

What would it take to get this fix included in a near term version push. It is holding up some of our pushes.

@posva
Copy link
Member Author

posva commented Sep 11, 2021

Getting feedback on the two questions above from people using this would help move this forward

@akibrk
Copy link

akibrk commented Sep 11, 2021

On Vue 2, all wc target build props are passed as a string, we then parse it within the component as a number/boolean/object or whatever the use case is. Leaving it as a string would not be a bad idea, instead of trying to parse it for all cases.

<av-component data-is-open="true" data-id="xpd154" data-amount="12.18"></av-component>
  • dataIsOpen will be a be string
  • dataId will be string
  • dataAmount will be string

In this case type assertion based on the prop type within defineCustomElement definition would not work.

@yyx990803 yyx990803 closed this in 0cfa211 Sep 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

in custom-element components not correct handling type
5 participants