Skip to content

Input 输入框

通过鼠标或键盘输入字符,是基础的表单组件。

TIP

Input 组件对应 GvInput,支持 v-model 双向绑定。下方示例可直接交互,点击「显示代码」查看源码。

基础用法

禁用状态

通过 disabled 属性禁用输入框。

一键清空

使用 clearable 属性即可得到一个可清空的输入框。仅当 type 不为 textarea 时生效。

密码框

使用 show-password 属性即可得到一个可切换显示隐藏的密码框。仅当 type 为 password 时生效。

带 icon 的输入框

可以通过 prefix-iconsuffix-icon 属性来设置前缀与后缀图标,值为 GvIcon 图标名称字符串(如 gv-icon-sousuoel-icon-search),不支持传入 Vue 组件。

文本域

type 设置为 textarea 即可使用文本域。可通过 rows 属性指定行数。

自适应高度文本域

设置 autosize 属性可以使得文本域高度根据内容自动调整。autosize 也可以接收对象,如 { minRows: 2, maxRows: 6 }

复合型输入

可通过 slot 来复合输入框,常用在输入框前后加入按钮或文字。

尺寸

可通过 size 属性指定输入框尺寸,可选值:large / default / small

输入长度限制

使用 maxlengthminlength 属性限制输入长度。使用 show-word-limit 显示字数统计,需配合 maxlength 使用。

API

Attributes

属性名说明类型默认值
model-value / v-model绑定值string / number
type类型'text' / 'textarea' / 'password' / 'number'text
maxlength最大输入长度number30
minlength最小输入长度string / number
show-word-limit是否显示字数统计booleanfalse
placeholder占位文本string
clearable是否可清空booleanfalse
show-password是否显示密码切换按钮booleanfalse
disabled是否禁用booleanfalse
size输入框尺寸'large' / 'default' / 'small'
prefix-icon前缀图标名称string
suffix-icon后缀图标名称string
rows文本域行数,仅 type 为 textarea 时有效number2
autosize文本域自适应高度boolean / { minRows?: number; maxRows?: number }false
autocomplete原生 autocomplete 属性stringoff
readonly是否只读booleanfalse
tabindex输入框 tabindexstring / number
validate-event输入时是否触发表单校验booleantrue
input-styleinput 或 textarea 的样式string / CSSProperties

Events

事件名说明类型
blur失去焦点时触发(event: FocusEvent) => void
focus获得焦点时触发(event: FocusEvent) => void
change绑定值变化且失焦时触发(value: string | number) => void
input输入值变化时触发(value: string | number) => void
clear点击清空按钮时触发() => void

Slots

插槽名说明
prefix输入框头部内容,只对非 textarea 有效
suffix输入框尾部内容,只对非 textarea 有效
prepend输入框前置内容,只对非 textarea 有效
append输入框后置内容,只对非 textarea 有效

Exposes

名称说明类型
blur使 input 失去焦点() => void
clear清空输入值() => void
focus使 input 获取焦点() => void
inputHTML input 元素Ref<HTMLInputElement>
refHTML 元素或组件实例Ref<HTMLInputElement>
resizeTextarea改变 textarea 大小() => void
select选中 input 中的文字() => void
textareaHTML textarea 元素Ref<HTMLTextAreaElement>
textareaStyletextarea 的样式Ref<StyleValue>

类型定义

typescript
/** Input 尺寸 */
type InputSize = 'large' | 'default' | 'small'

/** Input 类型 */
type InputType = 'text' | 'textarea' | 'password' | 'number' | 'email' | 'url'

/** autosize 配置 */
interface InputAutosize {
  minRows?: number
  maxRows?: number
}

/** GvInput Props */
interface InputProps {
  modelValue?: string | number
  type?: InputType
  maxlength?: number
  minlength?: string | number
  showWordLimit?: boolean
  placeholder?: string
  clearable?: boolean
  showPassword?: boolean
  disabled?: boolean
  size?: InputSize
  prefixIcon?: string
  suffixIcon?: string
  rows?: number
  autosize?: boolean | InputAutosize
  readonly?: boolean
  validateEvent?: boolean
}