Skip to content
On this page

Form 表单

表单包含 输入框, 单选框, 下拉选择, 多选框 等用户输入的组件。 使用表单,您可以收集、验证和提交数据。

基础用法

在每一个 form 组件中,你需要一个 form-item 字段作为输入项的容器,用于获取值与验证值。

<>
<template>
    <kl-form :model="form" labelWidth="120px">
        <kl-form-item label="username">
            <kl-input placeholder="请输入账号" v-model="form.name" clearable></kl-input>
        </kl-form-item>
        <kl-form-item label="password">
            <kl-input
                type="password"
                placeholder="请输入密码"
                v-model="form.password"
                showPassword
            ></kl-input>
        </kl-form-item>
        <kl-form-item>
            <kl-button type="primary" @click="login">login</kl-button>
            <kl-button>Cancel</kl-button>
        </kl-form-item>
    </kl-form>
</template>

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

const form = reactive({
    name: '',
    password: ''
})

const login = () => {
    console.log(form)
}
</script>

经典表单

最基础的表单包括各种输入表单项,比如 input、select、radio、checkbox 等。

在每一个 form 组件中,你需要一个 form-item 字段作为输入项的容器,用于获取值。

please select your zone
<>
<template>
    <kl-form :model="form" label-width="120px">
        <kl-form-item label="Activity name">
            <kl-input v-model="form.name" />
        </kl-form-item>
        <kl-form-item label="Activity zone">
            <kl-select v-model="form.region" placeholder="please select your zone">
                <kl-option label="Zone one" value="shanghai" />
                <kl-option label="Zone two" value="beijing" />
            </kl-select>
        </kl-form-item>
        <kl-form-item label="Activity time">
            <kl-date-picker isDateTime v-model="form.date"></kl-date-picker>
        </kl-form-item>
        <kl-form-item label="Instant delivery">
            <kl-switch v-model="form.delivery" />
        </kl-form-item>
        <kl-form-item label="Activity type">
            <kl-checkbox-group v-model="form.type">
                <kl-checkbox label="Online activities" value="Online activities" name="type" />
                <kl-checkbox
                    label="Promotion activities"
                    value="Promotion activities"
                    name="type"
                />
                <kl-checkbox label="Offline activities" value="Offline activities" name="type" />
                <kl-checkbox
                    label="Simple brand exposure"
                    value="Simple brand exposure"
                    name="type"
                />
            </kl-checkbox-group>
        </kl-form-item>
        <kl-form-item label="Resources">
            <kl-radio-group v-model="form.resource">
                <kl-radio label="Sponsor" value="Sponsor" />
                <kl-radio label="Venue" value="Venue" />
            </kl-radio-group>
        </kl-form-item>
        <kl-form-item label="Activity form">
            <kl-input v-model="form.desc" type="textarea" />
        </kl-form-item>
        <kl-form-item>
            <kl-button type="primary" @click="onSubmit">Create</kl-button>
            <kl-button type="info">Cancel</kl-button>
        </kl-form-item>
    </kl-form>
</template>

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

// do not use same name with ref
const form = reactive({
    name: '',
    region: '',
    date: new Date(),
    delivery: false,
    type: [],
    resource: '',
    desc: ''
})

const onSubmit = () => {
    console.log(form)
}
</script>

API

Form 属性

属性名说明类型默认值
v-model绑定值boolean
label-widthlabel 宽String80px

FormItem 属性

属性名说明类型默认值
label选项的标签,若不设置则默认与 value 相同string/number

Released under the MIT License.