microsoft / microsoft/AdaptiveCards

[JS] Actions not working when renderer used via ReactJS

Open
#6,192 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area-Renderers Question
Dominant language
C#
Stars
2k
Forks
595
Avg merge
1d 19h
Merged PRs (30d)
1

Description

Target Platform

Please list the platform(s) that your application is targeting and remove platforms you are not targeting:

  • ReactJS

Target Application

ReactJS chatbot using directline API

Problem Description

  • Not able to perform any action when clicked on buttons, StackOverflow Post link
  • Also, as the response was an object. In order to render object inside HTML, I am using <div dangerouslySetInnerHTML={{ __html: cardData?.innerHTML }}></div> while rendering. I was not able to render card directly inside div.

Card JSON

{
      "type": "AdaptiveCard",
      "version": "1.0",
      "body": [
          {
              "type": "Image",
              "url": "https://adaptivecards.io/content/adaptive-card-50.png"
          },
          {
              "type": "TextBlock",
              "text": "Hello **Adaptive Cards!**"
          }
      ],
      "actions": [
          {
              "type": "Action.OpenUrl",
              "title": "Learn more",
              "url": "https://adaptivecards.io"
          },
          {
              "type": "Action.OpenUrl",
              "title": "GitHub",
              "url": "https://github.com/Microsoft/AdaptiveCards"
          }
      ]
  };

Sample Code

import * as AdaptiveCards from "adaptivecards";
import {useState, useEffect} from 'react';


function AdaptiveCard() {

  const [cardData, setCardData] = useState()

  const {adaptiveCardData} = props
  
  useEffect(()=>{

    var card = {
      "type": "AdaptiveCard",
      "version": "1.0",
      "body": [
          {
              "type": "Image",
              "url": "https://adaptivecards.io/content/adaptive-card-50.png"
          },
          {
              "type": "TextBlock",
              "text": "Hello **Adaptive Cards!**"
          }
      ],
      "actions": [
          {
              "type": "Action.OpenUrl",
              "title": "Learn more",
              "url": "https://adaptivecards.io"
          },
          {
              "type": "Action.OpenUrl",
              "title": "GitHub",
              "url": "https://github.com/Microsoft/AdaptiveCards"
          }
      ]
  };
    
    var adaptiveCard = new AdaptiveCards.AdaptiveCard();
  
    adaptiveCard.hostConfig = new AdaptiveCards.HostConfig({
      fontFamily: "Segoe UI, Helvetica Neue, sans-serif"
    });
  
    adaptiveCard.onExecuteAction = function (action) { alert("Ow!"); }
  
    adaptiveCard.parse(card);
  
    var renderedCard = adaptiveCard.render();
    
    setCardData(renderedCard)
  },[])

  return (
    <div>
      <h1>Hello world</h1>
      <div dangerouslySetInnerHTML={{ __html: cardData?.innerHTML }}></div>
    </div>
  );
}

export default AdaptiveCard;

Version of SDK

2.9.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the AdaptiveCard component and reproduce the provided ReactJS example using adaptivecards SDK 2.9.0. Inspect the result of adaptiveCard.render(), the dangerouslySetInnerHTML usage, and the onExecuteAction callback; done means the rendered card displays correctly and its OpenUrl actions invoke the expected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend, web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.