Skip to content
Draft
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
7 changes: 7 additions & 0 deletions .changeset/selectable-card-group-value.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[feat] `SelectableCard` takes `value`. A card with a `value` in place of `isSelected` and `onChange` is a member of the card group around it, and the group gives the card its selection, its input and its disabled default. In a single-choice group the card is a native radio that selects and never deselects itself; in a multiple-choice group it is a checkbox that submits its `value` under the group's name. The new `SelectableCardMemberProps` type describes a member. `SelectableCardProps` stays the interface it was and gains only `value?: never`, so TypeScript rejects `isSelected` or `onChange` beside `value`. Outside a group, a card with a `value` is unselected and inert: it reads as disabled and ignores clicks. A standalone card keeps its props type, DOM and behavior.

@ernestt
25 changes: 25 additions & 0 deletions apps/storybook/stories/SelectableCard.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,31 @@ export const Disabled: Story = {
},
};

/**
* A card with a `value` outside any group has no selection to show or change,
* so it is unselected and inert: it reads as disabled and ignores clicks.
*/
export const ValueOutsideAGroup: Story = {
render: () => (
<HStack gap={3}>
<SelectableCard
label="Standalone"
isSelected={false}
onChange={() => {}}
width={200}>
<Text type="body" weight="bold">
Standalone
</Text>
</SelectableCard>
<SelectableCard label="Value outside a group" value="pro" width={200}>
<Text type="body" weight="bold">
Value outside a group
</Text>
</SelectableCard>
</HStack>
),
};

function useSelectedSet(initial: string[]) {
const [selected, setSelected] = useState(new Set(initial));
const toggle = (id: string, isNow: boolean) => {
Expand Down
46 changes: 14 additions & 32 deletions apps/storybook/stories/SelectableCardGroup.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import type {Meta, StoryObj} from '@storybook/react';
import {useState} from 'react';
import {Text} from '@astryxdesign/core/Text';
import {VStack} from '@astryxdesign/core/Layout';
import {
SelectableCardGroup,
SelectableCardGroupItem,
} from '@astryxdesign/lab/SelectableCardGroup';
import {SelectableCard} from '@astryxdesign/core/SelectableCard';
import {SelectableCardGroup} from '@astryxdesign/lab/SelectableCardGroup';

const meta: Meta<typeof SelectableCardGroup> = {
title: 'Lab/SelectableCardGroup',
Expand Down Expand Up @@ -56,13 +54,9 @@ export const SingleChoice: Story = {
value={plan}
onChange={setPlan}>
{PLANS.map(p => (
<SelectableCardGroupItem
key={p.id}
value={p.id}
label={p.name}
width={160}>
<SelectableCard key={p.id} value={p.id} label={p.name} width={160}>
<PlanContent name={p.name} price={p.price} />
</SelectableCardGroupItem>
</SelectableCard>
))}
</SelectableCardGroup>
);
Expand All @@ -80,13 +74,9 @@ export const MultipleChoice: Story = {
value={addOns}
onChange={setAddOns}>
{PLANS.map(p => (
<SelectableCardGroupItem
key={p.id}
value={p.id}
label={p.name}
width={160}>
<SelectableCard key={p.id} value={p.id} label={p.name} width={160}>
<PlanContent name={p.name} price={p.price} />
</SelectableCardGroupItem>
</SelectableCard>
))}
</SelectableCardGroup>
);
Expand All @@ -103,13 +93,9 @@ export const Vertical: Story = {
value={plan}
onChange={setPlan}>
{PLANS.map(p => (
<SelectableCardGroupItem
key={p.id}
value={p.id}
label={p.name}
maxWidth={320}>
<SelectableCard key={p.id} value={p.id} label={p.name} maxWidth={320}>
<PlanContent name={p.name} price={p.price} />
</SelectableCardGroupItem>
</SelectableCard>
))}
</SelectableCardGroup>
);
Expand All @@ -124,13 +110,9 @@ export const Disabled: Story = {
isDisabled
onChange={() => {}}>
{PLANS.map(p => (
<SelectableCardGroupItem
key={p.id}
value={p.id}
label={p.name}
width={160}>
<SelectableCard key={p.id} value={p.id} label={p.name} width={160}>
<PlanContent name={p.name} price={p.price} />
</SelectableCardGroupItem>
</SelectableCard>
))}
</SelectableCardGroup>
),
Expand All @@ -150,13 +132,13 @@ export const TwoFormsSharedName: Story = {
value={first}
onChange={setFirst}>
{PLANS.map(p => (
<SelectableCardGroupItem
<SelectableCard
key={p.id}
value={p.id}
label={`First ${p.name}`}
width={160}>
<PlanContent name={p.name} price={p.price} />
</SelectableCardGroupItem>
</SelectableCard>
))}
</SelectableCardGroup>
</form>
Expand All @@ -167,13 +149,13 @@ export const TwoFormsSharedName: Story = {
value={second}
onChange={setSecond}>
{PLANS.map(p => (
<SelectableCardGroupItem
<SelectableCard
key={p.id}
value={p.id}
label={`Second ${p.name}`}
width={160}>
<PlanContent name={p.name} price={p.price} />
</SelectableCardGroupItem>
</SelectableCard>
))}
</SelectableCardGroup>
</form>
Expand Down
Loading