Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Input): support format props #3213

Merged
merged 2 commits into from
Oct 24, 2024
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
1 change: 1 addition & 0 deletions src/input/README.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ cursor-spacing | Number | 0 | \- | N
disabled | Boolean | false | make input to be disabled | N
error-message | String | - | `deprecated` | N
focus | Boolean | false | \- | N
format | Function | - | input value formatter, `type=number` does not work. if you need to format number, `InputNumber` Component might be better。Typescript:`InputFormatType` `type InputFormatType = (value: InputValue) => string`。[see more ts definition](https://github.com/Tencent/tdesign-miniprogram/tree/develop/src/input/type.ts) | N
hold-keyboard | Boolean | false | \- | N
label | String / Slot | - | text on the left of input。[see more ts definition](https://github.com/Tencent/tdesign-miniprogram/blob/develop/src/common/common.ts) | N
layout | String | horizontal | options: vertical/horizontal | N
Expand Down
1 change: 1 addition & 0 deletions src/input/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ cursor-spacing | Number | 0 | 指定光标与键盘的距离,取 input 距离
disabled | Boolean | false | 是否禁用输入框 | N
error-message | String | - | 已废弃。错误提示文本,值为空不显示(废弃属性,如果需要,请更为使用 status 和 tips) | N
focus | Boolean | false | 获取焦点 | N
format | Function | - | 指定输入框展示值的格式。TS 类型:`InputFormatType` `type InputFormatType = (value: InputValue) => string`。[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/src/input/type.ts) | N
hold-keyboard | Boolean | false | focus时,点击页面的时候不收起键盘 | N
label | String / Slot | - | 左侧文本。[通用类型定义](https://github.com/Tencent/tdesign-miniprogram/blob/develop/src/common/common.ts) | N
layout | String | horizontal | 标题输入框布局方式。可选项:vertical/horizontal | N
Expand Down
1 change: 1 addition & 0 deletions src/input/__test__/__snapshots__/demo.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ exports[`Input Input special demo works fine 1`] = `
</t-input>
<t-input
align="right"
format="{{[Function]}}"
label="价格"
placeholder="0.00"
suffix="元"
Expand Down
7 changes: 7 additions & 0 deletions src/input/_example/special/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ Component({
phoneError: false,
phoneNumber: '17600600600',
priceError: false,
priceFormat: (v) => {
const isNumber = /^\d+(\.\d+)?$/.test(v);
if (isNumber) {
return parseFloat(v).toFixed(2);
}
return v;
},
},

methods: {
Expand Down
1 change: 1 addition & 0 deletions src/input/_example/special/index.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
suffix="元"
align="right"
type="number"
format="{{priceFormat}}"
bindchange="onPriceInput"
tips="{{priceError ? '请输入正确的价格' : ''}}"
t-class-tips="tips"
Expand Down
8 changes: 8 additions & 0 deletions src/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ export default class Input extends SuperComponent {
},
onBlur(e) {
this.updateClearIconVisible();

// 失焦时处理 format
if (typeof this.properties.format === 'function') {
const v = this.properties.format(e.detail.value);
this.updateValue(v);
this.triggerEvent('blur', { value: this.data.value, cursor: this.data.count });
return;
}
this.triggerEvent('blur', e.detail);
},
onConfirm(e) {
Expand Down
4 changes: 4 additions & 0 deletions src/input/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ const props: TdInputProps = {
type: Boolean,
value: false,
},
/** 指定输入框展示值的格式 */
format: {
type: null,
},
/** focus时,点击页面的时候不收起键盘 */
holdKeyboard: {
type: Boolean,
Expand Down
9 changes: 9 additions & 0 deletions src/input/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ export interface TdInputProps {
type: BooleanConstructor;
value?: boolean;
};
/**
* 指定输入框展示值的格式
*/
format?: {
type: undefined;
value?: InputFormatType;
};
/**
* focus时,点击页面的时候不收起键盘
* @default false
Expand Down Expand Up @@ -302,4 +309,6 @@ export interface TdInputProps {
};
}

export type InputFormatType = (value: InputValue) => string;

export type InputValue = string | number;
Loading