Skip to content
On this page

Form

The form contains 'input box', 'radio box', 'drop selection', 'multiple check box' and other user-input components. Using forms, you can collect, validate, and submit data.

Basic usage

In each 'form' component, you need a 'form-item' field as a container for inputs to get and validate values.

<>
<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>

Classic form

The most basic form includes various input form items, such as input, select, radio, checkbox, etc.

In each form component, you need a form-item field as a container for the input item to get the value.

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 property

Property nameDescriptionTypeDefault value
v-modelBinding valueboolean
label-widthlabel wideString80px

FormItem property

Property nameDescriptionTypeDefault value
labelThe label of the option, if not set, defaults to the same as valuestring/number

Released under the MIT License.