@@ -9,25 +9,38 @@ Provides the ability to control all form components.
99
1010``` html
1111<script setup >
12- import { reactive , ref } from ' vue'
12+ import { computed , reactive , ref } from ' vue'
1313
1414const formData = reactive ({
1515 username: ' ' ,
1616 password: ' ' ,
1717 otp: ' ' ,
18+ email: ' ' ,
1819 department: ' ' ,
20+ period: undefined ,
1921 gender: undefined ,
2022 license: false ,
21- range: 10 ,
23+ range: 0 ,
2224 count: 0 ,
2325 group: [],
2426 score: 0 ,
2527 like: [],
26- files: []
28+ files: [],
29+ custom: false ,
2730})
2831const form = ref (null )
2932const disabled = ref (false )
3033const readonly = ref (false )
34+
35+ const suggestions = computed (() =>
36+ [' @qq.com' , ' @163.com' , ' @gmail.com' ].map ((suffix ) => {
37+ const [prefix ] = formData .email .split (' @' )
38+ return {
39+ label: prefix + suffix,
40+ value: prefix + suffix,
41+ }
42+ }),
43+ )
3144 </script >
3245
3346<template >
@@ -36,98 +49,121 @@ const readonly = ref(false)
3649 :disabled =" disabled"
3750 :readonly =" readonly"
3851 scroll-to-error =" start"
52+ scroll-to-error-offset-y =" 14.4vmin"
3953 >
40- <var-space direction =" column" :size =" [14 , 0]" >
54+ <var-space direction =" column" :size =" ['4vmin' , 0]" >
4155 <var-input
42- placeholder =" Please input username"
43- :rules =" v => !!v || 'The username cannot be empty'"
4456 v-model =" formData.username"
57+ placeholder =" Please input username"
58+ :rules =" [(v) => !!v || 'The username cannot be empty']"
4559 />
4660 <var-input
61+ v-model =" formData.password"
4762 type =" password"
4863 placeholder =" Please input password"
4964 :rules =" [v => !!v || 'The password cannot be empty', (v) => v.length >= 8 || 'The password cannot be less than 8 characters']"
50- v-model =" formData.password"
65+ />
66+ <var-auto-complete
67+ v-model =" formData.email"
68+ placeholder =" Please input email"
69+ :rules =" [(v) => !!v || 'The email cannot be empty']"
70+ :options =" suggestions"
5171 />
5272 <var-select
53- placeholder =" Please select department"
54- :rules =" v => !!v || 'The select cannot be empty'"
5573 v-model =" formData.department"
74+ placeholder =" Please select department"
75+ :rules =" [(v) => !!v || 'The select cannot be empty']"
5676 >
5777 <var-option label =" Eat department" />
5878 <var-option label =" Sleep department" />
5979 <var-option label =" Play game department" />
6080 </var-select >
6181 <var-select
82+ v-model =" formData.group"
6283 multiple
6384 placeholder =" Please select group"
64- :rules =" v => v.length >= 1 || 'The select cannot be empty'"
65- v-model =" formData.group"
85+ :rules =" [(v) => v.length >= 1 || 'The select cannot be empty']"
6686 >
6787 <var-option label =" Eat group" />
6888 <var-option label =" Sleep group" />
6989 <var-option label =" Play game group" />
7090 </var-select >
91+ <var-segmented-buttons
92+ v-model =" formData.period"
93+ :rules =" [(v) => !!v || 'A period must be selected']"
94+ >
95+ <var-segmented-button checked-value =" day" >Day</var-segmented-button >
96+ <var-segmented-button checked-value =" week" >Week</var-segmented-button >
97+ <var-segmented-button checked-value =" month" >Month</var-segmented-button >
98+ </var-segmented-buttons >
7199 <var-radio-group
72- :rules =" v => !!v || 'The gender cannot be empty'"
73100 v-model =" formData.gender"
101+ :rules =" [(v) => !!v || 'The gender cannot be empty']"
74102 >
75103 <var-radio :checked-value =" 1" >Male</var-radio >
76104 <var-radio :checked-value =" 2" >Female</var-radio >
77105 </var-radio-group >
78106 <var-checkbox-group
79- :rules =" v => v.length > 0 || 'The select cannot be empty'"
80107 v-model =" formData.like"
108+ :rules =" [(v) => v.length > 0 || 'The select cannot be empty']"
81109 >
82110 <var-checkbox :checked-value =" 1" >Eat</var-checkbox >
83111 <var-checkbox :checked-value =" 2" >Sleep</var-checkbox >
84112 <var-checkbox :checked-value =" 3" >Play game</var-checkbox >
85113 </var-checkbox-group >
86114 <var-rate
87- :rules =" v => v >= 3 || 'It has to be greater than 2'"
88115 v-model =" formData.score"
116+ :rules =" [(v) => v >= 3 || 'It has to be greater than 2']"
89117 />
90118 <var-switch
91- variant
92- :rules =" v => !!v || 'You must turn on'"
93119 v-model =" formData.license"
120+ variant
121+ :rules =" [(v) => !!v || 'You must turn on']"
94122 />
95123 <var-counter
96- :rules =" v => v > 10 || 'It has to be greater than 10'"
97124 v-model =" formData.count"
125+ :rules =" [(v) => v > 10 || 'It has to be greater than 10']"
98126 />
99127 <var-slider
100- :rules =" v => v > 10 || 'It has to be greater than 10'"
101128 v-model =" formData.range"
129+ :rules =" [(v) => v > 10 || 'It has to be greater than 10']"
102130 />
103131 <var-otp-input
104132 v-model =" formData.otp"
105133 :rules =" [v => !!v || 'The OTP cannot be empty', (v) => v.length === 6 || 'The OTP length must be 6']"
106134 />
107135 <var-uploader
108- :rules =" v => v.length >= 1 || 'Upload at least one picture'"
109136 v-model =" formData.files"
137+ :rules =" [(v) => v.length >= 1 || 'Upload at least one picture']"
110138 />
111-
112- <var-space direction =" column" :size =" [14, 0]" >
113- <var-button block type =" danger" @click =" form.reset()" >
114- Empty form
115- </var-button >
116- <var-button block type =" warning" @click =" form.resetValidation()" >
117- Empty the validation
118- </var-button >
119- <var-button block type =" success" @click =" form.validate()" >
120- Trigger validation
121- </var-button >
122- <var-button block type =" info" @click =" disabled = !disabled" >
123- Form disabled
124- </var-button >
125- <var-button block type =" primary" @click =" readonly = !readonly" >
126- Form readonly
127- </var-button >
128- </var-space >
129139 </var-space >
140+
141+ <var-custom-form-component
142+ v-model =" formData.custom"
143+ extra-message =" Extra message"
144+ :rules =" [(v) => !!v || 'Please check it']"
145+ >
146+ Click toggle
147+ </var-custom-form-component >
130148 </var-form >
149+
150+ <var-space direction =" column" :size =" ['4vmin', 0]" >
151+ <var-button block type =" danger" @click =" form.reset()" >
152+ Empty form
153+ </var-button >
154+ <var-button block type =" warning" @click =" form.resetValidation()" >
155+ Empty the validation
156+ </var-button >
157+ <var-button block type =" success" @click =" form.validate()" >
158+ Trigger validation
159+ </var-button >
160+ <var-button block type =" info" @click =" disabled = !disabled" >
161+ Form disabled
162+ </var-button >
163+ <var-button block type =" primary" @click =" readonly = !readonly" >
164+ Form readonly
165+ </var-button >
166+ </var-space >
131167</template >
132168```
133169
0 commit comments