Skip to content

Commit 029f939

Browse files
committed
fix(context): Move context docs
1 parent ee9377a commit 029f939

File tree

2 files changed

+14
-44
lines changed

2 files changed

+14
-44
lines changed

packages/module/patternfly-docs/content/extensions/data-view/examples/Context/Context.md renamed to packages/module/patternfly-docs/content/extensions/data-view/examples/EventsContext/EventsContext.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ section: extensions
55
subsection: Data view
66
# Sidenav secondary level section
77
# should be the same for all markdown files
8-
id: Context
8+
id: Events context
99
# Tab (react | react-demos | html | html-demos | design-guidelines | accessibility)
1010
source: react
1111
# If you use typescript, the name of the interface to display props for
1212
# These are found through the sourceProps function provided in patternfly-docs.source.js
1313
sortValue: 3
14-
sourceLink: https://github.com/patternfly/react-data-view/blob/main/packages/module/patternfly-docs/content/extensions/data-view/examples/Events/Events.md
14+
sourceLink: https://github.com/patternfly/react-data-view/blob/main/packages/module/patternfly-docs/content/extensions/data-view/examples/EventsContext/EventsContext.md
1515
---
16-
import { useState, useEffect, useRef } from 'react';
16+
import { useState, useEffect, useRef, useMemo } from 'react';
1717
import { Table, Tbody, Th, Thead, Tr, Td } from '@patternfly/react-table';
1818
import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView';
19-
import { useDataViewEventsContext, DataViewProvider, EventTypes } from '@patternfly/react-data-view/dist/dynamic/DataViewEventsContext';
19+
import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
20+
import { useDataViewEventsContext, DataViewEventsContext, DataViewEventsProvider, EventTypes } from '@patternfly/react-data-view/dist/dynamic/DataViewEventsContext';
2021
import { Drawer, DrawerContent, DrawerContentBody } from '@patternfly/react-core';
2122

22-
The **data view** context provides a way to share data across the entire data view tree without having to pass props manually at every level. It also provides a way of listening to the data view events from the outside of the component.
23+
The **data view events context** provides a way of listening to the data view events from the outside of the component.
2324

24-
25-
### Events sharing example
25+
### Row click subscription example
2626
The following example demonstrates how to use the `DataViewEventsContext` to manage shared state and handle events. The `DataViewEventsProvider` is used to wrap components that need access to the shared context. This example illustrates how to set up a layout that listens for data view row click events and displays detailed information about the selected row in a [drawer component](/components/drawer).
2727

2828

packages/module/patternfly-docs/content/extensions/data-view/examples/Context/EventsExample.tsx renamed to packages/module/patternfly-docs/content/extensions/data-view/examples/EventsContext/EventsExample.tsx

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React, { useEffect, useState, useRef } from 'react';
1+
import React, { useEffect, useState, useRef, useMemo } from 'react';
22
import { Drawer, DrawerActions, DrawerCloseButton, DrawerContent, DrawerContentBody, DrawerHead, DrawerPanelContent, Title, Text } from '@patternfly/react-core';
3-
import { Table, Tbody, Th, Thead, Tr, Td } from '@patternfly/react-table';
43
import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView';
4+
import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
55
import { DataViewEventsProvider, EventTypes, useDataViewEventsContext } from '@patternfly/react-data-view/dist/dynamic/DataViewEventsContext';
66

77
interface Repository {
@@ -21,13 +21,7 @@ const repositories: Repository[] = [
2121
{ name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
2222
];
2323

24-
const cols: Record<keyof Repository, string> = {
25-
name: 'Repositories',
26-
branches: 'Branches',
27-
prs: 'Pull requests',
28-
workspaces: 'Workspaces',
29-
lastCommit: 'Last commit'
30-
};
24+
const columns = [ 'Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit' ];
3125

3226
const ouiaId = 'ContextExample';
3327

@@ -73,40 +67,16 @@ interface RepositoriesTableProps {
7367
const RepositoriesTable: React.FunctionComponent<RepositoriesTableProps> = ({ selectedRepo = undefined }) => {
7468
const { trigger } = useDataViewEventsContext();
7569

70+
// eslint-disable-next-line react-hooks/exhaustive-deps
71+
const rows = useMemo(() => repositories.map(repo => ({ row: Object.values(repo), props: { isClickable: true, onRowClick: () => handleRowClick(selectedRepo?.name === repo.name ? undefined : repo), isRowSelected: selectedRepo?.name === repo.name } } )), [ selectedRepo?.name ]);
72+
7673
const handleRowClick = (repo: Repository | undefined) => {
7774
trigger(EventTypes.rowClick, repo);
7875
};
7976

8077
return (
8178
<DataView>
82-
<Table aria-label="Repositories table" ouiaId={ouiaId}>
83-
<Thead data-ouia-component-id={`${ouiaId}-thead`}>
84-
<Tr ouiaId={`${ouiaId}-tr-head`}>
85-
{Object.values(cols).map((column, index) => (
86-
<Th key={index} data-ouia-component-id={`${ouiaId}-th-${index}`}>
87-
{column}
88-
</Th>
89-
))}
90-
</Tr>
91-
</Thead>
92-
<Tbody>
93-
{repositories.map((repo, rowIndex) => (
94-
<Tr
95-
isClickable
96-
key={repo.name}
97-
ouiaId={`${ouiaId}-tr-${rowIndex}`}
98-
onRowClick={() => handleRowClick(selectedRepo?.name === repo.name ? undefined : repo)}
99-
isRowSelected={selectedRepo?.name === repo.name}
100-
>
101-
{Object.keys(cols).map((column, colIndex) => (
102-
<Td key={colIndex} data-ouia-component-id={`${ouiaId}-td-${rowIndex}-${colIndex}`}>
103-
{repo[column as keyof Repository]}
104-
</Td>
105-
))}
106-
</Tr>
107-
))}
108-
</Tbody>
109-
</Table>
79+
<DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={rows} />
11080
</DataView>
11181
);
11282
};

0 commit comments

Comments
 (0)