Skip to content

Commit 2a7710e

Browse files
committed
feat(ui-checkbox): add data-checked attribute to reflect checked state
INSTUI-4902
1 parent de45942 commit 2a7710e

4 files changed

Lines changed: 28 additions & 0 deletions

File tree

packages/ui-checkbox/src/Checkbox/__tests__/Checkbox.test.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,25 @@ describe('<Checkbox />', () => {
240240
})
241241
})
242242

243+
it('reflects checked state changes via data-checked attribute', async () => {
244+
renderCheckbox({ defaultChecked: false })
245+
const input = screen.getByRole('checkbox')
246+
247+
expect(input).toHaveAttribute('data-checked', 'false')
248+
249+
userEvent.click(input)
250+
await waitFor(() => {
251+
expect(input).toHaveAttribute('data-checked', 'true')
252+
})
253+
})
254+
255+
it('sets data-checked to mixed when indeterminate', () => {
256+
renderCheckbox({ indeterminate: true })
257+
const input = screen.getByRole('checkbox')
258+
259+
expect(input).toHaveAttribute('data-checked', 'mixed')
260+
})
261+
243262
describe('for a11y', () => {
244263
it('`simple` variant should meet standards', async () => {
245264
const { container } = renderCheckbox({ variant: 'simple' })

packages/ui-checkbox/src/Checkbox/v2/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ type: example
162162
/>
163163
```
164164

165+
### Querying checked state from the DOM
166+
167+
The underlying `<input>` has a `data-checked` attribute (`"true"`, `"false"`, or `"mixed"` when indeterminate) that can be queried from the DOM to read the current state.
168+
165169
### Guidelines
166170

167171
```js

packages/ui-checkbox/src/Checkbox/v2/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> {
328328
>
329329
<div css={styles?.container}>
330330
<input
331+
data-checked={indeterminate ? 'mixed' : this.checked}
331332
{...props}
332333
id={this.id}
333334
value={value}

packages/ui-checkbox/src/CheckboxGroup/v2/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ type: example
8080
</CheckboxGroup>
8181
```
8282

83+
### Querying checked state from the DOM
84+
85+
Each `<Checkbox>` in the group exposes a `data-checked` attribute (`"true"` or `"false"`) on its underlying `<input>` that can be queried from the DOM to read the current state, for example for analytics tracking tools.
86+
8387
### Guidelines
8488

8589
```js

0 commit comments

Comments
 (0)