Skip to content

Commit 7e5f16e

Browse files
authored
feat(search): add readonly & clear-trigger props (#3430)
* feat(search): add readonly & clear-trigger for search * feat(search): add readme and tpyes --------- Co-authored-by: xinzzzhang <spacecave>
1 parent 59978c3 commit 7e5f16e

File tree

6 files changed

+52
-7
lines changed

6 files changed

+52
-7
lines changed

src/search/README.en-US.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ action | String / Slot | '' | [see more ts definition](https://github.com/Tencen
1111
adjust-position | Boolean | true | \- | N
1212
always-embed | Boolean | false | \- | N
1313
center | Boolean | false | \- | N
14+
clear-trigger | String | 'always' | Clear icon trigger, effective only when the input has a value. 。options: always/focus | N
1415
clearable | Boolean | true | \- | N
1516
confirm-hold | Boolean | false | \- | N
1617
confirm-type | String | search | options: send/search/next/go/done | N
@@ -21,11 +22,12 @@ focus | Boolean | false | \- | N
2122
hold-keyboard | Boolean | false | \- | N
2223
label | String | '' | `deprecated` | N
2324
left-icon | String / Slot | 'search' | [see more ts definition](https://github.com/Tencent/tdesign-miniprogram/blob/develop/src/common/common.ts) | N
24-
maxcharacter | Number | - | \- | N
25+
maxcharacter | Number | - | The maximum number of characters that the user can enter. One Chinese character represents two characters in length. Use either `maxcharacter` or `maxlength` | N
2526
maxlength | Number | -1 | \- | N
2627
placeholder | String | '' | \- | N
2728
placeholder-class | String | input-placeholder | \- | N
2829
placeholder-style | String | - | required | Y
30+
readonly | Boolean | false | If readonly the input | N
2931
result-list | Array | [] | Typescript:`Array<string>` | N
3032
right-icon | String / Slot | 'close-circle-filled' | `deprecated`[see more ts definition](https://github.com/Tencent/tdesign-miniprogram/blob/develop/src/common/common.ts) | N
3133
selection-end | Number | -1 | \- | N
@@ -44,6 +46,7 @@ change | `({ value: string })` | \-
4446
clear | `({ value: string })` | \-
4547
focus | `({ value: string })` | \-
4648
submit | `({ value: string })` | \-
49+
4750
### Search External Classes
4851

4952
className | Description
@@ -73,4 +76,4 @@ Name | Default Value | Description
7376
--td-search-placeholder-color | @text-color-placeholder | -
7477
--td-search-result-high-light-color | @brand-color | -
7578
--td-search-square-radius | @radius-default | -
76-
--td-search-text-color | @text-color-primary | -
79+
--td-search-text-color | @text-color-primary | -

src/search/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ action | String / Slot | '' | 自定义右侧操作按钮文字。[通用类型
5959
adjust-position | Boolean | true | 键盘弹起时,是否自动上推页面 | N
6060
always-embed | Boolean | false | 强制 input 处于同层状态,默认 focus 时 input 会切到非同层状态 (仅在 iOS 下生效) | N
6161
center | Boolean | false | 是否居中 | N
62+
clear-trigger | String | 'always' | 清空图标触发方式,仅在输入框有值时有效。。可选项:always/focus | N
6263
clearable | Boolean | true | 是否启用清除控件 | N
6364
confirm-hold | Boolean | false | 点击键盘右下角按钮时是否保持键盘不收起 | N
6465
confirm-type | String | search | 设置键盘右下角按钮的文字,仅在type='text'时生效。<br />具体释义:<br />`send` 右下角按钮为“发送”;<br />`search` 右下角按钮为“搜索”;<br />`next` 右下角按钮为“下一个”;<br />`go` 右下角按钮为“前往”;<br />`done` 右下角按钮为“完成”。<br />[小程序官方文档](https://developers.weixin.qq.com/miniprogram/dev/component/input.html)。可选项:send/search/next/go/done | N
@@ -74,6 +75,7 @@ maxlength | Number | -1 | 用户最多可以输入的文本长度,一个中文
7475
placeholder | String | '' | 占位符 | N
7576
placeholder-class | String | input-placeholder | 指定 placeholder 的样式类 | N
7677
placeholder-style | String | - | 必需。指定 placeholder 的样式 | Y
78+
readonly | Boolean | false | 只读模式 | N
7779
result-list | Array | [] | 预览结果列表。TS 类型:`Array<string>` | N
7880
right-icon | String / Slot | 'close-circle-filled' | 已废弃。右侧图标。[通用类型定义](https://github.com/Tencent/tdesign-miniprogram/blob/develop/src/common/common.ts) | N
7981
selection-end | Number | -1 | 光标结束位置,自动聚集时有效,需与 selection-start 搭配使用 | N
@@ -92,6 +94,7 @@ change | `({ value: string })` | 值发生变化时触发
9294
clear | `({ value: string })` | 点击清除时触发
9395
focus | `({ value: string })` | 聚焦时触发
9496
submit | `({ value: string })` | 提交时触发
97+
9598
### Search External Classes
9699

97100
类名 | 描述
@@ -121,4 +124,4 @@ t-class-left | 左侧图标样式类
121124
--td-search-placeholder-color | @text-color-placeholder | -
122125
--td-search-result-high-light-color | @brand-color | -
123126
--td-search-square-radius | @radius-default | -
124-
--td-search-text-color | @text-color-primary | -
127+
--td-search-text-color | @text-color-primary | -

src/search/props.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ const props: TdSearchProps = {
2626
type: Boolean,
2727
value: false,
2828
},
29+
/** 清空图标触发方式,仅在输入框有值时有效。 */
30+
clearTrigger: {
31+
type: String,
32+
value: 'always',
33+
},
2934
/** 是否启用清除控件 */
3035
clearable: {
3136
type: Boolean,
@@ -96,6 +101,11 @@ const props: TdSearchProps = {
96101
value: '',
97102
required: true,
98103
},
104+
/** 只读模式 */
105+
readonly: {
106+
type: Boolean,
107+
value: false,
108+
},
99109
/** 预览结果列表 */
100110
resultList: {
101111
type: Array,

src/search/search.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,28 @@ export default class Search extends SuperComponent {
4444
});
4545
}
4646
},
47+
'clearTrigger, clearable, disabled, readonly'() {
48+
this.updateClearIconVisible();
49+
},
4750
};
4851

4952
data = {
5053
classPrefix: name,
5154
prefix,
5255
isShowResultList: false,
5356
isSelected: false,
57+
showClearIcon: true,
5458
};
5559

60+
updateClearIconVisible(value: boolean = false) {
61+
const { clearTrigger, disabled, readonly } = this.properties;
62+
if (disabled || readonly) {
63+
this.setData({ showClearIcon: false });
64+
return;
65+
}
66+
this.setData({ showClearIcon: value || String(clearTrigger) === 'always' });
67+
}
68+
5669
onInput(e) {
5770
let { value } = e.detail;
5871
const { maxcharacter } = this.properties;
@@ -71,13 +84,13 @@ export default class Search extends SuperComponent {
7184

7285
onFocus(e) {
7386
const { value } = e.detail;
74-
87+
this.updateClearIconVisible(true);
7588
this.triggerEvent('focus', { value });
7689
}
7790

7891
onBlur(e) {
7992
const { value } = e.detail;
80-
93+
this.updateClearIconVisible();
8194
this.triggerEvent('blur', { value });
8295
}
8396

src/search/search.wxml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
type="{{type}}"
2020
name="input"
2121
maxlength="{{maxlength}}"
22-
disabled="{{disabled}}"
22+
disabled="{{disabled || readonly}}"
2323
class="{{prefix}}-input__keyword {{prefix}}-class-input"
2424
focus="{{focus}}"
2525
value="{{value}}"
@@ -41,7 +41,7 @@
4141
bind:confirm="onConfirm"
4242
/>
4343
<view
44-
wx:if="{{value !=='' && clearable}}"
44+
wx:if="{{value !=='' && clearable && showClearIcon}}"
4545
class="{{classPrefix}}__clear {{prefix}}-class-clear"
4646
catch:tap="handleClear"
4747
aria-role="button"

src/search/type.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ export interface TdSearchProps {
3737
type: BooleanConstructor;
3838
value?: boolean;
3939
};
40+
/**
41+
* 清空图标触发方式,仅在输入框有值时有效。
42+
* @default 'always'
43+
*/
44+
clearTrigger?: {
45+
type: StringConstructor;
46+
value?: 'always' | 'focus';
47+
};
4048
/**
4149
* 是否启用清除控件
4250
* @default true
@@ -149,6 +157,14 @@ export interface TdSearchProps {
149157
value?: string;
150158
required?: boolean;
151159
};
160+
/**
161+
* 只读模式
162+
* @default false
163+
*/
164+
readonly?: {
165+
type: BooleanConstructor;
166+
value?: boolean;
167+
};
152168
/**
153169
* 预览结果列表
154170
* @default []

0 commit comments

Comments
 (0)