Number Input default value undefined when value is set to zero
See original GitHub issuePlatform
- JavaScript
Author or host
Host Rendering Cards
Version of SDK
npm Adaptive Cards v1.2.0
Issue
Per issue https://github.com/microsoft/BotFramework-WebChat/issues/2192
Adaptive inputs of type Input.Number
don’t display the value 0
.
To Reproduce
Steps to reproduce:
- Go to Adaptive Cards Designer
- Select
Bot Framework WebChat
as the host app - Paste the following json in the
Card Payload Editor
:
{
"type": "AdaptiveCard",
"actions": [
{
"title": "Submit",
"type": "Action.Submit"
}
],
"body": [
{
"text": "Input 1",
"type": "TextBlock"
},
{
"id": "InputOne",
"placeholder": "Input 1 Placeholder",
"type": "Input.Number",
"value": 10
},
{
"text": "Input 2",
"type": "TextBlock"
},
{
"id": "InputTwo",
"placeholder": "Input 2 Placeholder",
"type": "Input.Number",
"value": -10
},
{
"text": "Input 3",
"type": "TextBlock"
},
{
"id": "InputThree",
"placeholder": "Input 3 Placeholder",
"type": "Input.Number",
"value": 0
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.0"
}
- See that the third input is not displaying the expected value
Likely cause
It looks like Adaptive Card’s Input
class’s parse
method uses the getStringValue
utility to set the input’s default value. The function uses a ternary to either return the value set in the Adaptive Card JSON as a string or the default value which would be undefined
. In the case of the value being 0
, the getStringValue
function would return undefined
.
parse(json: any, errors?: Array<HostConfig.IValidationError>) {
super.parse(json, errors);
this.id = Utils.getStringValue(json["id"]);
this.defaultValue = Utils.getStringValue(json["value"]);
if (AdaptiveCard.useBuiltInInputValidation) {
let jsonValidation = json["validation"];
if (jsonValidation) {
this.validation.parse(jsonValidation);
}
}
}
export function getStringValue(obj: any, defaultValue: string = undefined): string {
return obj ? obj.toString() : defaultValue;
}
Possible Fix
The getStringValue
method would likely have to be changed to check if the value is not undefined or equal to zero.
export function getStringValue(obj: any, defaultValue: string = undefined): string {
return (typeof obj !== 'undefined') ? obj.toString() : defaultValue;
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
for input type="number" how to set default value to 0
3 Answers. You're setting this default value to null , and nothing (in HTML) can change it to zero (or anything else). You...
Read more >3 Ways to Set Default Value in JavaScript
Most likely, we only want the default value to be set if no value or undefined is passed as the argument. The better...
Read more >Default value is not correctly updated for number input ...
I'm using the number input component and set its default value to be a certain number. The expected result is that the component...
Read more >Controlled inputs with the potential for `undefined` value
The input box uses placeholder to render in case the initial value is undefined and setting the value to anything else stops the...
Read more >Number Input, Allow null value, how do I set it to null?
I'm reading in some data into a Number Input. I have checked the "Allow null value" setting. I don't want it to default...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the detailed report!
🎉
AdaptiveCards@v1.2.3
has been released which fixes this issue.🎉Handy links: