Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array setFieldValue not behaving as expected. #864

Open
zthng opened this issue Jul 17, 2024 · 0 comments
Open

Array setFieldValue not behaving as expected. #864

zthng opened this issue Jul 17, 2024 · 0 comments

Comments

@zthng
Copy link

zthng commented Jul 17, 2024

Hello,

I understand that this function attempts to update the keys in place if they are defined by the incoming updated value.
However, this kind of breaks setFieldValue in my opinion, where we need to override an entry in the array with the exact value required.

For example, if I had the shape as { a: string, b?: string, c?; string }
and I do form.setFieldValue('myArray', 0, { a: '123' });
I expected the first item in form.state.values.myArray to be exactly
{ a: '123' }
rather than
{ a: '123', b: 'previousBValue', c: 'previousCValue' }

Particularly, lines 57-60.

export function setBy(obj: any, _path: any, updater: Updater<any>) {
const path = makePathArray(_path)
function doSet(parent?: any): any {
if (!path.length) {
return functionalUpdate(updater, parent)
}
const key = path.shift()
if (typeof key === 'string') {
if (typeof parent === 'object') {
if (parent === null) {
parent = {}
}
return {
...parent,
[key]: doSet(parent[key]),
}
}
return {
[key]: doSet(),
}
}
if (Array.isArray(parent) && key !== undefined) {
const prefix = parent.slice(0, key)
return [
...(prefix.length ? prefix : new Array(key)),
doSet(parent[key]),
...parent.slice(key + 1),
]
}
return [...new Array(key), doSet()]
}
return doSet(obj)
}

Is this an intentional use case?
How do I unset the values of b and c in this situation then?

Haha also, please do put me in my place if I misunderstood anything. 🙏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant