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

[Bug Report] field.insert 插入(非第一项和最后一项)内容后,path 内索引下标变成字符串 #4070

Open
1 task
fanfanyir opened this issue Jan 12, 2024 · 2 comments

Comments

@fanfanyir
Copy link

  • I have searched the issues of this repository and believe that this is not a duplicate.

Reproduction link

Edit on CodeSandbox

Steps to reproduce

  1. 点击 add 添加一项内容
  2. 点击 copy 第一行两次
    输出值
    // 0 : ['array']
    // 1: (2) ['array', 0]
    // 2: (2) ['array', '2']
    // 3: (2) ['array', 1]

What is expected?

// 0 : ['array']
// 1: (2) ['array', 0]
// 2: (2) ['array', 1]
// 3: (2) ['array', 2]

What is actually happening?

// 0 : ['array']
// 1: (2) ['array', 0]
// 2: (2) ['array', '2']
// 3: (2) ['array', 1]

Package

@formily/[email protected]


@lumdzeehol
Copy link
Contributor

这个问题发生在 ArrayField 往数组中间insert的时候,向数组尾部添加不会触发该问题。

该 issue 中的 case 在 ArrayField 执行 spliceArrayState 方法的时候,分别发生以下情况:

  • 第一次 copy, 尾部插入,元素不需要 move,插入的 field 对应 address 由 Form.createField() 生成。此时Path 对象由 FormPath.parse('array').concat(1)生成,因此segment的值为 ['array', 1]
  • 第二次 copy, 中间插入,触发 moveIndex 方法生成 fieldPatch。fieldPatch 使用 identifier 作为 address ,identifier为字符串 "array.2",丢失了数组下标的类型,因此 segment 的值为['array', '2']

const moveIndex = (identifier: string) => {
if (offset === 0) return identifier
const preStr = identifier.substring(0, addrLength)
const afterStr = identifier.substring(addrLength)
const number = afterStr.match(NumberIndexReg)?.[1]
if (number === undefined) return identifier
const index = Number(number) + offset
return `${preStr}${afterStr.replace(/^\.\d+/, `.${index}`)}`
}

@janryWang 我尝试将 moveIndex 的返回改成 FormPath.parse(preStr).concat(index) , 但是会破坏 interface INodePatch 的声明,INodePath 中的address 都是字符串类型,白总看看这个能改成Pattern 类型吗,或者还是考虑别的解决办法?

@fanfanyir
Copy link
Author

Is there any update on this issue?

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

2 participants