Skip to content

feat(textarea): support readonly and allowInputOverMax props #3474

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

Merged
merged 3 commits into from
Feb 6, 2025
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
4 changes: 4 additions & 0 deletions src/textarea/README.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

## API


### Textarea Props

name | type | default | description | required
-- | -- | -- | -- | --
style | Object | - | CSS(Cascading Style Sheets) | N
custom-style | Object | - | CSS(Cascading Style Sheets),used to set style on virtual component | N
adjust-position | Boolean | true | \- | N
allow-input-over-max | Boolean | false | \- | N
autofocus | Boolean | false | \- | N
autosize | Boolean / Object | false | Typescript:`boolean \| { maxHeight?: number, minHeight?: number }` | N
bordered | Boolean | false | \- | N
Expand All @@ -26,7 +28,9 @@ label | String / Slot | - | [see more ts definition](https://github.com/Tencent/
maxcharacter | Number | - | \- | N
maxlength | Number | -1 | \- | N
placeholder | String | undefined | \- | N
placeholder-class | String | textarea-placeholder | \- | N
placeholder-style | String | - | \- | N
readonly | Boolean | undefined | \- | N
selection-end | Number | -1 | \- | N
selection-start | Number | -1 | \- | N
show-confirm-bar | Boolean | true | \- | N
Expand Down
6 changes: 5 additions & 1 deletion src/textarea/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ isComponent: true

## API


### Textarea Props

名称 | 类型 | 默认值 | 描述 | 必传
-- | -- | -- | -- | --
style | Object | - | 样式 | N
custom-style | Object | - | 样式,一般用于开启虚拟化组件节点场景 | N
adjust-position | Boolean | true | 键盘弹起时,是否自动上推页面 | N
allow-input-over-max | Boolean | false | 超出maxlength或maxcharacter之后是否还允许输入 | N
autofocus | Boolean | false | 自动聚焦,拉起键盘 | N
autosize | Boolean / Object | false | 是否自动增高,值为 true 时,style.height 不生效。支持传入对象,如 { maxHeight: 120, minHeight: 20 }。TS 类型:`boolean \| { maxHeight?: number, minHeight?: number }` | N
bordered | Boolean | false | 是否显示外边框 | N
Expand All @@ -89,7 +91,9 @@ label | String / Slot | - | 左侧文本。[通用类型定义](https://github.c
maxcharacter | Number | - | 用户最多可以输入的字符个数,一个中文汉字表示两个字符长度 | N
maxlength | Number | -1 | 用户最多可以输入的字符个数,值为 -1 的时候不限制最大长度 | N
placeholder | String | undefined | 占位符 | N
placeholder-class | String | textarea-placeholder | 指定 placeholder 的样式类,目前仅支持color,font-size和font-weight | N
placeholder-style | String | - | 指定 placeholder 的样式,目前仅支持 color ,font-size和font-weight | N
readonly | Boolean | undefined | 只读状态 | N
selection-end | Number | -1 | 光标结束位置,自动聚集时有效,需与 selection-start 搭配使用 | N
selection-start | Number | -1 | 光标起始位置,自动聚集时有效,需与 selection-end 搭配使用 | N
show-confirm-bar | Boolean | true | 是否显示键盘上方带有”完成“按钮那一栏 | N
Expand All @@ -114,7 +118,7 @@ line-change | `(value: TextareaValue)` | 行高发生变化时触发
t-class | 根节点样式类
t-class-indicator | 计数器样式类
t-class-label | 左侧文本样式类
t-class-textarea | 占位符样式类
t-class-textarea | 多行文本框样式类

### CSS Variables

Expand Down
48 changes: 48 additions & 0 deletions src/textarea/__test__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,54 @@ describe('textarea', () => {
await simulate.sleep(0);
expect(component.instance.data.count).toBe(10);
});

it(': allowInputOverMax', async () => {
const handleChange = jest.fn();
const id = simulate.load({
template: `<t-textarea
class="base"
maxcharacter="{{maxcharacter}}"
allowInputOverMax
value="{{value}}"
bind:change="handleChange"
>
</t-textarea>`,
data: {
maxcharacter: 10,
value: 'tdesign',
},
methods: {
handleChange,
},
usingComponents: {
't-textarea': textarea,
},
});
const comp = simulate.render(id);
comp.attach(document.createElement('parent-wrapper'));
const component = comp.querySelector('.base');
expect(component.instance.data.count).toBe(7);

const $textarea = comp.querySelector('.base >>> .t-textarea__wrapper-inner');

$textarea.dispatchEvent('input', { detail: { value: 'tdesign123' } });
await simulate.sleep(0);
expect(handleChange).toHaveBeenCalledTimes(1);
expect(component.instance.data.count).toBe(10);

$textarea.dispatchEvent('input', { detail: { value: 'textarea用于多行文本信息输入' } });
await simulate.sleep(0);
expect(handleChange).toHaveBeenCalledTimes(2);
expect(component.instance.data.count).toBe(28);
expect(handleChange.mock.calls[1][0].detail).toStrictEqual({
value: 'textarea用于多行文本信息输入',
cursor: undefined,
});

$textarea.dispatchEvent('input', { detail: { value: 'textarea用于567' } });
await simulate.sleep(0);
expect(component.instance.data.count).toBe(15);
});
});

describe('slots', () => {
Expand Down
15 changes: 15 additions & 0 deletions src/textarea/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const props: TdTextareaProps = {
type: Boolean,
value: true,
},
/** 超出maxlength或maxcharacter之后是否还允许输入 */
allowInputOverMax: {
type: Boolean,
value: false,
},
/** 自动聚焦,拉起键盘 */
autofocus: {
type: Boolean,
Expand Down Expand Up @@ -94,11 +99,21 @@ const props: TdTextareaProps = {
type: String,
value: undefined,
},
/** 指定 placeholder 的样式类,目前仅支持color,font-size和font-weight */
placeholderClass: {
type: String,
value: 'textarea-placeholder',
},
/** 指定 placeholder 的样式,目前仅支持 color ,font-size和font-weight */
placeholderStyle: {
type: String,
value: '',
},
/** 只读状态 */
readonly: {
type: null,
value: undefined,
},
/** 光标结束位置,自动聚集时有效,需与 selection-start 搭配使用 */
selectionEnd: {
type: Number,
Expand Down
9 changes: 7 additions & 2 deletions src/textarea/textarea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,20 @@ export default class Textarea extends SuperComponent {
},

calculateValue(value, maxcharacter, maxlength) {
const { allowInputOverMax } = this.properties;
if (maxcharacter > 0 && !Number.isNaN(maxcharacter)) {
const { length, characters } = getCharacterLength('maxcharacter', value, maxcharacter);
const { length, characters } = getCharacterLength(
'maxcharacter',
value,
allowInputOverMax ? Infinity : maxcharacter,
);
return {
value: characters,
count: length,
};
}
if (maxlength > 0 && !Number.isNaN(maxlength)) {
const { length, characters } = getCharacterLength('maxlength', value, maxlength);
const { length, characters } = getCharacterLength('maxlength', value, allowInputOverMax ? Infinity : maxlength);
return {
value: characters,
count: length,
Expand Down
4 changes: 2 additions & 2 deletions src/textarea/textarea.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<textarea
class="{{classPrefix}}__wrapper-inner {{disabled? prefix + '-is-disabled' : ''}} {{prefix}}-class-textarea"
style="{{this.textareaStyle(autosize)}}"
maxlength="{{maxlength}}"
disabled="{{disabled}}"
maxlength="{{allowInputOverMax ? -1 : maxlength}}"
disabled="{{disabled || readonly}}"
placeholder="{{placeholder}}"
placeholder-class="{{classPrefix}}__placeholder"
placeholder-style="{{placeholderStyle}}"
Expand Down
23 changes: 23 additions & 0 deletions src/textarea/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export interface TdTextareaProps {
type: BooleanConstructor;
value?: boolean;
};
/**
* 超出maxlength或maxcharacter之后是否还允许输入
* @default false
*/
allowInputOverMax?: {
type: BooleanConstructor;
value?: boolean;
};
/**
* 自动聚焦,拉起键盘
* @default false
Expand Down Expand Up @@ -145,6 +153,14 @@ export interface TdTextareaProps {
type: StringConstructor;
value?: string;
};
/**
* 指定 placeholder 的样式类,目前仅支持color,font-size和font-weight
* @default textarea-placeholder
*/
placeholderClass?: {
type: StringConstructor;
value?: string;
};
/**
* 指定 placeholder 的样式,目前仅支持 color ,font-size和font-weight
* @default ''
Expand All @@ -153,6 +169,13 @@ export interface TdTextareaProps {
type: StringConstructor;
value?: string;
};
/**
* 只读状态
*/
readonly?: {
type: BooleanConstructor;
value?: boolean;
};
/**
* 光标结束位置,自动聚集时有效,需与 selection-start 搭配使用
* @default -1
Expand Down
Loading