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

关于useSelections的一个想法 #2497

Closed
coder-xiaotian opened this issue Mar 8, 2024 · 2 comments
Closed

关于useSelections的一个想法 #2497

coder-xiaotian opened this issue Mar 8, 2024 · 2 comments

Comments

@coder-xiaotian
Copy link

项目中经常会有类似下面的列表选项,搜索条件变化后需要重新请求数据,我对useSelections做了一点改造以支持items变化后保持之前的选择项不变,代码如下:
2024-03-08 16 06 03

export default function (items, defaultSelected, itemKey) {
  const selections = useSelections(items, defaultSelected);
  useEffect(() => {
    if (!selections.selected.length) return;

    const list = [];
    if (itemKey) {
      items.forEach((o) => {
        if (selections.selected.find((s) => s[itemKey] === o[itemKey])) {
          list.push(o);
        }
      });
    } else {
      items.forEach((o) => {
        if (selections.selected.find((s) => s === o)) {
          list.push(o);
        }
      });
    }
    selections.setSelected(list);
  }, [items]);

  return selections;
}

问题

我能否提一个pr把这段代码加到ahooks中去,我觉得这种场景应该很常见。

@liuyib
Copy link
Collaborator

liuyib commented Mar 11, 2024

useSelections 一直都支持 items 动态变化后,仍然保持选中。官网的例子就是动态的 items: https://ahooks.js.org/zh-CN/hooks/use-selections#%E5%9F%BA%E7%A1%80%E7%94%A8%E6%B3%95

你这里有这个问题,我猜是因为你使用了对象数组吧?目前 useSelections 是不支持对象数组的,已经有 PR 在处理支持对象数组了:#2485

@coder-xiaotian
Copy link
Author

useSelections 一直都支持 items 动态变化后,仍然保持选中。官网的例子就是动态的 items: https://ahooks.js.org/zh-CN/hooks/use-selections#%E5%9F%BA%E7%A1%80%E7%94%A8%E6%B3%95

你这里有这个问题,我猜是因为你使用了对象数组吧?目前 useSelections 是不支持对象数组的,已经有 PR 在处理支持对象数组了:#2485

是的,我的数据结构就是对象数组,既然已经有了,那我就期待下🥰

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