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')};
}
/* stylelint-disable-next-line order/properties-alphabetical-order */
${override('Wax.CommentTitle')}
`;
const Button = styled.button`
border: 0;
border-radius: 5px;
color: gray;
/* stylelint-disable-next-line order/properties-alphabetical-order */
${props => props.disabled && `cursor: not-allowed; opacity: 0.3;`}
const ButtonGroup = styled.div`
> button:not(:last-of-type) {
margin-right: 8px;
}
> textarea {
background: ${th('colorBackgroundHue')};
border: 3px solid ${th('colorBackgroundTabs')};
font-family: ${th('fontWriting')};
padding: 2px;
&:focus {
outline: 1px solid ${th('colorPrimary')};
}
}
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}>
<TextWrapper>
{isNewComment && showTitle && (
<CommentTitle
name="title"
onChange={e => {
setTitle(e.target.value);
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
) {
e.preventDefault();
if (commentValue) handleSubmit(e);
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
}}
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'
}...`
}
ref={commentInput}
rows="4"
value={commentValue}
>
{usersMentionList &&
usersMentionList.map(item => (
<Option key={item.id} value={item.displayName}>
{item.displayName}
</Option>
))}
</StyledMentions>
</TextWrapper>
<ActionWrapper>
<ButtonGroup>
<Button
disabled={commentValue.length === 0 || isReadOnly}
onClick={handleSubmit}
primary
type="submit"
{!isEmpty(i18n) && i18n.exists(`Wax.Comments.Post`)
? t(`Wax.Comments.Post`)
: 'Post'}
</Button>
<Button disabled={commentValue.length === 0} onClick={resetValue}>
{!isEmpty(i18n) && i18n.exists(`Wax.Comments.Cancel`)
? t(`Wax.Comments.Cancel`)
: 'Cancel'}
</Button>
</ButtonGroup>
</ActionWrapper>
showTitle: PropTypes.bool.isRequired,