Skip to content

fix: reset button restores searchable_default values instead of clearing all filters#596

Draft
Copilot wants to merge 2 commits into6.10from
copilot/optimize-reset-behavior
Draft

fix: reset button restores searchable_default values instead of clearing all filters#596
Copilot wants to merge 2 commits into6.10from
copilot/optimize-reset-behavior

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 25, 2026

When searchable_default is configured, the search form reset button was clearing all __searchable__ fields to null instead of restoring the configured defaults.

Changes in fields_filter.jsonCancelScript

  • Restore defaults on reset: After nulling all __searchable__ fields, re-reads searchable_default (injected at build time via JSON.stringify, same pattern as dataProviderInited), evaluates amis formulas with AmisCore.evaluate(), converts via SteedosUI.getSearchFilterFormValues(), and merges into removedValues
  • __changedFilterFormValues: Set to defaultFormValues instead of {} so the subsequent form submit triggers search with correct defaults
  • Red dot indicator: isFieldsFilterEmpty now computed from defaultFormValues rather than hardcoded true

Both isLookup and non-isLookup branches are covered. When no searchable_default is configured, behavior is unchanged (defaultFormValues is {}).

// Build-time injection (matches dataProviderInited pattern)
let searchableDefaultData = ${_.isObject(val) ? JSON.stringify(val) : ...} || {};

// Runtime: evaluate formulas, convert to __searchable__ prefixed keys, merge
if (_.isObject(searchableDefaultData) && !_.isEmpty(searchableDefaultData)){
  _.each(searchableDefaultData, function(v, k){
    if (typeof v === "string" && v.indexOf("\${") > -1)
      searchableDefaultData[k] = AmisCore.evaluate(v, event.data);
  });
  defaultFormValues = SteedosUI.getSearchFilterFormValues(searchableDefaultData, fields) || {};
  Object.assign(removedValues, defaultFormValues);
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • registry.npmmirror.com
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node node /usr/local/bin/yarn install (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>【搜索表单重置行为优化】重置按钮应恢复 searchable_default 默认过滤条件,而非全清空</issue_title>
<issue_description>## 问题描述

当前在列表视图或 lookup 字段 picker 弹窗的搜索表单内,点击"重置"按钮(btn_filter_form_cancel),会将所有 __searchable__ 前缀的字段全部清空(置为 null)。当配置了 searchable_default 时,这个行为不符合预期。

期望行为

如果已配置 searchable_default,点击重置应将搜索条件恢复为 searchable_default 配置的默认值,而不是全部清空。无 searchable_default 配置时保持现有全清空逻辑。


需要修改的文件

1. 核心文件:packages/@steedos-widgets/amis-lib/src/lib/converter/amis/fields_filter.js

修改 getObjectFieldsFilterBarSchema 函数中的 onCancelScript 变量(约第 176-260 行)。

当前逻辑(需要修改):

// 第 185-209 行
const removedValues = {};
for(var k in filterFormValues){
  if(/^__searchable__/.test(k)){
    removedValues[k] = null;  // 当前:全部清空
  }
}
// ... 对 isLookup 和非 isLookup 分支都是直接 set null
filterForm.setValues(removedValues);

期望修改为:

  1. 先将所有 __searchable__ 字段设为 null(清空当前值)
  2. 重新读取 searchable_default 配置(ctx.searchable_default,在构建时已通过 JSON.stringify 注入,参考同文件中 dataProviderInited 脚本第 321 行的写法)
  3. 对含 amis 公式(如 ${NOW()})的值用 AmisCore.evaluate() 求值
  4. 调用 SteedosUI.getSearchFilterFormValues(searchableDefault, fields) 转为带 __searchable__ 前缀的表单数据
  5. 将默认值合并到 removedValues 中:Object.assign(removedValues, defaultFormValues)
  6. filterForm.setValues(removedValues) 恢复默认
  7. __changedFilterFormValues 设为默认值(而非空对象),然后提交表单触发搜索

2. 参考代码

searchable_default 求值和转换的参考实现(同文件 dataProviderInited 中第 321-331 行):

let searchableFilterData = <searchableDefault的JSON> || {};
if (_.isObject(searchableFilterData) || !_.isEmpty(searchableFilterData)){
  _.each(searchableFilterData, function(v, k){
    const isAmisFormulaValue = typeof v === "string" && v.indexOf("\${") > -1;
    if (isAmisFormulaValue){
      searchableFilterData[k] = AmisCore.evaluate(v, data);
    }
  });
  let fields = data.uiSchema && data.uiSchema.fields;
  searchableFilterData = SteedosUI.getSearchFilterFormValues(searchableFilterData, fields);
}

onCancelScript 中需要复用这段逻辑。注意 onCancelScript 是运行时脚本,searchableDefault 的原始值需要在构建时通过 JSON.stringify 内联进去(和 dataProviderInited 的方式一致)。

SteedosUI.getSearchFilterFormValues 工具函数位于 packages/@steedos-widgets/steedos-lib/src/ui/index.jsx,作用是将原始键值对转换为带 __searchable__ 前缀的表单数据,其中 number/currency/date/datetime/time 类型字段会自动加上 between__ 中缀。

3. isLookup 和非 isLookup 两个分支都需要修改

  • isLookup 分支(第 205-208 行):lookup 场景不使用本地缓存,但同样需要恢复 searchable_default 默认值
  • 非 isLookup 分支(第 191-203 行):需要同时清除 sessionStorage 中的本地过滤条件缓存

4. 搜索按钮红点状态

当前重置后固定设置 isFieldsFilterEmpty: true(第 259 行)。修改后需要根据恢复的默认值是否为空来判断:

let isFieldsFilterEmpty = SteedosUI.isFilterFormValuesEmpty(defaultFormValues);
crudService && crudService.setData({isFieldsFilterEmpty, showFieldsFilter: false});

注意事项

  • searchable_default 中的 amis 公式值需要在重置时实时重新求值(如 NOW() 每次调用结果不同)
  • 重置逻辑应与初始化(dataProviderInited)行为保持一致,避免用户体验割裂
  • 需要覆盖测试:有/无 searchable_default 配置、含公式/不含公式、isLookup/非 isLookup 等场景
  • onCancelScript 中获取 uiSchema.fields 可通过 event.data.uiSchema.fields 获取</issue_description>

Comments on the Issue (you are @copilot in this section)

@yinlianghui https://github.com/copilot/c/c2fdd98e-6811-4455-8694-b8acf4343091

💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

…ing all filters

When searchable_default is configured, clicking the reset button now restores
the default filter values instead of clearing all fields to null. Amis formula
values (e.g. ${NOW()}) are re-evaluated at reset time. The search button red
dot indicator correctly reflects whether default values are non-empty.

Co-authored-by: yinlianghui <6219465+yinlianghui@users.noreply.github.com>
Agent-Logs-Url: https://github.com/steedos/steedos-widgets/sessions/4d0ad638-7626-425c-a8cd-57fa2e760274
Copilot AI changed the title [WIP] Optimize reset button behavior in search form fix: reset button restores searchable_default values instead of clearing all filters Mar 25, 2026
Copilot AI requested a review from yinlianghui March 25, 2026 09:34
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

Successfully merging this pull request may close these issues.

【搜索表单重置行为优化】重置按钮应恢复 searchable_default 默认过滤条件,而非全清空

2 participants