Newer
Older
import React, { useState, useRef, useEffect } from 'react';
import PropTypes from 'prop-types';
import styled, { css } from 'styled-components';
import { isEmpty } from 'lodash';
import { useTranslation } from 'react-i18next';
import { useOnClickOutside } from 'wax-prosemirror-core';
const CommentTitle = styled.input`
background: ${th('colorBackgroundHue')};
border: 3px solid ${th('colorBackgroundTabs')};
font-family: ${th('fontWriting')};
margin-bottom: 10px;
position: relative;
width: 100%;
&:focus {
outline: 1px solid ${th('colorPrimary')};
}
${override('Wax.CommentTitle')}
`;
const Button = styled.button`
border: 0;
border-radius: 5px;
color: gray;
${props => props.disabled && `cursor: not-allowed; opacity: 0.3;`}
const ButtonGroup = styled.div`
> button:not(:last-of-type) {
margin-right: 8px;
}
const StyledMentions = styled(Mentions)`
border: none;
> textarea {
font-size: 14px;
background: ${th('colorBackgroundHue')};
border: 3px solid ${th('colorBackgroundTabs')};
font-family: ${th('fontWriting')};
padding: 2px;
&:focus {
outline: 1px solid ${th('colorPrimary')};
}
&:focus {
outline: 1px solid ${th('colorPrimary')};
}
}
${override('Wax.CommentTextArea')}
`;
const {
className,
isNewComment,
onClickPost,
isReadOnly,
onTextAreaBlur,
const commentTitle = useRef(null);
const [title, setTitle] = useState('');
const ref = useRef(null);
useOnClickOutside(ref, onTextAreaBlur);
setTimeout(() => {
if (commentTitle.current && isNewComment) commentTitle.current.focus();
if (!commentTitle.current && isNewComment) commentInput.current.focus();
});
onClickPost({ title, commentValue });
<Wrapper className={className} ref={ref}>
{isNewComment && showTitle && (
name="title"
onChange={e => {
setTitle(e.target.value);
}}
placeholder={`${
!isEmpty(i18n) && i18n.exists(`Wax.Comments.Write title`)
? t(`Wax.Comments.Write title`)
: 'Write title'
}...`}
ref={commentTitle}
type="text"
value={title}
/>
)}
<StyledMentions
onChange={text => {
setCommentValue(text);
}}
onPressEnter={e => {
const mentionsOptionsEl = document.getElementsByClassName(
'rc-mentions-measure',
);
if (
e.keyCode === 13 &&
!e.shiftKey &&
mentionsOptionsEl.length === 0
) {
placeholder={
isNewComment
? `${
!isEmpty(i18n) && i18n.exists(`Wax.Comments.Write comment`)
? t(`Wax.Comments.Write comment`)
: 'Write comment'
}...`
: `${
!isEmpty(i18n) && i18n.exists(`Wax.Comments.Reply`)
? t(`Wax.Comments.Reply`)
: 'Reply'
}...`
}
{usersMentionList &&
usersMentionList.map(item => (
<Option key={item.id} value={item.displayName}>
{item.displayName}
</Option>
))}
<ActionWrapper>
<ButtonGroup>
<Button
disabled={commentValue.length === 0 || isReadOnly}
primary
type="submit"
>
{!isEmpty(i18n) && i18n.exists(`Wax.Comments.Post`)
? t(`Wax.Comments.Post`)
: 'Post'}
{!isEmpty(i18n) && i18n.exists(`Wax.Comments.Cancel`)
? t(`Wax.Comments.Cancel`)
: 'Cancel'}
</Button>
</ButtonGroup>
</ActionWrapper>
showTitle: PropTypes.bool.isRequired,