Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(Tabs): converted tabs & tables demo to use inputs for selection #9395

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 21 additions & 43 deletions packages/react-core/src/demos/examples/Tabs/TabsAndTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,12 @@ export const TablesAndTabs = () => {
lastCommit: 'Last commit'
};

const [selectedRepoNames, setSelectedRepoNames] = React.useState<string[]>([]);
const setRepoSelected = (event: React.FormEvent<HTMLInputElement>, repo: Repository, isSelecting = true) => {
setSelectedRepoNames((prevSelected) => {
const otherSelectedRepoNames = prevSelected.filter((r) => r !== repo.name);
return isSelecting ? [...otherSelectedRepoNames, repo.name] : otherSelectedRepoNames;
});
event.stopPropagation();
const [selectedRepoName, setSelectedRepoName] = React.useState<string>(repositories[0].name);
const isRepoSelected = (repo: Repository) => repo.name === selectedRepoName;
const setRepoSelected = (_event: React.FormEvent<HTMLInputElement>, repo: Repository) => {
setSelectedRepoName(repo.name);
setIsExpanded(true);
};
const onSelectAll = (isSelecting = true) => setSelectedRepoNames(isSelecting ? repositories.map((r) => r.name) : []);
const allRowsSelected = selectedRepoNames.length === repositories.length;
const isRepoSelected = (repo: Repository) => selectedRepoNames.includes(repo.name);

const [rowClicked, setRowClicked] = React.useState<string>('Node 1');
const isRowClicked = (repo: Repository) => rowClicked === repo.name;

const defaultActions: IAction[] = [
{
Expand Down Expand Up @@ -155,8 +147,6 @@ export const TablesAndTabs = () => {
const shouldFocus = [ArrowDown, ArrowUp, Enter, Space].includes(event.key);

if (shouldToggle) {
event.preventDefault();
event.stopPropagation();
onToggle(event);
}

Expand All @@ -167,6 +157,11 @@ export const TablesAndTabs = () => {
}
};

const handleDrawerClose = () => {
setIsExpanded(false);
setSelectedRepoName('');
};

const customActionsToggle = (props: CustomActionsToggleProps, toggleName: string) => (
<MenuToggle
isDisabled={props.isDisabled}
Expand Down Expand Up @@ -234,12 +229,7 @@ export const TablesAndTabs = () => {
<Table aria-label="`Composable` table">
<Thead noWrap>
<Tr>
<Th
select={{
onSelect: (_event, isSelecting) => onSelectAll(isSelecting),
isSelected: allRowsSelected
}}
/>
<Th />
<Th>{columnNames.name}</Th>
<Th>{columnNames.branches}</Th>
<Th>{columnNames.prs}</Th>
Expand All @@ -249,29 +239,21 @@ export const TablesAndTabs = () => {
</Thead>
<Tbody>
{repositories.map((repo, rowIndex) => (
<Tr
key={repo.name}
onRowClick={(event) => {
if ((event?.target as HTMLInputElement).type !== 'checkbox') {
setRowClicked(rowClicked === repo.name ? '' : repo.name);
setIsExpanded(!isRowClicked(repo));
}
}}
isHoverable
isRowSelected={repo.name === rowClicked}
>
<Tr key={repo.name}>
<Td
key={`${rowIndex}_0`}
select={{
rowIndex,
onSelect: (event, isSelected) => setRepoSelected(event, repo, isSelected),
isSelected: isRepoSelected(repo)
onSelect: (event) => setRepoSelected(event, repo),
isSelected: isRepoSelected(repo),
variant: 'radio'
}}
/>
<Td dataLabel={columnNames.name}>
{repo.name}
<div>
<a href="#">siemur/test-space</a>
{/** Preventing default behavior for demo purposes only */}
<a onClick={(event) => event.preventDefault()} href="#">siemur/test-space</a>
</div>
</Td>
<Td dataLabel={columnNames.branches}>
Expand Down Expand Up @@ -316,21 +298,17 @@ export const TablesAndTabs = () => {
<DrawerPanelContent widths={{ default: 'width_33', xl: 'width_33' }}>
<DrawerHead>
<DrawerActions>
<DrawerCloseButton
onClick={() => {
setRowClicked('');
setIsExpanded(false);
}}
/>
<DrawerCloseButton onClick={handleDrawerClose} />
</DrawerActions>
<Flex spaceItems={{ default: 'spaceItemsSm' }} direction={{ default: 'column' }}>
<FlexItem>
<Title headingLevel="h2" size="lg">
{rowClicked}
{selectedRepoName}
</Title>
</FlexItem>
<FlexItem>
<a href="#">siemur/test-space</a>
{/** Preventing default behavior for demo purposes only */}
<a onClick={(event) => event.preventDefault()} href="#">siemur/test-space</a>
</FlexItem>
</Flex>
</DrawerHead>
Expand Down