Skip to content

Commit

Permalink
feat: add reset functionality to radio
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanjones243 committed Dec 31, 2024
1 parent 292845e commit 5b961dc
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 9 deletions.
9 changes: 9 additions & 0 deletions components/radio/apiExamples/resetState.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<auro-button id="resetStateBtn">Reset</auro-button>
<br/><br/>

<auro-radio-group id="resetStateExample" required setCustomValidityValueMissing="Please select an option">
<span slot="legend">Form label goes here</span>
<auro-radio id="resetGroupRadio1" label="Yes" name="radioDemo" value="yes"></auro-radio>
<auro-radio id="resetGroupRadio2" label="No" name="radioDemo" value="no"></auro-radio>
<auro-radio id="resetGroupRadio3" label="Maybe" name="radioDemo" value="maybe"></auro-radio>
</auro-radio-group>
7 changes: 7 additions & 0 deletions components/radio/apiExamples/resetState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function resetStateExample() {
const elem = document.querySelector('#resetStateExample');

document.querySelector('#resetStateBtn').addEventListener('click', () => {
elem.reset();
});
}
1 change: 1 addition & 0 deletions components/radio/demo/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@

<!-- If additional elements are needed for the demo, add them here. -->
<script src="https://cdn.jsdelivr.net/npm/@aurodesignsystem/auro-accordion@latest/dist/auro-accordion__bundled.js" type="module"></script>
<script src="https://cdn.jsdelivr.net/npm/@aurodesignsystem/auro-button@latest/dist/auro-button__bundled.js" type="module"></script>
</body>
</html>
2 changes: 2 additions & 0 deletions components/radio/demo/api.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { dynamicExample } from "../apiExamples/dynamic";
import { resetStateExample } from "../apiExamples/resetState";
import './index.js';

export function initExamples(initCount) {
initCount = initCount || 0;

try {
dynamicExample();
resetStateExample();
} catch (error) {
if (initCount <= 20) {
// setTimeout handles issue where content is sometimes loaded after the functions get called
Expand Down
12 changes: 9 additions & 3 deletions components/radio/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

## Methods

| Method | Type | Description |
|---------|------------|------------------------------------------------|
| `reset` | `(): void` | Method for a total reset of the radio element. |
| Method | Type | Description |
|---------|------------|------------------------------------|
| `reset` | `(): void` | Resets component to initial state. |

## Events

Expand Down Expand Up @@ -59,6 +59,12 @@
| `tabIndex` | `tabIndex` | `number` | -1 | |
| `value` | `value` | `string` | | |

## Methods

| Method | Type | Description |
|---------|------------|------------------------------------|
| `reset` | `(): void` | Resets component to initial state. |

## Events

| Event | Type | Description |
Expand Down
20 changes: 20 additions & 0 deletions components/radio/docs/partials/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,26 @@ The `<auro-radio-group>` supports an `optionalLabel` slot, where users can can o

</auro-accordion>

### Reset State

Use the `reset()` method to reset the `<auro-radio-group>`'s `value` and `validity` state. Doing so will preserve all other attributes and properties.

<div class="exampleWrapper">
<!-- AURO-GENERATED-CONTENT:START (FILE:src=./../apiExamples/resetState.html) -->
<!-- AURO-GENERATED-CONTENT:END -->
</div>

<auro-accordion alignRight>
<span slot="trigger">See code</span>

<!-- AURO-GENERATED-CONTENT:START (CODE:src=./../apiExamples/resetState.html) -->
<!-- AURO-GENERATED-CONTENT:END -->

<!-- AURO-GENERATED-CONTENT:START (CODE:src=./../apiExamples/resetState.js) -->
<!-- AURO-GENERATED-CONTENT:END -->

</auro-accordion>

### Dynamic Example

This example demonstrates a data drive approach to rendering `<auro-radio>` buttons.
Expand Down
10 changes: 5 additions & 5 deletions components/radio/src/auro-radio-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,20 +216,20 @@ export class AuroRadioGroup extends LitElement {
}

/**
* Method for a total reset of the radio element.
* Resets component to initial state.
* @returns {void}
*/
reset() {
this.value = undefined;
// Sets first radio button to receive focus during keyboard navigation
this.index = 0;
this.optionSelected = undefined;

const buttons = this.querySelectorAll('auro-radio, [auro-radio]');

buttons.forEach((button) => {
button.checked = false;
button.reset();
});

this.validation.validate(this);
this.validation.reset(this);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions components/radio/src/auro-radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ export class AuroRadio extends LitElement {
}));
}

/**
* Resets component to initial state.
* @returns {void}
*/
reset() {
this.checked = false;
this.error = false;
}

updated(changedProperties) {
if (changedProperties.has('checked')) {
this.dispatchEvent(new CustomEvent('resetRadio', {
Expand Down
6 changes: 5 additions & 1 deletion components/radio/test/auro-radio.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('auro-radio', () => {

it('handles resetting all radio buttons', async () => {
const el = await fixture(html`
<auro-radio-group>
<auro-radio-group id="resetGroupTest" required>
<span slot="legend">Form label goes here</span>
<auro-radio id="radio1" label="Yes" name="radioDemo" value="yes" checked></auro-radio>
<auro-radio id="radio2" label="No" name="radioDemo" value="no" checked></auro-radio>
Expand All @@ -96,6 +96,10 @@ describe('auro-radio', () => {

await expect(radioButtonOne.hasAttribute('checked')).to.be.false;
await expect(radioButtonTwo.hasAttribute('checked')).to.be.false;

await expect(el.value).to.equal(undefined);
await expect(el.optionSelected).to.equal(undefined);
await expect(el.hasAttribute('validity')).to.be.false;
});

it('selects radio button by keyboard', async () => {
Expand Down

0 comments on commit 5b961dc

Please sign in to comment.