jaredpalmer / jaredpalmer/formik

onPaste + setFieldValue does not work as intended

Open
#3,932 2 comments 0 reactions 0 assignees View on GitHub
Type: Bug
Dominant language
TypeScript
Stars
34.3k
Forks
2.8k
PR merge metrics
No merged PRs in 30d

Description

## 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) => {
const reg = /:(?[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

Contributor guide

Open the contributing guide

Research direction

No source file or test is named. Start by reproducing the supplied TypeScript onPaste example with Formik 2.2.9, then trace how setFieldValue handles the paste event and field updates. Done means the url and port values replace the intended fields rather than being prepended, with regression coverage for this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.