Skip to content

Commit 159ab6b

Browse files
anlyyaojarmywang
andauthored
feat(Input): support format props (#3213)
* feat(Input): support format props * feat(Input): add format demo --------- Co-authored-by: jarmywang <[email protected]>
1 parent af6f9cf commit 159ab6b

File tree

8 files changed

+32
-0
lines changed

8 files changed

+32
-0
lines changed

src/input/README.en-US.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ cursor-spacing | Number | 0 | \- | N
2323
disabled | Boolean | false | make input to be disabled | N
2424
error-message | String | - | `deprecated` | N
2525
focus | Boolean | false | \- | N
26+
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
2627
hold-keyboard | Boolean | false | \- | N
2728
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
2829
layout | String | horizontal | options: vertical/horizontal | N

src/input/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ cursor-spacing | Number | 0 | 指定光标与键盘的距离,取 input 距离
102102
disabled | Boolean | false | 是否禁用输入框 | N
103103
error-message | String | - | 已废弃。错误提示文本,值为空不显示(废弃属性,如果需要,请更为使用 status 和 tips) | N
104104
focus | Boolean | false | 获取焦点 | N
105+
format | Function | - | 指定输入框展示值的格式。TS 类型:`InputFormatType` `type InputFormatType = (value: InputValue) => string`[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/src/input/type.ts) | N
105106
hold-keyboard | Boolean | false | focus时,点击页面的时候不收起键盘 | N
106107
label | String / Slot | - | 左侧文本。[通用类型定义](https://github.com/Tencent/tdesign-miniprogram/blob/develop/src/common/common.ts) | N
107108
layout | String | horizontal | 标题输入框布局方式。可选项:vertical/horizontal | N

src/input/__test__/__snapshots__/demo.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ exports[`Input Input special demo works fine 1`] = `
201201
</t-input>
202202
<t-input
203203
align="right"
204+
format="{{[Function]}}"
204205
label="价格"
205206
placeholder="0.00"
206207
suffix=""

src/input/_example/special/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ Component({
44
phoneError: false,
55
phoneNumber: '17600600600',
66
priceError: false,
7+
priceFormat: (v) => {
8+
const isNumber = /^\d+(\.\d+)?$/.test(v);
9+
if (isNumber) {
10+
return parseFloat(v).toFixed(2);
11+
}
12+
return v;
13+
},
714
},
815

916
methods: {

src/input/_example/special/index.wxml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
suffix="元"
3434
align="right"
3535
type="number"
36+
format="{{priceFormat}}"
3637
bindchange="onPriceInput"
3738
tips="{{priceError ? '请输入正确的价格' : ''}}"
3839
t-class-tips="tips"

src/input/input.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ export default class Input extends SuperComponent {
102102
},
103103
onBlur(e) {
104104
this.updateClearIconVisible();
105+
106+
// 失焦时处理 format
107+
if (typeof this.properties.format === 'function') {
108+
const v = this.properties.format(e.detail.value);
109+
this.updateValue(v);
110+
this.triggerEvent('blur', { value: this.data.value, cursor: this.data.count });
111+
return;
112+
}
105113
this.triggerEvent('blur', e.detail);
106114
},
107115
onConfirm(e) {

src/input/props.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ const props: TdInputProps = {
7676
type: Boolean,
7777
value: false,
7878
},
79+
/** 指定输入框展示值的格式 */
80+
format: {
81+
type: null,
82+
},
7983
/** focus时,点击页面的时候不收起键盘 */
8084
holdKeyboard: {
8185
type: Boolean,

src/input/type.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ export interface TdInputProps {
117117
type: BooleanConstructor;
118118
value?: boolean;
119119
};
120+
/**
121+
* 指定输入框展示值的格式
122+
*/
123+
format?: {
124+
type: undefined;
125+
value?: InputFormatType;
126+
};
120127
/**
121128
* focus时,点击页面的时候不收起键盘
122129
* @default false
@@ -302,4 +309,6 @@ export interface TdInputProps {
302309
};
303310
}
304311

312+
export type InputFormatType = (value: InputValue) => string;
313+
305314
export type InputValue = string | number;

0 commit comments

Comments
 (0)