Skip to content
Merged
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
19 changes: 15 additions & 4 deletions packages/components/form-item/form-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ import { validateRules, ValidateStatus } from './form-model';
import config from '../common/config';
import { SuperComponent, wxComponent, RelationsOptions } from '../common/src/index';
import usingConfig from '../mixins/using-config';
import { isNumeric } from '../common/validator';

const { prefix } = config;
const parentComponentName = 'form';
const componentName = `form-item`;

/** 规范化 labelWidth,确保输出带有 CSS 单位 */
function normalizeLabelWidth(value: string | number | undefined): string {
if (!value) return '';
if (isNumeric(value)) return `${value}px`;
return String(value);
}

@wxComponent()
export default class FormItem extends SuperComponent {
externalClasses = [
Expand Down Expand Up @@ -35,6 +43,8 @@ export default class FormItem extends SuperComponent {
needResetField: false,
resetValidating: false,
formRules: [],
innerLabelAlign: '',
innerLabelWidth: '',
form: {},
colon: false,
innerShowErrorMessage: true,
Expand All @@ -48,13 +58,14 @@ export default class FormItem extends SuperComponent {
this.form = target;
const { globalConfig } = this.data;
const { requiredMark, labelAlign, labelWidth, showErrorMessage } = this.properties;
const isRequired = target.data.rules[this.properties.name]?.some((rule) => rule.required);
const formRules = target.data.rules?.[this.properties.name];
const isRequired = formRules?.some((rule) => rule.required);

this.setData({
formRules: target.data.rules[this.properties.name],
formRules,
colon: target.data.colon,
labelAlign: labelAlign || target.data.labelAlign || 'right',
labelWidth: labelWidth || target.data.labelWidth,
innerLabelAlign: labelAlign || target.data.labelAlign,
innerLabelWidth: normalizeLabelWidth(labelWidth || target.data.labelWidth),
innerRequiredMark: requiredMark || target.data.requiredMark || globalConfig.requiredMark || isRequired,
innerShowErrorMessage:
typeof showErrorMessage === 'boolean' ? showErrorMessage : target.properties.showErrorMessage,
Expand Down
8 changes: 4 additions & 4 deletions packages/components/form-item/form-item.wxml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<wxs src="../common/utils.wxs" module="_" />

<view
class="{{classPrefix}} {{formItemClass}} {{formClass}}--{{labelAlign}} {{formClass}}-item__{{name}} {{prefix}}-class"
class="{{classPrefix}} {{formItemClass}} {{formClass}}--{{innerLabelAlign}} {{formClass}}-item__{{name}} {{prefix}}-class"
style="{{_._style([style, customStyle])}}"
>
<view class="{{formItemClass}}-wrap {{formItemClass}}--{{labelAlign}} {{prefix}}-class-wrap">
<view class="{{formItemClass}}-wrap {{formItemClass}}--{{innerLabelAlign}} {{prefix}}-class-wrap">
<!-- 标签区域 -->
<view
wx:if="{{label}}"
class="{{_.cls(labelClass, [labelAlign, ['required', innerRequiredMark], ['required-right', innerRequiredMark && requiredMarkPosition === 'right']])}} {{prefix}}-class-label"
style="width: {{labelWidth}}"
class="{{_.cls(labelClass, [innerLabelAlign, ['required', innerRequiredMark], ['required-right', innerRequiredMark && requiredMarkPosition === 'right']])}} {{prefix}}-class-label"
style="width: {{innerLabelWidth}}"
>
<label for="{{for}}">{{label}}</label>
<block wx:if="{{colon}}">{{globalConfig.colonText}}</block>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ exports[`ChatList ChatList base demo works fine 1`] = `
</wx-view>
</t-chat>
<t-chat-actionbar
actionBar="{{
Array [
"quote",
"copy",
"share",
]
}}"
class="popover-actionbar"
longPressPosition="{{null}}"
placement="longpress"
Expand Down
6 changes: 6 additions & 0 deletions packages/tdesign-miniprogram/.changelog/pr-4353.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
pr_number: 4353
contributor: anlyyao
---

- fix(Form): 修复 `rules` 属性为空时,`labelWidth` 属性无效以及控制台报错的问题 @anlyyao ([#4353](https://github.com/Tencent/tdesign-miniprogram/pull/4353))