Skip to content

Commit 8b8314c

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): 移除键盘事件处理器
1 parent a935cea commit 8b8314c

7 files changed

Lines changed: 69 additions & 5 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ const value14 = ref('')
203203
| --- | --- | --- | --- |
204204
| `focus` | Focus | `-` | `-` |
205205
| `blur` | Blur | `-` | `-` |
206+
| `select` | Select all text in the input | `-` | `-` |
206207
| `validate` | Trigger validate | `-` | `valid: Promise<boolean>` |
207208
| `resetValidation` | Clear validate messages | `-` | `-` |
208209
| `reset` | Clear the value of the binding and validate messages | `-` | `-` |
@@ -218,6 +219,7 @@ const value14 = ref('')
218219
| `input` | Triggered on input | `value: string`, `event: Event` |
219220
| `change` | Triggered on change | `value: string`, `event: Event` |
220221

222+
221223
### Slots
222224

223225
| Name | Description | SlotProps |

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

Lines changed: 27 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,19 @@ 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-input
85+
placeholder="按下回车键试试"
86+
v-model="value15"
87+
@enter="handleEnter"
88+
/>
89+
<var-space>
90+
<var-input
91+
ref="inputRef"
92+
placeholder="输入文本后点击按钮全选"
93+
v-model="value16"
94+
/>
95+
<var-button type="primary" @click="selectAll">全选</var-button>
96+
</var-space>
7297
</var-space>
7398
</template>
7499
@@ -203,6 +228,7 @@ const value14 = ref('')
203228
| --- | --- | --- | --- |
204229
| `focus` | 聚焦 | `-` | `-` |
205230
| `blur` | 失焦 | `-` | `-` |
231+
| `select` | 选中输入框中的所有文本 | `-` | `-` |
206232
| `validate` | 触发校验 | `-` | `valid: Promise<boolean>` |
207233
| `resetValidation` | 清空校验信息 | `-` | `-` |
208234
| `reset` | 清空绑定的值和校验信息 | `-` | `-` |
@@ -218,6 +244,7 @@ const value14 = ref('')
218244
| `input` | 输入时触发 | `value: string`, `event: Event` |
219245
| `change` | 更新时触发 | `value: string`, `event: Event` |
220246
247+
221248
### 插槽
222249
223250
| 插槽名 | 说明 | 参数 |

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup>
22
import { ref } from 'vue'
3-
import { AppType, onThemeChange, watchLang } from '@varlet/cli/client'
3+
import { AppType, onThemeChange, Snackbar, watchLang } from '@varlet/cli/client'
44
import { z } from 'zod'
55
import { t, use } from './locale'
66
@@ -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)