Skip to content

Commit

Permalink
chore(number-field): added default-value example to the doc site (#4698)
Browse files Browse the repository at this point in the history
* chore(number-field): added a default-value example in docs

* chore: update number-field docs

Co-authored-by: Rajdeep Chandra <[email protected]>

---------

Co-authored-by: Rajdeep Chandra <[email protected]>
  • Loading branch information
TarunAdobe and Rajdeepc authored Sep 2, 2024
1 parent 1501a5c commit cf112cf
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/number-field/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,41 @@ If the user types a value that is between two steps and blurs the input, the val
step="3"
></sp-number-field>
```

## Default value

The `<sp-number-field>` component doesn't manage a default value by itself. This means that consumers can set the value of the number-field as an empty string by clearing the input. If we want the number-field to reset to a `default-value` when the user clears the input, we can listen for the `change` event on the number-field component and set its value to the desired `default-value` if the input is empty.

```html-live
<sp-field-label for="default">
Default value of this number field is 42
</sp-field-label>
<sp-number-field id="default" value="20"></sp-number-field>
<script type="module">
customElements.whenDefined('sp-number-field').then(() => {
const numberField = document.querySelector('#default');
numberField.addEventListener('change', (event) => {
const target = event.target;
if (isNaN(target.value)) {
target.value = '42';
}
});
});
</script>
```

<script type="module">
customElements.whenDefined('sp-number-field').then(() => {
const numberField = document.querySelector('#default');

numberField.addEventListener('change', (event) => {
const target = event.target;
if (isNaN(target.value)) {
target.value = '42';
}
});
});
</script>

0 comments on commit cf112cf

Please sign in to comment.