| 1 |
import { Fragment, useEffect, useState } from 'react'; |
| 2 |
import { |
| 3 |
Avatar, |
| 4 |
Brand, |
| 5 |
Breadcrumb, |
| 6 |
BreadcrumbItem, |
| 7 |
Button, |
| 8 |
ButtonVariant, |
| 9 |
Content, |
| 10 |
Divider, |
| 11 |
Dropdown, |
| 12 |
DropdownGroup, |
| 13 |
DropdownItem, |
| 14 |
DropdownList, |
| 15 |
Label, |
| 16 |
LabelStatus, |
| 17 |
Masthead, |
| 18 |
MastheadMain, |
| 19 |
MastheadLogo, |
| 20 |
MastheadContent, |
| 21 |
MastheadBrand, |
| 22 |
MastheadToggle, |
| 23 |
MenuToggle, |
| 24 |
Nav, |
| 25 |
NavItem, |
| 26 |
NavList, |
| 27 |
Page, |
| 28 |
PageBreadcrumb, |
| 29 |
PageSection, |
| 30 |
PageSidebar, |
| 31 |
PageSidebarBody, |
| 32 |
PageToggleButton, |
| 33 |
Pagination, |
| 34 |
PaginationVariant, |
| 35 |
Toolbar, |
| 36 |
ToolbarContent, |
| 37 |
ToolbarGroup, |
| 38 |
ToolbarItem, |
| 39 |
Truncate |
| 40 |
} from '@patternfly/react-core'; |
| 41 |
|
| 42 |
import { Table, Thead, Tr, Th, Tbody, Td } from '@patternfly/react-table'; |
| 43 |
import translationsEn from './examples/translations.en.json'; |
| 44 |
import translationsHe from './examples/translations.he.json'; |
| 45 |
import AlignRightIcon from '@patternfly/react-icons/dist/esm/icons/align-right-icon'; |
| 46 |
import ToolsIcon from '@patternfly/react-icons/dist/esm/icons/tools-icon'; |
| 47 |
import RhUiRunningIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-running-icon'; |
| 48 |
import RhUiClockFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-clock-fill-icon'; |
| 49 |
import RhUiStopCircleFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-stop-circle-fill-icon'; |
| 50 |
import pfLogo from '@patternfly/react-core/src/demos/assets/pf-logo.svg'; |
| 51 |
import RhUiSettingsFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-settings-fill-icon'; |
| 52 |
import RhUiQuestionMarkCircleFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-question-mark-circle-fill-icon'; |
| 53 |
import HelpIcon from '@patternfly/react-icons/dist/esm/icons/help-icon'; |
| 54 |
import RhUiEllipsisVerticalFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-ellipsis-vertical-fill-icon'; |
| 55 |
import imgAvatar from '@patternfly/react-core/src/components/assets/avatarImg.svg'; |
| 56 |
import { rows } from '@patternfly/react-core/dist/esm/demos/sampleDataRTL'; |
| 57 |
|
| 58 |
interface Row { |
| 59 |
name: string; |
| 60 |
status: string; |
| 61 |
location: string; |
| 62 |
lastModified: string; |
| 63 |
url: string; |
| 64 |
} |
| 65 |
|
| 66 |
interface Translation { |
| 67 |
[key: string]: any; |
| 68 |
} |
| 69 |
|
| 70 |
export const PaginatedTableAction: React.FunctionComponent = () => { |
| 71 |
const [translation, setTranslation] = useState<Translation>(translationsEn); |
| 72 |
const [page, setPage] = useState<number>(1); |
| 73 |
const [perPage, setPerPage] = useState<number>(10); |
| 74 |
|
| 75 |
const columns = [ |
| 76 |
translation.table.columns.servers, |
| 77 |
translation.table.columns.status, |
| 78 |
translation.table.columns.location, |
| 79 |
translation.table.columns.modified, |
| 80 |
translation.table.columns.url |
| 81 |
]; |
| 82 |
|
| 83 |
const generateRowsFromTranslation = (translation: Translation): Row[] => |
| 84 |
rows.map((row, index) => ({ |
| 85 |
name: `${translation.table.rows.node}${index + 1}`, |
| 86 |
status: translation.table.rows.status[row.status], |
| 87 |
location: translation.table.rows.locations[row.location], |
| 88 |
lastModified: translation.table.rows.lastModified[row.lastModified], |
| 89 |
url: `http://www.redhat.com/en/office-locations/node${index + 1}` |
| 90 |
})); |
| 91 |
|
| 92 |
const [paginatedRows, setPaginatedRows] = useState<Row[]>(rows.slice(0, 10)); |
| 93 |
const [isDirRTL, setIsDirRTL] = useState<boolean>(false); |
| 94 |
|
| 95 |
const capitalize = (input: string) => input[0].toUpperCase() + input.substring(1); |
| 96 |
|
| 97 |
const switchTranslation = () => { |
| 98 |
setIsDirRTL((prevIsDirRTL) => !prevIsDirRTL); |
| 99 |
setTranslation((prevTranslation: string) => (prevTranslation === translationsEn ? translationsHe : translationsEn)); |
| 100 |
}; |
| 101 |
|
| 102 |
useEffect(() => { |
| 103 |
const fullRows = generateRowsFromTranslation(translation); |
| 104 |
setPaginatedRows(fullRows.slice((page - 1) * perPage, page * perPage)); |
| 105 |
}, [translation]); |
| 106 |
|
| 107 |
useEffect(() => { |
| 108 |
const html = document.querySelector('html'); |
| 109 |
html.dir = isDirRTL ? 'rtl' : 'ltr'; |
| 110 |
}, [isDirRTL]); |
| 111 |
|
| 112 |
// Pagination logic |
| 113 |
|
| 114 |
const handleSetPage = ( |
| 115 |
_evt: React.MouseEvent | React.KeyboardEvent | MouseEvent, |
| 116 |
newPage: number, |
| 117 |
_perPage: number | undefined, |
| 118 |
startIdx: number | undefined, |
| 119 |
endIdx: number | undefined |
| 120 |
) => { |
| 121 |
const fullRows = generateRowsFromTranslation(translation); |
| 122 |
setPaginatedRows(fullRows.slice(startIdx, endIdx)); |
| 123 |
setPage(newPage); |
| 124 |
}; |
| 125 |
|
| 126 |
const handlePerPageSelect = ( |
| 127 |
_evt: React.MouseEvent | React.KeyboardEvent | MouseEvent, |
| 128 |
newPerPage: number, |
| 129 |
_newPage: number, |
| 130 |
startIdx: number | undefined, |
| 131 |
endIdx: number | undefined |
| 132 |
) => { |
| 133 |
const fullRows = generateRowsFromTranslation(translation); |
| 134 |
setPaginatedRows(fullRows.slice(startIdx, endIdx)); |
| 135 |
setPerPage(newPerPage); |
| 136 |
}; |
| 137 |
|
| 138 |
const renderPagination = (variant: PaginationVariant) => { |
| 139 |
const { pagination } = translation; |
| 140 |
|
| 141 |
return ( |
| 142 |
<Pagination |
| 143 |
itemCount={rows.length} |
| 144 |
page={page} |
| 145 |
perPage={perPage} |
| 146 |
onSetPage={handleSetPage} |
| 147 |
onPerPageSelect={handlePerPageSelect} |
| 148 |
variant={variant} |
| 149 |
titles={{ |
| 150 |
paginationAriaLabel: pagination?.[`${variant}VariantAriaLabel`] || `${variant} pagination`, |
| 151 |
ofWord: pagination?.ofWord, |
| 152 |
items: pagination?.items, |
| 153 |
perPageSuffix: pagination?.perPageSuffix, |
| 154 |
toNextPageAriaLabel: pagination?.toNextPageAriaLabel, |
| 155 |
toPreviousPageAriaLabel: pagination?.toPreviousPageAriaLabel, |
| 156 |
toFirstPageAriaLabel: pagination?.toFirstPageAriaLabel, |
| 157 |
toLastPageAriaLabel: pagination?.perPageSuffix, |
| 158 |
currPageAriaLabel: pagination?.currPageAriaLabel |
| 159 |
}} |
| 160 |
/> |
| 161 |
); |
| 162 |
}; |
| 163 |
|
| 164 |
const breadcrumbItems = { |
| 165 |
home: { |
| 166 |
url: '#home' |
| 167 |
}, |
| 168 |
category: { |
| 169 |
url: '#category' |
| 170 |
}, |
| 171 |
subCategory: { |
| 172 |
url: 'sub-category' |
| 173 |
} |
| 174 |
}; |
| 175 |
|
| 176 |
const renderLabel = (labelText: string) => { |
| 177 |
switch (labelText) { |
| 178 |
case 'Running': |
| 179 |
case 'רץ': |
| 180 |
return ( |
| 181 |
<Label status={LabelStatus.success} icon={<RhUiRunningIcon className="pf-v6-m-mirror-inline-rtl" />}> |
| 182 |
{translation.table.rows.status.running} |
| 183 |
</Label> |
| 184 |
); |
| 185 |
case 'Stopped': |
| 186 |
case 'עצר': |
| 187 |
return ( |
| 188 |
<Label status={LabelStatus.danger} icon={<RhUiStopCircleFillIcon className="pf-v6-m-mirror-inline-rtl" />}> |
| 189 |
{translation.table.rows.status.stopped} |
| 190 |
</Label> |
| 191 |
); |
| 192 |
case 'Needs maintenance': |
| 193 |
case 'זקוק לתחזוקה': |
| 194 |
return ( |
| 195 |
<Label status={LabelStatus.info} icon={<ToolsIcon />}> |
| 196 |
{translation.table.rows.status.needsMaintenance} |
| 197 |
</Label> |
| 198 |
); |
| 199 |
case 'Down': |
| 200 |
case 'מטה': |
| 201 |
return ( |
| 202 |
<Label status={LabelStatus.warning} icon={<RhUiClockFillIcon />}> |
| 203 |
{translation.table.rows.status.down} |
| 204 |
</Label> |
| 205 |
); |
| 206 |
} |
| 207 |
}; |
| 208 |
|
| 209 |
const toolbarItems = ( |
| 210 |
<Fragment> |
| 211 |
<Toolbar id="rtl-paginated-table"> |
| 212 |
<ToolbarContent> |
| 213 |
<ToolbarItem> |
| 214 |
<Button |
| 215 |
variant="primary" |
| 216 |
icon={<AlignRightIcon className="pf-v6-m-mirror-inline-rtl" />} |
| 217 |
iconPosition="end" |
| 218 |
onClick={switchTranslation} |
| 219 |
> |
| 220 |
{translation.switchBtn} |
| 221 |
</Button> |
| 222 |
</ToolbarItem> |
| 223 |
<ToolbarItem variant="pagination">{renderPagination(PaginationVariant.top)}</ToolbarItem> |
| 224 |
</ToolbarContent> |
| 225 |
</Toolbar> |
| 226 |
</Fragment> |
| 227 |
); |
| 228 |
|
| 229 |
const pageNav = ( |
| 230 |
<Nav aria-label={translation.nav.ariaLabel || undefined}> |
| 231 |
<NavList> |
| 232 |
<NavItem itemId={0} isActive to="#system-panel"> |
| 233 |
{translation.nav.systemPanel} |
| 234 |
</NavItem> |
| 235 |
<NavItem itemId={1} to="#policy"> |
| 236 |
{translation.nav.policy} |
| 237 |
</NavItem> |
| 238 |
<NavItem itemId={2} to="#auth"> |
| 239 |
{translation.nav.authentication} |
| 240 |
</NavItem> |
| 241 |
<NavItem itemId={3} to="#network"> |
| 242 |
{translation.nav.networkServices} |
| 243 |
</NavItem> |
| 244 |
<NavItem itemId={4} to="#server"> |
| 245 |
{translation.nav.server} |
| 246 |
</NavItem> |
| 247 |
</NavList> |
| 248 |
</Nav> |
| 249 |
); |
| 250 |
|
| 251 |
const sidebar = ( |
| 252 |
<PageSidebar> |
| 253 |
<PageSidebarBody>{pageNav}</PageSidebarBody> |
| 254 |
</PageSidebar> |
| 255 |
); |
| 256 |
|
| 257 |
const [isDropdownOpen, setIsDropdownOpen] = useState<boolean>(false); |
| 258 |
const [isKebabDropdownOpen, setIsKebabDropdownOpen] = useState<boolean>(false); |
| 259 |
const [isFullKebabDropdownOpen, setIsFullKebabDropdownOpen] = useState<boolean>(false); |
| 260 |
|
| 261 |
const kebabDropdownItems = ( |
| 262 |
<> |
| 263 |
<DropdownItem icon={<RhUiSettingsFillIcon />}>{translation.kebabDropdown.settings}</DropdownItem> |
| 264 |
<DropdownItem icon={<HelpIcon />}>{translation.kebabDropdown.help}</DropdownItem> |
| 265 |
</> |
| 266 |
); |
| 267 |
|
| 268 |
const userDropdownItems = ( |
| 269 |
<> |
| 270 |
<DropdownItem key="group 2 profile">{translation.userDropdown.myProfile}</DropdownItem> |
| 271 |
<DropdownItem key="group 2 user">{translation.userDropdown.userManagement}</DropdownItem> |
| 272 |
<DropdownItem key="group 2 logout">{translation.userDropdown.logout}</DropdownItem> |
| 273 |
</> |
| 274 |
); |
| 275 |
|
| 276 |
const onDropdownToggle = () => { |
| 277 |
setIsDropdownOpen(!isDropdownOpen); |
| 278 |
}; |
| 279 |
|
| 280 |
const onDropdownSelect = () => { |
| 281 |
setIsDropdownOpen(false); |
| 282 |
}; |
| 283 |
|
| 284 |
const onKebabDropdownToggle = () => { |
| 285 |
setIsKebabDropdownOpen(!isKebabDropdownOpen); |
| 286 |
}; |
| 287 |
|
| 288 |
const onKebabDropdownSelect = () => { |
| 289 |
setIsKebabDropdownOpen(false); |
| 290 |
}; |
| 291 |
|
| 292 |
const onFullKebabToggle = () => { |
| 293 |
setIsFullKebabDropdownOpen(!isFullKebabDropdownOpen); |
| 294 |
}; |
| 295 |
|
| 296 |
const onFullKebabSelect = () => { |
| 297 |
setIsFullKebabDropdownOpen(false); |
| 298 |
}; |
| 299 |
|
| 300 |
const masthead = ( |
| 301 |
<Masthead> |
| 302 |
<MastheadMain> |
| 303 |
<MastheadToggle> |
| 304 |
<PageToggleButton isHamburgerButton aria-label={translation.mastheadToggleAriaLabel} /> |
| 305 |
</MastheadToggle> |
| 306 |
<MastheadBrand> |
| 307 |
<MastheadLogo dir="ltr"> |
| 308 |
<Brand src={pfLogo} alt="PatternFly" heights={{ default: '36px' }} /> |
| 309 |
{translation.brandLanguage && <span className="brand-language">{translation.brandLanguage}</span>} |
| 310 |
</MastheadLogo> |
| 311 |
</MastheadBrand> |
| 312 |
</MastheadMain> |
| 313 |
<MastheadContent> |
| 314 |
<Toolbar id="toolbar" isStatic> |
| 315 |
<ToolbarContent> |
| 316 |
<ToolbarGroup |
| 317 |
variant="action-group-plain" |
| 318 |
align={{ default: 'alignEnd' }} |
| 319 |
gap={{ default: 'gapNone', md: 'gapMd' }} |
| 320 |
> |
| 321 |
<ToolbarGroup variant="action-group-plain" visibility={{ default: 'hidden', lg: 'visible' }}> |
| 322 |
<ToolbarItem> |
| 323 |
<Button aria-label={translation.kebabDropdown.settings} isSettings variant="plain" /> |
| 324 |
</ToolbarItem> |
| 325 |
<ToolbarItem> |
| 326 |
<Button |
| 327 |
aria-label={translation.kebabDropdown.help} |
| 328 |
variant={ButtonVariant.plain} |
| 329 |
icon={<RhUiQuestionMarkCircleFillIcon />} |
| 330 |
/> |
| 331 |
</ToolbarItem> |
| 332 |
</ToolbarGroup> |
| 333 |
<ToolbarItem visibility={{ default: 'hidden', md: 'visible', lg: 'hidden' }}> |
| 334 |
<Dropdown |
| 335 |
isOpen={isKebabDropdownOpen} |
| 336 |
onSelect={onKebabDropdownSelect} |
| 337 |
onOpenChange={setIsKebabDropdownOpen} |
| 338 |
popperProps={{ position: 'right' }} |
| 339 |
toggle={(toggleRef) => ( |
| 340 |
<MenuToggle |
| 341 |
ref={toggleRef} |
| 342 |
isExpanded={isKebabDropdownOpen} |
| 343 |
onClick={onKebabDropdownToggle} |
| 344 |
variant="plain" |
| 345 |
aria-label={translation.kebabDropdown.settingsAndHelp} |
| 346 |
icon={<RhUiEllipsisVerticalFillIcon />} |
| 347 |
/> |
| 348 |
)} |
| 349 |
> |
| 350 |
<DropdownList>{kebabDropdownItems}</DropdownList> |
| 351 |
</Dropdown> |
| 352 |
</ToolbarItem> |
| 353 |
<ToolbarItem visibility={{ md: 'hidden' }}> |
| 354 |
<Dropdown |
| 355 |
isOpen={isFullKebabDropdownOpen} |
| 356 |
onSelect={onFullKebabSelect} |
| 357 |
onOpenChange={setIsFullKebabDropdownOpen} |
| 358 |
popperProps={{ position: 'right' }} |
| 359 |
toggle={(toggleRef) => ( |
| 360 |
<MenuToggle |
| 361 |
ref={toggleRef} |
| 362 |
isExpanded={isFullKebabDropdownOpen} |
| 363 |
onClick={onFullKebabToggle} |
| 364 |
variant="plain" |
| 365 |
aria-label={translation.kebabAndUserDropdown.toolbarMenuAriaLabel} |
| 366 |
icon={<RhUiEllipsisVerticalFillIcon />} |
| 367 |
/> |
| 368 |
)} |
| 369 |
> |
| 370 |
<DropdownGroup key="group 2" aria-label={translation.kebabAndUserDropdown.groupAriaLabel}> |
| 371 |
<DropdownList>{userDropdownItems}</DropdownList> |
| 372 |
</DropdownGroup> |
| 373 |
<Divider /> |
| 374 |
<DropdownList>{kebabDropdownItems}</DropdownList> |
| 375 |
</Dropdown> |
| 376 |
</ToolbarItem> |
| 377 |
</ToolbarGroup> |
| 378 |
<ToolbarItem visibility={{ default: 'hidden', md: 'visible' }}> |
| 379 |
<Dropdown |
| 380 |
isOpen={isDropdownOpen} |
| 381 |
onSelect={onDropdownSelect} |
| 382 |
onOpenChange={setIsDropdownOpen} |
| 383 |
popperProps={{ position: 'right' }} |
| 384 |
toggle={(toggleRef) => ( |
| 385 |
<MenuToggle |
| 386 |
ref={toggleRef} |
| 387 |
isExpanded={isDropdownOpen} |
| 388 |
onClick={onDropdownToggle} |
| 389 |
icon={<Avatar src={imgAvatar} alt="" size="sm" />} |
| 390 |
> |
| 391 |
{translation.username} |
| 392 |
</MenuToggle> |
| 393 |
)} |
| 394 |
> |
| 395 |
<DropdownList>{userDropdownItems}</DropdownList> |
| 396 |
</Dropdown> |
| 397 |
</ToolbarItem> |
| 398 |
</ToolbarContent> |
| 399 |
</Toolbar> |
| 400 |
</MastheadContent> |
| 401 |
</Masthead> |
| 402 |
); |
| 403 |
|
| 404 |
const breadcrumbItemsLength = Object.keys(breadcrumbItems).length; |
| 405 |
|
| 406 |
return ( |
| 407 |
<Fragment> |
| 408 |
<Page sidebar={sidebar} masthead={masthead} isManagedSidebar> |
| 409 |
<PageBreadcrumb> |
| 410 |
<Breadcrumb aria-label={translation.breadcrumbs.ariaLabel || undefined}> |
| 411 |
{Object.keys(breadcrumbItems).map((key: string, idx: number, arr: string[]) => ( |
| 412 |
<BreadcrumbItem key={idx} isActive={arr.length - 1 === idx} to={`${breadcrumbItems[key].url}`}> |
| 413 |
{translation.breadcrumbs[key]} |
| 414 |
{breadcrumbItemsLength} |
| 415 |
</BreadcrumbItem> |
| 416 |
))} |
| 417 |
</Breadcrumb> |
| 418 |
</PageBreadcrumb> |
| 419 |
<PageSection variant="light" aria-labelledby="main-title"> |
| 420 |
<Content> |
| 421 |
<h1 id="main-title">{translation.title}</h1> |
| 422 |
<p>{translation.body}</p> |
| 423 |
</Content> |
| 424 |
</PageSection> |
| 425 |
<PageSection> |
| 426 |
{toolbarItems} |
| 427 |
<Table variant="compact" aria-label={translation.table.ariaLabel}> |
| 428 |
<Thead> |
| 429 |
<Tr> |
| 430 |
{columns.map((column, columnIndex) => ( |
| 431 |
<Th key={columnIndex}>{column}</Th> |
| 432 |
))} |
| 433 |
</Tr> |
| 434 |
</Thead> |
| 435 |
<Tbody> |
| 436 |
{paginatedRows.map((row: Row, rowIndex: number) => ( |
| 437 |
<Tr key={rowIndex}> |
| 438 |
<> |
| 439 |
{Object.entries(row).map(([key, value]) => { |
| 440 |
if (key === 'status') { |
| 441 |
return ( |
| 442 |
<Td key={key} width={15} dataLabel="Status"> |
| 443 |
{renderLabel(value)} |
| 444 |
</Td> |
| 445 |
); |
| 446 |
} else if (key === 'url') { |
| 447 |
return ( |
| 448 |
// Passing dir="rtl" forces truncation at the start of the URL, |
| 449 |
// resulting in the unique portion being visible regardless of language |
| 450 |
<Td key={key} dataLabel="URL" width={15}> |
| 451 |
<a href="#"> |
| 452 |
<Truncate content={row.url} position={isDirRTL ? 'end' : 'start'} /> |
| 453 |
</a> |
| 454 |
</Td> |
| 455 |
); |
| 456 |
} else { |
| 457 |
return ( |
| 458 |
<Td key={key} dataLabel={key === 'lastModified' ? 'Last modified' : capitalize(key)}> |
| 459 |
{value} |
| 460 |
</Td> |
| 461 |
); |
| 462 |
} |
| 463 |
})} |
| 464 |
</> |
| 465 |
</Tr> |
| 466 |
))} |
| 467 |
</Tbody> |
| 468 |
</Table> |
| 469 |
{renderPagination(PaginationVariant.bottom)} |
| 470 |
</PageSection> |
| 471 |
</Page> |
| 472 |
</Fragment> |
| 473 |
); |
| 474 |
}; |