Skip to content

Commit 2b17dd5

Browse files
committed
refactor: radio group
1 parent c59c574 commit 2b17dd5

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@zag-js/radio-group": patch
3+
---
4+
5+
Improve accessibility of radio group by adding `invalid` and `required` props with corresponding `data-*` and `aria-*`
6+
attributes

packages/machines/radio-group/src/radio-group.connect.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ export function connect<T extends PropTypes>(
1313
const { context, send, computed, prop, scope } = service
1414

1515
const groupDisabled = computed("isDisabled")
16+
const groupInvalid = prop("invalid")
1617
const readOnly = prop("readOnly")
1718

1819
function getItemState(props: ItemProps): ItemState {
1920
return {
2021
value: props.value,
21-
invalid: !!props.invalid,
22+
invalid: !!props.invalid || !!groupInvalid,
2223
disabled: !!props.disabled || groupDisabled,
2324
checked: context.get("value") === props.value,
2425
focused: context.get("focusedValue") === props.value,
@@ -64,8 +65,13 @@ export function connect<T extends PropTypes>(
6465
role: "radiogroup",
6566
id: dom.getRootId(scope),
6667
"aria-labelledby": dom.getLabelId(scope),
68+
"aria-required": prop("required") || undefined,
69+
"aria-disabled": groupDisabled || undefined,
70+
"aria-readonly": readOnly || undefined,
6771
"data-orientation": prop("orientation"),
6872
"data-disabled": dataAttr(groupDisabled),
73+
"data-invalid": dataAttr(groupInvalid),
74+
"data-required": dataAttr(prop("required")),
6975
"aria-orientation": prop("orientation"),
7076
dir: prop("dir"),
7177
style: {
@@ -80,6 +86,8 @@ export function connect<T extends PropTypes>(
8086
dir: prop("dir"),
8187
"data-orientation": prop("orientation"),
8288
"data-disabled": dataAttr(groupDisabled),
89+
"data-invalid": dataAttr(groupInvalid),
90+
"data-required": dataAttr(prop("required")),
8391
id: dom.getLabelId(scope),
8492
onClick: focus,
8593
})
@@ -159,6 +167,8 @@ export function connect<T extends PropTypes>(
159167
name: prop("name") || prop("id"),
160168
form: prop("form"),
161169
value: props.value,
170+
required: prop("required"),
171+
"aria-invalid": itemState.invalid || undefined,
162172
onClick(event) {
163173
if (readOnly) {
164174
event.preventDefault()

packages/machines/radio-group/src/radio-group.props.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ export const props = createProps<RadioGroupProps>()([
99
"getRootNode",
1010
"id",
1111
"ids",
12+
"invalid",
1213
"name",
1314
"onValueChange",
1415
"orientation",
1516
"readOnly",
17+
"required",
1618
"value",
1719
"defaultValue",
1820
])

packages/machines/radio-group/src/radio-group.types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ export interface RadioGroupProps extends DirectionProperty, CommonProperties {
4747
*/
4848
disabled?: boolean | undefined
4949
/**
50-
* Whether the checkbox is read-only
50+
* If `true`, the radio group is marked as invalid.
51+
*/
52+
invalid?: boolean | undefined
53+
/**
54+
* If `true`, the radio group is marked as required.
55+
*/
56+
required?: boolean | undefined
57+
/**
58+
* Whether the radio group is read-only
5159
*/
5260
readOnly?: boolean | undefined
5361
/**

0 commit comments

Comments
 (0)