fix(小程序独立加工): 成品搜索框改回受控输入(value 绑定 + data 声明 + 事件兜底)——非受控+epoch 重建导致真机输入始终为空
This commit is contained in:
@@ -216,11 +216,11 @@ Component({
|
||||
/** 打开弹窗时一次性载入的在售商品(与 Web 同交互:输入本地即时过滤) */
|
||||
allOutputSkus: [],
|
||||
/**
|
||||
* 搜索框的重建计数:输入框**不绑定 value**(否则数据层一旦出现异常值,框架会把它写回输入框,
|
||||
* 又被 bindinput 当成关键字提交——实测出现过 `search=undefined` 的请求)。
|
||||
* 需要清空时改变这个数组元素,让输入框重新创建。
|
||||
* ⚠️ 2026-10-23 复查结论:搜索框必须**受控**(wxml 绑定 `value="{{outputSkuSearch}}"`)。
|
||||
* 之前为绕开 `undefined` 改成"非受控 + epoch 重建输入框",真机上表现为**输入什么都被清空**,
|
||||
* 根因是重建/未绑定导致框架不回写输入值。现在 data 声明了 `outputSkuSearch`,
|
||||
* 处理器用 `String((e && e.detail && e.detail.value) || '')` 兜底 ⇒ 不会再把 undefined 写进输入框。
|
||||
*/
|
||||
outputSearchEpochList: [0],
|
||||
activeTab: 'work',
|
||||
viewMode: 'today',
|
||||
dateField: 'created_at',
|
||||
@@ -810,19 +810,12 @@ Component({
|
||||
outputSkuSearch: '',
|
||||
outputSkuOptions: [],
|
||||
allOutputSkus: [],
|
||||
outputSearchEpochList: this.nextOutputSearchEpochList(),
|
||||
});
|
||||
this.recalcOutput();
|
||||
// 与 Web 一致:打开弹窗就给出候选,不必先打字
|
||||
this.loadOutputSkuOptions();
|
||||
},
|
||||
|
||||
/** 输入框重建计数(清空输入框用;见 data.outputSearchEpochList 注释) */
|
||||
nextOutputSearchEpochList() {
|
||||
const cur = this.data.outputSearchEpochList;
|
||||
const epoch = Array.isArray(cur) && cur.length ? Number(cur[0]) || 0 : 0;
|
||||
return [epoch + 1];
|
||||
},
|
||||
|
||||
/** 一次性载入在售商品(本地过滤用;limit 走接口上限) */
|
||||
async loadOutputSkuOptions() {
|
||||
@@ -943,8 +936,7 @@ Component({
|
||||
{
|
||||
outputSkuOptions: this.data.allOutputSkus || [],
|
||||
outputSkuSearch: '',
|
||||
outputSearchEpochList: this.nextOutputSearchEpochList(),
|
||||
outputRows: [
|
||||
outputRows: [
|
||||
...this.data.outputRows,
|
||||
{
|
||||
sku_code: opt.sku_code,
|
||||
@@ -972,7 +964,6 @@ Component({
|
||||
outputSkuSearch: '',
|
||||
outputSkuOptions: [],
|
||||
allOutputSkus: [],
|
||||
outputSearchEpochList: this.nextOutputSearchEpochList(),
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
@@ -204,9 +204,8 @@
|
||||
</view>
|
||||
<view class="field">
|
||||
<text class="field-label">成品明细(可多行)</text>
|
||||
<block wx:for="{{outputSearchEpochList}}" wx:key="*this">
|
||||
<input class="field-input" placeholder="搜索成品商品,点下方加入" bindinput="searchOutputSkus" />
|
||||
</block>
|
||||
<!-- 受控输入框:必须绑定 value,否则真机上输入内容会被框架丢弃(2026-10-23 复查:非受控 + epoch 重建 ⇒ 输入始终为空) -->
|
||||
<input class="field-input" placeholder="搜索成品商品,点下方加入" value="{{outputSkuSearch}}" bindinput="searchOutputSkus" />
|
||||
<view wx:if="{{outputSkuOptions.length}}" class="sku-options">
|
||||
<view wx:for="{{outputSkuOptions}}" wx:key="sku_code" class="sku-option" catchtap="addOutputSku" data-sku="{{item.sku_code}}">
|
||||
<text class="sku-name">{{item.sku_name}}</text>
|
||||
|
||||
@@ -85,18 +85,20 @@ it('成品搜索:input 绑定的键必须在 data 中声明', () => {
|
||||
});
|
||||
|
||||
/**
|
||||
* 2026-10-23 真机复现:搜索框选中商品后又出现字面量 `undefined`,且请求是
|
||||
* `sku-options?search=undefined&limit=50` —— 数据层的值被框架回写进输入框、又被当成关键字提交。
|
||||
* 修法:输入框**不再绑定 value**(框架无从写入),需要清空时改变 `outputSearchEpochList` 让输入框重建。
|
||||
* 2026-10-23 真机两轮复现:
|
||||
* ① 先出现字面量 `undefined`(受控但数据层被写入 undefined,又被 bindinput 当关键字提交);
|
||||
* ② 改成"非受控 + epoch 重建"后,真机表现为**输入什么都被清空**。
|
||||
* 现行修法:**受控输入 + data 声明 + 处理器 `|| ''` 兜底**(不再重建输入框)。
|
||||
*/
|
||||
it('搜索框不绑定 value(框架不得回写),清空靠重建', () => {
|
||||
assert.ok(!/value="\{\{outputSkuSearch/.test(wxml), '搜索框不得绑定 outputSkuSearch');
|
||||
assert.ok(wxml.includes('wx:for="{{outputSearchEpochList}}"'), '搜索框应放在重建计数循环里');
|
||||
it('搜索框必须**受控**(绑定 value),且处理器对空事件兜底', () => {
|
||||
// 2026-10-23 复查:改成"非受控 + epoch 重建"后真机表现为"输入什么都被清空" ⇒ 必须受控
|
||||
assert.ok(/value="\{\{outputSkuSearch\}\}"/.test(wxml), '搜索框必须绑定 value(否则真机上输入会被丢弃)');
|
||||
assert.ok(!js.includes('outputSearchEpochList'), '不应再用 epoch 重建输入框');
|
||||
// 只认「搜索处理器内部」的兜底(整个文件里别处也有同样写法,全局 includes 会误判)
|
||||
const searchFn = js.slice(js.indexOf('async searchOutputSkus(e)'), js.indexOf('addOutputSku(e)'));
|
||||
assert.ok(searchFn.includes("String((e && e.detail && e.detail.value) || '')"), '搜索处理器必须对 undefined 事件兜底');
|
||||
const dataBlock = js.slice(js.indexOf(' data: {'), js.indexOf('\n observers:'));
|
||||
assert.ok(/outputSearchEpochList:\s*\[0\]/.test(dataBlock), 'data 缺少 outputSearchEpochList');
|
||||
assert.ok(js.includes('nextOutputSearchEpochList()'));
|
||||
assert.ok((js.match(/outputSearchEpochList: this\.nextOutputSearchEpochList\(\)/g) || []).length >= 3,
|
||||
'打开 / 选中 / 关闭三处都要重建输入框');
|
||||
assert.ok(/outputSkuSearch:\s*''/.test(dataBlock), 'data 必须声明 outputSkuSearch');
|
||||
});
|
||||
|
||||
it('成品搜索与 Web 同交互:打开即载入候选 + 本地即时过滤 + 本地没有再回服务端', () => {
|
||||
|
||||
Reference in New Issue
Block a user