From 907e548cb048733dfebd7030672aa4d8600577aa Mon Sep 17 00:00:00 2001 From: yang <2636098325@qq.com> Date: Wed, 22 Feb 2023 19:57:54 +0800 Subject: [PATCH] [input ]: Add formatting api --- examples/docs/zh-CN/input.md | 28 +- packages/input/src/input.vue | 704 ++++++++++++++++++---------------- test/unit/specs/input.spec.js | 26 ++ types/input.d.ts | 12 +- 4 files changed, 432 insertions(+), 338 deletions(-) diff --git a/examples/docs/zh-CN/input.md b/examples/docs/zh-CN/input.md index eb74717db..30561a5e3 100644 --- a/examples/docs/zh-CN/input.md +++ b/examples/docs/zh-CN/input.md @@ -673,7 +673,29 @@ export default { ``` ::: +### 格式化 +:::demo `formatter` 和 `parser` 可配合实现格式化能力(仅`type`为text生效)。 +```html + + +``` +::: ### Input Attributes | 参数 | 说明 | 类型 | 可选值 | 默认值 | @@ -703,8 +725,10 @@ export default { | autofocus | 原生属性,自动获取焦点 | boolean | true, false | false | | form | 原生属性 | string | — | — | | label | 输入框关联的label文字 | string | — | — | -| tabindex | 输入框的tabindex | string | - | - | -| validate-event | 输入时是否触发表单的校验 | boolean | - | true | +| tabindex | 输入框的tabindex | string | — | — | +| validate-event | 输入时是否触发表单的校验 | boolean | — | true | +| formatter | 指定输入值的格式。(仅当 type 是"text"时才能工作) | Function | — | — | +| parser | 指定从格式化器输入中提取的值。(仅当 type 是"text"时才起作用) | Function | — | — | ### Input Slots | name | 说明 | diff --git a/packages/input/src/input.vue b/packages/input/src/input.vue index 26321f1cf..84b723e50 100644 --- a/packages/input/src/input.vue +++ b/packages/input/src/input.vue @@ -1,16 +1,18 @@ diff --git a/test/unit/specs/input.spec.js b/test/unit/specs/input.spec.js index f12b87ba5..f952e1438 100644 --- a/test/unit/specs/input.spec.js +++ b/test/unit/specs/input.spec.js @@ -480,4 +480,30 @@ describe('Input', () => { await waitImmediate(); expect(inputElm4.classList.contains('is-exceed')).to.false; }); + it('formatter Processing test', async() => { + vm = createVue({ + template: ` + + `, + data() { + return { + input: '' + }; + } + }, true); + let inputElm = vm.$el.querySelector('input'); + expect(inputElm.getAttribute('placeholder')).to.equal('请输入内容'); + let evt = document.createEvent('HTMLEvents'); + evt = new Event('input', {'bubbles': true, 'cancelable': true}); + inputElm.value = 'text'; + inputElm.dispatchEvent(evt); + await waitImmediate(); + expect(inputElm.value).to.equal('textinput'); + }); + }); diff --git a/types/input.d.ts b/types/input.d.ts index a67ef88d0..a88e09450 100644 --- a/types/input.d.ts +++ b/types/input.d.ts @@ -12,7 +12,12 @@ export interface AutoSize { /** Maximum rows to show */ maxRows: number } - +export interface FormatterHandler { + /** + * @param value Current value of the text input + */ + (value: string | number): void +} /** Input Component */ export declare class ElInput extends ElementUIComponent { /** Type of input */ @@ -90,6 +95,11 @@ export declare class ElInput extends ElementUIComponent { /** Whether to show wordCount when setting maxLength */ showWordLimit: boolean + /**Format value program */ + formatter:FormatterHandler + + /**Extract value program */ + parser:FormatterHandler /** * Focus the Input component */