| 1 |
/* eslint-disable no-console */ |
| 2 |
import { Fragment, useState } from 'react'; |
| 3 |
import { |
| 4 |
Button, |
| 5 |
Checkbox, |
| 6 |
MenuToggle, |
| 7 |
ToggleGroup, |
| 8 |
ToggleGroupItem, |
| 9 |
ToggleGroupItemProps, |
| 10 |
Toolbar, |
| 11 |
ToolbarContent, |
| 12 |
ToolbarItem |
| 13 |
} from '@patternfly/react-core'; |
| 14 |
import { |
| 15 |
CustomActionsToggleProps, |
| 16 |
fitContent, |
| 17 |
headerCol, |
| 18 |
IActions, |
| 19 |
IActionsResolver, |
| 20 |
TableText |
| 21 |
} from '@patternfly/react-table'; |
| 22 |
import { Table, TableBody, TableHeader, TableProps } from '@patternfly/react-table/deprecated'; |
| 23 |
|
| 24 |
interface Repository { |
| 25 |
name: string; |
| 26 |
branches: string; |
| 27 |
prs: string; |
| 28 |
workspaces: string; |
| 29 |
lastCommit: string; |
| 30 |
singleAction: string; |
| 31 |
} |
| 32 |
|
| 33 |
type ExampleType = 'actions' | 'actionResolver'; |
| 34 |
|
| 35 |
export const LegacyTableActions: React.FunctionComponent = () => { |
| 36 |
// In real usage, this data would come from some external source like an API via props. |
| 37 |
const repositories: Repository[] = [ |
| 38 |
{ name: 'a', branches: 'two', prs: '1', workspaces: 'four', lastCommit: 'five', singleAction: 'Start' }, |
| 39 |
{ name: 'disable actions', branches: 'two', prs: '3', workspaces: 'four', lastCommit: 'five', singleAction: '' }, |
| 40 |
{ name: 'green actions', branches: 'two', prs: '4', workspaces: 'four', lastCommit: 'five', singleAction: 'Start' }, |
| 41 |
{ |
| 42 |
name: 'extra action props', |
| 43 |
branches: 'two', |
| 44 |
prs: '5', |
| 45 |
workspaces: 'four', |
| 46 |
lastCommit: 'five', |
| 47 |
singleAction: 'Start' |
| 48 |
}, |
| 49 |
{ name: 'blue actions', branches: 'two', prs: '6', workspaces: 'four', lastCommit: 'five', singleAction: 'Start' } |
| 50 |
]; |
| 51 |
|
| 52 |
// This state is just for the ToggleGroup in this example and isn't necessary for Table usage. |
| 53 |
const [propToUse, setPropToUse] = useState<ExampleType>('actions'); |
| 54 |
const onPropToUseChange: ToggleGroupItemProps['onChange'] = (event, _isSelected) => { |
| 55 |
const id = event.currentTarget.id; |
| 56 |
setPropToUse(id as ExampleType); |
| 57 |
}; |
| 58 |
|
| 59 |
const [useCustomToggle, setUseCustomToggle] = useState(false); |
| 60 |
|
| 61 |
const customActionsToggle = (props: CustomActionsToggleProps) => ( |
| 62 |
<MenuToggle ref={props.toggleRef} onClick={props.onToggle} isDisabled={props.isDisabled}> |
| 63 |
Actions |
| 64 |
</MenuToggle> |
| 65 |
); |
| 66 |
|
| 67 |
const columns: TableProps['cells'] = [ |
| 68 |
{ title: 'Repositories', cellTransforms: [headerCol('actions')] }, |
| 69 |
'Branches', |
| 70 |
'Pull requests', |
| 71 |
'Workspaces', |
| 72 |
'Last commit', |
| 73 |
{ title: '', dataLabel: 'Action', cellTransforms: [fitContent] } |
| 74 |
]; |
| 75 |
|
| 76 |
const rows: TableProps['rows'] = repositories.map((repo) => { |
| 77 |
let singleActionButton: React.ReactNode = null; |
| 78 |
if (repo.singleAction !== '') { |
| 79 |
singleActionButton = ( |
| 80 |
<TableText> |
| 81 |
<Button variant="secondary">{repo.singleAction}</Button> |
| 82 |
</TableText> |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
const cells = [repo.name, repo.branches, repo.prs, repo.workspaces, repo.lastCommit, singleActionButton]; |
| 87 |
|
| 88 |
// These rows have arbitrary differences for this example, but these could be based on some other conditions |
| 89 |
if (repo.name === 'disable actions') { |
| 90 |
return { cells, disableActions: true }; |
| 91 |
} |
| 92 |
if (repo.name === 'green actions') { |
| 93 |
return { cells, type: 'green' }; |
| 94 |
} |
| 95 |
if (repo.name === 'extra action props') { |
| 96 |
return { cells, actionProps: { 'data-specific-attr': 'some-value' } }; |
| 97 |
} |
| 98 |
if (repo.name === 'blue actions') { |
| 99 |
return { cells, type: 'blue' }; |
| 100 |
} |
| 101 |
return { cells }; |
| 102 |
}); |
| 103 |
|
| 104 |
const actions: IActions = [ |
| 105 |
{ |
| 106 |
title: 'Some action', |
| 107 |
onClick: (_event, rowId, rowData, extra) => console.log('clicked on Some action, on row: ', rowId, rowData, extra) |
| 108 |
}, |
| 109 |
{ |
| 110 |
title: <a href="#">Link action</a> |
| 111 |
}, |
| 112 |
{ |
| 113 |
isSeparator: true |
| 114 |
}, |
| 115 |
{ |
| 116 |
title: 'Third action', |
| 117 |
onClick: (_event, rowId, rowData, extra) => |
| 118 |
console.log('clicked on Third action, on row: ', rowId, rowData, extra) |
| 119 |
} |
| 120 |
]; |
| 121 |
|
| 122 |
/** |
| 123 |
* Use actions or actionResolver, not both |
| 124 |
*/ |
| 125 |
const actionResolver: IActionsResolver = (rowData, { rowIndex }) => { |
| 126 |
if (rowIndex === 1) { |
| 127 |
return []; |
| 128 |
} |
| 129 |
|
| 130 |
const thirdAction: IActions = [ |
| 131 |
{ |
| 132 |
isSeparator: true |
| 133 |
}, |
| 134 |
{ |
| 135 |
title: `${rowData.type} action`, |
| 136 |
onClick: (_event, rowId, rowData, extra) => |
| 137 |
console.log(`clicked on ${rowData.type} action, on row ${rowId} of type ${rowData.type}`, extra) |
| 138 |
} |
| 139 |
]; |
| 140 |
|
| 141 |
return [ |
| 142 |
{ |
| 143 |
title: 'actionResolver action', |
| 144 |
onClick: (_event, rowId, rowData, extra) => |
| 145 |
console.log(`clicked on Some action, on row ${rowId} of type ${rowData.type}`, extra) |
| 146 |
}, |
| 147 |
{ |
| 148 |
title: <div>Another action</div>, |
| 149 |
onClick: (_event, rowId, rowData, extra) => |
| 150 |
console.log(`clicked on Another action, on row ${rowId} of type ${rowData.type}`, extra) |
| 151 |
}, |
| 152 |
...(rowData.type ? thirdAction : []) |
| 153 |
]; |
| 154 |
}; |
| 155 |
|
| 156 |
return ( |
| 157 |
<Fragment> |
| 158 |
<Toolbar> |
| 159 |
<ToolbarContent> |
| 160 |
<ToolbarItem> |
| 161 |
<ToggleGroup aria-label="actions or actionResolver"> |
| 162 |
<ToggleGroupItem |
| 163 |
text="Use actions prop" |
| 164 |
buttonId="actions" |
| 165 |
isSelected={propToUse === 'actions'} |
| 166 |
onChange={onPropToUseChange} |
| 167 |
/> |
| 168 |
<ToggleGroupItem |
| 169 |
text="Use actionResolver prop" |
| 170 |
buttonId="actionResolver" |
| 171 |
isSelected={propToUse === 'actionResolver'} |
| 172 |
onChange={onPropToUseChange} |
| 173 |
/> |
| 174 |
</ToggleGroup> |
| 175 |
</ToolbarItem> |
| 176 |
<ToolbarItem> |
| 177 |
<Checkbox |
| 178 |
label="Use custom actions toggle" |
| 179 |
isChecked={useCustomToggle} |
| 180 |
onChange={(_event, checked) => setUseCustomToggle(checked)} |
| 181 |
aria-label="toggle use of custom actions toggle" |
| 182 |
id="toggle-custom-actions-toggle" |
| 183 |
name="toggle-custom-actions-toggle" |
| 184 |
/> |
| 185 |
</ToolbarItem> |
| 186 |
</ToolbarContent> |
| 187 |
</Toolbar> |
| 188 |
<Table |
| 189 |
aria-label="Actions table" |
| 190 |
cells={columns} |
| 191 |
rows={rows} |
| 192 |
{...(propToUse === 'actions' && { actions })} |
| 193 |
{...(propToUse === 'actionResolver' && { actionResolver })} |
| 194 |
areActionsDisabled={(rowData) => !!rowData.disableActions} |
| 195 |
dropdownPosition="left" |
| 196 |
dropdownDirection="down" |
| 197 |
actionsToggle={useCustomToggle ? customActionsToggle : undefined} |
| 198 |
> |
| 199 |
<TableHeader /> |
| 200 |
<TableBody /> |
| 201 |
</Table> |
| 202 |
</Fragment> |
| 203 |
); |
| 204 |
}; |