Skip to content

Commit 0bea62e

Browse files
Add a new Stroybook page for AdaptiveCard (#5859)
1 parent 4a7cfe1 commit 0bea62e

8 files changed

Lines changed: 454 additions & 46 deletions

File tree

common/config/rush/pnpm-lock.yaml

Lines changed: 85 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/config/rush/variants/stable/pnpm-lock.yaml

Lines changed: 80 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/storybook8/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
"resize-observer-polyfill": "^1.5.1",
4242
"tslib": "^2.5.0",
4343
"browserslist": "^4.22.2",
44-
"uuid": "^9.0.0"
44+
"uuid": "^9.0.0",
45+
"adaptivecards": "~3.0.5",
46+
"adaptivecards-react": "~1.1.1"
4547
},
4648
"devDependencies": {
4749
"@babel/core": "^7.27.1",
@@ -116,6 +118,7 @@
116118
"ts-loader": "^8.0.12",
117119
"ts-node": "^10.9.2",
118120
"typescript": "5.4.5",
119-
"webpack": "5.95.0"
121+
"webpack": "5.95.0",
122+
"swiper": "~11.2.7"
120123
}
121-
}
124+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
import { FluentThemeProvider, MessageThread, MessageProps, MessageRenderer } from '@azure/communication-react';
5+
import { Action } from 'adaptivecards';
6+
import { AdaptiveCard } from 'adaptivecards-react';
7+
import React from 'react';
8+
import { ArgsFrom } from '../../../controlsUtils';
9+
import { GetHistoryWithAdaptiveCardMessages } from '../snippets/placeholdermessages';
10+
11+
const storyControls = {
12+
border: { control: 'text', name: 'Border' },
13+
adaptiveCardTitle: { control: 'text', name: 'Adaptive Card Title' }
14+
};
15+
16+
const CustomizedAdaptiveCardMessageStory = (args: ArgsFrom<typeof storyControls>): JSX.Element => {
17+
const adaptiveCardMessageStyles = {
18+
border: args.border,
19+
borderRadius: '5px',
20+
marginRight: '20px',
21+
marginLeft: '40px'
22+
};
23+
const onRenderMessage = (messageProps: MessageProps, defaultOnRender?: MessageRenderer): JSX.Element => {
24+
if (messageProps.message.messageType !== 'custom') {
25+
return defaultOnRender ? defaultOnRender(messageProps) : <></>;
26+
}
27+
try {
28+
// host config setup
29+
const hostConfig = {
30+
fontFamily: 'Times New Roman'
31+
};
32+
33+
// parse the adaptive card from the message content
34+
const adaptiveCard = JSON.parse(messageProps.message.content);
35+
36+
return (
37+
<AdaptiveCard
38+
payload={adaptiveCard}
39+
hostConfig={hostConfig}
40+
onExecuteAction={(action: Action) => console.log('onExecuteAction ' + action)}
41+
onActionOpenUrl={(action: Action) => {
42+
console.log('onActionOpenUrl' + action);
43+
}}
44+
onActionShowCard={(action: Action) => console.log('onActionShowCard' + action)}
45+
onError={(error: any) => console.log('onError', error)}
46+
style={adaptiveCardMessageStyles}
47+
/>
48+
);
49+
} catch (error) {
50+
console.log('adaptive card error', messageProps.message, error);
51+
// use default render in case if adaptive cards can't be parsed
52+
return defaultOnRender ? defaultOnRender(messageProps) : <></>;
53+
}
54+
};
55+
56+
return (
57+
<FluentThemeProvider>
58+
<MessageThread
59+
userId={'1'}
60+
messages={GetHistoryWithAdaptiveCardMessages(args.adaptiveCardTitle)}
61+
onRenderMessage={onRenderMessage}
62+
/>
63+
</FluentThemeProvider>
64+
);
65+
};
66+
67+
export const CustomizedAdaptiveCardMessage = CustomizedAdaptiveCardMessageStory.bind({});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Canvas, Meta } from '@storybook/blocks';
2+
import * as CustomizedAdaptiveCardMessageStories from './index.stories';
3+
4+
import MessageThreadWithAdaptiveCardExample from '!!raw-loader!./snippets/MessageWithAdaptiveCard.snippet.tsx';
5+
6+
<Meta of={CustomizedAdaptiveCardMessageStories} />
7+
8+
## Messages with Customized Adaptive Card
9+
10+
The example below shows how to render an `adaptive card` message with `onRenderMessage` in `MessageThread`
11+
12+
## Example
13+
14+
<Canvas
15+
of={CustomizedAdaptiveCardMessageStories.CustomAdaptiveCardMessageDocsOnly}
16+
source={{ code: MessageThreadWithAdaptiveCardExample }}
17+
layout="padded"
18+
/>

0 commit comments

Comments
 (0)