Debian Codegraph @

node/@patternfly/react-core/src/demos/examples/Tabs/ModalTabs.tsx

raw
1 import { Fragment, useCallback, useState } from 'react';
2 import { DashboardWrapper } from '@patternfly/react-core/dist/js/demos/DashboardWrapper';
3 import {
4 PageSection,
5 Content,
6 Gallery,
7 Card,
8 CardHeader,
9 CardBody,
10 CardTitle,
11 Modal,
12 ModalBody,
13 ModalHeader,
14 Tab,
15 Tabs,
16 TabTitleText,
17 List,
18 ListItem,
19 Grid,
20 GridItem,
21 TabContent
22 } from '@patternfly/react-core';
23
24 interface Product {
25 id: string;
26 name: string;
27 description: string;
28 }
29
30 const products: Product[] = [
31 {
32 id: 'pf-card',
33 name: 'PatternFly',
34 description: 'PatternFly is a community project that promotes design commonality and improves user experience.'
35 },
36 {
37 id: 'mq-card',
38 name: 'ActiveMQ',
39 description:
40 'The ActiveMQ component allows messages to be sent to a JMS Queue or Topic; or messages to be consumed from a JMS Queue or Topic using Apache ActiveMQ.'
41 },
42 {
43 id: 'apache-card',
44 name: 'Apache Spark',
45 description: 'This documentation page covers the Apache Spark component for the Apache Camel.'
46 }
47 ];
48
49 export const ModalTabs: React.FunctionComponent = () => {
50 const [isModalOpen, setIsModalOpen] = useState(true);
51 const [selectedProduct, setSelectedProduct] = useState<Product | undefined>(products[0]);
52 const [activeTabKey, setActiveTabKey] = useState<string | number>(0);
53
54 const onCardClick = useCallback(
55 (product: Product) => () => {
56 setSelectedProduct(product);
57 setIsModalOpen(true);
58 },
59 []
60 );
61
62 const closeModal = useCallback(() => {
63 setSelectedProduct(undefined);
64 setIsModalOpen(false);
65 setActiveTabKey(0);
66 }, []);
67
68 const onTabSelect = useCallback(
69 (_event: React.MouseEvent<HTMLElement, MouseEvent>, tabIndex: string | number) => setActiveTabKey(tabIndex),
70 []
71 );
72
73 return (
74 <Fragment>
75 <DashboardWrapper mainContainerId="main-content-card-view-default-nav">
76 <PageSection aria-labelledby="projects-heading">
77 <Content>
78 <h1 id="projects-heading">Projects</h1>
79 <p>Click any project card to view tabs within modals.</p>
80 </Content>
81 </PageSection>
82 <PageSection isFilled aria-label="Selectable card gallery">
83 <Gallery hasGutter aria-label="Selectable card container">
84 {products.map((product) => (
85 <Card
86 isSelectable
87 isSelected={selectedProduct?.id === product.id}
88 isCompact
89 key={product.id}
90 id={product.name.replace(/ /g, '-')}
91 >
92 <CardHeader
93 selectableActions={{
94 selectableActionId: product.id,
95 selectableActionAriaLabelledby: 'single-selectable-card-example-1',
96 name: 'single-selectable-card-example',
97 variant: 'single',
98 onChange: onCardClick(product),
99 hasNoOffset: true
100 }}
101 >
102 <CardTitle>{product.name}</CardTitle>
103 </CardHeader>
104 <CardBody>{product.description}</CardBody>
105 </Card>
106 ))}
107 </Gallery>
108 </PageSection>
109 </DashboardWrapper>
110
111 {selectedProduct && (
112 <Modal
113 variant="small"
114 isOpen={isModalOpen}
115 onClose={closeModal}
116 ouiaId="TabsModal"
117 aria-labelledby="tabs-modal-title"
118 aria-describedby="tabs-modal-box-body-basic"
119 >
120 <ModalHeader title={selectedProduct.name} labelId="tabs-modal-title" />
121 <ModalBody id="tabs-modal-box-body-basic">
122 <Grid hasGutter>
123 <GridItem>
124 <Tabs activeKey={activeTabKey} onSelect={onTabSelect}>
125 <Tab eventKey={0} tabContentId="details-tab" title={<TabTitleText>Details</TabTitleText>} />
126 <Tab eventKey={1} tabContentId="doc-tab" title={<TabTitleText>Documentation</TabTitleText>} />
127 </Tabs>
128 </GridItem>
129 <GridItem>
130 <TabContent eventKey={0} id="details-tab" hidden={activeTabKey !== 0}>
131 {selectedProduct.description}
132 </TabContent>
133 <TabContent eventKey={1} id="doc-tab" hidden={activeTabKey !== 1}>
134 <List>
135 <ListItem>
136 <a>Doc link 1</a>
137 </ListItem>
138 <ListItem>
139 <a>Doc link 2</a>
140 </ListItem>
141 <ListItem>
142 <a>Doc link 3</a>
143 </ListItem>
144 </List>
145 </TabContent>
146 </GridItem>
147 </Grid>
148 </ModalBody>
149 </Modal>
150 )}
151 </Fragment>
152 );
153 };