Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
b2e9340
feat: add name
william-xue Mar 3, 2025
dda43c3
feat: add function signature
william-xue Mar 3, 2025
960ad7b
fix: adjust css
william-xue Mar 3, 2025
46e4e7b
fix: adjust css
william-xue Mar 4, 2025
ca79f54
feat: signature finish
william-xue Mar 4, 2025
f4cf2a7
Merge branch 'varletjs:dev' into dev
william-xue Mar 4, 2025
cf7df45
feat: add test cases
william-xue Mar 4, 2025
b15d038
feat: add test cases
william-xue Mar 5, 2025
0796c74
feat: test cases
william-xue Mar 5, 2025
e8bf4ca
feat: resolve
william-xue Mar 9, 2025
600a11c
feat: fix
william-xue Mar 10, 2025
375c477
Merge branch 'varletjs:dev' into dev
william-xue Mar 10, 2025
bb63493
feat: test case
william-xue Mar 10, 2025
62895d4
feat: test case
william-xue Mar 10, 2025
bd7fe46
feat: expose empty
william-xue Mar 10, 2025
5b746cd
Merge branch 'varletjs:dev' into dev
william-xue Mar 11, 2025
8857fb8
feat: resolve
william-xue Mar 11, 2025
34158dc
refactor: optimize event handling with useEventListener hook
william-xue Mar 11, 2025
5cfc324
refactor: remove duplicate onMounted hook and clean up unnecessary code
william-xue Mar 11, 2025
f228328
refactor: add
william-xue Mar 11, 2025
faf5c08
Merge branch 'varletjs:dev' into dev
william-xue Mar 11, 2025
fd9a16c
refactor: add
william-xue Mar 11, 2025
f8ab947
refactor: add
william-xue Mar 11, 2025
c5ad7cd
feat: resolve comment
william-xue Mar 14, 2025
75682a1
feat: resolve comment
william-xue Mar 14, 2025
1268d01
feat: resolve comment
william-xue Mar 14, 2025
a52f477
feat: cleanup
haoziqaq Mar 15, 2025
01f61e7
feat: test cases
william-xue Mar 18, 2025
4e61cfb
feat: docs finish
william-xue Mar 18, 2025
d6811dd
feat: resolve comment
william-xue Mar 18, 2025
e71f0f3
feat: "resolve comment"
william-xue Mar 19, 2025
3e7c88f
refactor: cleanup
haoziqaq Mar 20, 2025
f50640e
types: fix types
haoziqaq Mar 20, 2025
8a6c042
types: fix types
haoziqaq Mar 20, 2025
05a17bd
Merge branch 'varletjs:dev' into dev
william-xue Mar 20, 2025
d150b65
Merge branch 'varletjs:dev' into dev
william-xue Jun 3, 2025
f71fde3
feat(virtual-list): 添加虚拟滚动列表组件及相关文档和示例
Jun 4, 2025
4c0ce3e
refactor(virtual-list): update comments and improve code readability
Jun 27, 2025
d6d2f09
feat(ui/input): add select method and keyboard events support
Jul 2, 2025
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
26 changes: 26 additions & 0 deletions packages/varlet-ui/src/input/Input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
@blur="handleBlur"
@input="handleInput"
@change="handleChange"
@keydown="handleKeydown"
@keyup="handleKeyup"
@compositionstart="handleCompositionStart"
@compositionend="handleCompositionEnd"
/>
Expand Down Expand Up @@ -108,6 +110,8 @@
@blur="handleBlur"
@input="handleInput"
@change="handleChange"
@keydown="handleKeydown"
@keyup="handleKeyup"
@compositionstart="handleCompositionStart"
@compositionend="handleCompositionEnd"
/>
Expand Down Expand Up @@ -368,6 +372,25 @@ export default defineComponent({
;(el.value as HTMLInputElement).blur()
}

// expose
function select() {
;(el.value as HTMLInputElement).select()
}

function handleKeydown(e: KeyboardEvent) {
call(props.onKeydown, e)

// 处理回车键事件
if (e.key === 'Enter' && !isComposing.value) {
const value = props.modelValue || ''
call(props.onEnter, value, e)
}
}

function handleKeyup(e: KeyboardEvent) {
call(props.onKeyup, e)
}

return {
el,
id,
Expand All @@ -392,11 +415,14 @@ export default defineComponent({
handleCompositionStart,
handleCompositionEnd,
handleMousedown,
handleKeydown,
handleKeyup,
validate,
resetValidation,
reset,
focus,
blur,
select,
}
},
})
Expand Down
112 changes: 112 additions & 0 deletions packages/varlet-ui/src/input/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,115 @@ test('input aria-label', async () => {
expect(wrapper.html()).toMatchSnapshot()
wrapper.unmount()
})

test('input select method', () => {
const wrapper = mount(VarInput, {
props: {
modelValue: 'hello world',
},
})

const input = wrapper.find('.var-input__input').element
const selectSpy = vi.spyOn(input, 'select')

wrapper.vm.select()
expect(selectSpy).toHaveBeenCalled()

wrapper.unmount()
})

test('input keyboard events', async () => {
const onKeydown = vi.fn()
const onKeyup = vi.fn()
const onEnter = vi.fn()

const wrapper = mount(VarInput, {
props: {
modelValue: 'test',
onKeydown,
onKeyup,
onEnter,
},
})

const input = wrapper.find('.var-input__input')

// Test keydown event
await input.trigger('keydown', { key: 'a' })
expect(onKeydown).toHaveBeenCalledTimes(1)
expect(onKeydown).toHaveBeenCalledWith(expect.objectContaining({ key: 'a' }))

// Test keyup event
await input.trigger('keyup', { key: 'a' })
expect(onKeyup).toHaveBeenCalledTimes(1)
expect(onKeyup).toHaveBeenCalledWith(expect.objectContaining({ key: 'a' }))

// Test enter key event
await input.trigger('keydown', { key: 'Enter' })
expect(onEnter).toHaveBeenCalledTimes(1)
expect(onEnter).toHaveBeenCalledWith('test', expect.objectContaining({ key: 'Enter' }))

wrapper.unmount()
})

test('input textarea keyboard events', async () => {
const onKeydown = vi.fn()
const onKeyup = vi.fn()
const onEnter = vi.fn()

const wrapper = mount(VarInput, {
props: {
modelValue: 'test',
textarea: true,
onKeydown,
onKeyup,
onEnter,
},
})

const textarea = wrapper.find('.var-input__input')

// Test keydown event
await textarea.trigger('keydown', { key: 'a' })
expect(onKeydown).toHaveBeenCalledTimes(1)

// Test keyup event
await textarea.trigger('keyup', { key: 'a' })
expect(onKeyup).toHaveBeenCalledTimes(1)

// Test enter key event
await textarea.trigger('keydown', { key: 'Enter' })
expect(onEnter).toHaveBeenCalledTimes(1)
expect(onEnter).toHaveBeenCalledWith('test', expect.objectContaining({ key: 'Enter' }))

wrapper.unmount()
})

test('input enter key not triggered during composition', async () => {
const onEnter = vi.fn()

const wrapper = mount(VarInput, {
props: {
modelValue: 'test',
onEnter,
},
})

const input = wrapper.find('.var-input__input')

// Start composition
await input.trigger('compositionstart')

// Trigger enter during composition
await input.trigger('keydown', { key: 'Enter' })
expect(onEnter).toHaveBeenCalledTimes(0)

// End composition
await input.trigger('compositionend')

// Trigger enter after composition
await input.trigger('keydown', { key: 'Enter' })
expect(onEnter).toHaveBeenCalledTimes(1)

wrapper.unmount()
})
4 changes: 4 additions & 0 deletions packages/varlet-ui/src/input/docs/en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ const value14 = ref('')
| --- | --- | --- | --- |
| `focus` | Focus | `-` | `-` |
| `blur` | Blur | `-` | `-` |
| `select` | Select all text in the input | `-` | `-` |
| `validate` | Trigger validate | `-` | `valid: Promise<boolean>` |
| `resetValidation` | Clear validate messages | `-` | `-` |
| `reset` | Clear the value of the binding and validate messages | `-` | `-` |
Expand All @@ -217,6 +218,9 @@ const value14 = ref('')
| `clear` | Triggered on Clearance | `value: string` |
| `input` | Triggered on input | `value: string`, `event: Event` |
| `change` | Triggered on change | `value: string`, `event: Event` |
| `keydown` | Triggered on keydown | `event: KeyboardEvent` |
| `keyup` | Triggered on keyup | `event: KeyboardEvent` |
| `enter` | Triggered when Enter key is pressed | `value: string`, `event: KeyboardEvent` |

### Slots

Expand Down
29 changes: 29 additions & 0 deletions packages/varlet-ui/src/input/docs/zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<script setup>
import { ref } from 'vue'
import { z } from 'zod'
import { Snackbar } from '@varlet/ui'

const value = ref('')
const value2 = ref('')
Expand All @@ -25,6 +26,17 @@ const value11 = ref('')
const value12 = ref('')
const value13 = ref('')
const value14 = ref('')
const value15 = ref('')
const value16 = ref('')
const inputRef = ref()

const handleEnter = (value) => {
Snackbar.info(`回车键被按下,当前值:${value}`)
}

const selectAll = () => {
inputRef.value?.select()
}
</script>

<template>
Expand Down Expand Up @@ -69,6 +81,19 @@ const value14 = ref('')
<var-input placeholder="文本域" textarea v-model="value10" />
<var-input placeholder="小尺寸" size="small" v-model="value11" />
<var-input placeholder="移除空白字符" v-model.trim="value12" />
<var-input
placeholder="按下回车键试试"
v-model="value15"
@enter="handleEnter"
/>
<var-space>
<var-input
ref="inputRef"
placeholder="输入文本后点击按钮全选"
v-model="value16"
/>
<var-button type="primary" @click="selectAll">全选</var-button>
</var-space>
</var-space>
</template>

Expand Down Expand Up @@ -203,6 +228,7 @@ const value14 = ref('')
| --- | --- | --- | --- |
| `focus` | 聚焦 | `-` | `-` |
| `blur` | 失焦 | `-` | `-` |
| `select` | 选中输入框中的所有文本 | `-` | `-` |
| `validate` | 触发校验 | `-` | `valid: Promise<boolean>` |
| `resetValidation` | 清空校验信息 | `-` | `-` |
| `reset` | 清空绑定的值和校验信息 | `-` | `-` |
Expand All @@ -217,6 +243,9 @@ const value14 = ref('')
| `clear` | 清除时触发 | `value: string` |
| `input` | 输入时触发 | `value: string`, `event: Event` |
| `change` | 更新时触发 | `value: string`, `event: Event` |
| `keydown` | 键盘按下时触发 | `event: KeyboardEvent` |
| `keyup` | 键盘释放时触发 | `event: KeyboardEvent` |
| `enter` | 按下回车键时触发 | `value: string`, `event: KeyboardEvent` |

### 插槽

Expand Down
23 changes: 22 additions & 1 deletion packages/varlet-ui/src/input/example/index.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup>
import { ref } from 'vue'
import { AppType, onThemeChange, watchLang } from '@varlet/cli/client'
import { AppType, onThemeChange, Snackbar, watchLang } from '@varlet/cli/client'
import { z } from 'zod'
import { t, use } from './locale'

Expand All @@ -18,6 +18,8 @@ const standardValue11 = ref('')
const standardValue12 = ref('')
const standardValue13 = ref('')
const standardValue14 = ref('')
const standardValue15 = ref('')
const standardValue16 = ref('')

const outlinedValue = ref('')
const outlinedValue2 = ref('')
Expand All @@ -34,6 +36,20 @@ const outlinedValue12 = ref('')
const outlinedValue13 = ref('')
const outlinedValue14 = ref('')

const inputRef = ref()

const handleEnter = (value, e) => {
Snackbar.info(`回车键被按下,当前值:${value}`)
}

const handleKeydown = (e) => {
console.log('keydown:', e.key)
}

const selectAll = () => {
inputRef.value?.select()
}

watchLang(use)
onThemeChange()
</script>
Expand Down Expand Up @@ -78,6 +94,11 @@ onThemeChange()
<var-input v-model="standardValue10" :placeholder="t('textarea')" textarea />
<var-input v-model="standardValue11" :placeholder="t('smallSize')" size="small" />
<var-input v-model.trim="standardValue12" :placeholder="t('trim')" />
<var-input v-model="standardValue15" :placeholder="t('enterKey')" @enter="handleEnter" @keydown="handleKeydown" />
<var-space>
<var-input ref="inputRef" v-model="standardValue16" :placeholder="t('selectAll')" />
<var-button type="primary" @click="selectAll">{{ t('selectAllButton') }}</var-button>
</var-space>
</var-space>

<app-type style="margin-top: 10vmin">{{ t('outlined') }}</app-type>
Expand Down
11 changes: 7 additions & 4 deletions packages/varlet-ui/src/input/example/locale/en-US.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export default {
standard: 'Standard Variant',
outlined: 'Outlined Variant',
standard: 'Standard Appearance',
outlined: 'Outlined Appearance',
smallSize: 'Small Size',
plainMode: 'Plain Mode',
textarea: 'Textarea',
maxlength: 'Maxlength',
disabled: 'Disabled',
trim: 'Removes whitespace from both ends of this string',
trim: 'Trim',
readonly: 'Readonly',
clearable: 'Clearable',
clearIconSlot: 'Use the clear icon slot',
clearIconSlot: 'Custom Clear Icon',
displayIcon: 'Display Icon',
customIconSize: 'Custom Icon Size',
validate: 'Validate',
Expand All @@ -18,4 +18,7 @@ export default {
numberPlaceholder: 'Please enter number',
maxMessage: 'Text length must be greater than 6',
clearableText: 'Clearable Text',
enterKey: 'Try pressing Enter',
selectAll: 'Type text and click button to select all',
selectAllButton: 'Select All',
}
3 changes: 3 additions & 0 deletions packages/varlet-ui/src/input/example/locale/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ export default {
numberPlaceholder: '请输入数字',
maxMessage: '文本长度必须大于6',
clearableText: '可清除文本',
enterKey: '按下回车键试试',
selectAll: '输入文本后点击按钮全选',
selectAllButton: '全选',
}
3 changes: 3 additions & 0 deletions packages/varlet-ui/src/input/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export const props = {
onInput: defineListenerProp<(value: string, e: Event) => void>(),
onChange: defineListenerProp<(value: string, e: Event) => void>(),
onClear: defineListenerProp<(value: string) => void>(),
onKeydown: defineListenerProp<(e: KeyboardEvent) => void>(),
onKeyup: defineListenerProp<(e: KeyboardEvent) => void>(),
onEnter: defineListenerProp<(value: string, e: KeyboardEvent) => void>(),
'onUpdate:modelValue': defineListenerProp<(value: string) => void>(),
...pickProps(fieldDecoratorProps, [
'size',
Expand Down
Loading