Debian Codegraph @

node/@patternfly/react-core/src/components/Card/examples/CardClickableSelectable.tsx

raw
1 import { useState } from 'react';
2 import { Card, CardHeader, CardTitle, CardBody, Button, Checkbox, Gallery } from '@patternfly/react-core';
3
4 export const CardClickable: React.FunctionComponent = () => {
5 const [isChecked1, setIsChecked1] = useState(false);
6 const [isChecked2, setIsChecked2] = useState(false);
7 const [isChecked3, setIsChecked3] = useState(false);
8 const [isClicked, setIsClicked] = useState(false);
9 const [isSecondary, setIsSecondary] = useState<boolean>(false);
10
11 const id1 = 'clickable-selectable-card-input-1';
12 const id2 = 'clickable-selectable-card-input-2';
13 const id3 = 'clickable-selectable-card-input-3';
14
15 const onClick = () => {
16 setIsClicked((prevState) => !prevState);
17 };
18
19 const onChange = (event: React.FormEvent<HTMLInputElement>, checked: boolean) => {
20 const name = event.currentTarget.name;
21
22 switch (name) {
23 case id1:
24 setIsChecked1(checked);
25 break;
26 case id2:
27 setIsChecked2(checked);
28 break;
29 case id3:
30 setIsChecked3(checked);
31 break;
32 }
33 };
34
35 const toggleVariant = (checked: boolean) => {
36 setIsSecondary(checked);
37 };
38
39 return (
40 <>
41 <Checkbox
42 label="secondary styling"
43 isChecked={isSecondary}
44 onChange={(_event, checked) => toggleVariant(checked)}
45 aria-label="add secondary styling"
46 id="toggle-variant-clickable-selectable"
47 name="toggle-variant"
48 />
49 <div style={{ marginTop: '15px' }}>
50 <Gallery hasGutter>
51 <Card
52 id="clickable-selectable-card-example-1"
53 isClickable
54 isSelectable
55 isSelected={isChecked1}
56 isClicked={isClicked}
57 variant={isSecondary ? 'secondary' : 'default'}
58 >
59 <CardHeader
60 selectableActions={{
61 selectableActionId: id1,
62 selectableActionAriaLabelledby: 'clickable-selectable-card-example-1',
63 name: id1,
64 onChange,
65 hasNoOffset: true
66 }}
67 >
68 <CardTitle>
69 <Button variant="link" isInline onClick={onClick}>
70 First card
71 </Button>
72 </CardTitle>
73 </CardHeader>
74 <CardBody>This card performs an action upon clicking the card title and is selectable.</CardBody>
75 </Card>
76 <Card
77 id="clickable-selectable-card-example-2"
78 isClickable
79 isSelectable
80 isSelected={isChecked2}
81 variant={isSecondary ? 'secondary' : 'default'}
82 >
83 <CardHeader
84 selectableActions={{
85 selectableActionId: id2,
86 selectableActionAriaLabelledby: 'clickable-selectable-card-example-2',
87 name: id2,
88 onChange,
89 hasNoOffset: true
90 }}
91 >
92 <CardTitle>Second Card</CardTitle>
93 </CardHeader>
94 <CardBody>
95 This card is selectable and has a link in the card body that navigates to{' '}
96 <Button variant="link" isInline component="a" href="#">
97 Patternfly
98 </Button>
99 .
100 </CardBody>
101 </Card>
102 <Card
103 id="clickable-selectable-card-example-3"
104 isClickable
105 isSelectable
106 isDisabled
107 isSelected={isChecked3}
108 variant={isSecondary ? 'secondary' : 'default'}
109 >
110 <CardHeader
111 selectableActions={{
112 selectableActionId: id3,
113 selectableActionAriaLabelledby: 'clickable-selectable-card-example-3',
114 name: id3,
115 onChange,
116 hasNoOffset: true
117 }}
118 >
119 <CardTitle>
120 <Button isDisabled variant="link" isInline component="a" href="https://www.patternfly.org/">
121 Third card
122 </Button>
123 </CardTitle>
124 </CardHeader>
125 <CardBody>This card is clickable and selectable, but disabled.</CardBody>
126 </Card>
127 </Gallery>
128 </div>
129 </>
130 );
131 };