Files
3dealer/resources/js/stimulus/star-rating-controller.js
T

49 lines
1.5 KiB
JavaScript
Raw Normal View History

2026-07-31 17:41:55 +03:00
import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
static targets = ['star', 'input', 'error']
2026-07-31 17:41:55 +03:00
static values = { rating: { type: Number, default: 0 } }
connect() {
this.#fill(this.ratingValue)
}
// Bound to the form's submit event (this controller sits on the <form>
// itself, not just the star widget) — a plain `required` on the hidden
// rating input would never surface: browsers exclude type="hidden" from
// constraint validation entirely, so there'd be nothing to see or hear.
validate(event) {
if (this.ratingValue < 1) {
event.preventDefault()
this.errorTarget.hidden = false
this.starTargets[0]?.focus()
}
}
2026-07-31 17:41:55 +03:00
hover(event) {
this.#fill(parseInt(event.currentTarget.dataset.value))
}
leave() {
this.#fill(this.ratingValue)
}
select(event) {
const val = parseInt(event.currentTarget.dataset.value)
this.ratingValue = val
this.inputTarget.value = val
this.errorTarget.hidden = true
2026-07-31 17:41:55 +03:00
this.starTargets.forEach(star => {
star.setAttribute('aria-pressed', String(parseInt(star.dataset.value) === val))
})
}
#fill(upTo) {
this.starTargets.forEach(star => {
const filled = parseInt(star.dataset.value) <= upTo
star.querySelector('svg').setAttribute('fill', filled ? 'currentColor' : 'none')
})
}
}