Skip to content

Commit 47bf2e5

Browse files
author
xueyuan
committed
feat(ui/input): add select method
- Add select() method to select all text in input Closes #1912 docs(input): 移除输入组件文档中的键盘事件相关内容 refactor(input): 移除不必要的输入组件测试用例 refactor(input): 移除键盘事件处理器 refactor(input/example): 移除未使用的Snackbar导入 feat(input/docs): 添加en-US新的输入示例和选择全部功能 docs(input): 移除输入组件文档中的回车键事件示例
1 parent a935cea commit 47bf2e5

7 files changed

Lines changed: 93 additions & 4 deletions

File tree

packages/varlet-ui/src/input/Input.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,11 @@ export default defineComponent({
368368
;(el.value as HTMLInputElement).blur()
369369
}
370370
371+
// expose
372+
function select() {
373+
;(el.value as HTMLInputElement).select()
374+
}
375+
371376
return {
372377
el,
373378
id,
@@ -397,6 +402,7 @@ export default defineComponent({
397402
reset,
398403
focus,
399404
blur,
405+
select,
400406
}
401407
},
402408
})

packages/varlet-ui/src/input/docs/en-US.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ const value11 = ref('')
2525
const value12 = ref('')
2626
const value13 = ref('')
2727
const value14 = ref('')
28+
const value15 = ref('')
29+
const value16 = ref('')
30+
const inputRef = ref()
31+
32+
const selectAll = () => {
33+
inputRef.value?.select()
34+
}
2835
</script>
2936
3037
<template>
@@ -69,6 +76,14 @@ const value14 = ref('')
6976
<var-input placeholder="Textarea" textarea v-model="value10" />
7077
<var-input placeholder="Small Size" size="small" v-model="value11" />
7178
<var-input placeholder="Removes whitespace from both ends of this string" v-model.trim="value12" />
79+
<var-space>
80+
<var-input
81+
ref="inputRef"
82+
placeholder="Enter text and click the button to select all"
83+
v-model="value16"
84+
/>
85+
<var-button type="primary" @click="selectAll">Select All</var-button>
86+
</var-space>
7287
</var-space>
7388
</template>
7489
@@ -104,6 +119,13 @@ const value11 = ref('')
104119
const value12 = ref('')
105120
const value13 = ref('')
106121
const value14 = ref('')
122+
const value15 = ref('')
123+
const value16 = ref('')
124+
const inputRef = ref()
125+
126+
const selectAll = () => {
127+
inputRef.value?.select()
128+
}
107129
</script>
108130
109131
<template>
@@ -154,6 +176,14 @@ const value14 = ref('')
154176
<var-input variant="outlined" placeholder="Textarea" textarea v-model="value10" />
155177
<var-input variant="outlined" placeholder="Small Size" size="small" v-model="value11" />
156178
<var-input variant="outlined" placeholder="Removes whitespace from both ends of this string" v-model.trim="value12" />
179+
<var-space>
180+
<var-input
181+
ref="inputRef"
182+
placeholder="Enter text and click the button to select all"
183+
v-model="value16"
184+
/>
185+
<var-button type="primary" @click="selectAll">Select All</var-button>
186+
</var-space>
157187
</var-space>
158188
</template>
159189
@@ -203,6 +233,7 @@ const value14 = ref('')
203233
| --- | --- | --- | --- |
204234
| `focus` | Focus | `-` | `-` |
205235
| `blur` | Blur | `-` | `-` |
236+
| `select` | Select all text in the input | `-` | `-` |
206237
| `validate` | Trigger validate | `-` | `valid: Promise<boolean>` |
207238
| `resetValidation` | Clear validate messages | `-` | `-` |
208239
| `reset` | Clear the value of the binding and validate messages | `-` | `-` |
@@ -218,6 +249,7 @@ const value14 = ref('')
218249
| `input` | Triggered on input | `value: string`, `event: Event` |
219250
| `change` | Triggered on change | `value: string`, `event: Event` |
220251
252+
221253
### Slots
222254
223255
| Name | Description | SlotProps |

packages/varlet-ui/src/input/docs/zh-CN.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<script setup>
1111
import { ref } from 'vue'
1212
import { z } from 'zod'
13+
import { Snackbar } from '@varlet/ui'
1314
1415
const value = ref('')
1516
const value2 = ref('')
@@ -25,6 +26,17 @@ const value11 = ref('')
2526
const value12 = ref('')
2627
const value13 = ref('')
2728
const value14 = ref('')
29+
const value15 = ref('')
30+
const value16 = ref('')
31+
const inputRef = ref()
32+
33+
const handleEnter = (value) => {
34+
Snackbar.info(`回车键被按下,当前值:${value}`)
35+
}
36+
37+
const selectAll = () => {
38+
inputRef.value?.select()
39+
}
2840
</script>
2941
3042
<template>
@@ -69,6 +81,14 @@ const value14 = ref('')
6981
<var-input placeholder="文本域" textarea v-model="value10" />
7082
<var-input placeholder="小尺寸" size="small" v-model="value11" />
7183
<var-input placeholder="移除空白字符" v-model.trim="value12" />
84+
<var-space>
85+
<var-input
86+
ref="inputRef"
87+
placeholder="输入文本后点击按钮全选"
88+
v-model="value16"
89+
/>
90+
<var-button type="primary" @click="selectAll">全选</var-button>
91+
</var-space>
7292
</var-space>
7393
</template>
7494
@@ -203,6 +223,7 @@ const value14 = ref('')
203223
| --- | --- | --- | --- |
204224
| `focus` | 聚焦 | `-` | `-` |
205225
| `blur` | 失焦 | `-` | `-` |
226+
| `select` | 选中输入框中的所有文本 | `-` | `-` |
206227
| `validate` | 触发校验 | `-` | `valid: Promise<boolean>` |
207228
| `resetValidation` | 清空校验信息 | `-` | `-` |
208229
| `reset` | 清空绑定的值和校验信息 | `-` | `-` |
@@ -218,6 +239,7 @@ const value14 = ref('')
218239
| `input` | 输入时触发 | `value: string`, `event: Event` |
219240
| `change` | 更新时触发 | `value: string`, `event: Event` |
220241
242+
221243
### 插槽
222244
223245
| 插槽名 | 说明 | 参数 |

packages/varlet-ui/src/input/example/index.vue

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const standardValue11 = ref('')
1818
const standardValue12 = ref('')
1919
const standardValue13 = ref('')
2020
const standardValue14 = ref('')
21+
const standardValue15 = ref('')
22+
const standardValue16 = ref('')
2123
2224
const outlinedValue = ref('')
2325
const outlinedValue2 = ref('')
@@ -34,6 +36,20 @@ const outlinedValue12 = ref('')
3436
const outlinedValue13 = ref('')
3537
const outlinedValue14 = ref('')
3638
39+
const inputRef = ref()
40+
41+
const handleEnter = (value, e) => {
42+
Snackbar.info(`回车键被按下,当前值:${value}`)
43+
}
44+
45+
const handleKeydown = (e) => {
46+
console.log('keydown:', e.key)
47+
}
48+
49+
const selectAll = () => {
50+
inputRef.value?.select()
51+
}
52+
3753
watchLang(use)
3854
onThemeChange()
3955
</script>
@@ -78,6 +94,11 @@ onThemeChange()
7894
<var-input v-model="standardValue10" :placeholder="t('textarea')" textarea />
7995
<var-input v-model="standardValue11" :placeholder="t('smallSize')" size="small" />
8096
<var-input v-model.trim="standardValue12" :placeholder="t('trim')" />
97+
<var-input v-model="standardValue15" :placeholder="t('enterKey')" @enter="handleEnter" @keydown="handleKeydown" />
98+
<var-space>
99+
<var-input ref="inputRef" v-model="standardValue16" :placeholder="t('selectAll')" />
100+
<var-button type="primary" @click="selectAll">{{ t('selectAllButton') }}</var-button>
101+
</var-space>
81102
</var-space>
82103
83104
<app-type style="margin-top: 10vmin">{{ t('outlined') }}</app-type>
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
export default {
2-
standard: 'Standard Variant',
3-
outlined: 'Outlined Variant',
2+
standard: 'Standard Appearance',
3+
outlined: 'Outlined Appearance',
44
smallSize: 'Small Size',
55
plainMode: 'Plain Mode',
66
textarea: 'Textarea',
77
maxlength: 'Maxlength',
88
disabled: 'Disabled',
9-
trim: 'Removes whitespace from both ends of this string',
9+
trim: 'Trim',
1010
readonly: 'Readonly',
1111
clearable: 'Clearable',
12-
clearIconSlot: 'Use the clear icon slot',
12+
clearIconSlot: 'Custom Clear Icon',
1313
displayIcon: 'Display Icon',
1414
customIconSize: 'Custom Icon Size',
1515
validate: 'Validate',
@@ -18,4 +18,7 @@ export default {
1818
numberPlaceholder: 'Please enter number',
1919
maxMessage: 'Text length must be greater than 6',
2020
clearableText: 'Clearable Text',
21+
enterKey: 'Try pressing Enter',
22+
selectAll: 'Type text and click button to select all',
23+
selectAllButton: 'Select All',
2124
}

packages/varlet-ui/src/input/example/locale/zh-CN.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ export default {
1818
numberPlaceholder: '请输入数字',
1919
maxMessage: '文本长度必须大于6',
2020
clearableText: '可清除文本',
21+
enterKey: '按下回车键试试',
22+
selectAll: '输入文本后点击按钮全选',
23+
selectAllButton: '全选',
2124
}

packages/varlet-ui/types/input.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ export class Input extends VarComponent {
6868

6969
blur(): void
7070

71+
select(): void
72+
7173
validate(): Promise<boolean>
7274

7375
resetValidation(): void

0 commit comments

Comments
 (0)