Add a counter than cannot be negative
This commit is contained in:
parent
7146a7f66d
commit
ed81ff1bfc
|
@ -4,6 +4,7 @@ export default class extends Controller {
|
|||
static targets = ["result"];
|
||||
|
||||
static values = {
|
||||
canBeNegative: Boolean,
|
||||
count: Number,
|
||||
};
|
||||
|
||||
|
@ -22,9 +23,11 @@ export default class extends Controller {
|
|||
decrement() {
|
||||
console.log("count#decrement");
|
||||
|
||||
if (this.countValue > 0) {
|
||||
this.countValue = this.countValue - 1;
|
||||
if (this.canBeNegativeValue === false && this.countValue === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.countValue = this.countValue - 1;
|
||||
}
|
||||
|
||||
increment() {
|
||||
|
|
|
@ -4,7 +4,16 @@
|
|||
<title>Stimulus esbuild example</title>
|
||||
</head>
|
||||
<body>
|
||||
<div data-controller="count">
|
||||
<div data-controller="count" data-count-can-be-negative-value="true">
|
||||
<p>Count: <span data-count-target="result"></span></p>
|
||||
|
||||
<div>
|
||||
<button type="button" data-action="count#increment">+</button>
|
||||
<button type="button" data-action="count#decrement">-</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div data-controller="count" data-count-can-be-negative-value="false">
|
||||
<p>Count: <span data-count-target="result"></span></p>
|
||||
|
||||
<div>
|
||||
|
|
Loading…
Reference in a new issue