From ed81ff1bfc85bd023df006b46b3fb1dc53ae8564 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Fri, 29 Mar 2024 00:50:46 +0000 Subject: [PATCH] Add a counter than cannot be negative --- assets/js/controllers/count_controller.js | 7 +++++-- public/index.html | 11 ++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/assets/js/controllers/count_controller.js b/assets/js/controllers/count_controller.js index 2123d88..2d93cbb 100644 --- a/assets/js/controllers/count_controller.js +++ b/assets/js/controllers/count_controller.js @@ -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() { diff --git a/public/index.html b/public/index.html index 41e4c05..b11de50 100644 --- a/public/index.html +++ b/public/index.html @@ -4,7 +4,16 @@ Stimulus esbuild example -
+
+

Count:

+ +
+ + +
+
+ +

Count: