|
| 1 | +import React from 'react'; |
| 2 | +import { Card, CardTitle, CardBody } from '@patternfly/react-core'; |
| 3 | + |
| 4 | +export const CardSelectableA11yHighlight: React.FunctionComponent = () => { |
| 5 | + const [selected, setSelected] = React.useState<string>(''); |
| 6 | + |
| 7 | + const onKeyDown = (event: React.KeyboardEvent) => { |
| 8 | + if (event.target !== event.currentTarget) { |
| 9 | + return; |
| 10 | + } |
| 11 | + if ([' ', 'Enter'].includes(event.key)) { |
| 12 | + event.preventDefault(); |
| 13 | + const newSelected = event.currentTarget.id === selected ? null : event.currentTarget.id; |
| 14 | + setSelected(newSelected); |
| 15 | + } |
| 16 | + }; |
| 17 | + |
| 18 | + const onClick = (event: React.MouseEvent) => { |
| 19 | + const newSelected = event.currentTarget.id === selected ? null : event.currentTarget.id; |
| 20 | + setSelected(newSelected); |
| 21 | + }; |
| 22 | + |
| 23 | + const onChange = (labelledById: string, _event: React.FormEvent<HTMLInputElement>) => { |
| 24 | + const newSelected = labelledById === selected ? null : labelledById; |
| 25 | + setSelected(newSelected); |
| 26 | + }; |
| 27 | + |
| 28 | + return ( |
| 29 | + <React.Fragment> |
| 30 | + <Card |
| 31 | + id="selectable-first-card" |
| 32 | + onKeyDown={onKeyDown} |
| 33 | + onClick={onClick} |
| 34 | + hasSelectableInput |
| 35 | + onSelectableInputChange={onChange} |
| 36 | + isSelectableRaised |
| 37 | + isSelected={selected === 'selectable-first-card'} |
| 38 | + > |
| 39 | + <CardTitle>Selectable card with proper accessibility considerations</CardTitle> |
| 40 | + <CardBody> |
| 41 | + When using a screen reader a checkbox will become navigable that indicates this card is selectable and |
| 42 | + communicate if it is currently selected. |
| 43 | + </CardBody> |
| 44 | + </Card> |
| 45 | + <br /> |
| 46 | + <Card |
| 47 | + id="selectable-second-card" |
| 48 | + onKeyDown={onKeyDown} |
| 49 | + onClick={onClick} |
| 50 | + isSelectableRaised |
| 51 | + isSelected={selected === 'selectable-second-card'} |
| 52 | + > |
| 53 | + <CardTitle>Selectable card without proper accessibility considerations</CardTitle> |
| 54 | + <CardBody> |
| 55 | + When using a screen reader there are no indications that this card is selectable or if it is currently |
| 56 | + selected. |
| 57 | + </CardBody> |
| 58 | + </Card> |
| 59 | + </React.Fragment> |
| 60 | + ); |
| 61 | +}; |
0 commit comments