Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

refactor(ui): improved answering questions interaction

robine5ebde028a04
diff --git a/ui/src/components/Editor/index.scss b/ui/src/components/Editor/index.scss
index abbda7cb..ac29b42d 100644
--- a/ui/src/components/Editor/index.scss
+++ b/ui/src/components/Editor/index.scss
@@ -169,7 +169,7 @@
}
}
.content-wrap {
- height: 270px;
+ height: 264px;
}
.CodeMirror {
diff --git a/ui/src/components/TextArea/index.tsx b/ui/src/components/TextArea/index.tsx
index 6dd0a4f7..0d53f8db 100644
--- a/ui/src/components/TextArea/index.tsx
+++ b/ui/src/components/TextArea/index.tsx
@@ -1,7 +1,9 @@
import { FC, useRef, useEffect, memo } from 'react';
import { FormControl, FormControlProps } from 'react-bootstrap';
-const TextArea: FC<FormControlProps> = ({ value, onChange, size }) => {
+const TextArea: FC<
+ FormControlProps & { rows?: number; autoFocus?: boolean }
+> = ({ value, onChange, size, rows = 1, autoFocus = true, ...rest }) => {
const ref = useRef<HTMLTextAreaElement>(null);
const autoGrow = () => {
@@ -21,13 +23,14 @@ const TextArea: FC<FormControlProps> = ({ value, onChange, size }) => {
<FormControl
as="textarea"
className="resize-none font-monospace"
- rows={1}
+ rows={rows}
size={size}
value={value}
onChange={onChange}
- autoFocus
+ autoFocus={autoFocus}
ref={ref}
onInput={autoGrow}
+ {...rest}
/>
);
};
diff --git a/ui/src/pages/Questions/Detail/components/WriteAnswer/index.tsx b/ui/src/pages/Questions/Detail/components/WriteAnswer/index.tsx
index 8d1ef1be..38e4a4ae 100644
--- a/ui/src/pages/Questions/Detail/components/WriteAnswer/index.tsx
+++ b/ui/src/pages/Questions/Detail/components/WriteAnswer/index.tsx
@@ -5,7 +5,7 @@ import { useTranslation } from 'react-i18next';
import { marked } from 'marked';
import classNames from 'classnames';
-import { Editor, Modal } from '@/components';
+import { Editor, Modal, TextArea } from '@/components';
import { FormDataType } from '@/common/interface';
import { postAnswer } from '@/services';
@@ -81,10 +81,13 @@ const Index: FC<Props> = ({ visible = false, data, callback }) => {
handleSubmit();
};
+ const handleFocusForTextArea = () => {
+ setShowEditor(true);
+ };
return (
<Form noValidate className="mt-4">
- {showEditor && (
+ {(!data.answered || showEditor) && (
<Form.Group className="mb-3">
<Form.Label>
<h5>{t('title')}</h5>
@@ -93,28 +96,40 @@ const Index: FC<Props> = ({ visible = false, data, callback }) => {
isInvalid={formData.content.isInvalid}
className="d-none"
/>
- <Editor
- className={classNames(
- 'form-control p-0',
- focusType === 'answer' && 'focus',
- )}
- value={formData.content.value}
- onChange={(val) => {
- setFormData({
- content: {
- value: val,
- isInvalid: false,
- errorMsg: '',
- },
- });
- }}
- onFocus={() => {
- setForceType('answer');
- }}
- onBlur={() => {
- setForceType('');
- }}
- />
+ {!showEditor && !data.answered && (
+ <div className="d-flex">
+ <TextArea
+ className="w-100"
+ rows={12}
+ autoFocus={false}
+ onFocus={handleFocusForTextArea}
+ />
+ </div>
+ )}
+ {showEditor && (
+ <Editor
+ className={classNames(
+ 'form-control p-0',
+ focusType === 'answer' && 'focus',
+ )}
+ value={formData.content.value}
+ onChange={(val) => {
+ setFormData({
+ content: {
+ value: val,
+ isInvalid: false,
+ errorMsg: '',
+ },
+ });
+ }}
+ onFocus={() => {
+ setForceType('answer');
+ }}
+ onBlur={() => {
+ setForceType('');
+ }}
+ />
+ )}
<Form.Control.Feedback type="invalid">
{formData.content.errorMsg}
@@ -122,7 +137,11 @@ const Index: FC<Props> = ({ visible = false, data, callback }) => {
</Form.Group>
)}
- <Button onClick={clickBtn}>{t('btn_name')}</Button>
+ {data.answered && !showEditor ? (
+ <Button onClick={clickBtn}>{t('add_another_answer')}</Button>
+ ) : (
+ <Button onClick={clickBtn}>{t('btn_name')}</Button>
+ )}
</Form>
);
};
diff --git a/ui/src/pages/Questions/Detail/index.tsx b/ui/src/pages/Questions/Detail/index.tsx
index 249f1f39..bb3fe99e 100644
--- a/ui/src/pages/Questions/Detail/index.tsx
+++ b/ui/src/pages/Questions/Detail/index.tsx
@@ -128,6 +128,13 @@ const Index = () => {
count: answers.count + 1,
list: [...answers.list, obj],
});
+
+ if (question) {
+ setQuestion({
+ ...question,
+ answered: true,
+ });
+ }
};
useEffect(() => {