Skip to content

onPaste + setFieldValue does not work as intended #3932

@gnesher

Description

@gnesher

Bug report

Current Behavior

When trying to setFieldValue within onPaste callback prepends the field value instead of replacing it.

Expected behavior

setFieldValue should replace the field value

Reproducible example

The templates are outdated (very old React/Formik versions) I couldn't get them to work without refactoring the entire thing.
This is the code I'm using

onPaste={async (event: ClipboardEvent<HTMLInputElement>) => {
  const reg = /:(?<port>[0-9]+)/
  const valueArray = event.clipboardData.getData('text/plain').split(reg);
  if (Number(valueArray[1])) {
    setFieldValue('url', valueArray[0]);
    setFieldValue('port', valueArray[1]);
  }
}

Your environment

Software Version(s)
Formik 2.2.9
React 17.0.2
TypeScript 4.4.2
Browser Chrome latest
npm/Yarn Yarn
Operating System Ubuntu 20

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions