question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InputAttributes should accept (boolean | string | number) values

See original GitHub issue

As you can see here: https://github.com/sweetalert2/sweetalert2/blob/138b5c87e84cb73e917c2efb81a5578b17067391/sweetalert2.d.ts#L632

inputAttributes only accepts string values, however even in examples, you can see maxLength attribute being passed as number.

Also, I think it could accept also boolean allowing people to do something like:

inputAttributes: {
  autofocus: true
  // ...
}

Also, I think the inputValue parameter should be boolean | number | string instead of any:

https://github.com/sweetalert2/sweetalert2/blob/138b5c87e84cb73e917c2efb81a5578b17067391/sweetalert2.d.ts#L602

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
toveruxcommented, Feb 27, 2018

Hello @rafaelss95, thanks for your careful reading of the declaration file 😃

… inputAttributes only accepts string values, however even in examples, you can see maxLength attribute being passed as number.

SweetAlert2 uses Element.setAttribute, which takes a string for the attribute values. In fact, HTML attributes can only bear string values, don’t forget that. If you set a number like 42, it will be stored as "42" because JavaScript would call .toString() on anything that’s not already a string. That’s why passing a number will “work”, but technically you can also pass objects, symbols, space shuttles and cats. But this is wrong as JS will perform type coercion, and your space shuttle will silently be transformed in "[object Object]".

image

In TypeScript, you’ll have to be stricter here and explicitely use toString(), so you are explicit about the fact that you’re transforming the value and loosing information.

The same goes for inputValue, SweetAlert2 uses HTMLInputElement.value, which exclusively takes strings.

1reaction
rafaelss95commented, Mar 3, 2018

@toverux Just submtted a PR. Please, have a look and let me know if I’m missing something…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using boolean-value of attributes in JSX - Stack Overflow
You can just omit the value assignment; the attribute will be assigned a value of true by default. Instead of doing any of:...
Read more >
<input>: The Input (Form Input) element - HTML
(Not a Boolean attribute!) The autocomplete attribute takes as its value a space-separated string that describes what, if any, type of ...
Read more >
How To Use HTML5 Input Types | DigitalOcean
required - boolean to indicate if the value is required before the <form> can be submitted; tabindex - number to indicate which order...
Read more >
Boolean attributes - HTML Spec
A number of attributes are boolean attributes . ... If the attribute is present, its value must either be the empty string or...
Read more >
lightning-input - documentation - Salesforce Developers
As a result, a number that should be invalid can be accepted as valid due to the loss of precision in the stored...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found