formik icon indicating copy to clipboard operation
formik copied to clipboard

isDirty but for individual inputs | check if specific inputs are dirty

Open TamirCode opened this issue 2 years ago • 1 comments

Feature request

Current Behavior

Currently there is no clean way to check if an input has been modified from it's initial value. Touched does not accomplish this, however touched is specific to each input, which is what I need but for dirty inputs. The current way to accomplish this is

console.log(formik.getFieldMeta("inputName").value === formik.getFieldMeta("inputName").initialValue) // boolean

Desired Behavior

Suggested Solution

My suggestion is to have a way to check if an individual input is dirty, just like you can do with touched. If I have a form that edits an item, I want to color inputs that have been modified from their initial value, but there is no simple way of doing this in formik, but formik is supposed to help make our code less verbose and help us do simple tasks quickly. Ideally formik.isDirty will be just like formik.touched , where it would have all the individual inputs, and you can still use it as a boolean by parsing the empty object, and in that way it would keep the current functionality that isDirty has (checking if ANY of the inputs are dirty) Though changing isDirty to an object might be an issue because all the typescript users will have to go back and parse it as boolean when they update their formik version. There are other ways such as adding a .dirty instead of .isDirty , or having an isDirty property in each field's metadata.

Who does this impact? Who is this for?

This feature impacts anyone who needs to check if an individual input has been dirtied, and this is a common use case especially when you have an form which edits an item that has default initial values already, you would want to help the user by showing him which fields he has modified with different classes or styles.

Describe alternatives you've considered

Currently I am making my own function to check specific input dirt, but I am very opposed to this as formik should have something like this built in, and maybe not even in a function which also looks bad for booleans

	function isInputDirty(name: keyof IInput) {
		let boolean = formik.getFieldMeta(name).value === formik.getFieldMeta(name).initialValue
		return !boolean
	}

Additional context

TamirCode avatar Aug 09 '22 11:08 TamirCode

related https://github.com/jaredpalmer/formik/issues/1421 https://github.com/jaredpalmer/formik/issues/2909

TamirCode avatar Aug 09 '22 11:08 TamirCode