DEV Community


Stimulus component: Calculator for input fields

Thomas R. Koll
Ruby on Rails dev since 2007, and a few more years of php before that. Drop me a mail if you want me to work on an interesting project.
Originally published at ・1 min read

Here's a small stimulusjs component at I wrote for Budget Fox.

It calculates the value of an input with basic arithmetic like 2 + 10/3
and shows the result in the result target.

When leaving the input or submitting the form, the input's value is
replaced with the calculated value.:w

If the result target is set its innerText will be replace with the
value while the user is typing numbers and restored on leaving the input.

<div data-controller="input-calculator">
  <label for="amount" data-target="input-calculator.result">Amount</label>
  <input name="amount" data-action="keydown->input-calculator#calculate keyup->input-calculator#calculate blur->input-calculator#calculate" />
Enter fullscreen mode Exit fullscreen mode

The data-action has three event listeners simply because stimulus is opinionated about
what events to listen to. We need more to update the result on key-strokes (keyup)
and update it when the users hits enter to submit the form (keydown)

import { Controller } from "stimulus"

export default class extends Controller
  @targets: ['result']

  connect: ->
    if @resultTarget && @resultTarget.dataset
      @flipResult = true
      @resultTarget.dataset['oldInnerHTML'] = @resultTarget.innerHTML

  calculate: (event) ->
    val =
    if @flipResult
      @resultTarget.innerHTML = @resultTarget.dataset['oldInnerHTML']
    if val.match(/[+\-\*\/]/)
        val = eval(val)
        if event.type == 'blur' || event.code == "Enter" || event.keyCode == 13 || event.which == 13
 = val
        else if @flipResult
          @resultTarget.innerText = val
Enter fullscreen mode Exit fullscreen mode

The code if of course MIT licensed.

Discussion (0)