import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import { debounce } from 'lodash'; import classnames from 'classnames'; export default class SignatureRequestMessage extends PureComponent { static propTypes = { data: PropTypes.object.isRequired, onMessageScrolled: PropTypes.func, setMessageRootRef: PropTypes.func, messageRootRef: PropTypes.object, messageIsScrollable: PropTypes.bool, }; static contextTypes = { t: PropTypes.func, }; state = { messageIsScrolled: false, }; setMessageIsScrolled = () => { if (!this.props.messageRootRef || this.state.messageIsScrolled) { return; } const { scrollTop, offsetHeight, scrollHeight } = this.props.messageRootRef; const isAtBottom = Math.round(scrollTop) + offsetHeight >= scrollHeight; if (isAtBottom) { this.setState({ messageIsScrolled: true }); this.props.onMessageScrolled(); } }; onScroll = debounce(this.setMessageIsScrolled, 25); renderNode(data) { return (
{Object.entries(data).map(([label, value], i) => (
{label}:{' '} {typeof value === 'object' && value !== null ? ( this.renderNode(value) ) : ( {`${value}`} )}
))}
); } renderScrollButton() { return (
{ this.setState({ messageIsScrolled: true }); this.props.onMessageScrolled(); this.props.messageRootRef.scrollTo( 0, this.props.messageRootRef.scrollHeight, ); }} className="signature-request-message__scroll-button" data-testid="signature-request-scroll-button" >
); } render() { const { data, messageIsScrollable } = this.props; return (
{messageIsScrollable ? this.renderScrollButton() : null}
{this.context.t('signatureRequest1')}
{this.renderNode(data)}
); } }