Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/uniapp-components/cascader/cascader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ export default {
this.isSearching = false;
},
onFilterChange(e) {
const value = e?.value ?? e?.detail?.value ?? '';
const value = coalesce(e?.value, e?.detail?.value, '');
if (!this.filterDebounced) {
this.filterDebounced = debounce((kw) => this.applyFilter(kw), 200);
}
Expand All @@ -583,7 +583,7 @@ export default {
this.resetFilter();
},
applyFilter(rawKeyword) {
const keyword = String(rawKeyword ?? '').trim();
const keyword = String(coalesce(rawKeyword, '')).trim();
if (!keyword) {
this.resetFilter();
return;
Expand Down
15 changes: 10 additions & 5 deletions packages/uniapp-components/form-item/form-item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
' ' +
formItemClass +
'__' +
name +
dataNameKey +
' ' +
errorClasses +
' ' +
Expand Down Expand Up @@ -86,7 +86,7 @@ import { isNumeric } from '../common/validator';
import TIcon from '../icon/icon.vue';
import usingConfig from '../mixins/using-config';

import { validateRules, ValidateStatus } from './form-model';
import { validateRules, ValidateStatus, getByPath } from './form-model';
import props from './props';

const parentComponentName = 'form';
Expand Down Expand Up @@ -160,6 +160,10 @@ export default {
const contentAlign = this.dataContentAlign;
return contentAlign ? `text-align: ${contentAlign}` : '';
},
// 供 :class 拼接使用:name="pat.acctType" → "pat-acctType",避免 CSS class 里的点号被当成选择器分隔符
dataNameKey() {
return String(this.name || '').replace(/\./g, '-');
},
},
watch: {},
created() {
Expand Down Expand Up @@ -217,7 +221,8 @@ export default {
const { name } = this;
if (name && this.form) {
const { formData } = this.form;
this.initialValue = formData[name];
// 支持 name="pat.acctType" 嵌套路径:走 form 表单层的 data 兜底(form-item 拿不到时回退到 form.data)
this.initialValue = coalesce(getByPath(formData, name), getByPath(this.form.data, name));
}
},

Expand All @@ -234,7 +239,7 @@ export default {
const { name } = this;
if (name && this.form) {
const { formData } = this.form;
return formData[name];
return coalesce(getByPath(formData, name), getByPath(this.form.data, name));
}
return undefined;
},
Expand Down Expand Up @@ -267,7 +272,7 @@ export default {
return { [this.name]: true };
}

const value = data[this.name];
const value = getByPath(data, this.name);
const context = { formData: data, name: this.name };
const results = await validateRules(value, filteredRules, context);

Expand Down
43 changes: 43 additions & 0 deletions packages/uniapp-components/form-item/form-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,46 @@ export async function validate(value: any, rules: any[], context?: { formData: a

// 兼容已有导入
export const validateRules = validate;

/**
* 按点号路径从对象中取值。
* 支持 'a'、'a.b'、'a.b.c' 三种形式;当任意一级不是对象或不存在时返回 undefined。
* 用于支持 form-item 的 name="pat.acctType" 嵌套对象属性命名。
*/
export function getByPath(obj: any, path: string): any {
if (obj == null || !path) return undefined;
if (path.indexOf('.') === -1) return obj[path];

const keys = path.split('.');
let cursor: any = obj;
for (let i = 0; i < keys.length; i += 1) {
if (cursor == null) return undefined;
cursor = cursor[keys[i]];
}
return cursor;
}

/**
* 按点号路径向对象中写值,沿途不存在的中转对象会被创建为 {}。
* 返回 boolean 表示是否成功写入(仅 path 非法时返回 false)。
*/
export function setByPath(obj: any, path: string, value: any): boolean {
if (obj == null || !path) return false;
if (path.indexOf('.') === -1) {
obj[path] = value;
return true;
}

const keys = path.split('.');
let cursor: any = obj;
for (let i = 0; i < keys.length - 1; i += 1) {
const key = keys[i];
const next = cursor[key];
if (next == null || typeof next !== 'object') {
cursor[key] = {};
}
cursor = cursor[key];
}
cursor[keys[keys.length - 1]] = value;
return true;
}
12 changes: 7 additions & 5 deletions packages/uniapp-components/form/_example/horizontal/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@
/>
</t-form-item>

<t-form-item arrow label="籍贯" name="place" content-align="left">
<t-form-item arrow label="籍贯" name="address.city" content-align="left">
<t-input
ref="input"
:value="formData.place"
:value="formData.address.city"
:disabled="disabled"
borderless
align="right"
Expand Down Expand Up @@ -224,7 +224,9 @@ export default {
password: '',
gender: '',
birth: '2020-01-01',
place: '',
address: {
city: '',
},
age: 3,
description: 2,
resume: '',
Expand Down Expand Up @@ -409,7 +411,7 @@ export default {
description: [{ validator: (val) => val > 3, message: '分数过低会影响整体评价' }],
gender: [{ validator: (val) => val !== '', message: '不能为空' }],
birth: [{ validator: (val) => val !== '', message: '不能为空' }],
place: [{ validator: (val) => val !== '', message: '不能为空' }],
'address.city': [{ validator: (val) => val !== '', message: '不能为空' }],
resume: [{ validator: (val) => val !== '', message: '不能为空' }],
},
};
Expand Down Expand Up @@ -456,7 +458,7 @@ export default {
onChangeCascader(e) {
const { selectedOptions } = e;
const placeText = selectedOptions?.map((item) => item.label).join('/');
this.formData.place = placeText;
this.formData.address.city = placeText;
this.visibleCascader = false;
},
showCascader() {
Expand Down
9 changes: 6 additions & 3 deletions packages/uniapp-components/form/form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { coalesce } from '../common/utils';
import tools from '../common/utils.wxs';

import props from './props';
import { getByPath, setByPath } from '../form-item/form-model';

const name = `${prefix}-form`;

Expand Down Expand Up @@ -83,7 +84,8 @@ export default {
// 更新表单数据
updateFormData(name, value) {
const { formData } = this;
formData[name] = value;
// 支持 name="pat.acctType" 嵌套路径:按点号路径写值,自动补齐中转对象
setByPath(formData, name, value);
this.formData = formData;
},

Expand Down Expand Up @@ -230,7 +232,7 @@ export default {
if (resetType === 'empty') {
this.updateFormData(child.name, this.getEmptyValue(child.name));
} else if (resetType === 'initial') {
this.updateFormData(child.name, initialData[child.name]);
this.updateFormData(child.name, getByPath(initialData, child.name));
}
child.resetField();
});
Expand Down Expand Up @@ -265,7 +267,8 @@ export default {
// 获取空值
getEmptyValue(name) {
const { formData } = this;
const currentValue = formData[name];
// 支持 name="pat.acctType" 嵌套路径
const currentValue = getByPath(formData, name);

if (Array.isArray(currentValue)) {
return [];
Expand Down
4 changes: 2 additions & 2 deletions packages/uniapp-components/image-viewer/image-viewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
<script>
import { prefix } from '../common/config';
import { uniComponent } from '../common/src/index';
import { styles, calcIcon, systemInfo } from '../common/utils';
import { styles, calcIcon, systemInfo, coalesce } from '../common/utils';

import tools from '../common/utils.wxs';
import TIcon from '../icon/icon';
Expand Down Expand Up @@ -318,7 +318,7 @@ export default {
},

onMovableScale(e) {
const scale = e?.detail?.scale ?? 1;
const scale = coalesce(e?.detail?.scale, 1);
this.currentScale = scale;
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@
<view>
<t-navbar t-class="custom-navbar" left-arrow title="标题文字" :fixed="false" @right-click="handleRightClick">
<template #right>
<div>右侧内容</div>
<t-icon name="ellipsis" size="24px" />
<!-- <div>右侧内容</div> -->
</template>
</t-navbar>
</view>
</template>

<script>
import TNavbar from '@tdesign/uniapp/navbar/navbar.vue';
import TIcon from '@tdesign/uniapp/icon/icon.vue';

export default {
options: {
styleIsolation: 'shared',
},
components: {
TNavbar,
TIcon,
},
data() {
return {};
Expand Down
17 changes: 16 additions & 1 deletion packages/uniapp-components/navbar/navbar.less
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@
box-sizing: content-box;
}

&__left {
&__left,
&__right {
position: relative;
box-sizing: border-box;
}

&__left {
max-width: var(--td-navbar-left-max-width);
overflow: hidden;
display: flex;
Expand All @@ -93,6 +97,17 @@
}
}

&__right {
position: absolute;
top: 0;
bottom: 0;
right: 0;
display: flex;
align-items: center;
margin-right: @spacer-1;
transition: opacity 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
}

&__capsule {
box-sizing: border-box;
width: @navbar-capsule-width;
Expand Down
7 changes: 5 additions & 2 deletions packages/uniapp-components/navbar/navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,16 @@ export default {

const iBoxStyle = {
'--td-navbar-padding-top': `${windowInfo.statusBarHeight}px`,
'--td-navbar-right': `${windowInfo.windowWidth - iMenuRect.left}px`, // 导航栏右侧小程序胶囊按钮宽度
'--td-navbar-left-max-width': `${iMenuRect.left}px`, // 左侧内容最大宽度
'--td-navbar-capsule-height': `${iMenuRect.height}px`, // 胶囊高度
'--td-navbar-capsule-width': `${iMenuRect.width}px`, // 胶囊宽度
'--td-navbar-height': `${(iMenuRect.top - windowInfo.statusBarHeight) * 2 + iMenuRect.height}px`,
};
// #ifdef H5 || APP
// #ifdef MP
iBoxStyle['--td-navbar-right'] = `${windowInfo.windowWidth - iMenuRect.left}px`; // 小程序端保留胶囊按钮宽度
// #endif
// #ifndef MP
iBoxStyle['--td-navbar-right'] = '0px'; // 非小程序端不预留胶囊按钮空间,对齐 mobile-vue
delete iBoxStyle['--td-navbar-height'];
// #endif

Expand Down
4 changes: 2 additions & 2 deletions packages/uniapp-components/picker-item/picker-item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
import { prefix } from '../common/config';
import { ChildrenMixin, RELATION_MAP } from '../common/relation';
import { uniComponent } from '../common/src/index';
import { nextTick } from '../common/utils';
import { nextTick, coalesce } from '../common/utils';
import tools from '../common/utils.wxs';
import TIcon from '../icon/icon.vue';

Expand Down Expand Up @@ -325,7 +325,7 @@ export default {
if (optionsCount > 500) {
// 构建临时 Map(只在查找时构建,不缓存)
const valueMap = new Map(formatOptions.map((item, idx) => [item[keys?.value], idx]));
index = valueMap.get(value) ?? -1;
index = coalesce(valueMap.get(value), -1);
} else {
index = formatOptions.findIndex((item) => item[keys?.value] === value);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/uniapp-components/segmented/segmented.vue
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default {
}
return {
...option,
label: option.label ?? String(option.value),
label: coalesce(option.label, String(option.value)),
icon: option.icon ? calcIcon(option.icon) : null,
};
});
Expand Down
Loading