[strapi]关系归档搜索无法像以前一样工作+布局错误

2024-05-13 561 views
2
错误报告 所需的系统信息
  • Node.js 版本:v16.20.1
  • NPM版本:7.24.2
  • 斯特拉皮版本:4.11.7
  • 数据库:postgres
  • 操作系统:macOS Ventura
  • 你的项目是Javascript还是Typescript:ts
描述错误

在关系字段中搜索时,它返回仅匹配“关系显示字段”开头的结果。之前,它匹配所有结果。

例如,我们有 childPages,用作path主要关系显示字段。键入ubytovanie应该返回bratislava-pre-ukrajinu/ubytovanie,但没有。

手动选择一些关系是可行的。搜索匹配开头的作品。

但布局不知何故被破坏了。

重现行为的步骤
  1. 去 '...'
  2. 点击 '....'
  3. 向下滚动到“...”
  4. 查看错误
预期行为

关系字段中的搜索应该可以正常工作。布局应正确显示。如果它也忽略重音(áéšťýö 等...),那就太好了

截图

图像 图像

图像

回答

9

我可以确认搜索关系时搜索行为的变化。

改变它的代码似乎在 v4.11.4 版本中。在 v4.11.4 之前,搜索的工作方式类似于“包含”过滤器,更新到 v4.11.4 后,它的工作方式类似于“开头为”过滤器。

也许是由#16671引起的

7

这个问题似乎报告了同样的问题#17521

有计划修复它吗?它阻止我们升级到较新的 Strapi,即使有一些安全更新,这种行为只会使我们的管理员用户的工作变得过于复杂。

现在使用的新Combobox组件看起来它同时支持startsWithcontains比较。切换到默认值不是很有用吗contains

7

你好

我可以确认我们有同样的问题。目前版本为 4.13.6,出于安全原因,我们更愿意进行所有最新更新,但这确实使我们编辑的工作变得复杂。

0

@joshuaellis 只是为了清楚起见。我的 PR 确实修复了管理面板 RelationInput 以正确搜索$startsWithi(因为 ComboBox 使用的当前配置startsWith)。它不是$containsi

如果有人想更改为$containsi

您可以在此处 更改https://github.com/strapi/strapi/blame/d2c02ba7d58eb81b8c45c3d12076d6413ecde204/packages/core/admin/admin/src/content-manager/components/RelationInput/RelationInput.js#L224C2-L224C32

autocomplete="list"

autocomplete="none"

它会正确搜索并带有一点跳跃的突出显示

--

而且我的 PR 并不能解决 UI 问题(如果它仍然存在的话)。

我不知道以前的UI是什么样子的。最近刚刚加入

但如果你改变 https://github.com/strapi/strapi/blame/d2c02ba7d58eb81b8c45c3d12076d6413ecde204/packages/core/admin/admin/src/content-manager/components/RelationInput/RelationInput.js#L221

<ComboboxWrapper marginRight="auto" maxWidth={size <= 6 ? '100%' : '70%'} width="100%">

<ComboboxWrapper marginRight="auto" maxWidth="100%" width="100%">

看起来会更好

5

同样在这里。我们必须降低 Strapi 版本,因为这个问题使我们的内容管理器的工作变得更加复杂。

6

同样在这里。我们必须降低 Strapi 版本,因为这个问题使我们的内容管理器的工作变得更加复杂。

我可以确认我们有同样的问题。我们目前使用的是 4.12.5,您降级到哪个版本?看起来它在您的缩减版本中工作正常。

1

同样在这里。我们必须降低 Strapi 版本,因为这个问题使我们的内容管理器的工作变得更加复杂。

我可以确认我们有同样的问题。我们目前使用的是 4.12.5,您降级到哪个版本?看起来它在您的缩减版本中工作正常。

我们使用 4.10.2,因为这是之前可以使用的版本,我刚刚恢复了 PR,并没有调查此行为更改的确切版本。

4

大家好,提到的 PR 并不能解决这个问题(正如 PR 作者所说),请重新打开。

0

大家好,@joshuaellis @derrickmehaffy 这个 PR 没有解决本期提到的任何问题。可以请您重新打开吗?

是否有计划何时恢复以前的功能?

谢谢你!

3

根据请求重新打开,我猜您想要一个选项来切换包含和开头之间使用的模式/过滤器类型?

1

如果我们可以在 contains/startsWith 之间进行选择,那就没问题了。问题是:行为在没有通知的情况下突然从 contains 变为 startWith,这对于内容团队来说是一个真正的痛苦。我的建议:恢复为仅“包含”,并考虑在未来添加startsWith选项(如果相关)?

0

这也破坏了我们的管理。以中间件的形式创建了一个棘手的权宜之计解决方案,也许对其他人同时有用。

  1. 它将过滤器参数更改为 $containsi
  2. 它使用查询字符串作为结果前缀,这样它们就不会在客户端上被过滤掉
屏幕截图 2023-10-19 at 20 47 02@2x

/src/middlewares/fix-search-middleware.js

const { prop, flow } = require("lodash/fp");

// Not pretty, but it seems to work and allows our content team to continue to work

module.exports = (config, { strapi }) => {
  return async (context, next) => {
    const isRelationSearch = context.request.url.startsWith(
      "/content-manager/relations"
    );
    const hasQuery = !!context.query._q;

    // Only run fix if we're searching a relation and there's a query
    if (!isRelationSearch || !hasQuery) {
      return await next();
    }

    // Update query to $containsi instead of $startsWith in:
    // https://github.com/strapi/strapi/commit/b39586bf8d6885ecb23ecd22a9136a8675348d67
    context.query._filter = "$containsi";

    // Perform rest of middlewares and the actual query
    await next();

    // Update results to prefix mainField with the query string so it actually shows up and not filtered out client-side with "startsWith"
    // https://github.com/strapi/design-system/blob/0c8968cd9b9c22ed565b17c8de9120e8b4779abc/packages/primitives/src/components/Combobox/Combobox.tsx#L970-L974
    try {
      const mainField = await getMainField(context, strapi);
      context.response.body.results = context.response.body.results.map(
        (r) => ({
          ...r,
          [mainField]: `${context.query._q} ➡ ${r[mainField]}`,
        })
      );
    } catch (error) {
      console.error("Failed patching the search results");
      console.error(error);
    }
  };
};

const getMainField = async (context, strapi) => {
  // Exstracted from: https://github.com/strapi/strapi/blob/b39586bf8d6885ecb23ecd22a9136a8675348d67/packages/core/content-manager/server/controllers/relations.js
  const { targetField, model } = context.params;
  const modelSchema = strapi.getModel(model);
  const isComponent = modelSchema.modelType === "component";
  const modelConfig = isComponent
    ? await strapi
        .plugin("content-manager")
        .service("components")
        .findConfiguration(modelSchema)
    : await strapi
        .plugin("content-manager")
        .service("content-types")
        .findConfiguration(modelSchema);

  const mainField = flow(
    prop(`metadatas.${targetField}.edit.mainField`),
    (mainField) => mainField || "id"
  )(modelConfig);

  return mainField;
};

添加"global::fix-search-middleware",到 的末尾/config/middlewares.js