多行文本框
多行文本框组件用于收集大量文本数据。
用例
v-textarea
最简单的形式是多行文本字段,可用于大量文本。
template
<template>
<v-container fluid>
<v-row>
<v-col cols="12" md="6">
<v-textarea
name="input-7-1"
label="Default style"
value="The Woodman set to work at once, and so sharp was his axe that the tree was soon chopped nearly through."
hint="Hint text"
></v-textarea>
</v-col>
<v-col cols="12" md="6">
<v-textarea
solo
name="input-7-4"
label="Solo textarea"
></v-textarea>
</v-col>
<v-col cols="12" md="6">
<v-textarea
filled
name="input-7-4"
label="Filled textarea"
value="The Woodman set to work at once, and so sharp was his axe that the tree was soon chopped nearly through."
></v-textarea>
</v-col>
<v-col cols="12" md="6">
<v-textarea
outlined
name="input-7-4"
label="Outlined textarea"
value="The Woodman set to work at once, and so sharp was his axe that the tree was soon chopped nearly through."
></v-textarea>
</v-col>
</v-row>
</v-container>
</template>
API
从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。
实战场
template script
<template>
<v-container fluid>
<v-row>
<v-col
sm="6"
cols="12"
>
<v-text-field
v-model="label"
label="Label"
></v-text-field>
<v-text-field
v-model="hint"
label="Hint"
></v-text-field>
<v-text-field
v-model="placeholder"
label="Placeholder"
></v-text-field>
</v-col>
<v-col cols="12" sm="6">
<v-text-field
v-model="rowHeight"
:disabled="!autoGrow"
class="mx-4"
label="Row height - px"
max="500"
min="1"
step="1"
style="width: 125px"
type="number"
></v-text-field>
<v-text-field
v-model="rows"
class="mx-4"
label="Rows"
max="50"
min="1"
step="1"
style="width: 125px"
type="number"
></v-text-field>
</v-col>
<v-col cols="12"></v-col>
<v-col cols="12" md="6">
<v-switch
v-model="autoGrow"
class="ma-1"
label="Auto-grow"
></v-switch>
<v-switch
v-model="clearable"
class="ma-1"
label="Clearable"
></v-switch>
<v-switch
v-model="filled"
class="ma-1"
label="Filled"
></v-switch>
<v-switch
v-model="flat"
class="ma-1"
label="Flat (requires Solo)"
:disabled="!solo"
></v-switch>
<v-switch
v-model="loading"
class="ma-1"
label="Loading"
></v-switch>
<v-switch
v-model="outlined"
class="ma-1"
label="Outlined"
></v-switch>
<v-switch
v-model="persistentHint"
class="ma-1"
label="Persistent Hint"
></v-switch>
<v-switch
v-model="rounded"
class="ma-1"
label="Rounded (requires Filled or Outlined)"
></v-switch>
<v-switch
v-model="shaped"
class="ma-1"
label="Shaped (requires Filled or Outlined)"
></v-switch>
<v-switch
v-model="singleLine"
class="ma-1"
label="Single-line"
></v-switch>
<v-switch
v-model="solo"
class="ma-1"
label="Solo"
></v-switch>
</v-col>
<v-col
cols="12"
md="6"
>
<v-sheet
elevation="12"
class="pa-12"
>
<v-textarea
v-model="model"
:auto-grow="autoGrow"
:clearable="clearable"
:counter="counter ? counter : false"
:filled="filled"
:flat="flat"
:hint="hint"
:label="label"
:loading="loading"
:no-resize="noResize"
:outlined="outlined"
:persistent-hint="persistentHint"
:placeholder="placeholder"
:rounded="rounded"
:row-height="rowHeight"
:rows="rows"
:shaped="shaped"
:single-line="singleLine"
:solo="solo"
></v-textarea>
<div class="mt-12 text-center">
Value: {{ model }}
</div>
</v-sheet>
</v-col>
</v-row>
</v-container>
</template>
<script>
export default {
data: () => ({
autoGrow: false,
autofocus: true,
clearable: false,
counter: 0,
filled: false,
flat: false,
hint: '',
label: '',
loading: false,
model: 'I\'m a textarea.',
noResize: false,
outlined: false,
persistentHint: false,
placeholder: '',
rounded: false,
rowHeight: 24,
rows: 1,
shaped: false,
singleLine: false,
solo: false,
}),
}
</script>
示例
下面是一些简单到复杂的例子。
图标
append-icon
和 prepend-icon
属性有助于将上下文添加到 v-textarea
。
template
<template>
<v-container>
<v-row>
<v-col cols="12" sm="6">
<v-textarea
class="mx-2"
label="prepend-icon"
rows="1"
prepend-icon="comment"
></v-textarea>
</v-col>
<v-col cols="12" sm="6">
<v-textarea
append-icon="comment"
class="mx-2"
label="append-icon"
rows="1"
></v-textarea>
</v-col>
<v-col cols="12" sm="6">
<v-textarea
prepend-inner-icon="comment"
class="mx-2"
label="prepend-inner-icon"
rows="1"
></v-textarea>
</v-col>
<v-col cols="12" sm="6">
<v-textarea
append-outer-icon="comment"
class="mx-2"
label="append-outer-icon"
rows="1"
></v-textarea>
</v-col>
</v-row>
</v-container>
</template>
自动增长
当使用 auto-grow
属性时,当包含的文本超过其大小时,多行文本框的大小将自动增加。
template
<template>
<v-container fluid>
<v-textarea
name="input-7-1"
filled
label="Label"
auto-grow
value="The Woodman set to work at once, and so sharp was his axe that the tree was soon chopped nearly through."
></v-textarea>
</v-container>
</template>
背景颜色
background-color
和 color
属性让您更好地控制样式 v-textarea
的样式。
template
<template>
<v-container>
<v-textarea
background-color="light-blue"
color="black"
label="Label"
></v-textarea>
<v-textarea
background-color="grey lighten-2"
color="cyan"
label="Label"
></v-textarea>
<v-textarea
background-color="amber lighten-4"
color="orange orange-darken-4"
label="Label"
></v-textarea>
</v-container>
</template>
浏览器自动完成
autocomplete
属性为您提供使浏览器能够预测用户输入的选项。
template
<template>
<v-container fluid>
<v-textarea
autocomplete="email"
label="Email"
></v-textarea>
</v-container>
</template>
可清除
您可以使用 clearable
属性清除 v-textarea
中的文本,并自定义与 clearable-icon
属性一起使用的图标。
template
<template>
<v-container fluid>
<v-textarea
clearable
clear-icon="cancel"
label="Text"
value="This is clearable text."
></v-textarea>
</v-container>
</template>
Alpha Theme
Complete theme experience including enhanced Vue CLI, full documentation, 5 custom components and much more!
ads by Vuetify
](https://store.vuetifyjs.com/product/alpha-theme?ref=vuetifyjs.com)
计数器
counter
属性通知用户 v-textarea
的字符数限制。
template script
<template>
<v-container fluid>
<v-textarea
counter
label="Text"
:rules="rules"
:value="value"
></v-textarea>
</v-container>
</template>
<script>
export default {
data: () => ({
rules: [v => v.length <= 25 || 'Max 25 characters'],
value: 'Hello!',
}),
}
</script>
没有调整大小
v-textarea
使用 no-resize
属性,可以选择保持大小不变,而不管其内容的大小如何。
template script
<template>
<v-container fluid>
<v-textarea
label="Text"
no-resize
rows="1"
:value="value"
></v-textarea>
</v-container>
</template>
<script>
export default {
data: () => ({
value: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
}),
}
</script>
行
rows
属性可让您定义文本区域有多少行,当与 row-height
属性结合使用时,您可以通过定义行的高度进一步自定义行。
template
<template>
<v-container fluid>
<v-row>
<v-col cols="12" sm="6">
<v-textarea
label="One row"
auto-grow
outlined
rows="1"
row-height="15"
></v-textarea>
</v-col>
<v-col cols="12" sm="6">
<v-textarea
filled
auto-grow
label="Two rows"
rows="2"
row-height="20"
></v-textarea>
</v-col>
<v-col cols="12" sm="6">
<v-textarea
label="Three rows"
auto-grow
outlined
rows="3"
row-height="25"
shaped
></v-textarea>
</v-col>
<v-col cols="12" sm="6">
<v-textarea
filled
auto-grow
label="Four rows"
rows="4"
row-height="30"
shaped
></v-textarea>
</v-col>
</v-row>
</v-container>
</template>
美丽的表单
利用替代的输入样式,您可以创建易于构建和使用的惊人界面。
template script
<template>
<v-card
class="mx-auto"
style="max-width: 500px;"
>
<v-system-bar
color="deep-purple darken-4"
dark
>
<v-spacer></v-spacer>
<v-icon small>mdi-square</v-icon>
<v-icon
class="ml-1"
small
>mdi-circle</v-icon>
<v-icon
class="ml-1"
small
>mdi-triangle</v-icon>
</v-system-bar>
<v-toolbar
color="deep-purple accent-4"
cards
dark
flat
>
<v-btn icon>
<v-icon>mdi-arrow-left</v-icon>
</v-btn>
<v-card-title class="title font-weight-regular">Sign up</v-card-title>
<v-spacer></v-spacer>
<v-btn icon>
<v-icon>mdi-magnify</v-icon>
</v-btn>
<v-btn icon>
<v-icon>mdi-dots-vertical</v-icon>
</v-btn>
</v-toolbar>
<v-form
ref="form"
v-model="form"
class="pa-4 pt-6"
>
<v-text-field
v-model="password"
:rules="[rules.password, rules.length(6)]"
filled
color="deep-purple"
counter="6"
label="Password"
style="min-height: 96px"
type="password"
></v-text-field>
<v-text-field
v-model="phone"
filled
color="deep-purple"
label="Phone number"
></v-text-field>
<v-text-field
v-model="email"
:rules="[rules.email]"
filled
color="deep-purple"
label="Email address"
type="email"
></v-text-field>
<v-textarea
v-model="bio"
auto-grow
filled
color="deep-purple"
label="Bio"
rows="1"
></v-textarea>
<v-checkbox
v-model="agreement"
:rules="[rules.required]"
color="deep-purple"
>
<template v-slot:label>
I agree to the
<a href="#" @click.stop.prevent="dialog = true">Terms of Service</a>
and
<a href="#" @click.stop.prevent="dialog = true">Privacy Policy</a>*
</template>
</v-checkbox>
</v-form>
<v-divider></v-divider>
<v-card-actions>
<v-btn
text
@click="$refs.form.reset()"
>
Clear
</v-btn>
<v-spacer></v-spacer>
<v-btn
:disabled="!form"
:loading="isLoading"
class="white--text"
color="deep-purple accent-4"
depressed
>Submit</v-btn>
</v-card-actions>
<v-dialog
v-model="dialog"
absolute
max-width="400"
persistent
>
<v-card>
<v-card-title class="headline grey lighten-3">Legal</v-card-title>
<v-card-text>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</v-card-text>
<v-divider></v-divider>
<v-card-actions>
<v-btn
text
@click="agreement = false, dialog = false"
>
No
</v-btn>
<v-spacer></v-spacer>
<v-btn
class="white--text"
color="deep-purple accent-4"
@click="agreement = true, dialog = false"
>
Yes
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-card>
</template>
<script>
export default {
data: () => ({
agreement: false,
bio: 'Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts',
dialog: false,
email: undefined,
form: false,
isLoading: false,
password: undefined,
phone: undefined,
rules: {
email: v => (v || '').match(/@/) || 'Please enter a valid email',
length: len => v => (v || '').length >= len || `Invalid character length, required ${len}`,
password: v => (v || '').match(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$/) ||
'Password must contain an upper case letter, a numeric character, and a special character',
required: v => !!v || 'This field is required',
},
}),
}
</script>