Skip to content

Number Input default value undefined when value is set to zero #3237

Description

@tdurnford

Platform

  • JavaScript

Author or host

Host Rendering Cards

Version of SDK

npm Adaptive Cards v1.2.0

Issue

Per issue microsoft/BotFramework-WebChat#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;
}

Metadata

Metadata

Assignees

Labels

BugPlatform-JavaScriptBugs or features related to the JavaScript renderer

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions