form icon indicating copy to clipboard operation
form copied to clipboard

New API - resetField('fieldName')

Open crutchcorn opened this issue 11 months ago • 5 comments

Discussed in https://github.com/TanStack/form/discussions/1159

Originally posted by wilson12365 February 19, 2025 right now the only way to handle reset a field based on other actions of the form is the following

listeners={{
  onChange: ({value}) => {
    const defaultMeta = {
      isValidating: false,
      isTouched: false,
      isBlurred: false,
      isDirty: false,
      errorMap: {}
    }

    if (value === ‘personal’) {
      setFieldValue(‘businessName’, ‘’)
      setFieldValue(‘businessLabel’, ‘’)
      setFieldMeta(’businessName’, defaultMeta)
      setFieldMeta(’businessLabel’, defaultMeta)
    }
}}

@crutchcorn has suggested to add resetField('fieldName')

crutchcorn avatar Feb 25 '25 06:02 crutchcorn

maybe an updateFieldMeta function, setFieldMetaAndValue function and a defaultMeta export would make this a lot more ergonomic but still flexible

blipk avatar Feb 26 '25 02:02 blipk

@crutchcorn, I could pick this up if you could give a little more detail. I'm back from holidays so I should have some time around the weekend to open a pr

So this is form.resetField() and should revert to the default field value and cleared meta?

harry-whorlow avatar Mar 03 '25 12:03 harry-whorlow

@blipk what about my https://github.com/TanStack/form/pull/1125 pr, would that help with the updateFieldMeta part?

harry-whorlow avatar Mar 04 '25 09:03 harry-whorlow

@selah-EN-HC-glitch I just released it to main, so update your packages then you can use form.resetField('firstName')

harry-whorlow avatar Mar 16 '25 18:03 harry-whorlow

Thank you for adding this! For nested fields, this only works on the leaves , and not on the parent field. Is there a solution that propagates the reset from the parent field to all of its nested fields?

Laura1590 avatar Mar 28 '25 15:03 Laura1590

Closed in #1223

harry-whorlow avatar Jun 16 '25 11:06 harry-whorlow