Git Product home page Git Product logo

Comments (13)

nik-lampe avatar nik-lampe commented on May 29, 2024 3

Interesting.
This does not happen, when you specify the field as a regex.
And, it gets triggered, but it seems that the changes won't be applied to the values.

Came across this, because I'm looking for a way to do a initial calculation with the initial values

from final-form-calculate.

leonardopolly avatar leonardopolly commented on May 29, 2024 1

@cordial, I got to work around this by wrapping createDecorator function in my own really small decorator that returns the original decorator called with form as its argument. In the updates property you can now access form and check if it is pristine. If it is, simply return an empty object :)

Instead of writing

const myDecorator = createDecorator({
  field: 'fieldA',
  updates: {
    b: (fieldAValue, allValues) => { 
      //your calculation 
    }
  }
});

you can write

const myDecorator = form => createDecorator({
  field: 'fieldA',
  updates: form.getState().pristine? {} : {
    b: (fieldAValue, allValues) => { 
      //your calculation 
    }
  }
})(form)

from final-form-calculate.

MrBlackRain avatar MrBlackRain commented on May 29, 2024 1

Sorry for necroposting.
I recently stumbled across the same problem.
@leonardopolly suggestion didn't work for me in case when updates is a function, not an object.
So I just put an if statement inside a function, like this

const myDecorator = form => createDecorator({
  field: 'fieldA',
  updates: (fieldAValue, fieldName, allValues, prevAllValues) => { 
      if (form.getState().pristine) return {}
      //your calculation 
    }
  }
})(form)

Maybe, it will be useful for somebody.

from final-form-calculate.

Babbz75 avatar Babbz75 commented on May 29, 2024

Did anyone find a work around for this?

from final-form-calculate.

Soundvessel avatar Soundvessel commented on May 29, 2024

Perhaps you can use the field's pristine prop to watch for a change on that specific field? Pristine will respect initialValues.
https://github.com/final-form/final-form#pristine-boolean

Edit: Although I don't see the API able to get meta of the field?

from final-form-calculate.

Soundvessel avatar Soundvessel commented on May 29, 2024

Just a thought but is there a chance the form is rendered before the initial values are available and adding them to an existing form could be the culprit? This could be confirmed either with the React profiler tool or logic to prevent render until you have the data for initialValues ready?

from final-form-calculate.

Dragomir-Ivanov avatar Dragomir-Ivanov commented on May 29, 2024

I think that this is valid behavior. I am using exactly this functionality, in order to calculate additional fields, based on initial ones.

from final-form-calculate.

AdamGerthel avatar AdamGerthel commented on May 29, 2024

@Dragomir-Ivanov Right, but just because use exploit that fact that it functions like this doesn't mean that it's expected behaviour.

I personally would expect an "update" hook to run on updates only, not creation/initialisation. I mean, that could easily be handled outside of Final Form entirely.

I do think that this is a an issue that would potentially cause a new major release since it might break a lot of existing uses of this plugin.

from final-form-calculate.

AdamGerthel avatar AdamGerthel commented on May 29, 2024

Just a thought but is there a chance the form is rendered before the initial values are available and adding them to an existing form could be the culprit? This could be confirmed either with the React profiler tool or logic to prevent render until you have the data for initialValues ready?

@Soundvessel If you look at my sandbox sample in the original post, you'll see that the value is there from the start.

from final-form-calculate.

Dragomir-Ivanov avatar Dragomir-Ivanov commented on May 29, 2024

@AdamGerthel You may be right. For the time being I guess you can fork your own Decorator, final-form-calculate is rather small, until this issue is settled.

from final-form-calculate.

cordial avatar cordial commented on May 29, 2024

Is there any way round this? I load my initial values from a DB and this sets off an update which can make the values different from the DB values.

from final-form-calculate.

dozen1488 avatar dozen1488 commented on May 29, 2024

@cordial, I got to work around this by wrapping createDecorator function in my own really small decorator that returns the original decorator called with form as its argument. In the updates property you can now access form and check if it is pristine. If it is, simply return an empty object :)

Instead of writing

const myDecorator = createDecorator({
  field: 'fieldA',
  updates: {
    b: (fieldAValue, allValues) => { 
      //your calculation 
    }
  }
});

you can write

const myDecorator = form => createDecorator({
  field: 'fieldA',
  updates: form.getState().pristine? {} : {
    b: (fieldAValue, allValues) => { 
      //your calculation 
    }
  }
})(form)

How can you get access to the form state, when you need to set decorator to render form, and only inside form render callback you can get form state.

from final-form-calculate.

leonardopolly avatar leonardopolly commented on May 29, 2024

@dozen1488

How can you get access to the form state, when you need to set decorator to render form, and only inside form render callback you can get form state.

According to the official docs, a decorator is a function that receives the form api as a parameter, subscribes to it, making changes as the state changes, and returns an Unsubscribe function.
https://final-form.org/docs/final-form/types/Decorator

So when you create a decorator using the createDecorator function, that function call is returning another function in the shape of formApi => { // whatever change you told the decorator to make }

from final-form-calculate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.