-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathuseRowActions.js
More file actions
129 lines (122 loc) · 3.75 KB
/
useRowActions.js
File metadata and controls
129 lines (122 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import React, { useMemo } from "react"
import Flex from "@/components/templates/flex"
import Action from "../components/action"
export const supportedRowActions = {
delete: {
icon: "trashcan",
confirmation: true,
tooltipText: "Delete",
confirmationTitle: "Delete Row",
confirmationMessage: "You are about to delete a row, are you sure?",
confirmLabel: "Yes",
declineLabel: "No",
actionButtonDirection: "reverse",
disabledTooltipText: "Delete is disabled",
},
edit: { icon: "pencilOutline", confirmation: false, tooltipText: "Edit" },
replace: {
icon: "refresh",
confirmation: true,
tooltipText: "Replace",
confirmationTitle: "Replace Row",
confirmationMessage: "You are about to replace a row, are you sure you want to continue?",
confirmLabel: "Yes",
declineLabel: "No",
actionButtonDirection: "reverse",
},
info: { icon: "information", confirmation: false, tooltipText: "Information" },
toggleAlarm: { icon: "alarm_off", confirmation: false, tooltipText: "Turn of Alarms" },
userSettings: { icon: "user", confirmation: false, tooltipText: "User Settings" },
remove: {
icon: "removeNode",
confirmation: true,
actionButtonDirection: "reverse",
confirmLabel: "Yes",
declineLabel: "No",
},
goto: {
icon: "nav_arrow_goto",
confirmation: false,
tooltipText: "Go to",
},
}
const MIN_ACTIONS_COLUMN_WIDTH = 70
export default (rowActions, { testPrefix, tableMeta } = {}) => {
const availableRowActions = useMemo(
() =>
Object.keys(rowActions).reduce((acc, key) => {
const defaultAction = supportedRowActions[key] || supportedRowActions.delete
const currentAction = rowActions[key]
acc.push({
id: key,
...defaultAction,
...currentAction,
})
return acc
}, []),
[rowActions]
)
if (availableRowActions.length < 1) return null
return useMemo(
() => ({
id: "actions",
enableResizing: false,
header: "Actions",
headerString: "Actions",
cell: ({ row, table }) => (
<Flex
data-testid="action-cell"
height="100%"
gap={2}
flex={false}
onClick={e => e.stopPropagation()}
>
{availableRowActions.map(
({
id,
handleAction,
isDisabled,
isVisible = true,
dataGa,
disabledTooltipText,
tooltipText,
...rest
}) => (
<Action
{...rest}
disabled={typeof isDisabled === "function" ? isDisabled(row.original) : isDisabled}
visible={typeof isVisible === "function" ? isVisible(row.original) : isVisible}
dataGa={typeof dataGa === "function" ? dataGa(row.original) : dataGa}
disabledTooltipText={
typeof disabledTooltipText === "function"
? disabledTooltipText(row.original)
: disabledTooltipText
}
tooltipText={
typeof tooltipText === "function" ? tooltipText(row.original) : tooltipText
}
key={id}
id={id}
handleAction={() => handleAction(row.original, table)}
testPrefix={testPrefix}
currentRow={row}
/>
)
)}
</Flex>
),
enableColumnFilter: false,
enableSorting: false,
tableMeta,
size: Math.max(MIN_ACTIONS_COLUMN_WIDTH, availableRowActions.length * 35),
align: "end",
notFlex: true,
meta: {
cellStyles: {
justifyContent: "end",
},
},
}),
[availableRowActions]
)
}