Skip to content

Commit e246ddc

Browse files
committed
fix but with cursor jumping to end of input on comment box
1 parent cb2a17e commit e246ddc

File tree

1 file changed

+57
-25
lines changed

1 file changed

+57
-25
lines changed

src/Comments.js

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import MarkdownRenderer from './MarkdownRenderer';
1515
import {Box} from 'grommet/components/Box';
1616
import {Text} from 'grommet/components/Text';
1717
import {TextArea} from 'grommet/components/TextArea';
18-
import {Tabs} from 'grommet/components/Tabs';
19-
import {Tab} from 'grommet/components/Tab';
2018
import {Button} from 'grommet/components/Button';
2119
import {Stack} from 'grommet/components/Stack';
2220
import Comment from './Comment';
@@ -66,6 +64,9 @@ function CommentInput({
6664

6765
const [comment, setComment] = React.useState('');
6866
const [saving, setSaving] = React.useState(false);
67+
const [activeTab, setActiveTab] = React.useState<'write' | 'preview'>(
68+
'write',
69+
);
6970

7071
const onInputChange = React.useCallback(
7172
(e) => {
@@ -98,7 +99,7 @@ function CommentInput({
9899
const count = post.getLinkedRecord('comments')?.getValue('totalCount');
99100
if (Number.isInteger(count)) {
100101
// $FlowFixMe: count has been checked by isInteger
101-
const newCount = count + 1;
102+
const newCount = count + 1;
102103
// eslint-disable-next-line no-unused-expressions
103104
post.getLinkedRecord('comments')?.setValue(newCount, 'totalCount');
104105
}
@@ -156,28 +157,59 @@ function CommentInput({
156157
anchor="center">
157158
<Box style={{opacity: isLoggedIn ? 1 : 0.3}}>
158159
<Box height={{min: 'small'}}>
159-
<Tabs justify="start">
160-
<Tab title={<Text size="small">Write</Text>}>
161-
<Box pad="small" height="small">
162-
<TextArea
163-
focusIndicator={false}
164-
disabled={saving}
165-
placeholder="Leave a comment (supports markdown)"
166-
value={comment}
167-
style={{height: '100%', fontWeight: 'normal'}}
168-
onChange={onInputChange}
169-
/>
170-
</Box>
171-
</Tab>
172-
<Tab title={<Text size="small">Preview</Text>}>
173-
<Box pad="small" height={{min: 'small'}}>
174-
<MarkdownRenderer
175-
trustedInput={false}
176-
source={comment.trim() ? comment : 'Nothing to preview.'}
177-
/>
178-
</Box>
179-
</Tab>
180-
</Tabs>
160+
<Box pad="small" direction="row" gap="medium">
161+
<Button
162+
plain
163+
focusIndicator={false}
164+
label={
165+
<Box
166+
pad={{bottom: 'xsmall'}}
167+
border={{
168+
color: activeTab === 'write' ? 'black' : 'brand',
169+
side: 'bottom',
170+
size: 'small',
171+
}}>
172+
<Text size="small">Write</Text>
173+
</Box>
174+
}
175+
onClick={() => setActiveTab('write')}
176+
/>
177+
<Button
178+
plain
179+
focusIndicator={false}
180+
label={
181+
<Box
182+
pad={{bottom: 'xsmall'}}
183+
border={{
184+
color: activeTab === 'preview' ? 'black' : 'brand',
185+
side: 'bottom',
186+
size: 'small',
187+
}}>
188+
<Text size="small">Preview</Text>
189+
</Box>
190+
}
191+
onClick={() => setActiveTab('preview')}
192+
/>
193+
</Box>
194+
{activeTab === 'write' ? (
195+
<Box pad="small" height="small">
196+
<TextArea
197+
focusIndicator={false}
198+
disabled={saving}
199+
placeholder="Leave a comment (supports markdown)"
200+
value={comment}
201+
style={{height: '100%', fontWeight: 'normal'}}
202+
onChange={onInputChange}
203+
/>
204+
</Box>
205+
) : (
206+
<Box pad="small" height={{min: 'small'}}>
207+
<MarkdownRenderer
208+
trustedInput={false}
209+
source={comment.trim() ? comment : 'Nothing to preview.'}
210+
/>
211+
</Box>
212+
)}
181213
</Box>
182214
<Box>
183215
<Box pad="small" align="end">

0 commit comments

Comments
 (0)