| 1 |
import { useState } from 'react'; |
| 2 |
import { Card, CardHeader, CardTitle, CardBody, Checkbox, Gallery } from '@patternfly/react-core'; |
| 3 |
|
| 4 |
export const SelectableCard: React.FunctionComponent = () => { |
| 5 |
const [isChecked1, setIsChecked1] = useState(false); |
| 6 |
const [isChecked2, setIsChecked2] = useState(false); |
| 7 |
const [isChecked3, setIsChecked3] = useState(false); |
| 8 |
const [isSecondary, setIsSecondary] = useState(false); |
| 9 |
const [displayCheckbox, setDisplayCheckbox] = useState(false); |
| 10 |
|
| 11 |
const id1 = 'selectable-card-input-1'; |
| 12 |
const id2 = 'selectable-card-input-2'; |
| 13 |
const id3 = 'selectable-card-input-3'; |
| 14 |
|
| 15 |
const onChange = (event: React.FormEvent<HTMLInputElement>, checked: boolean) => { |
| 16 |
const name = event.currentTarget.name; |
| 17 |
|
| 18 |
switch (name) { |
| 19 |
case id1: |
| 20 |
setIsChecked1(checked); |
| 21 |
break; |
| 22 |
case id2: |
| 23 |
setIsChecked2(checked); |
| 24 |
break; |
| 25 |
case id3: |
| 26 |
setIsChecked3(checked); |
| 27 |
break; |
| 28 |
} |
| 29 |
}; |
| 30 |
|
| 31 |
const toggleVariant = (checked: boolean) => { |
| 32 |
setIsSecondary(checked); |
| 33 |
}; |
| 34 |
|
| 35 |
const toggleHide = (checked: boolean) => { |
| 36 |
setDisplayCheckbox(checked); |
| 37 |
}; |
| 38 |
|
| 39 |
return ( |
| 40 |
<> |
| 41 |
<Checkbox |
| 42 |
label="Secondary card styling" |
| 43 |
isChecked={isSecondary} |
| 44 |
onChange={(_event, checked) => toggleVariant(checked)} |
| 45 |
id="toggle-variant-selectable" |
| 46 |
name="toggle-variant" |
| 47 |
/> |
| 48 |
<Checkbox |
| 49 |
label="Hide checkbox" |
| 50 |
isChecked={displayCheckbox} |
| 51 |
onChange={(_event, checked) => toggleHide(checked)} |
| 52 |
id="selectable-toggle-hide-checkbox" |
| 53 |
name="toggle-hide" |
| 54 |
/> |
| 55 |
<Gallery hasGutter style={{ marginTop: '15px' }}> |
| 56 |
<Card |
| 57 |
id="selectable-card-example-1" |
| 58 |
isSelectable |
| 59 |
isSelected={isChecked1} |
| 60 |
variant={isSecondary ? 'secondary' : 'default'} |
| 61 |
> |
| 62 |
<CardHeader |
| 63 |
selectableActions={{ |
| 64 |
selectableActionId: id1, |
| 65 |
selectableActionAriaLabelledby: 'selectable-card-example-1', |
| 66 |
name: id1, |
| 67 |
onChange, |
| 68 |
hasNoOffset: true, |
| 69 |
isHidden: displayCheckbox |
| 70 |
}} |
| 71 |
> |
| 72 |
<CardTitle>First card</CardTitle> |
| 73 |
</CardHeader> |
| 74 |
<CardBody>This card is selectable.</CardBody> |
| 75 |
</Card> |
| 76 |
<Card |
| 77 |
id="selectable-card-example-2" |
| 78 |
isSelectable |
| 79 |
isSelected={isChecked2} |
| 80 |
variant={isSecondary ? 'secondary' : 'default'} |
| 81 |
> |
| 82 |
<CardHeader |
| 83 |
selectableActions={{ |
| 84 |
selectableActionId: id2, |
| 85 |
selectableActionAriaLabelledby: 'selectable-card-example-2', |
| 86 |
name: id2, |
| 87 |
onChange, |
| 88 |
hasNoOffset: true, |
| 89 |
isHidden: displayCheckbox |
| 90 |
}} |
| 91 |
> |
| 92 |
<CardTitle>Second card</CardTitle> |
| 93 |
</CardHeader> |
| 94 |
<CardBody>This card is selectable.</CardBody> |
| 95 |
</Card> |
| 96 |
<Card |
| 97 |
id="selectable-card-example-3" |
| 98 |
isSelectable |
| 99 |
isDisabled |
| 100 |
isSelected={isChecked3} |
| 101 |
variant={isSecondary ? 'secondary' : 'default'} |
| 102 |
> |
| 103 |
<CardHeader |
| 104 |
selectableActions={{ |
| 105 |
selectableActionId: id3, |
| 106 |
selectableActionAriaLabelledby: 'selectable-card-example-3', |
| 107 |
name: id3, |
| 108 |
onChange, |
| 109 |
hasNoOffset: true, |
| 110 |
isHidden: displayCheckbox |
| 111 |
}} |
| 112 |
> |
| 113 |
<CardTitle>Third card</CardTitle> |
| 114 |
</CardHeader> |
| 115 |
<CardBody>This card is selectable but disabled.</CardBody> |
| 116 |
</Card> |
| 117 |
</Gallery> |
| 118 |
</> |
| 119 |
); |
| 120 |
}; |