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

useDynamicList 的 getIndex 能否指定比较的参数? #2280

Open
Lizhooh opened this issue Aug 3, 2023 · 3 comments
Open

useDynamicList 的 getIndex 能否指定比较的参数? #2280

Lizhooh opened this issue Aug 3, 2023 · 3 comments
Assignees
Labels
feature New feature or request

Comments

@Lizhooh
Copy link

Lizhooh commented Aug 3, 2023

看代码是直接对比,太局限了,基本只能原子类型的数组能用
我的 list 是 { key: string, name: string }[],需要根据 key 找出 index,理解性用 getIndex(key),看源码发现不对劲

 const getIndex = useCallback(
    (key: number) => keyList.current.findIndex((ele) => ele === key),
    [],
  );

建议加个第二个参数

 const getIndex = useCallback(
    (key: number, attr?: string | (val: T) => boolean) => keyList.current.findIndex((ele) => {
         if (typeof attr === 'function') return attr(ele)
         if (typeof  attr === 'string') return ele[attr] === key
         return ele === key
      }),
    [],
  );
@hchlq
Copy link
Collaborator

hchlq commented Aug 7, 2023

key 有什么使用场景是其他类型的吗

@hchlq
Copy link
Collaborator

hchlq commented Aug 7, 2023

可以增加这个能力

@hchlq hchlq self-assigned this Aug 7, 2023
@Lizhooh
Copy link
Author

Lizhooh commented Aug 21, 2023

key 有什么使用场景是其他类型的吗

列表的元素主键,可能为 key,可能为 id,不确定,可能是 string 或 number 类型

@liuyib liuyib added the feature New feature or request label Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants