Input 输入框

用于接收用户输入的文本框组件

<template>
  <div class="p-4 w-full">
    <SkInput />
  </div>
</template>

用法

Controlled 输入值

Prop类型默认值描述
v-modelstring | number-设置输入框的内容 (双向绑定)
<script setup lang="ts">
import { ref } from 'vue'

const value = ref('Hello')
</script>

<template>
  <div class="p-4 w-full">
    <SkInput v-model="value" />
  </div>
</template>

Placeholder 占位符

Prop类型默认值描述
placeholderstring''设置输入框占位符文本
<script setup lang="ts">
import { ref } from 'vue'

const value = ref('')
</script>

<template>
  <div class="flex flex-col gap-3 p-4 w-full">
    <SkInput v-model="value" placeholder="请输入您的姓名" />
  </div>
</template>

Type 类型

Prop类型默认值描述
type{Type}text设置输入框类型
Type描述
text文本输入键盘
number数字输入键盘
digit带小数点的数字键盘
safe-password密码安全输入键盘
tel电话输入键盘
idcard身份证输入键盘
nickname昵称输入键盘
<script setup lang="ts">
import { ref } from 'vue'

const textValue = ref()
const numberValue = ref()
const digitValue = ref()
</script>

<template>
  <div class="flex flex-col p-4 gap-3 w-full">
    <SkInput v-model="textValue" type="text" placeholder="请输入文本" />
    <SkInput v-model="numberValue" type="number" placeholder="请输入数字" />
    <SkInput v-model="digitValue" type="digit" placeholder="请输入带小数点的数字" />
  </div>
</template>

Size 尺寸

Prop类型默认值描述
size{Size}md设置输入框的尺寸大小
Size描述
small小型
medium中型
large大型
<script setup lang="ts">
import { ref } from 'vue'

const smallValue = ref('')
const mediumValue = ref('')
const largeValue = ref('')
</script>

<template>
  <div class="flex flex-col p-4 gap-3 w-full">
    <SkInput v-model="smallValue" size="small" placeholder="小尺寸输入框" />
    <SkInput v-model="mediumValue" size="medium" placeholder="中尺寸输入框" />
    <SkInput v-model="largeValue" size="large" placeholder="大尺寸输入框" />
  </div>
</template>

Password 密码状态

Prop类型默认值描述
passwordbooleanfalse设置输入框是否为密码状态
<script setup lang="ts">
import { ref } from 'vue'

const passwordValue = ref()
const numericPasswordValue = ref()
</script>

<template>
  <div class="flex flex-col gap-3 p-4 w-full">
    <SkInput
      v-model="passwordValue"
      placeholder="请输入密码"
      password
    />
    <SkInput
      v-model="numericPasswordValue"
      type="number"
      placeholder="请输入6位数字密码"
      password
    />
  </div>
</template>

Readonly 只读

Prop类型默认值描述
readonlybooleanfalse设置输入框是否为只读状态
<script setup lang="ts">
import { ref } from 'vue'

const readonlyValue = ref()
const readonlyWithValue = ref('这是只读状态含值的输入框')
</script>

<template>
  <div class="flex flex-col gap-3 p-4 w-full">
    <SkInput
      v-model="readonlyValue"
      placeholder="只读状态,无法编辑"
      readonly
    />
    <SkInput
      v-model="readonlyWithValue"
      placeholder="只读状态,有值"
      readonly
    />
  </div>
</template>

Disabled 禁用

Prop类型默认值描述
disabledbooleanfalse设置输入框是否为禁用状态
<script setup lang="ts">
import { ref } from 'vue'

const disabledValue = ref()
const disabledWithValue = ref('这是禁用状态含值的输入框')
</script>

<template>
  <div class="flex flex-col gap-3 p-4 w-full">
    <SkInput v-model="disabledValue" disabled placeholder="禁用状态,无法输入" />
    <SkInput v-model="disabledWithValue" disabled />
  </div>
</template>

Maxlength 字符限制

Prop类型默认值描述
maxlengthnumber140设置输入框最大长度
<script setup lang="ts">
import { ref } from 'vue'

const value = ref('')
</script>

<template>
  <div class="p-4 w-full">
    <SkInput
      v-model="value"
      :maxlength="10"
      placeholder="最多输入10个字符"
    />
  </div>
</template>

例子

计数功能

通过 maxlength 属性可以限制输入框的最大长度,同时配合 trailing 插槽可以实现计数功能

<script setup lang="ts">
import { ref } from 'vue'

const value = ref('')
</script>

<template>
  <div class="p-4 w-full">
    <SkInput
      v-model="value"
      placeholder="写下你的名言..."
      :maxlength="20"
    >
      <template #trailing="{ clax }">
        <span :class="clax">{{ value.length }}/20</span>
      </template>
    </SkInput>
  </div>
</template>

清空功能

通过 clearable 属性可以显示清空按钮,方便用户快速清空输入内容

<script setup lang="ts">
import { ref } from 'vue'

const basicValue = ref()
const iconValue = ref()
const readonlyValue = ref('这是只读状态的值')
const isReadonly = ref(true)
</script>

<template>
<div class="flex flex-col p-4 gap-3 w-full">
  <!-- 普通写法 -->
  <SkInput
    v-model="basicValue"
    placeholder="输入内容后可清空"
  >
    <template v-if="basicValue && String(basicValue).length" #trailing="{ clax }">
      <div
        class="text-secondary hover:text-primary transition-colors cursor-pointer"
        :class="clax"
        @click="basicValue = ''"
      >
        <SkIcon name="i-lucide:close" clax="sk-unit:size-18" />
      </div>
    </template>
  </SkInput>

  <!-- 进阶写法 -->
  <SkInput
    v-model="iconValue"
    placeholder="进阶版内容可清空"
    :clax="{
      trailing: 'text-secondary hover:text-primary transition-colors cursor-pointer',
    }"
  >
    <template v-if="iconValue && String(iconValue).length" #trailing="{ clax }">
      <div :class="clax" @click="iconValue = ''">
        <SkIcon name="i-lucide:close" clax="sk-unit:size-18" />
      </div>
    </template>
  </SkInput>

  <SkInput
    v-model="readonlyValue"
    placeholder="只读状态不显示清空按钮"
    :readonly="isReadonly"
    :clax="{
      trailing: 'text-secondary hover:text-primary transition-colors cursor-pointer',
    }"
  >
    <template
      v-if="!isReadonly && String(readonlyValue).length"
      #trailing="{ clax }"
    >
      <div :class="clax" @click="readonlyValue = ''">
        <SkIcon name="i-lucide:close" clax="sk-unit:size-18" />
      </div>
    </template>
  </SkInput>
</div>
</template>

配合图标

通过 leadingtrailing 属性可以在输入框前后添加图标或文本,提供更丰富的交互体验

图标的设置可以配合 SkIcon 组件进行使用,前往获取该组件更多信息
<script setup lang="ts">
import { ref } from 'vue'

const searchValue = ref('')
const infoValue = ref('')
const prefixValue = ref('')
const combinedValue = ref('')
</script>

<template>
<div class="flex flex-col gap-3 p-4 w-full">
  <SkInput
    v-model="searchValue"
    placeholder="搜索内容..."
  >
    <template #leading="{ clax }">
      <SkIcon name="i-lucide:search" size="18px" :clax="clax" />
    </template>
  </SkInput>
  <SkInput
    v-model="infoValue"
    placeholder="输入信息..."
  >
    <template #trailing="{ clax }">
      <SkIcon name="i-lucide:info" size="18px" :clax="clax" />
    </template>
  </SkInput>

  <SkInput
    v-model="prefixValue"
    type="number"
    placeholder="0.00"
  >
    <template #leading="{ clax }">
      <span :class="clax"></span>
    </template>
  </SkInput>
  <SkInput
    v-model="combinedValue"
    placeholder="距离"
    type="number"
  >
    <template #leading="{ clax }">
      <SkIcon name="i-lucide:map-pin" size="16px" :clax="[clax, 'text-brand']" />
    </template>
    <template #trailing="{ clax }">
      <span :class="clax">km</span>
    </template>
  </SkInput>
</div>
</template>

定制样式

目前只展示了部分自定义样式,后续会继续补充

<script setup lang="ts">
import { ref } from 'vue'

const roundedValue = ref('')
const gradientValue = ref('')
</script>

<template>
  <div class="flex flex-col gap-3 p-4 w-full">
    <SkInput
      v-model="roundedValue"
      placeholder="圆角输入框"
      :clax="{
        root: 'rounded-full bg-transparent border-2 border-solid border-brand',
      }"
    />
    <SkInput
      v-model="gradientValue"
      placeholder="渐变背景输入框"
      :clax="{
        root: 'bg-gradient-to-r from-brand/10 to-success/10 border border-brand/30',
      }"
    />
  </div>
</template>

接口

Props

属性名类型默认值描述
v-modelstring|number-详见 Controlled 输入值
placeholderstring-详见 Placeholder 占位符
typestring'text'详见 Type 类型
size{Size}'medium'详见 Size 尺寸
passwordbooleanfalse详见 Password 密码状态
disabledbooleanfalse详见 Disabled 禁用
readonlybooleanfalse详见 Readonly 只读
maxlengthnumber140详见 Maxlength 最大长度
claxWIP-用于拓展当前基础样式

Emits

事件名参数描述
focus(event: Event)当输入框获得焦点时触发
blur(event: Event)当输入框失去焦点时触发
confirm(event: Event)当点击确认输入时触发
Event<T extend typeof v-model>描述
value: T输入框的值

Slots

插槽名属性描述
leading{ clax: string }输入框前缀内容
trailing{ clax: string }输入框后缀内容

交互

正在完善中,敬请期待!😊