Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SerializationContext to React wrapper interface. #9015

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions source/nodejs/adaptivecards-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,24 @@ import {ProvidesHostConfigContext, AdaptiveCardUsingHostConfigContext } from "ad
</...>
</ProvidesHostConfigContext>
```

- extend AdativeCard with [custom elements](https://learn.microsoft.com/en-us/adaptive-cards/sdk/rendering-cards/javascript/extensibility):

```tsx
import { CardElement, CardObjectRegistry, GlobalRegistry, SerializationContext } from "adaptivecards";

// populate element registry
const elementRegistry = new CardObjectRegistry<CardElement>();
GlobalRegistry.populateWithDefaultElements(elementRegistry);
elementRegistry.register(CustomElement.JsonTypeName, CustomElement);

// customize serialization context
const serializationContext = new SerializationContext();
serializationContext.setElementRegistry(elementRegistry);

// ...

// pass context to the adaptive card host
<AdaptiveCard payload={card} hostConfig={hostConfig} serializationContext={serializationContext} />

```
6 changes: 5 additions & 1 deletion source/nodejs/adaptivecards-react/src/adaptive-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Props {
onError?: Function;
style?: object;
hostConfig?: object;
serializationContext?: AdaptiveCards.SerializationContext;
}

const propTypes = {
Expand All @@ -40,6 +41,8 @@ const propTypes = {
style: PropTypes.object,
/** HostConfig. [More Info](https://docs.microsoft.com/en-us/adaptive-cards/rendering-cards/host-config) */
hostConfig: PropTypes.object,
/** Custom serialization context for elements customization. [More Info](https://learn.microsoft.com/en-us/adaptive-cards/sdk/rendering-cards/javascript/extensibility) */
serializationContext: PropTypes.object
};

const defaultOpenUrlHandler = (action: AdaptiveCards.OpenUrlAction) => {
Expand All @@ -66,6 +69,7 @@ export const AdaptiveCard = ({
onError,
style,
hostConfig,
serializationContext
}: Props) => {
const [error, setError] = useState<Error>();
const targetRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -122,7 +126,7 @@ export const AdaptiveCard = ({
const card = cardRef.current;

try {
card.parse(payload);
card.parse(payload, serializationContext);
const result = card.render() as HTMLElement;
const trustedHtml = (typeof window === 'undefined') ? "" : (window.trustedTypes?.emptyHTML ?? "");
targetRef.current.innerHTML = trustedHtml as string;
Expand Down