Skip to content

Commit fd57521

Browse files
authored
PLU-817: [TILES-ATOMIC-INCREMENT-1] allowing copying of row id in tiles (#817)
### TL;DR Added ability to copy row IDs from the context menu in the table view ![image.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/ycT2Xb5nBEjN2kf2gy95/97f1d9c5-c030-4a21-9eae-8492981e80a5.png) ### What changed? - Added a "Copy row ID" option to the context menu when a single row is selected - Displays a toast notification when a row ID is copied - Renamed internal state variable from `rowIdsToDelete` to `rowIdsSelected` for better clarity - Added BiCopy icon from react-icons library ### How to test? 1. Navigate to tile 2. Right-click on a single row 3. Click "Copy row ID" from the context menu 4. Verify that: - A success toast appears - The row ID is copied to your clipboard - The option only appears when a single row is selected ### Why make this change? To improve user experience by making it easier to copy row IDs, which can be used in the "Update single row" step. This eliminates the need for Find single row if the user knows which row to update. This is mainly use for counters.
1 parent 4d96b4a commit fd57521

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

packages/frontend/src/pages/Tile/contexts/ContextMenuContext.tsx

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
useContext,
66
useState,
77
} from 'react'
8-
import { BiTrash } from 'react-icons/bi'
8+
import { BiCopy, BiTrash } from 'react-icons/bi'
99
import {
1010
Icon,
1111
Menu,
@@ -14,6 +14,7 @@ import {
1414
MenuList,
1515
Portal,
1616
} from '@chakra-ui/react'
17+
import { useToast } from '@opengovsg/design-system-react'
1718

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

@@ -46,7 +47,13 @@ export const ContextMenuContextProvider = ({
4647
children,
4748
}: ContextMenuContextProviderProps) => {
4849
const [position, setPosition] = useState<[number, number] | null>(null)
49-
const [rowIdsToDelete, setRowIdsToDelete] = useState<string[]>([])
50+
const [rowIdsSelected, setRowIdsSelected] = useState<string[]>([])
51+
const toast = useToast({
52+
title: 'Row ID copied to clipboard',
53+
description: 'You can use this in the Update single row step',
54+
status: 'success',
55+
isClosable: true,
56+
})
5057

5158
const rowsSelected = Object.keys(rowSelection)
5259

@@ -57,14 +64,22 @@ export const ContextMenuContextProvider = ({
5764
setPosition(pos)
5865
if (!rowsSelected.includes(rowId)) {
5966
clearRowSelection()
60-
setRowIdsToDelete([rowId])
67+
setRowIdsSelected([rowId])
6168
} else {
62-
setRowIdsToDelete(rowsSelected)
69+
setRowIdsSelected(rowsSelected)
6370
}
6471
},
6572
[clearRowSelection, rowsSelected],
6673
)
6774

75+
const onRowIdCopy = useCallback(
76+
(rowId: string) => {
77+
navigator.clipboard.writeText(rowId)
78+
toast()
79+
},
80+
[toast],
81+
)
82+
6883
return (
6984
<ContextMenuContext.Provider
7085
value={{
@@ -90,7 +105,17 @@ export const ContextMenuContextProvider = ({
90105
left={position[0]}
91106
top={position[1]}
92107
/>
93-
<MenuList m={0}>
108+
<MenuList m={0} gap={1} display="flex" flexDir="column">
109+
{rowsSelected.length <= 1 && (
110+
<MenuItem
111+
icon={<Icon as={BiCopy} boxSize={5} />}
112+
display="flex"
113+
alignItems="center"
114+
onClick={() => onRowIdCopy(rowIdsSelected[0])}
115+
>
116+
Copy row ID
117+
</MenuItem>
118+
)}
94119
<MenuItem
95120
icon={<Icon as={BiTrash} boxSize={5} />}
96121
color="interaction.critical.default"
@@ -106,7 +131,7 @@ export const ContextMenuContextProvider = ({
106131
<DeleteRowsModal
107132
removeRows={removeRows}
108133
onClose={() => setIsDeleteModalOpen(false)}
109-
rowIdsToDelete={rowIdsToDelete}
134+
rowIdsToDelete={rowIdsSelected}
110135
/>
111136
)}
112137
</ContextMenuContext.Provider>

0 commit comments

Comments
 (0)