Skip to content

Commit

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

<auro-input id="resetStateExample" bordered minlength="12" value="Auro Team" setCustomValidityTooShort="Please enter your full name!">
<slot slot="label">Full Name</slot>
<slot slot="helptext">Please enter your full name.</slot>
</auro-input>
7 changes: 7 additions & 0 deletions components/input/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();
});
}
2 changes: 1 addition & 1 deletion components/input/apiExamples/value.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<auro-button id="setValidValueBtn">Set Value to Alaska</auro-button>
<auro-button id="resetValueBtn">Reset</auro-button>
<auro-button id="setUndefinedValueBtn">Set Value to Undefined</auro-button>

<auro-input id="setProgrammaticValueExample" value="Press one of the buttons above!" bordered>
<span slot="label">Name</span>
Expand Down
6 changes: 3 additions & 3 deletions components/input/apiExamples/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ export function programmaticallySetValue() {

// set value of auro-input element
document.querySelector('#setValidValueBtn').addEventListener('click', () => {
elem.value = "Alaska Airlines is the best";
elem.value = "Alaska Airlines is the best";
});

// reset the value of auro-input element
document.querySelector('#resetValueBtn').addEventListener('click', () => {
elem.value = '';
document.querySelector('#setUndefinedValueBtn').addEventListener('click', () => {
elem.value = undefined;
});
}
2 changes: 2 additions & 0 deletions components/input/demo/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { customError } from "../apiExamples/error";
import { setReadonlyValue } from "../apiExamples/readonly";
import { swapInputValues } from "../apiExamples/swapValue";
import { programmaticallySetValue } from "../apiExamples/value";
import { resetStateExample } from "../apiExamples/resetState";
import './index.js';

export function initExamples(initCount) {
Expand All @@ -14,6 +15,7 @@ export function initExamples(initCount) {
setReadonlyValue();
swapInputValues();
programmaticallySetValue();
resetStateExample();
} catch (error) {
if (initCount <= 20) {
// setTimeout handles issue where content is sometimes loaded after the functions get called
Expand Down
1 change: 1 addition & 0 deletions components/input/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Generate unique names for dependency components.
| Method | Type | Description |
|--------------|---------------|------------------------------------------|
| `isDateType` | `(): boolean` | |
| `reset` | `(): void` | Resets component to initial state. |
| `validate` | `(): void` | Public method force validation of input. |

## Events
Expand Down
20 changes: 20 additions & 0 deletions components/input/docs/partials/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,26 @@ Use the `type="fullYear"` attribute for a date formatted input.

## Additional Use Cases

### Reset State

Use the `reset()` method to reset the `<auro-input>`'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>

### Swapping Values Between Inputs

Example illustrates using a JavaScript function attached to an `auro-button` component `click` event to swap the values of two `auro-input` elements. An example of this use case would be swapping the departure and arrival airports in a flight search form.
Expand Down
12 changes: 8 additions & 4 deletions components/input/src/base-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,7 @@ export default class BaseInput extends LitElement {
name: { type: String },
type: { type: String,
reflect: true },
value: {
type: String,
reflect: true
},
value: { type: String },
lang: { type: String },
pattern: {
type: String,
Expand Down Expand Up @@ -691,6 +688,13 @@ export default class BaseInput extends LitElement {
this.validation.validate(this);
}

/**
* Resets component to initial state.
* @returns {void}
*/
reset() {
this.validation.reset(this);
}

/**
* Sets configuration data used elsewhere based on the `type` attribute.
Expand Down
20 changes: 17 additions & 3 deletions components/input/test/auro-input.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { fixture, html, expect, elementUpdated, oneEvent } from '@open-wc/testin
import '../src/index.js';

describe('auro-input', () => {

it('web component is successfully created in the document', async () => {
// This test fails when attributes are put onto the component before it is attached to the DOM
const el = document.createElement('auro-input');
Expand Down Expand Up @@ -510,7 +509,7 @@ describe('auro-input', () => {

it('type numeric checked correctly when using the min attribute', async () => {
const el = await fixture(html`
<auro-input type="numeric" min="10"></auro-input>
<auro-input type="number" min="10"></auro-input>
`)

el.value = '10';
Expand All @@ -528,7 +527,7 @@ describe('auro-input', () => {

it('type numeric checked correctly when using the min attribute', async () => {
const el = await fixture(html`
<auro-input type="numeric" max="10"></auro-input>
<auro-input type="number" max="10"></auro-input>
`)

el.value = '10';
Expand Down Expand Up @@ -578,6 +577,21 @@ describe('auro-input', () => {
expect(el.shadowRoot.querySelector('#checkSpellCheck')).to.not.have.attribute('autocapitalize');
});

it('reset method clears the value and validity state', async () => {
const el = await fixture(html`
<auro-input required minlength="12" value="Auro Team"></auro-input>
`);

expect(el.getAttribute('validity')).to.be.equal('tooShort');

el.reset();

await elementUpdated(el);

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

describe('handles date formatting', () => {
it('MM/DD/YYYY', async () => {
const el = await fixture(html`
Expand Down

0 comments on commit 2027706

Please sign in to comment.