Skip to main content

Calculator block

Use the calculator block to instantly perform calculations inside forms, using given answers. The block includes operations, scores, comparisons, formulas, functions, constants and lots more features to be able to do all kinds of calculations.

info

This is a headless block. It doesn't require a UI implementation in the runner. See the runner section for instructions on how to use the block in a runner.

📺 Preview

📽️ Demo

Run Try on CodePen

📦 Package contents

🏗️ Builder part

The builder part instructs the builder how to manage the block. It defines the configurable properties and settings for the block, which can then be managed with the visual builder. This package contains:

  • Classes:
    • Calculator: Calculator headless block;
    • Operation: Operation item in the calculator collection;
    • CalculatorCondition: Calculator condition block to verify a calculator outcome;
  • TypeScript typings (should work out-of-the-box);
  • Translations (located in the ./translations folder of the package).

🏃 Runner part

The runner part of this headless block contains all the code to use the block in a runner (the thing that runs the form). This package contains:

  • Classes:
    • Calculator: Base class for implementing the calculator block in a runner;
    • CalculatorCondition: Runner part of the calculator condition block;
  • TypeScript typings (should work out-of-the-box).

👩‍💻 Usage

✨ Installation

npm install tripetto-block-calculator
Typescript support

This package contains type declarations and supports TypeScript out-of-the-box.

🏗️ Builder part

ESM/ES5

Importing the block is all you need to do to self-register the block to the builder (see the builder plain JS implementation for an example or the Block loading guide for more information about loading blocks).

import "tripetto-block-calculator";

// The code above imports the ES5 or ESM version based on your project configuration.
// If you want to use the ES5 version, you can do an explicit import:
import "tripetto-block-calculator/es5";

CDN

You can also use the builder part directly in a browser using a CDN (see the builder HTML implementation for more information).

<html>
<body>
<script src="https://unpkg.com/tripetto"></script>
<script src="https://unpkg.com/tripetto-block-calculator"></script>
<script>
Tripetto.Builder.open();
</script>
</body>
</html>

Translations

The available translation for the block are located in the translations folder of the package.

tip

See the Loading a translation for a stock block guide to learn how to load the block translation into the builder.

🏃 Runner part

Since this is a headless block, the runner part contains everything you need to use this block in a runner (the thing that runs the form). You only need to import the block somewhere in your runner project, and you are good to go:

import "tripetto-block-calculator/runner";

// The code above imports the ES5 or ESM version based on your project configuration.
// If you want to use the ES5 version, you can do an explicit import:
import "tripetto-block-calculator/runner/es5";

🎭 Stock runners

This block is included in the following stock runners:

tip

If you are integrating the builder together with one of the stock runners, you can use the builder block bundle that is included in the stock runner packages to load the builder part of all the blocks with a single import. See the Import block bundles guide for more information.

🚢 Distribution

This block is distributed through npm:

▶️ https://www.npmjs.com/package/tripetto-block-calculator

📁 Source code

This block is open-source and the code is on GitLab:

▶️ https://gitlab.com/tripetto/blocks/calculator