Skip to content

Commit 6f8e542

Browse files
authored
[uniapp] form 支持嵌套对象 (#4578)
* feat(form): 支持嵌套表单 * feat: 优化navbar右侧插槽样式 * refactor: use coalesce * fix: 修复app下粗体无效问题
1 parent 4a0c594 commit 6f8e542

19 files changed

Lines changed: 157 additions & 141 deletions

File tree

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,47 @@
11
// pages/gulp-error/index.js
22
Page({
3-
43
/**
54
* 页面的初始数据
65
*/
7-
data: {
8-
9-
},
6+
data: {},
107

118
/**
129
* 生命周期函数--监听页面加载
1310
*/
14-
onLoad() {
15-
16-
},
11+
onLoad() {},
1712

1813
/**
1914
* 生命周期函数--监听页面初次渲染完成
2015
*/
21-
onReady() {
22-
23-
},
16+
onReady() {},
2417

2518
/**
2619
* 生命周期函数--监听页面显示
2720
*/
28-
onShow() {
29-
30-
},
21+
onShow() {},
3122

3223
/**
3324
* 生命周期函数--监听页面隐藏
3425
*/
35-
onHide() {
36-
37-
},
26+
onHide() {},
3827

3928
/**
4029
* 生命周期函数--监听页面卸载
4130
*/
42-
onUnload() {
43-
44-
},
31+
onUnload() {},
4532

4633
/**
4734
* 页面相关事件处理函数--监听用户下拉动作
4835
*/
49-
onPullDownRefresh() {
50-
51-
},
36+
onPullDownRefresh() {},
5237

5338
/**
5439
* 页面上拉触底事件的处理函数
5540
*/
56-
onReachBottom() {
57-
58-
},
41+
onReachBottom() {},
5942

6043
/**
6144
* 用户点击右上角分享
6245
*/
63-
onShareAppMessage() {
64-
65-
}
66-
})
46+
onShareAppMessage() {},
47+
});
Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,47 @@
11
// pages/home/home.js
22
Page({
3-
43
/**
54
* 页面的初始数据
65
*/
7-
data: {
8-
9-
},
6+
data: {},
107

118
/**
129
* 生命周期函数--监听页面加载
1310
*/
14-
onLoad() {
15-
16-
},
11+
onLoad() {},
1712

1813
/**
1914
* 生命周期函数--监听页面初次渲染完成
2015
*/
21-
onReady() {
22-
23-
},
16+
onReady() {},
2417

2518
/**
2619
* 生命周期函数--监听页面显示
2720
*/
28-
onShow() {
29-
30-
},
21+
onShow() {},
3122

3223
/**
3324
* 生命周期函数--监听页面隐藏
3425
*/
35-
onHide() {
36-
37-
},
26+
onHide() {},
3827

3928
/**
4029
* 生命周期函数--监听页面卸载
4130
*/
42-
onUnload() {
43-
44-
},
31+
onUnload() {},
4532

4633
/**
4734
* 页面相关事件处理函数--监听用户下拉动作
4835
*/
49-
onPullDownRefresh() {
50-
51-
},
36+
onPullDownRefresh() {},
5237

5338
/**
5439
* 页面上拉触底事件的处理函数
5540
*/
56-
onReachBottom() {
57-
58-
},
41+
onReachBottom() {},
5942

6043
/**
6144
* 用户点击右上角分享
6245
*/
63-
onShareAppMessage() {
64-
65-
}
66-
})
46+
onShareAppMessage() {},
47+
});
Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,47 @@
11
// pages/home/navigateFail/navigateFail.js
22
Page({
3-
43
/**
54
* 页面的初始数据
65
*/
7-
data: {
8-
9-
},
6+
data: {},
107

118
/**
129
* 生命周期函数--监听页面加载
1310
*/
14-
onLoad() {
15-
16-
},
11+
onLoad() {},
1712

1813
/**
1914
* 生命周期函数--监听页面初次渲染完成
2015
*/
21-
onReady() {
22-
23-
},
16+
onReady() {},
2417

2518
/**
2619
* 生命周期函数--监听页面显示
2720
*/
28-
onShow() {
29-
30-
},
21+
onShow() {},
3122

3223
/**
3324
* 生命周期函数--监听页面隐藏
3425
*/
35-
onHide() {
36-
37-
},
26+
onHide() {},
3827

3928
/**
4029
* 生命周期函数--监听页面卸载
4130
*/
42-
onUnload() {
43-
44-
},
31+
onUnload() {},
4532

4633
/**
4734
* 页面相关事件处理函数--监听用户下拉动作
4835
*/
49-
onPullDownRefresh() {
50-
51-
},
36+
onPullDownRefresh() {},
5237

5338
/**
5439
* 页面上拉触底事件的处理函数
5540
*/
56-
onReachBottom() {
57-
58-
},
41+
onReachBottom() {},
5942

6043
/**
6144
* 用户点击右上角分享
6245
*/
63-
onShareAppMessage() {
64-
65-
}
66-
})
46+
onShareAppMessage() {},
47+
});

packages/tdesign-uniapp-chat/site/vite.config.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import vueJsx from '@vitejs/plugin-vue-jsx';
88
import changelog2Json from './web/plugins/changelog-to-json';
99
import tdocPlugin from './web/plugins/plugin-tdoc';
1010

11-
const resolvePath = r => path.resolve(__dirname, r);
11+
const resolvePath = (r) => path.resolve(__dirname, r);
1212

1313
const publicPathMap = {
1414
preview: '/',
1515
production: 'https://static.tdesign.tencent.com/uniapp-chat/',
1616
};
1717

18-
const isCustomElement = tag => tag.startsWith('td-');
18+
const isCustomElement = (tag) => tag.startsWith('td-');
1919

2020
// Rollup 4+ 的 tree-shaking 策略调整, 这里是为了让样式在站点构建正常
21-
const disableTreeShakingPlugin = paths => ({
21+
const disableTreeShakingPlugin = (paths) => ({
2222
name: 'disable-treeshake',
2323
transform(code, id) {
2424
for (const path of paths) {
@@ -37,27 +37,27 @@ const config: UserConfig | (({ mode }: { mode: string }) => UserConfig) = define
3737
const vueAppBase = env.VUE_APP_PUBLICPATH;
3838
const experimentalConfig = vueAppBase
3939
? {
40-
experimental: {
41-
renderBuiltUrl(
42-
filename: string,
43-
{
44-
hostId,
45-
hostType,
46-
type,
47-
}: {
48-
hostId: string;
49-
hostType: string;
50-
type: string;
51-
},
52-
) {
53-
console.log('[experimental] ', hostType, hostId, type, filename);
40+
experimental: {
41+
renderBuiltUrl(
42+
filename: string,
43+
{
44+
hostId,
45+
hostType,
46+
type,
47+
}: {
48+
hostId: string;
49+
hostType: string;
50+
type: string;
51+
},
52+
) {
53+
console.log('[experimental] ', hostType, hostId, type, filename);
5454

55-
// 确保基础路径以 / 结尾
56-
const basePath = vueAppBase.endsWith('/') ? vueAppBase : `${vueAppBase}/`;
57-
return `${basePath}${filename}`;
55+
// 确保基础路径以 / 结尾
56+
const basePath = vueAppBase.endsWith('/') ? vueAppBase : `${vueAppBase}/`;
57+
return `${basePath}${filename}`;
58+
},
5859
},
59-
},
60-
}
60+
}
6161
: {};
6262

6363
return {

packages/uniapp-components/cascader/cascader.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ export default {
573573
this.isSearching = false;
574574
},
575575
onFilterChange(e) {
576-
const value = e?.value ?? e?.detail?.value ?? '';
576+
const value = coalesce(e?.value, e?.detail?.value, '');
577577
if (!this.filterDebounced) {
578578
this.filterDebounced = debounce((kw) => this.applyFilter(kw), 200);
579579
}
@@ -583,7 +583,7 @@ export default {
583583
this.resetFilter();
584584
},
585585
applyFilter(rawKeyword) {
586-
const keyword = String(rawKeyword ?? '').trim();
586+
const keyword = String(coalesce(rawKeyword, '')).trim();
587587
if (!keyword) {
588588
this.resetFilter();
589589
return;

packages/uniapp-components/form-item/form-item.vue

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
' ' +
1212
formItemClass +
1313
'__' +
14-
name +
14+
dataNameKey +
1515
' ' +
1616
errorClasses +
1717
' ' +
@@ -86,7 +86,7 @@ import { isNumeric } from '../common/validator';
8686
import TIcon from '../icon/icon.vue';
8787
import usingConfig from '../mixins/using-config';
8888
89-
import { validateRules, ValidateStatus } from './form-model';
89+
import { validateRules, ValidateStatus, getByPath } from './form-model';
9090
import props from './props';
9191
9292
const parentComponentName = 'form';
@@ -160,6 +160,10 @@ export default {
160160
const contentAlign = this.dataContentAlign;
161161
return contentAlign ? `text-align: ${contentAlign}` : '';
162162
},
163+
// 供 :class 拼接使用:name="pat.acctType" → "pat-acctType",避免 CSS class 里的点号被当成选择器分隔符
164+
dataNameKey() {
165+
return String(this.name || '').replace(/\./g, '-');
166+
},
163167
},
164168
watch: {},
165169
created() {
@@ -217,7 +221,8 @@ export default {
217221
const { name } = this;
218222
if (name && this.form) {
219223
const { formData } = this.form;
220-
this.initialValue = formData[name];
224+
// 支持 name="pat.acctType" 嵌套路径:走 form 表单层的 data 兜底(form-item 拿不到时回退到 form.data)
225+
this.initialValue = coalesce(getByPath(formData, name), getByPath(this.form.data, name));
221226
}
222227
},
223228
@@ -234,7 +239,7 @@ export default {
234239
const { name } = this;
235240
if (name && this.form) {
236241
const { formData } = this.form;
237-
return formData[name];
242+
return coalesce(getByPath(formData, name), getByPath(this.form.data, name));
238243
}
239244
return undefined;
240245
},
@@ -267,7 +272,7 @@ export default {
267272
return { [this.name]: true };
268273
}
269274
270-
const value = data[this.name];
275+
const value = getByPath(data, this.name);
271276
const context = { formData: data, name: this.name };
272277
const results = await validateRules(value, filteredRules, context);
273278

0 commit comments

Comments
 (0)