Skip to content

Commit

Permalink
fix: #205
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien-ml committed May 4, 2023
1 parent c12b93a commit 7b3f414
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/radio-group/radio-group-item-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ export interface RadioGroupItemDataSet {
"data-valid": string | undefined;
"data-invalid": string | undefined;
"data-checked": string | undefined;
"data-required": string | undefined;
"data-disabled": string | undefined;
"data-readonly": string | undefined;
}

export interface RadioGroupItemContextValue {
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/radio-group/radio-group-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ export function RadioGroupItem(props: RadioGroupItemProps) {
};

const dataset: Accessor<RadioGroupItemDataSet> = createMemo(() => ({
"data-valid": formControlContext.dataset()["data-valid"],
"data-invalid": formControlContext.dataset()["data-invalid"],
...formControlContext.dataset(),
"data-checked": isSelected() ? "" : undefined,
"data-disabled": isDisabled() ? "" : undefined,
}));
Expand Down
46 changes: 43 additions & 3 deletions packages/core/src/radio-group/radio-group.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1312,9 +1312,9 @@ describe("RadioGroup", () => {
}
});

it("should have 'data-checked' attribute on checked radio", async () => {
it("should have 'data-required' attribute on radios when radio group is required", async () => {
render(() => (
<RadioGroup.Root value="cats">
<RadioGroup.Root value="cats" required>
<RadioGroup.Item data-testid="radio-root" value="cats">
<RadioGroup.ItemInput />
<RadioGroup.ItemControl data-testid="radio-control">
Expand All @@ -1328,7 +1328,27 @@ describe("RadioGroup", () => {
const elements = screen.getAllByTestId(/^radio/);

for (const el of elements) {
expect(el).toHaveAttribute("data-checked");
expect(el).toHaveAttribute("data-required");
}
});

it("should have 'data-readonly' attribute on radios when radio group is readonly", async () => {
render(() => (
<RadioGroup.Root value="cats" readOnly>
<RadioGroup.Item data-testid="radio-root" value="cats">
<RadioGroup.ItemInput />
<RadioGroup.ItemControl data-testid="radio-control">
<RadioGroup.ItemIndicator data-testid="radio-indicator" />
</RadioGroup.ItemControl>
<RadioGroup.ItemLabel data-testid="radio-label">Cats</RadioGroup.ItemLabel>
</RadioGroup.Item>
</RadioGroup.Root>
));

const elements = screen.getAllByTestId(/^radio/);

for (const el of elements) {
expect(el).toHaveAttribute("data-readonly");
}
});

Expand Down Expand Up @@ -1371,6 +1391,26 @@ describe("RadioGroup", () => {
expect(el).toHaveAttribute("data-disabled");
}
});

it("should have 'data-checked' attribute on checked radio", async () => {
render(() => (
<RadioGroup.Root value="cats">
<RadioGroup.Item data-testid="radio-root" value="cats">
<RadioGroup.ItemInput />
<RadioGroup.ItemControl data-testid="radio-control">
<RadioGroup.ItemIndicator data-testid="radio-indicator" />
</RadioGroup.ItemControl>
<RadioGroup.ItemLabel data-testid="radio-label">Cats</RadioGroup.ItemLabel>
</RadioGroup.Item>
</RadioGroup.Root>
));

const elements = screen.getAllByTestId(/^radio/);

for (const el of elements) {
expect(el).toHaveAttribute("data-checked");
}
});
});
});
});

0 comments on commit 7b3f414

Please sign in to comment.