All Projects → final-form → final-form-calculate

final-form / final-form-calculate

Licence: MIT license
Decorator for calculating field values based on other field values in 🏁 Final Form

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to final-form-calculate

Ioc
🦄 lightweight (<1kb) inversion of control javascript library for dependency injection written in typescript
Stars: ✭ 171 (+54.05%)
Mutual labels:  decorators
Mobx Task
Makes async function state management in MobX fun.
Stars: ✭ 218 (+96.4%)
Mutual labels:  decorators
decapi
Create GraphQL API by decorating TypeScript classes
Stars: ✭ 81 (-27.03%)
Mutual labels:  decorators
Joiful
TypeScript Declarative Validation for Joi
Stars: ✭ 177 (+59.46%)
Mutual labels:  decorators
Testdeck
Object oriented testing
Stars: ✭ 206 (+85.59%)
Mutual labels:  decorators
Node Decorators
node-decorators
Stars: ✭ 230 (+107.21%)
Mutual labels:  decorators
Deal
Design by contract for Python with static checker and tests' generation.
Stars: ✭ 164 (+47.75%)
Mutual labels:  decorators
restgoose
Model-driven REST API framework using decorators
Stars: ✭ 28 (-74.77%)
Mutual labels:  decorators
Memo Decorator
Decorator which applies memoization to a method of a class.
Stars: ✭ 213 (+91.89%)
Mutual labels:  decorators
vue-class-state
面向对象风格的vue状态管理
Stars: ✭ 14 (-87.39%)
Mutual labels:  decorators
Strudel
A front-end framework for the back-end powered web
Stars: ✭ 180 (+62.16%)
Mutual labels:  decorators
Lambda Decorators
🐍λ✨ - A collection of useful decorators for making AWS Lambda handlers
Stars: ✭ 197 (+77.48%)
Mutual labels:  decorators
Kaop Ts
Simple Yet Powerful Library of ES2016 Decorators with Strongly typed method Interceptors like BeforeMethod, AfterMethod, OnException, etc
Stars: ✭ 235 (+111.71%)
Mutual labels:  decorators
Discord.ts
🤖 Create your discord bot by using TypeScript and decorators!
Stars: ✭ 172 (+54.95%)
Mutual labels:  decorators
knockout-decorators
Decorators for use Knockout JS in TypeScript and ESNext environments
Stars: ✭ 45 (-59.46%)
Mutual labels:  decorators
Sequelize Typescript
Decorators and some other features for sequelize
Stars: ✭ 2,200 (+1881.98%)
Mutual labels:  decorators
Plumier
A TypeScript backend framework focuses on development productivity, uses dedicated reflection library to help you create a robust, secure and fast API delightfully.
Stars: ✭ 223 (+100.9%)
Mutual labels:  decorators
djburger
Framework for safe and maintainable web-projects.
Stars: ✭ 75 (-32.43%)
Mutual labels:  decorators
deco
Minimalist Function Decorators for Elixir
Stars: ✭ 21 (-81.08%)
Mutual labels:  decorators
Decorating
decorating: Literally decorating your terminal with decorators
Stars: ✭ 248 (+123.42%)
Mutual labels:  decorators

🏁 Final Form Calculate

NPM Version NPM Downloads Build Status codecov.io styled with prettier

Decorator for 🏁 Final Form that allows you to define calculations that happen between fields, i.e. "When field X changes, update field Y."


Installation

npm install --save final-form-calculate

or

yarn add final-form-calculate

Usage

import { createForm, getIn } from 'final-form'
import createDecorator from 'final-form-calculate'

// Create Form
const form = createForm({ onSubmit })

// Create Decorator
const decorator = createDecorator(
  // Calculations:
  {
    field: 'foo', // when the value of foo changes...
    updates: {
      // ...set field "doubleFoo" to twice the value of foo
      doubleFoo: (fooValue, allValues) => fooValue * 2
    }
  },
  {
    field: 'bar', // when the value of bar changes...
    updates: {
      // ...set field "foo" to previous value of bar
      foo: (fooValue, allValues, prevValues) => prevValues.bar
    }
  },
  {
    field: /items\[\d+\]/, // when a field matching this pattern changes...
    updates: {
      // ...sets field "total" to the sum of all items
      total: (itemValue, allValues) =>
        (allValues.items || []).reduce((sum, value) => sum + value, 0)
    }
  },
  {
    field: 'foo', // when the value of foo changes...
    updates: {
      // ...asynchronously set field "doubleFoo" to twice the value using a promise
      doubleFoo: (fooValue, allValues) =>
        new Promise(resolve => {
          setTimeout(() => resolve(fooValue * 2), 100)
        })
    }
  },
  {
    field: /\.timeFrom/, // when a deeper field matching this pattern changes...
    updates: (value, name, allValues) => {
      const toField = name.replace('timeFrom', 'timeTo')
      const toValue = getIn(allValues, toField)
      if (toValue && value > toValue) {
        return {
          [toField]: value
        }
      }

      return {}
    }
  }
)

// Decorate form
const undecorate = decorator(form)

// Use form as normal

Table of Contents

Example

Calculated Fields Example

Example using 🏁 React Final Form.

API

createDecorator: (...calculations: Calculation[]) => Decorator

A function that takes a set of calculations and returns a 🏁 Final Form Decorator.

Types

Calculation: { field: FieldPattern, isEqual?: (any, any) => boolean, updates: Updates }

A calculation to perform, with an optional isEqual predicate to determine if a value has really changed (defaults to ===).

FieldName: string

FieldPattern: FieldName | RegExp | (FieldName | RegExp)[]

A pattern to match a field with.

Updates: UpdatesByName | UpdatesForAll

Either an object of updater functions or a function that generates updates for multiple fields.

UpdatesByName: { [FieldName]: (value: any, allValues: Object, prevValues: Object) => Promise | any }

Updater functions for each calculated field.

UpdatesForAll: (value: any, field: string, allValues: Object, prevValues: Object) => Promise | { [FieldName]: any }

Takes the value and name of the field that just changed, as well as all the values, and returns an object of fields and new values.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].