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.

Number Input default value undefined when value is set to zero

See original GitHub issue

Platform

  • 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.

image

To Reproduce

Steps to reproduce:

  1. Go to Adaptive Cards Designer
  2. Select Bot Framework WebChat as the host app
  3. 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"
}
  1. 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.

Input Parse Method

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);
        }
    }
}

Get String Value Method

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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
matthidingercommented, Jul 16, 2019

Thanks for the detailed report!

0reactions
msftbot[bot]commented, Sep 26, 2019

🎉AdaptiveCards@v1.2.3 has been released which fixes this issue.🎉

Handy links:

Read more comments on GitHub >

github_iconTop 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 >

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