2019-11-07 21:36:16 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
export type PropsType = {
|
|
|
|
id: string;
|
|
|
|
conversationId: string;
|
|
|
|
isSelected: boolean;
|
2019-11-21 19:16:06 +00:00
|
|
|
selectMessage?: (messageId: string, conversationId: string) => unknown;
|
2019-11-07 21:36:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export class InlineNotificationWrapper extends React.Component<PropsType> {
|
|
|
|
public focusRef: React.RefObject<HTMLDivElement> = React.createRef();
|
|
|
|
|
|
|
|
public setFocus = () => {
|
2019-11-14 23:54:50 +00:00
|
|
|
const container = this.focusRef.current;
|
|
|
|
|
|
|
|
if (container && !container.contains(document.activeElement)) {
|
|
|
|
container.focus();
|
2019-11-07 21:36:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-21 19:16:06 +00:00
|
|
|
public handleFocus = () => {
|
|
|
|
// @ts-ignore
|
|
|
|
if (window.getInteractionMode() === 'keyboard') {
|
|
|
|
this.setSelected();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-07 21:36:16 +00:00
|
|
|
public setSelected = () => {
|
|
|
|
const { id, conversationId, selectMessage } = this.props;
|
|
|
|
|
2019-11-21 19:16:06 +00:00
|
|
|
if (selectMessage) {
|
|
|
|
selectMessage(id, conversationId);
|
|
|
|
}
|
2019-11-07 21:36:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public componentDidMount() {
|
|
|
|
const { isSelected } = this.props;
|
|
|
|
if (isSelected) {
|
|
|
|
this.setFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public componentDidUpdate(prevProps: PropsType) {
|
|
|
|
if (!prevProps.isSelected && this.props.isSelected) {
|
|
|
|
this.setFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public render() {
|
|
|
|
const { children } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="module-inline-notification-wrapper"
|
|
|
|
tabIndex={0}
|
|
|
|
ref={this.focusRef}
|
2019-11-21 19:16:06 +00:00
|
|
|
onFocus={this.handleFocus}
|
2019-11-07 21:36:16 +00:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|