Sha256: c523b81488ad0935d5e88bd21b42502ebe37a99aed0b1b156ec609c3c988ac72
Contents?: true
Size: 1.04 KB
Versions: 42
Compression:
Stored size: 1.04 KB
Contents
import {Controller} from '@hotwired/stimulus' export default class AccumulatorController extends Controller { static targets = ['sum', 'accumulator'] static values = { syncAttrs: { type: Array, default: ['aria-valuenow'], }, sumAttr: { type: 'string', default: 'data-value', }, } declare sumTargets: Array<HTMLElement> declare accumulatorTarget?: HTMLElement declare syncAttrsValue: Array<string> declare sumAttrValue: string connect(): void { this.accumulate() } accumulate() { let sum = 0 for (let i in this.sumTargets) { const target = this.sumTargets[i] const value = Number(target.getAttribute(this.sumAttrValue)) if (!isNaN(value)) { sum += value } } this.setAttributesTo(sum) } setAttributesTo(sum: number) { for (let i in this.syncAttrsValue) { const attr = this.syncAttrsValue[i] this.accumulator.setAttribute(attr, sum.toString()) } } get accumulator() { return this.accumulatorTarget ?? this.element } }
Version data entries
42 entries across 42 versions & 1 rubygems