Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLU-817: [TILES-ATOMIC-INCREMENT-1] allowing copying of row id in tiles #817

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
useContext,
useState,
} from 'react'
import { BiTrash } from 'react-icons/bi'
import { BiCopy, BiTrash } from 'react-icons/bi'
import {
Icon,
Menu,
Expand All @@ -14,6 +14,7 @@ import {
MenuList,
Portal,
} from '@chakra-ui/react'
import { useToast } from '@opengovsg/design-system-react'

import DeleteRowsModal from '../components/TableFooter/DeleteRowsModal'

Expand Down Expand Up @@ -46,7 +47,13 @@ export const ContextMenuContextProvider = ({
children,
}: ContextMenuContextProviderProps) => {
const [position, setPosition] = useState<[number, number] | null>(null)
const [rowIdsToDelete, setRowIdsToDelete] = useState<string[]>([])
const [rowIdsSelected, setRowIdsSelected] = useState<string[]>([])
const toast = useToast({
title: 'Row ID copied to clipboard',
description: 'You can use this in the Update single row step',
status: 'success',
isClosable: true,
})

const rowsSelected = Object.keys(rowSelection)

Expand All @@ -57,14 +64,22 @@ export const ContextMenuContextProvider = ({
setPosition(pos)
if (!rowsSelected.includes(rowId)) {
clearRowSelection()
setRowIdsToDelete([rowId])
setRowIdsSelected([rowId])
} else {
setRowIdsToDelete(rowsSelected)
setRowIdsSelected(rowsSelected)
}
},
[clearRowSelection, rowsSelected],
)

const onRowIdCopy = useCallback(
(rowId: string) => {
navigator.clipboard.writeText(rowId)
toast()
},
[toast],
)

return (
<ContextMenuContext.Provider
value={{
Expand All @@ -90,7 +105,17 @@ export const ContextMenuContextProvider = ({
left={position[0]}
top={position[1]}
/>
<MenuList m={0}>
<MenuList m={0} gap={1} display="flex" flexDir="column">
{rowsSelected.length <= 1 && (
<MenuItem
icon={<Icon as={BiCopy} boxSize={5} />}
display="flex"
alignItems="center"
onClick={() => onRowIdCopy(rowIdsSelected[0])}
>
Copy row ID
</MenuItem>
)}
<MenuItem
icon={<Icon as={BiTrash} boxSize={5} />}
color="interaction.critical.default"
Expand All @@ -106,7 +131,7 @@ export const ContextMenuContextProvider = ({
<DeleteRowsModal
removeRows={removeRows}
onClose={() => setIsDeleteModalOpen(false)}
rowIdsToDelete={rowIdsToDelete}
rowIdsToDelete={rowIdsSelected}
/>
)}
</ContextMenuContext.Provider>
Expand Down
Loading