Skip to content

Commit 7c978e0

Browse files
Rokt33rDavy-c
authored andcommitted
Extract new folder row and change blur behavior
1 parent f508da9 commit 7c978e0

2 files changed

Lines changed: 127 additions & 84 deletions

File tree

src/cloud/components/Views/Table/TableViewContentManager.tsx

Lines changed: 8 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import { FormSelectOption } from '../../../../design/components/molecules/Form/a
5858
import FormInput from '../../../../design/components/molecules/Form/atoms/FormInput'
5959
import { useCloudApi } from '../../../lib/hooks/useCloudApi'
6060
import Spinner from '../../../../design/components/atoms/Spinner'
61+
import TableViewContentManagerNewFolderRow from './TableViewContentmanagerNewFolderRow'
6162

6263
interface ContentManagerProps {
6364
team: SerializedTeam
@@ -85,7 +86,7 @@ const TableViewContentManager = ({
8586
}: ContentManagerProps) => {
8687
const { translate } = useI18n()
8788
const { openContextModal, closeAllModals } = useModal()
88-
const { createFolder, createDoc } = useCloudApi()
89+
const { createDoc } = useCloudApi()
8990
const { push } = useRouter()
9091
const { preferences, setPreferences } = usePreferences()
9192
const [order, setOrder] = useState<typeof sortingOrders[number]['value']>(
@@ -254,37 +255,6 @@ const TableViewContentManager = ({
254255
[setPreferences]
255256
)
256257

257-
const [newFolderRowState, setNewFolderRowState] = useState<
258-
'idle' | 'editing' | 'submitting'
259-
>('idle')
260-
const [newFolderName, setNewFolderName] = useState('')
261-
const compositionStateRef = useRef(false)
262-
const newFolderInputRef = useRef<HTMLInputElement>(null)
263-
264-
useEffect(() => {
265-
if (newFolderInputRef.current != null && newFolderRowState === 'editing') {
266-
newFolderInputRef.current.focus()
267-
}
268-
}, [newFolderRowState])
269-
270-
const createNewFolder = useCallback(async () => {
271-
setNewFolderRowState('submitting')
272-
if (currentWorkspaceId != null) {
273-
await createFolder(
274-
team,
275-
{
276-
folderName: newFolderName,
277-
description: '',
278-
workspaceId: currentWorkspaceId,
279-
parentFolderId: currentFolderId,
280-
},
281-
{ skipRedirect: true }
282-
)
283-
}
284-
setNewFolderName('')
285-
setNewFolderRowState('idle')
286-
}, [currentWorkspaceId, currentFolderId, createFolder, team, newFolderName])
287-
288258
const [newDocRowState, setNewDocRowState] = useState<
289259
'idle' | 'editing' | 'submitting'
290260
>('idle')
@@ -585,58 +555,12 @@ const TableViewContentManager = ({
585555
))}
586556

587557
{currentWorkspaceId != null && (
588-
<TableContentManagerRow className='content__manager--no-border'>
589-
{newFolderRowState === 'idle' ? (
590-
<Button
591-
className='content__manager--no-padding'
592-
onClick={() => {
593-
setNewFolderRowState('editing')
594-
}}
595-
variant='transparent'
596-
iconPath={mdiPlus}
597-
>
598-
{translate(lngKeys.ModalsCreateNewFolder)}
599-
</Button>
600-
) : newFolderRowState === 'editing' ? (
601-
<FormInput
602-
ref={newFolderInputRef}
603-
value={newFolderName}
604-
onChange={(event) => {
605-
setNewFolderName(event.target.value)
606-
}}
607-
onCompositionStart={() => {
608-
compositionStateRef.current = true
609-
}}
610-
onCompositionEnd={() => {
611-
compositionStateRef.current = false
612-
if (newFolderInputRef.current != null) {
613-
newFolderInputRef.current.focus()
614-
}
615-
}}
616-
onKeyPress={(event) => {
617-
if (compositionStateRef.current) {
618-
return
619-
}
620-
switch (event.key) {
621-
case 'Escape':
622-
event.preventDefault()
623-
setNewFolderRowState('idle')
624-
setNewFolderName('')
625-
return
626-
case 'Enter':
627-
event.preventDefault()
628-
createNewFolder()
629-
return
630-
}
631-
}}
632-
onBlur={() => {
633-
createNewFolder()
634-
}}
635-
/>
636-
) : (
637-
<Spinner />
638-
)}
639-
</TableContentManagerRow>
558+
<TableViewContentManagerNewFolderRow
559+
className='content__manager--no-border'
560+
team={team}
561+
folderId={currentFolderId}
562+
workspaceId={currentWorkspaceId}
563+
/>
640564
)}
641565
</>
642566
)}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import React, { useState, useRef, useEffect, useCallback } from 'react'
2+
import { mdiPlus } from '@mdi/js'
3+
import Button from '../../../../design/components/atoms/Button'
4+
import TableContentManagerRow from './TableContentManagerRow'
5+
import { useCloudApi } from '../../../lib/hooks/useCloudApi'
6+
import { SerializedTeam } from '../../../interfaces/db/team'
7+
import FormInput from '../../../../design/components/molecules/Form/atoms/FormInput'
8+
import Spinner from '../../../../design/components/atoms/Spinner'
9+
import { useI18n } from '../../../lib/hooks/useI18n'
10+
import { lngKeys } from '../../../lib/i18n/types'
11+
12+
interface TableViewContentManagerNewFolderRowProps {
13+
team: SerializedTeam
14+
workspaceId: string
15+
folderId?: string
16+
className?: string
17+
}
18+
19+
const TableViewContentManagerNewFolderRow = ({
20+
team,
21+
workspaceId,
22+
folderId,
23+
className,
24+
}: TableViewContentManagerNewFolderRowProps) => {
25+
const [newFolderRowState, setNewFolderRowState] = useState<
26+
'idle' | 'editing' | 'submitting'
27+
>('idle')
28+
29+
const { translate } = useI18n()
30+
const { createFolder } = useCloudApi()
31+
const [newFolderName, setNewFolderName] = useState('')
32+
const compositionStateRef = useRef(false)
33+
const newFolderInputRef = useRef<HTMLInputElement>(null)
34+
35+
useEffect(() => {
36+
if (newFolderInputRef.current != null && newFolderRowState === 'editing') {
37+
newFolderInputRef.current.focus()
38+
}
39+
}, [newFolderRowState])
40+
41+
const createNewFolder = useCallback(async () => {
42+
setNewFolderRowState('submitting')
43+
if (workspaceId != null) {
44+
await createFolder(
45+
team,
46+
{
47+
folderName: newFolderName,
48+
description: '',
49+
workspaceId: workspaceId,
50+
parentFolderId: folderId,
51+
},
52+
{ skipRedirect: true }
53+
)
54+
}
55+
setNewFolderName('')
56+
setNewFolderRowState('idle')
57+
}, [workspaceId, folderId, createFolder, team, newFolderName])
58+
59+
const cancelEditing = useCallback(() => {
60+
setNewFolderRowState('idle')
61+
setNewFolderName('')
62+
}, [])
63+
64+
return (
65+
<TableContentManagerRow className={className}>
66+
{newFolderRowState === 'idle' ? (
67+
<Button
68+
className='content__manager--no-padding'
69+
onClick={() => {
70+
setNewFolderRowState('editing')
71+
}}
72+
variant='transparent'
73+
iconPath={mdiPlus}
74+
>
75+
{translate(lngKeys.ModalsCreateNewFolder)}
76+
</Button>
77+
) : newFolderRowState === 'editing' ? (
78+
<FormInput
79+
ref={newFolderInputRef}
80+
value={newFolderName}
81+
onChange={(event) => {
82+
setNewFolderName(event.target.value)
83+
}}
84+
onCompositionStart={() => {
85+
compositionStateRef.current = true
86+
}}
87+
onCompositionEnd={() => {
88+
compositionStateRef.current = false
89+
if (newFolderInputRef.current != null) {
90+
newFolderInputRef.current.focus()
91+
}
92+
}}
93+
onKeyPress={(event) => {
94+
if (compositionStateRef.current) {
95+
return
96+
}
97+
switch (event.key) {
98+
case 'Escape':
99+
event.preventDefault()
100+
cancelEditing()
101+
return
102+
case 'Enter':
103+
event.preventDefault()
104+
createNewFolder()
105+
return
106+
}
107+
}}
108+
onBlur={() => {
109+
cancelEditing()
110+
}}
111+
/>
112+
) : (
113+
<Spinner />
114+
)}
115+
</TableContentManagerRow>
116+
)
117+
}
118+
119+
export default TableViewContentManagerNewFolderRow

0 commit comments

Comments
 (0)