Skip to content

chore(card): add ability to hide radio button in example #11832

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion packages/react-core/src/components/Card/examples/Card.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ You must avoid rendering any other interactive content within the `<Card>` when

### Single selectable

When a group of single selectable cards are related, you must pass the same `name` property to each card's `selectableActions` property.
When a group of single selectable cards are related, you must pass the same `name` property to each card's `selectableActions` property. For more guidance on selectable cards with hidden input, see our [cards as tiles examples](#cards-as-tiles).

```ts file='./CardSingleSelectable.tsx'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,63 +1,81 @@
import { useState } from 'react';
import { Card, CardHeader, CardTitle, CardBody, Gallery } from '@patternfly/react-core';
import { Card, CardHeader, CardTitle, CardBody, Gallery, Checkbox } from '@patternfly/react-core';

export const SingleSelectableCard: React.FunctionComponent = () => {
const [isChecked, setIsChecked] = useState('');
const id1 = 'single-selectable-card-input-1';
const id2 = 'single-selectable-card-input-2';
const id3 = 'single-selectable-card-input-3';
const [displayRadioButton, setDisplayRadioButton] = useState<boolean>(false);

const onChange = (event: React.FormEvent<HTMLInputElement>) => {
setIsChecked(event.currentTarget.id);
};

const toggleHide = (checked: boolean) => {
setDisplayRadioButton(checked);
};

return (
<Gallery hasGutter>
<Card id="single-selectable-card-example-1" isSelectable isSelected={isChecked === id1}>
<CardHeader
selectableActions={{
selectableActionId: id1,
selectableActionAriaLabelledby: 'single-selectable-card-example-1',
name: 'single-selectable-card-example',
variant: 'single',
onChange,
hasNoOffset: true
}}
>
<CardTitle>First card</CardTitle>
</CardHeader>
<CardBody>This card is single selectable.</CardBody>
</Card>
<Card id="single-selectable-card-example-2" isSelectable isSelected={isChecked === id2}>
<CardHeader
selectableActions={{
selectableActionId: id2,
selectableActionAriaLabelledby: 'single-selectable-card-example-2',
name: 'single-selectable-card-example',
variant: 'single',
onChange,
hasNoOffset: true
}}
>
<CardTitle>Second card</CardTitle>
</CardHeader>
<CardBody>This card is single selectable.</CardBody>
</Card>
<Card id="single-selectable-card-example-3" isSelectable isDisabled isSelected={isChecked === id3}>
<CardHeader
selectableActions={{
selectableActionId: id3,
selectableActionAriaLabelledby: 'single-selectable-card-example-3',
name: 'single-selectable-card-example',
variant: 'single',
onChange,
hasNoOffset: true
}}
>
<CardTitle>Third card</CardTitle>
</CardHeader>
<CardBody>This card is single selectable but disabled.</CardBody>
</Card>
</Gallery>
<>
<Checkbox
label="Hide radio button"
isChecked={displayRadioButton}
onChange={(_event, checked) => toggleHide(checked)}
id="toggle-hide-radio-button"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
id="toggle-hide-radio-button"
id="single-select-toggle-hide-radio-button"

It's a bit verbose, but just to ensure it's unique

name="toggle-hide"
/>
<div style={{ marginTop: '15px' }}></div>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will depend on Erin's response to my general review comment above, but if we do want to update the "Selectable" example as well to include this "hide input" logic, I think we could remove these empty divs and instead either apply a margin to the checkbox or gallery, or try wrapping everything in a Flex layout and utilizing gap.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RE "do you think we should also update the "Selectable" example to match with a checkbox that can hide the Card inputs?" yes, I do! Would be good to match, I actually thought it was already there 😅

<Gallery hasGutter>
<Card id="single-selectable-card-example-1" isSelectable isSelected={isChecked === id1}>
<CardHeader
selectableActions={{
selectableActionId: id1,
selectableActionAriaLabelledby: 'single-selectable-card-example-1',
name: 'single-selectable-card-example',
variant: 'single',
onChange,
hasNoOffset: true,
isHidden: displayRadioButton
}}
>
<CardTitle>First card</CardTitle>
</CardHeader>
<CardBody>This card is single selectable.</CardBody>
</Card>
<Card id="single-selectable-card-example-2" isSelectable isSelected={isChecked === id2}>
<CardHeader
selectableActions={{
selectableActionId: id2,
selectableActionAriaLabelledby: 'single-selectable-card-example-2',
name: 'single-selectable-card-example',
variant: 'single',
onChange,
hasNoOffset: true,
isHidden: displayRadioButton
}}
>
<CardTitle>Second card</CardTitle>
</CardHeader>
<CardBody>This card is single selectable.</CardBody>
</Card>
<Card id="single-selectable-card-example-3" isSelectable isDisabled isSelected={isChecked === id3}>
<CardHeader
selectableActions={{
selectableActionId: id3,
selectableActionAriaLabelledby: 'single-selectable-card-example-3',
name: 'single-selectable-card-example',
variant: 'single',
onChange,
hasNoOffset: true,
isHidden: displayRadioButton
}}
>
<CardTitle>Third card</CardTitle>
</CardHeader>
<CardBody>This card is single selectable but disabled.</CardBody>
</Card>
</Gallery>
</>
);
};
Loading