forked from BoostIO/BoostNote-App
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add editable item component for all views
Add editable item to Kanban, List, and Table
- Loading branch information
1 parent
b641971
commit 18d96e8
Showing
7 changed files
with
223 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { lngKeys } from '../../lib/i18n/types' | ||
import { mdiDotsVertical, mdiPencilOutline, mdiTrashCanOutline } from '@mdi/js' | ||
import React, { useCallback, useState } from 'react' | ||
import { SerializedDocWithSupplemental } from '../../interfaces/db/doc' | ||
import { useCloudApi } from '../../lib/hooks/useCloudApi' | ||
import { useI18n } from '../../lib/hooks/useI18n' | ||
import { | ||
MenuItem, | ||
MenuTypes, | ||
useContextMenu, | ||
} from '../../../design/lib/stores/contextMenu' | ||
import Icon from '../../../design/components/atoms/Icon' | ||
import styled from '../../../design/lib/styled' | ||
import EditableInput from '../../../design/components/atoms/EditableInput' | ||
|
||
interface ItemProps { | ||
doc: SerializedDocWithSupplemental | ||
teamId?: string | ||
children?: React.ReactNode | ||
} | ||
|
||
const EditableItemContainer = ({ doc, teamId, children }: ItemProps) => { | ||
const [editingItemTitle, setEditingItemTitle] = useState<boolean>(false) | ||
const [showingContextMenuActions, setShowingContextMenuActions] = useState< | ||
boolean | ||
>(false) | ||
|
||
const { updateDoc, deleteDocApi } = useCloudApi() | ||
const { translate } = useI18n() | ||
const { popup } = useContextMenu() | ||
|
||
const updateDocTitle = useCallback( | ||
async (doc, newTitle) => { | ||
await updateDoc(doc, { | ||
workspaceId: doc.workspaceId, | ||
title: newTitle, | ||
}) | ||
setEditingItemTitle(false) | ||
}, | ||
[updateDoc] | ||
) | ||
|
||
const onDocRemove = useCallback( | ||
(doc) => { | ||
if (teamId == null) { | ||
if (doc.teamId != null && doc.teamId != '') { | ||
deleteDocApi({ id: doc.id, teamId: doc.teamId }) | ||
} | ||
return | ||
} | ||
deleteDocApi({ id: doc.id, teamId: teamId }) | ||
}, | ||
[deleteDocApi, teamId] | ||
) | ||
|
||
const openActionMenu: ( | ||
event: React.MouseEvent<HTMLDivElement>, | ||
doc: SerializedDocWithSupplemental | ||
) => void = useCallback( | ||
( | ||
event: React.MouseEvent<HTMLDivElement>, | ||
doc: SerializedDocWithSupplemental | ||
) => { | ||
const editTitleAction: MenuItem = { | ||
icon: <Icon path={mdiPencilOutline} />, | ||
type: MenuTypes.Normal, | ||
label: translate(lngKeys.GeneralEditTitle), | ||
onClick: () => setEditingItemTitle(true), | ||
} | ||
const deleteDocAction: MenuItem = { | ||
icon: <Icon path={mdiTrashCanOutline} />, | ||
type: MenuTypes.Normal, | ||
label: translate(lngKeys.GeneralDelete), | ||
onClick: () => onDocRemove(doc), | ||
} | ||
const actions: MenuItem[] = [editTitleAction, deleteDocAction] | ||
|
||
event.preventDefault() | ||
event.stopPropagation() | ||
popup(event, actions) | ||
}, | ||
[onDocRemove, popup, translate] | ||
) | ||
|
||
return ( | ||
<ItemContainer | ||
onMouseEnter={() => setShowingContextMenuActions(true)} | ||
onMouseLeave={() => setShowingContextMenuActions(false)} | ||
> | ||
{editingItemTitle && ( | ||
<EditableInput | ||
editOnStart={true} | ||
placeholder={'Title...'} | ||
text={doc.title} | ||
onTextChange={(newText) => updateDocTitle(doc, newText)} | ||
/> | ||
)} | ||
|
||
{!editingItemTitle && <>{children}</>} | ||
|
||
{showingContextMenuActions && ( | ||
<div className={'item__container__item__actions'}> | ||
<div | ||
onClick={(event) => openActionMenu(event, doc)} | ||
className='doc__action' | ||
> | ||
<Icon size={20} path={mdiDotsVertical} /> | ||
</div> | ||
</div> | ||
)} | ||
</ItemContainer> | ||
) | ||
} | ||
|
||
const ItemContainer = styled.div` | ||
position: relative; | ||
.item__container__item__actions { | ||
position: absolute; | ||
right: 5px; | ||
z-index: 1; | ||
margin: 0; | ||
top: 50%; | ||
transform: translate(-50%, -50%); | ||
} | ||
` | ||
|
||
export default EditableItemContainer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.