Sha256: ed60ff51282d9db9d67175c41ef4f81a360b86e1a42c12a7496be6480533bf53
Contents?: true
Size: 1.05 KB
Versions: 24
Compression:
Stored size: 1.05 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 (const 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 (const 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
24 entries across 24 versions & 1 rubygems