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

Props to disable interactivity #988

Merged
merged 4 commits into from
Aug 16, 2018
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Changed
- Update `PULL_REQUEST_TEMPLATE.md` [@corinagum](https://github.com/corinagum) in PR [#1065](https://github.com/Microsoft/BotFramework-WebChat/pull/1065)
- Add `role === 'user'` to `fromMe` check in [#1053](https://github.com/Microsoft/BotFramework-WebChat/pull/1053)
- Add `disabled` props to disable all controls in PR [#988](https://github.com/Microsoft/BotFramework-WebChat/pull/988)

## [0.14.1] - 2018-07-31
### Added
Expand Down
29 changes: 19 additions & 10 deletions src/ActivityView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,34 @@ import { FormattedText } from './FormattedText';
import { FormatState, SizeState } from './Store';

const Attachments = (props: {
attachments: Attachment[],
attachmentLayout: AttachmentLayout,
format: FormatState,
size: SizeState,
onCardAction: IDoCardAction,
onImageLoad: () => void
attachmentLayout: AttachmentLayout;
attachments: Attachment[];
disabled: boolean;
format: FormatState;
onCardAction: IDoCardAction;
onImageLoad: () => void;
size: SizeState;
}) => {
const { attachments, attachmentLayout, ...otherProps } = props;

if (!attachments || attachments.length === 0) {
return null;
}

return attachmentLayout === 'carousel' ?
<Carousel
attachments={ attachments }
disabled={ props.disabled }
{ ...otherProps }
/>
:
<div className="wc-list">
{ attachments.map((attachment, index) =>
<AttachmentView
key={ index }
attachment={ attachment }
format={ props.format }
disabled={ props.disabled }
key={ index }
onCardAction={ props.onCardAction }
onImageLoad={ props.onImageLoad }
/>
Expand All @@ -38,11 +43,12 @@ const Attachments = (props: {
};

export interface ActivityViewProps {
format: FormatState;
size: SizeState;
activity: Activity;
disabled: boolean;
format: FormatState;
onCardAction: IDoCardAction;
onImageLoad: () => void;
size: SizeState;
}

export class ActivityView extends React.Component<ActivityViewProps, {}> {
Expand All @@ -58,11 +64,13 @@ export class ActivityView extends React.Component<ActivityViewProps, {}> {
// if it's a carousel and the size changed, re-render
|| (this.props.activity.type === 'message'
&& this.props.activity.attachmentLayout === 'carousel'
&& this.props.size !== nextProps.size);
&& this.props.size !== nextProps.size)
|| !this.props.disabled !== !nextProps.disabled;
}

render() {
const { activity, ...props } = this.props;

switch (activity.type) {
case 'message':
return (
Expand All @@ -75,6 +83,7 @@ export class ActivityView extends React.Component<ActivityViewProps, {}> {
<Attachments
attachments={ activity.attachments }
attachmentLayout={ activity.attachmentLayout }
disabled={ props.disabled }
format={ props.format }
onCardAction={ props.onCardAction }
onImageLoad={ props.onImageLoad }
Expand Down
35 changes: 28 additions & 7 deletions src/AdaptiveCardContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { AdaptiveCardsState, ChatState } from './Store';

export interface Props {
className?: string;
disabled?: boolean;
hostConfig: HostConfig;
jsonCard?: IAdaptiveCard;
nativeCard?: AdaptiveCard;
Expand Down Expand Up @@ -90,7 +91,7 @@ class AdaptiveCardContainer extends React.Component<Props, State> {
}

private onClick(e: React.MouseEvent<HTMLElement>) {
if (!this.props.onClick) {
if (this.props.disabled || !this.props.onClick) {
return;
}

Expand All @@ -112,7 +113,11 @@ class AdaptiveCardContainer extends React.Component<Props, State> {
}

private onExecuteAction(action: Action) {
if (action instanceof OpenUrlAction) {
if (this.props.disabled) {
return;
} else if (action instanceof OpenUrlAction) {
// TODO: Should we let this one bubble to Chat.tsx?
// this.props.onCardAction('openUrl', action.url) might work
window.open(action.url);
} else if (action instanceof SubmitAction) {
if (action.data !== undefined) {
Expand All @@ -135,6 +140,7 @@ class AdaptiveCardContainer extends React.Component<Props, State> {
if (
prevProps.hostConfig !== this.props.hostConfig
|| prevProps.jsonCard !== this.props.jsonCard
|| !prevProps.disabled !== !this.props.disabled
|| prevProps.nativeCard !== this.props.nativeCard
) {
this.unmountAdaptiveCards();
Expand Down Expand Up @@ -166,7 +172,7 @@ class AdaptiveCardContainer extends React.Component<Props, State> {
errors = adaptiveCard.validate();
}

adaptiveCard.onExecuteAction = action => this.onExecuteAction(action);
adaptiveCard.onExecuteAction = this.onExecuteAction.bind(this);

if (errors.length === 0) {
let renderedCard: HTMLElement;
Expand All @@ -187,11 +193,28 @@ class AdaptiveCardContainer extends React.Component<Props, State> {
}

if (renderedCard) {
if (this.props.disabled) {
const hyperlinks = renderedCard.querySelectorAll('a');
const inputs = renderedCard.querySelectorAll('button, input, select, textarea');

[].forEach.call(inputs, (input: HTMLInputElement) => {
input.disabled = true;
});

[].forEach.call(hyperlinks, (hyperlink: HTMLAnchorElement) => {
hyperlink.addEventListener('click', event => {
event.preventDefault();
event.stopImmediatePropagation();
event.stopPropagation();
});
});
}

if (this.props.onImageLoad) {
const imgs = renderedCard.querySelectorAll('img');

if (imgs && imgs.length > 0) {
Array.prototype.forEach.call(imgs, (img: HTMLImageElement) => {
[].forEach.call(imgs, (img: HTMLImageElement) => {
img.addEventListener('load', this.handleImageLoad);
});
}
Expand All @@ -201,9 +224,7 @@ class AdaptiveCardContainer extends React.Component<Props, State> {

return;
}
}

if (errors.length > 0) {
} else {
console.log('Error(s) rendering AdaptiveCard:');
errors.forEach(e => console.log(e.message));
this.setState({ errors: errors.map(e => e.message) });
Expand Down
Loading