Skip to content

Commit b62ae5f

Browse files
authored
feat(textarea): support readonly and allowInputOverMax props (#3474)
* feat(textarea): support readonly and allowInputOverMax props * chore(textarea): remove extra demo, adjust word count and maxlength when allowInputOverMax is true * chore(textarea): generate api
1 parent 2810efa commit b62ae5f

File tree

7 files changed

+88
-4
lines changed

7 files changed

+88
-4
lines changed

src/textarea/README.en-US.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
## API
44

5+
56
### Textarea Props
67

78
name | type | default | description | required
89
-- | -- | -- | -- | --
910
style | Object | - | CSS(Cascading Style Sheets) | N
1011
custom-style | Object | - | CSS(Cascading Style Sheets),used to set style on virtual component | N
1112
adjust-position | Boolean | true | \- | N
13+
allow-input-over-max | Boolean | false | \- | N
1214
autofocus | Boolean | false | \- | N
1315
autosize | Boolean / Object | false | Typescript:`boolean \| { maxHeight?: number, minHeight?: number }` | N
1416
bordered | Boolean | false | \- | N
@@ -28,6 +30,7 @@ maxlength | Number | -1 | \- | N
2830
placeholder | String | undefined | \- | N
2931
placeholder-class | String | textarea-placeholder | \- | N
3032
placeholder-style | String | - | \- | N
33+
readonly | Boolean | undefined | \- | N
3134
selection-end | Number | -1 | \- | N
3235
selection-start | Number | -1 | \- | N
3336
show-confirm-bar | Boolean | true | \- | N

src/textarea/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ isComponent: true
6565

6666
## API
6767

68+
6869
### Textarea Props
6970

7071
名称 | 类型 | 默认值 | 描述 | 必传
7172
-- | -- | -- | -- | --
7273
style | Object | - | 样式 | N
7374
custom-style | Object | - | 样式,一般用于开启虚拟化组件节点场景 | N
7475
adjust-position | Boolean | true | 键盘弹起时,是否自动上推页面 | N
76+
allow-input-over-max | Boolean | false | 超出maxlength或maxcharacter之后是否还允许输入 | N
7577
autofocus | Boolean | false | 自动聚焦,拉起键盘 | N
7678
autosize | Boolean / Object | false | 是否自动增高,值为 true 时,style.height 不生效。支持传入对象,如 { maxHeight: 120, minHeight: 20 }。TS 类型:`boolean \| { maxHeight?: number, minHeight?: number }` | N
7779
bordered | Boolean | false | 是否显示外边框 | N
@@ -91,6 +93,7 @@ maxlength | Number | -1 | 用户最多可以输入的字符个数,值为 -1
9193
placeholder | String | undefined | 占位符 | N
9294
placeholder-class | String | textarea-placeholder | 指定 placeholder 的样式类,目前仅支持color,font-size和font-weight | N
9395
placeholder-style | String | - | 指定 placeholder 的样式,目前仅支持 color ,font-size和font-weight | N
96+
readonly | Boolean | undefined | 只读状态 | N
9497
selection-end | Number | -1 | 光标结束位置,自动聚集时有效,需与 selection-start 搭配使用 | N
9598
selection-start | Number | -1 | 光标起始位置,自动聚集时有效,需与 selection-end 搭配使用 | N
9699
show-confirm-bar | Boolean | true | 是否显示键盘上方带有”完成“按钮那一栏 | N

src/textarea/__test__/index.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,54 @@ describe('textarea', () => {
9999
await simulate.sleep(0);
100100
expect(component.instance.data.count).toBe(10);
101101
});
102+
103+
it(': allowInputOverMax', async () => {
104+
const handleChange = jest.fn();
105+
const id = simulate.load({
106+
template: `<t-textarea
107+
class="base"
108+
maxcharacter="{{maxcharacter}}"
109+
allowInputOverMax
110+
value="{{value}}"
111+
bind:change="handleChange"
112+
>
113+
</t-textarea>`,
114+
data: {
115+
maxcharacter: 10,
116+
value: 'tdesign',
117+
},
118+
methods: {
119+
handleChange,
120+
},
121+
usingComponents: {
122+
't-textarea': textarea,
123+
},
124+
});
125+
const comp = simulate.render(id);
126+
comp.attach(document.createElement('parent-wrapper'));
127+
const component = comp.querySelector('.base');
128+
expect(component.instance.data.count).toBe(7);
129+
130+
const $textarea = comp.querySelector('.base >>> .t-textarea__wrapper-inner');
131+
132+
$textarea.dispatchEvent('input', { detail: { value: 'tdesign123' } });
133+
await simulate.sleep(0);
134+
expect(handleChange).toHaveBeenCalledTimes(1);
135+
expect(component.instance.data.count).toBe(10);
136+
137+
$textarea.dispatchEvent('input', { detail: { value: 'textarea用于多行文本信息输入' } });
138+
await simulate.sleep(0);
139+
expect(handleChange).toHaveBeenCalledTimes(2);
140+
expect(component.instance.data.count).toBe(28);
141+
expect(handleChange.mock.calls[1][0].detail).toStrictEqual({
142+
value: 'textarea用于多行文本信息输入',
143+
cursor: undefined,
144+
});
145+
146+
$textarea.dispatchEvent('input', { detail: { value: 'textarea用于567' } });
147+
await simulate.sleep(0);
148+
expect(component.instance.data.count).toBe(15);
149+
});
102150
});
103151

104152
describe('slots', () => {

src/textarea/props.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ const props: TdTextareaProps = {
1111
type: Boolean,
1212
value: true,
1313
},
14+
/** 超出maxlength或maxcharacter之后是否还允许输入 */
15+
allowInputOverMax: {
16+
type: Boolean,
17+
value: false,
18+
},
1419
/** 自动聚焦,拉起键盘 */
1520
autofocus: {
1621
type: Boolean,
@@ -104,6 +109,11 @@ const props: TdTextareaProps = {
104109
type: String,
105110
value: '',
106111
},
112+
/** 只读状态 */
113+
readonly: {
114+
type: null,
115+
value: undefined,
116+
},
107117
/** 光标结束位置,自动聚集时有效,需与 selection-start 搭配使用 */
108118
selectionEnd: {
109119
type: Number,

src/textarea/textarea.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,20 @@ export default class Textarea extends SuperComponent {
6161
},
6262

6363
calculateValue(value, maxcharacter, maxlength) {
64+
const { allowInputOverMax } = this.properties;
6465
if (maxcharacter > 0 && !Number.isNaN(maxcharacter)) {
65-
const { length, characters } = getCharacterLength('maxcharacter', value, maxcharacter);
66+
const { length, characters } = getCharacterLength(
67+
'maxcharacter',
68+
value,
69+
allowInputOverMax ? Infinity : maxcharacter,
70+
);
6671
return {
6772
value: characters,
6873
count: length,
6974
};
7075
}
7176
if (maxlength > 0 && !Number.isNaN(maxlength)) {
72-
const { length, characters } = getCharacterLength('maxlength', value, maxlength);
77+
const { length, characters } = getCharacterLength('maxlength', value, allowInputOverMax ? Infinity : maxlength);
7378
return {
7479
value: characters,
7580
count: length,

src/textarea/textarea.wxml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
<textarea
1414
class="{{classPrefix}}__wrapper-inner {{disabled? prefix + '-is-disabled' : ''}} {{prefix}}-class-textarea"
1515
style="{{this.textareaStyle(autosize)}}"
16-
maxlength="{{maxlength}}"
17-
disabled="{{disabled}}"
16+
maxlength="{{allowInputOverMax ? -1 : maxlength}}"
17+
disabled="{{disabled || readonly}}"
1818
placeholder="{{placeholder}}"
1919
placeholder-class="{{classPrefix}}__placeholder {{placeholderClass}}"
2020
placeholder-style="{{placeholderStyle}}"

src/textarea/type.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ export interface TdTextareaProps {
1313
type: BooleanConstructor;
1414
value?: boolean;
1515
};
16+
/**
17+
* 超出maxlength或maxcharacter之后是否还允许输入
18+
* @default false
19+
*/
20+
allowInputOverMax?: {
21+
type: BooleanConstructor;
22+
value?: boolean;
23+
};
1624
/**
1725
* 自动聚焦,拉起键盘
1826
* @default false
@@ -161,6 +169,13 @@ export interface TdTextareaProps {
161169
type: StringConstructor;
162170
value?: string;
163171
};
172+
/**
173+
* 只读状态
174+
*/
175+
readonly?: {
176+
type: BooleanConstructor;
177+
value?: boolean;
178+
};
164179
/**
165180
* 光标结束位置,自动聚集时有效,需与 selection-start 搭配使用
166181
* @default -1

0 commit comments

Comments
 (0)