Unexpected behavior with `insert` function in field arrays - value transformation issue
Description:
When using the `insert` function with field arrays, there appears to be an unexpected transformation of the inserted value. The value being inserted is not preserved as-is, but rather transformed into a unique ID.
Steps to reproduce:
1. Create a field array using `createForm`
2. Use the `insert` function to add a value to the array
3. Check the actual value stored in the field array items
Expected behavior:
```typescript
insert(formStore, field.name, {
at: 0,
value: 2
});
// field.items should contain [2]
```
Actual behavior:
```typescript
insert(formStore, field.name, {
at: 0,
value: 2
});
// field.items contains [3] (or another unique ID)
```
Root cause:
After investigating the source code, I found that in `setFieldArrayValue.js`, the value is being processed through `getUniqueId()` when it's treated as an array. This happens in the `updateStores` function:
```javascript
if (Array.isArray(value)) {
const items = value.map(() => getUniqueId());
setFieldArrayState(form, compoundPath, {
startItems: [...items],
items,
error: '',
touched: false,
dirty: false,
});
}
```
This behavior makes it impossible to store the actual values in the field array items, as they are always transformed into unique IDs.
Possible solutions:
1. Add an option to disable the unique ID generation
2. Modify the behavior to preserve the original values
3. Provide a clear way to map between the unique IDs and the actual values
Environment:
- @modular-forms/solid version: 0.25.1
- SolidJS version: 1.9.5
关闭于 2025-05-20 0 条评论