Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Add ability to customize the quantity picker Stimulus controller (#12734)

Damian Legawiecec2b78da501f
diff --git a/storefront/app/javascript/spree/storefront/controllers/quantity_picker_controller.js b/storefront/app/javascript/spree/storefront/controllers/quantity_picker_controller.js
index ecc1dec94a..860e5a6de2 100644
--- a/storefront/app/javascript/spree/storefront/controllers/quantity_picker_controller.js
+++ b/storefront/app/javascript/spree/storefront/controllers/quantity_picker_controller.js
@@ -3,41 +3,48 @@ import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = [ 'quantity', 'increase', 'decrease' ]
+ static values = {
+ min: { type: Number, default: 1 },
+ max: { type: Number, default: 9999 }
+ }
+
+ static classes = ['disabled']
+
connect() {
- if (this.quantity <= 1) this.disableButton(this.decreaseTarget)
+ if (this.quantity <= this.minValue) this.disableButton(this.decreaseTarget)
}
get quantity() {
- return parseInt(this.quantityTarget.value) || 1
+ return parseInt(this.quantityTarget.value) || this.minValue
}
get maxQuantity() {
- return parseInt(this.quantityTarget.max) || 9999
+ return parseInt(this.quantityTarget.max) || this.maxValue
}
set quantity(value) {
- this.quantityTarget.value = parseInt(value) || 1
+ this.quantityTarget.value = parseInt(value) || this.minValue
}
increase() {
if (this.quantity < this.maxQuantity) this.quantity = this.quantity + 1
- if (this.quantity > 1) this.enableButton(this.decreaseTarget)
+ if (this.quantity > this.minValue) this.enableButton(this.decreaseTarget)
if (this.quantity == this.maxQuantity && this.increaseTarget.type != 'submit') this.disableButton(this.increaseTarget)
}
decrease() {
- if (this.quantity > 1) this.quantity = this.quantity - 1
- if (this.quantity == 1 && this.decreaseTarget.type != 'submit') this.disableButton(this.decreaseTarget)
+ if (this.quantity > this.minValue) this.quantity = this.quantity - 1
+ if (this.quantity == this.minValue && this.decreaseTarget.type != 'submit') this.disableButton(this.decreaseTarget)
if (this.quantity < this.maxQuantity) this.enableButton(this.increaseTarget)
}
disableButton(button) {
button.setAttribute('disabled', 'disabled')
- button.classList.add('opacity-50', 'cursor-not-allowed')
+ button.classList.add(...this.disabledClasses)
}
enableButton(button) {
button.removeAttribute('disabled')
- button.classList.remove('opacity-50', 'cursor-not-allowed')
+ button.classList.remove(...this.disabledClasses)
}
}
diff --git a/storefront/app/views/themes/default/spree/orders/_line_item_quantity.html.erb b/storefront/app/views/themes/default/spree/orders/_line_item_quantity.html.erb
index 8f6bd9c531..8a6efaf6f7 100644
--- a/storefront/app/views/themes/default/spree/orders/_line_item_quantity.html.erb
+++ b/storefront/app/views/themes/default/spree/orders/_line_item_quantity.html.erb
@@ -1,6 +1,6 @@
<div class="flex items-center">
<%= form_for line_item, url: spree.line_item_url(line_item, order_token: line_item.order.token), data: { controller: 'turbo-stream-form' } do |item_form| %>
- <div class="quantity-picker" data-controller="quantity-picker">
+ <div class="quantity-picker" data-controller="quantity-picker" data-quantity-picker-disabled-class="opacity-50 cursor-not-allowed">
<!-- this is a dummy button to work with turbo frame forms when hitting ENTER -->
<%= button_tag render('spree/shared/icons/minus'), type: 'submit', class: 'hidden' %>
<%= quantity_modifier_button_tag render('spree/shared/icons/minus'), type: 'submit', action: 'decrease', class: 'quantity-decrease-button' %>