mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
2 lines
44 KiB
JavaScript
2 lines
44 KiB
JavaScript
/*! For license information please see 4150.3a3c8703.js.LICENSE.txt */
|
|
(self.webpackChunk=self.webpackChunk||[]).push([["4150"],{40732:function(s,a,n){"use strict";n.r(a);var l=n("80681");let t=["innerHTML"];a.default={setup:()=>({html:""}),render:()=>((0,l.wg)(),(0,l.iD)("div",{class:"van-doc-markdown-body",innerHTML:'<h1>Form</h1>\n<div class="van-doc-card"><h3 id="intro" tabindex="-1">Intro</h3>\n<p>Used for data entry and verification, and supports input boxes, radio buttons, check boxes, file uploads and other types. Should be used with <a href="#/en-US/field" target="_blank">Field</a> component.</p>\n</div><div class="van-doc-card"><h3 id="install" tabindex="-1">Install</h3>\n<p>Register component globally via <code>app.use</code>, refer to <a href="#/en-US/advanced-usage#zu-jian-zhu-ce" target="_blank">Component Registration</a> for more registration ways.</p>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> { createApp } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;\n<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Form</span>, <span class="hljs-title class_">Field</span>, <span class="hljs-title class_">CellGroup</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'vant'</span>;\n\n<span class="hljs-keyword">const</span> app = <span class="hljs-title function_">createApp</span>();\napp.<span class="hljs-title function_">use</span>(<span class="hljs-title class_">Form</span>);\napp.<span class="hljs-title function_">use</span>(<span class="hljs-title class_">Field</span>);\napp.<span class="hljs-title function_">use</span>(<span class="hljs-title class_">CellGroup</span>);\n</code></pre>\n</div><h2 id="usage" tabindex="-1">Usage</h2>\n<div class="van-doc-card"><h3 id="basic-usage" tabindex="-1">Basic Usage</h3>\n<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-form</span> @<span class="hljs-attr">submit</span>=<span class="hljs-string">"onSubmit"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-cell-group</span> <span class="hljs-attr">inset</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-field</span>\n <span class="hljs-attr">v-model</span>=<span class="hljs-string">"username"</span>\n <span class="hljs-attr">name</span>=<span class="hljs-string">"Username"</span>\n <span class="hljs-attr">label</span>=<span class="hljs-string">"Username"</span>\n <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Username"</span>\n <span class="hljs-attr">:rules</span>=<span class="hljs-string">"[{ required: true, message: 'Username is required' }]"</span>\n /></span>\n <span class="hljs-tag"><<span class="hljs-name">van-field</span>\n <span class="hljs-attr">v-model</span>=<span class="hljs-string">"password"</span>\n <span class="hljs-attr">type</span>=<span class="hljs-string">"password"</span>\n <span class="hljs-attr">name</span>=<span class="hljs-string">"Password"</span>\n <span class="hljs-attr">label</span>=<span class="hljs-string">"Password"</span>\n <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Password"</span>\n <span class="hljs-attr">:rules</span>=<span class="hljs-string">"[{ required: true, message: 'Password is required' }]"</span>\n /></span>\n <span class="hljs-tag"></<span class="hljs-name">van-cell-group</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"margin: 16px;"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-button</span> <span class="hljs-attr">round</span> <span class="hljs-attr">block</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"primary"</span> <span class="hljs-attr">native-type</span>=<span class="hljs-string">"submit"</span>></span>\n Submit\n <span class="hljs-tag"></<span class="hljs-name">van-button</span>></span>\n <span class="hljs-tag"></<span class="hljs-name">div</span>></span>\n<span class="hljs-tag"></<span class="hljs-name">van-form</span>></span>\n</code></pre>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;\n\n<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {\n <span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {\n <span class="hljs-keyword">const</span> username = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);\n <span class="hljs-keyword">const</span> password = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);\n <span class="hljs-keyword">const</span> <span class="hljs-title function_">onSubmit</span> = (<span class="hljs-params">values</span>) => {\n <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'submit'</span>, values);\n };\n\n <span class="hljs-keyword">return</span> {\n username,\n password,\n onSubmit,\n };\n },\n};\n</code></pre>\n</div><div class="van-doc-card"><h3 id="validate-rules" tabindex="-1">Validate Rules</h3>\n<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-form</span> @<span class="hljs-attr">failed</span>=<span class="hljs-string">"onFailed"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-cell-group</span> <span class="hljs-attr">inset</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-field</span>\n <span class="hljs-attr">v-model</span>=<span class="hljs-string">"value1"</span>\n <span class="hljs-attr">name</span>=<span class="hljs-string">"pattern"</span>\n <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Use pattern"</span>\n <span class="hljs-attr">:rules</span>=<span class="hljs-string">"[{ pattern, message: 'Error message' }]"</span>\n /></span>\n <span class="hljs-tag"><<span class="hljs-name">van-field</span>\n <span class="hljs-attr">v-model</span>=<span class="hljs-string">"value2"</span>\n <span class="hljs-attr">name</span>=<span class="hljs-string">"validator"</span>\n <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Use validator"</span>\n <span class="hljs-attr">:rules</span>=<span class="hljs-string">"[{ validator, message: 'Error message' }]"</span>\n /></span>\n <span class="hljs-tag"><<span class="hljs-name">van-field</span>\n <span class="hljs-attr">v-model</span>=<span class="hljs-string">"value3"</span>\n <span class="hljs-attr">name</span>=<span class="hljs-string">"validatorMessage"</span>\n <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Use validator to return message"</span>\n <span class="hljs-attr">:rules</span>=<span class="hljs-string">"[{ validator: validatorMessage }]"</span>\n /></span>\n <span class="hljs-tag"><<span class="hljs-name">van-field</span>\n <span class="hljs-attr">v-model</span>=<span class="hljs-string">"value4"</span>\n <span class="hljs-attr">name</span>=<span class="hljs-string">"asyncValidator"</span>\n <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Use async validator"</span>\n <span class="hljs-attr">:rules</span>=<span class="hljs-string">"[{ validator: asyncValidator, message: 'Error message' }]"</span>\n /></span>\n <span class="hljs-tag"></<span class="hljs-name">van-cell-group</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"margin: 16px;"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-button</span> <span class="hljs-attr">round</span> <span class="hljs-attr">block</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"primary"</span> <span class="hljs-attr">native-type</span>=<span class="hljs-string">"submit"</span>></span>\n Submit\n <span class="hljs-tag"></<span class="hljs-name">van-button</span>></span>\n <span class="hljs-tag"></<span class="hljs-name">div</span>></span>\n<span class="hljs-tag"></<span class="hljs-name">van-form</span>></span>\n</code></pre>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;\n<span class="hljs-keyword">import</span> { closeToast, showLoadingToast } <span class="hljs-keyword">from</span> <span class="hljs-string">'vant'</span>;\n\n<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {\n <span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {\n <span class="hljs-keyword">const</span> value1 = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);\n <span class="hljs-keyword">const</span> value2 = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);\n <span class="hljs-keyword">const</span> value3 = <span class="hljs-title function_">ref</span>(<span class="hljs-string">'abc'</span>);\n <span class="hljs-keyword">const</span> value4 = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);\n <span class="hljs-keyword">const</span> pattern = <span class="hljs-regexp">/\\d{6}/</span>;\n\n <span class="hljs-keyword">const</span> <span class="hljs-title function_">validator</span> = (<span class="hljs-params">val</span>) => <span class="hljs-regexp">/1\\d{10}/</span>.<span class="hljs-title function_">test</span>(val);\n\n <span class="hljs-keyword">const</span> <span class="hljs-title function_">validatorMessage</span> = (<span class="hljs-params">val</span>) => <span class="hljs-string">`<span class="hljs-subst">${val}</span> is invalid`</span>;\n\n <span class="hljs-keyword">const</span> <span class="hljs-title function_">asyncValidator</span> = (<span class="hljs-params">val</span>) =>\n <span class="hljs-keyword">new</span> <span class="hljs-title class_">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve</span>) =></span> {\n <span class="hljs-title function_">showLoadingToast</span>(<span class="hljs-string">'Validating...'</span>);\n\n <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {\n <span class="hljs-title function_">closeToast</span>();\n <span class="hljs-title function_">resolve</span>(val === <span class="hljs-string">'1234'</span>);\n }, <span class="hljs-number">1000</span>);\n });\n\n <span class="hljs-keyword">const</span> <span class="hljs-title function_">onFailed</span> = (<span class="hljs-params">errorInfo</span>) => {\n <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'failed'</span>, errorInfo);\n };\n\n <span class="hljs-keyword">return</span> {\n value1,\n value2,\n value3,\n value4,\n pattern,\n onFailed,\n validator,\n asyncValidator,\n validatorMessage,\n };\n },\n};\n</code></pre>\n</div><div class="van-doc-card"><h3 id="field-type---switch" tabindex="-1">Field Type - Switch</h3>\n<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-field</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"switch"</span> <span class="hljs-attr">label</span>=<span class="hljs-string">"Switch"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">template</span> #<span class="hljs-attr">input</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-switch</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">"checked"</span> /></span>\n <span class="hljs-tag"></<span class="hljs-name">template</span>></span>\n<span class="hljs-tag"></<span class="hljs-name">van-field</span>></span>\n</code></pre>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;\n\n<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {\n <span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {\n <span class="hljs-keyword">const</span> checked = <span class="hljs-title function_">ref</span>(<span class="hljs-literal">false</span>);\n <span class="hljs-keyword">return</span> { checked };\n },\n};\n</code></pre>\n</div><div class="van-doc-card"><h3 id="field-type---checkbox" tabindex="-1">Field Type - Checkbox</h3>\n<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-field</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"checkbox"</span> <span class="hljs-attr">label</span>=<span class="hljs-string">"Checkbox"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">template</span> #<span class="hljs-attr">input</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-checkbox</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">"checked"</span> <span class="hljs-attr">shape</span>=<span class="hljs-string">"square"</span> /></span>\n <span class="hljs-tag"></<span class="hljs-name">template</span>></span>\n<span class="hljs-tag"></<span class="hljs-name">van-field</span>></span>\n<span class="hljs-tag"><<span class="hljs-name">van-field</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"checkboxGroup"</span> <span class="hljs-attr">label</span>=<span class="hljs-string">"CheckboxGroup"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">template</span> #<span class="hljs-attr">input</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-checkbox-group</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">"groupChecked"</span> <span class="hljs-attr">direction</span>=<span class="hljs-string">"horizontal"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-checkbox</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"1"</span> <span class="hljs-attr">shape</span>=<span class="hljs-string">"square"</span>></span>Checkbox 1<span class="hljs-tag"></<span class="hljs-name">van-checkbox</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-checkbox</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"2"</span> <span class="hljs-attr">shape</span>=<span class="hljs-string">"square"</span>></span>Checkbox 2<span class="hljs-tag"></<span class="hljs-name">van-checkbox</span>></span>\n <span class="hljs-tag"></<span class="hljs-name">van-checkbox-group</span>></span>\n <span class="hljs-tag"></<span class="hljs-name">template</span>></span>\n<span class="hljs-tag"></<span class="hljs-name">van-field</span>></span>\n</code></pre>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;\n\n<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {\n <span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {\n <span class="hljs-keyword">const</span> checked = <span class="hljs-title function_">ref</span>(<span class="hljs-literal">false</span>);\n <span class="hljs-keyword">const</span> groupChecked = <span class="hljs-title function_">ref</span>([]);\n <span class="hljs-keyword">return</span> {\n checked,\n groupChecked,\n };\n },\n};\n</code></pre>\n</div><div class="van-doc-card"><h3 id="field-type---radio" tabindex="-1">Field Type - Radio</h3>\n<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-field</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"radio"</span> <span class="hljs-attr">label</span>=<span class="hljs-string">"Radio"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">template</span> #<span class="hljs-attr">input</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-radio-group</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">"checked"</span> <span class="hljs-attr">direction</span>=<span class="hljs-string">"horizontal"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-radio</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"1"</span>></span>Radio 1<span class="hljs-tag"></<span class="hljs-name">van-radio</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-radio</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"2"</span>></span>Radio 2<span class="hljs-tag"></<span class="hljs-name">van-radio</span>></span>\n <span class="hljs-tag"></<span class="hljs-name">van-radio-group</span>></span>\n <span class="hljs-tag"></<span class="hljs-name">template</span>></span>\n<span class="hljs-tag"></<span class="hljs-name">van-field</span>></span>\n</code></pre>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;\n\n<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {\n <span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {\n <span class="hljs-keyword">const</span> checked = <span class="hljs-title function_">ref</span>(<span class="hljs-string">'1'</span>);\n <span class="hljs-keyword">return</span> { checked };\n },\n};\n</code></pre>\n</div><div class="van-doc-card"><h3 id="field-type---stepper" tabindex="-1">Field Type - Stepper</h3>\n<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-field</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"stepper"</span> <span class="hljs-attr">label</span>=<span class="hljs-string">"Stepper"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">template</span> #<span class="hljs-attr">input</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-stepper</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">"value"</span> /></span>\n <span class="hljs-tag"></<span class="hljs-name">template</span>></span>\n<span class="hljs-tag"></<span class="hljs-name">van-field</span>></span>\n</code></pre>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;\n\n<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {\n <span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {\n <span class="hljs-keyword">const</span> value = <span class="hljs-title function_">ref</span>(<span class="hljs-number">1</span>);\n <span class="hljs-keyword">return</span> { value };\n },\n};\n</code></pre>\n</div><div class="van-doc-card"><h3 id="field-type---rate" tabindex="-1">Field Type - Rate</h3>\n<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-field</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"rate"</span> <span class="hljs-attr">label</span>=<span class="hljs-string">"Rate"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">template</span> #<span class="hljs-attr">input</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-rate</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">"value"</span> /></span>\n <span class="hljs-tag"></<span class="hljs-name">template</span>></span>\n<span class="hljs-tag"></<span class="hljs-name">van-field</span>></span>\n</code></pre>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;\n\n<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {\n <span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {\n <span class="hljs-keyword">const</span> value = <span class="hljs-title function_">ref</span>(<span class="hljs-number">3</span>);\n <span class="hljs-keyword">return</span> { value };\n },\n};\n</code></pre>\n</div><div class="van-doc-card"><h3 id="field-type---slider" tabindex="-1">Field Type - Slider</h3>\n<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-field</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"slider"</span> <span class="hljs-attr">label</span>=<span class="hljs-string">"Slider"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">template</span> #<span class="hljs-attr">input</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-slider</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">"value"</span> /></span>\n <span class="hljs-tag"></<span class="hljs-name">template</span>></span>\n<span class="hljs-tag"></<span class="hljs-name">van-field</span>></span>\n</code></pre>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;\n\n<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {\n <span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {\n <span class="hljs-keyword">const</span> value = <span class="hljs-title function_">ref</span>(<span class="hljs-number">50</span>);\n <span class="hljs-keyword">return</span> { value };\n },\n};\n</code></pre>\n</div><div class="van-doc-card"><h3 id="field-type---uploader" tabindex="-1">Field Type - Uploader</h3>\n<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-field</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"uploader"</span> <span class="hljs-attr">label</span>=<span class="hljs-string">"Uploader"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">template</span> #<span class="hljs-attr">input</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-uploader</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">"value"</span> /></span>\n <span class="hljs-tag"></<span class="hljs-name">template</span>></span>\n<span class="hljs-tag"></<span class="hljs-name">van-field</span>></span>\n</code></pre>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;\n\n<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {\n <span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {\n <span class="hljs-keyword">const</span> value = <span class="hljs-title function_">ref</span>([\n { <span class="hljs-attr">url</span>: <span class="hljs-string">'https://fastly.jsdelivr.net/npm/@vant/assets/leaf.jpeg'</span> },\n ]);\n <span class="hljs-keyword">return</span> { value };\n },\n};\n</code></pre>\n</div><div class="van-doc-card"><h3 id="field-type---picker" tabindex="-1">Field Type - Picker</h3>\n<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-field</span>\n <span class="hljs-attr">v-model</span>=<span class="hljs-string">"result"</span>\n <span class="hljs-attr">is-link</span>\n <span class="hljs-attr">readonly</span>\n <span class="hljs-attr">name</span>=<span class="hljs-string">"picker"</span>\n <span class="hljs-attr">label</span>=<span class="hljs-string">"Picker"</span>\n <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Select city"</span>\n @<span class="hljs-attr">click</span>=<span class="hljs-string">"showPicker = true"</span>\n/></span>\n<span class="hljs-tag"><<span class="hljs-name">van-popup</span> <span class="hljs-attr">v-model:show</span>=<span class="hljs-string">"showPicker"</span> <span class="hljs-attr">position</span>=<span class="hljs-string">"bottom"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-picker</span>\n <span class="hljs-attr">:columns</span>=<span class="hljs-string">"columns"</span>\n @<span class="hljs-attr">confirm</span>=<span class="hljs-string">"onConfirm"</span>\n @<span class="hljs-attr">cancel</span>=<span class="hljs-string">"showPicker = false"</span>\n /></span>\n<span class="hljs-tag"></<span class="hljs-name">van-popup</span>></span>\n</code></pre>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;\n\n<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {\n <span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {\n <span class="hljs-keyword">const</span> result = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);\n <span class="hljs-keyword">const</span> showPicker = <span class="hljs-title function_">ref</span>(<span class="hljs-literal">false</span>);\n <span class="hljs-keyword">const</span> columns = [\n { <span class="hljs-attr">text</span>: <span class="hljs-string">'Delaware'</span>, <span class="hljs-attr">value</span>: <span class="hljs-string">'Delaware'</span> },\n { <span class="hljs-attr">text</span>: <span class="hljs-string">'Florida'</span>, <span class="hljs-attr">value</span>: <span class="hljs-string">'Florida'</span> },\n { <span class="hljs-attr">text</span>: <span class="hljs-string">'Georgia'</span>, <span class="hljs-attr">value</span>: <span class="hljs-string">'Georgia'</span> },\n { <span class="hljs-attr">text</span>: <span class="hljs-string">'Indiana'</span>, <span class="hljs-attr">value</span>: <span class="hljs-string">'Indiana'</span> },\n { <span class="hljs-attr">text</span>: <span class="hljs-string">'Maine'</span>, <span class="hljs-attr">value</span>: <span class="hljs-string">'Maine'</span> },\n ];\n\n <span class="hljs-keyword">const</span> <span class="hljs-title function_">onConfirm</span> = (<span class="hljs-params">{ selectedOptions }</span>) => {\n result.<span class="hljs-property">value</span> = selectedOptions[<span class="hljs-number">0</span>]?.<span class="hljs-property">text</span>;\n showPicker.<span class="hljs-property">value</span> = <span class="hljs-literal">false</span>;\n };\n\n <span class="hljs-keyword">return</span> {\n result,\n columns,\n onConfirm,\n showPicker,\n };\n },\n};\n</code></pre>\n</div><div class="van-doc-card"><h3 id="field-type---datepicker" tabindex="-1">Field Type - DatePicker</h3>\n<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-field</span>\n <span class="hljs-attr">v-model</span>=<span class="hljs-string">"result"</span>\n <span class="hljs-attr">is-link</span>\n <span class="hljs-attr">readonly</span>\n <span class="hljs-attr">name</span>=<span class="hljs-string">"datePicker"</span>\n <span class="hljs-attr">label</span>=<span class="hljs-string">"Date Picker"</span>\n <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Select date"</span>\n @<span class="hljs-attr">click</span>=<span class="hljs-string">"showPicker = true"</span>\n/></span>\n<span class="hljs-tag"><<span class="hljs-name">van-popup</span> <span class="hljs-attr">v-model:show</span>=<span class="hljs-string">"showPicker"</span> <span class="hljs-attr">position</span>=<span class="hljs-string">"bottom"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-date-picker</span> @<span class="hljs-attr">confirm</span>=<span class="hljs-string">"onConfirm"</span> @<span class="hljs-attr">cancel</span>=<span class="hljs-string">"showPicker = false"</span> /></span>\n<span class="hljs-tag"></<span class="hljs-name">van-popup</span>></span>\n</code></pre>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;\n\n<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {\n <span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {\n <span class="hljs-keyword">const</span> result = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);\n <span class="hljs-keyword">const</span> showPicker = <span class="hljs-title function_">ref</span>(<span class="hljs-literal">false</span>);\n <span class="hljs-keyword">const</span> <span class="hljs-title function_">onConfirm</span> = (<span class="hljs-params">{ selectedValues }</span>) => {\n result.<span class="hljs-property">value</span> = selectedValues.<span class="hljs-title function_">join</span>(<span class="hljs-string">'/'</span>);\n showPicker.<span class="hljs-property">value</span> = <span class="hljs-literal">false</span>;\n };\n\n <span class="hljs-keyword">return</span> {\n result,\n onConfirm,\n showPicker,\n };\n },\n};\n</code></pre>\n</div><div class="van-doc-card"><h3 id="field-type---area" tabindex="-1">Field Type - Area</h3>\n<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-field</span>\n <span class="hljs-attr">v-model</span>=<span class="hljs-string">"result"</span>\n <span class="hljs-attr">is-link</span>\n <span class="hljs-attr">readonly</span>\n <span class="hljs-attr">name</span>=<span class="hljs-string">"area"</span>\n <span class="hljs-attr">label</span>=<span class="hljs-string">"Area Picker"</span>\n <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Select area"</span>\n @<span class="hljs-attr">click</span>=<span class="hljs-string">"showArea = true"</span>\n/></span>\n<span class="hljs-tag"><<span class="hljs-name">van-popup</span> <span class="hljs-attr">v-model:show</span>=<span class="hljs-string">"showArea"</span> <span class="hljs-attr">position</span>=<span class="hljs-string">"bottom"</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-area</span>\n <span class="hljs-attr">:area-list</span>=<span class="hljs-string">"areaList"</span>\n @<span class="hljs-attr">confirm</span>=<span class="hljs-string">"onConfirm"</span>\n @<span class="hljs-attr">cancel</span>=<span class="hljs-string">"showArea = false"</span>\n /></span>\n<span class="hljs-tag"></<span class="hljs-name">van-popup</span>></span>\n</code></pre>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;\n<span class="hljs-keyword">import</span> { areaList } <span class="hljs-keyword">from</span> <span class="hljs-string">'@vant/area-data'</span>;\n\n<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {\n <span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {\n <span class="hljs-keyword">const</span> result = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);\n <span class="hljs-keyword">const</span> showArea = <span class="hljs-title function_">ref</span>(<span class="hljs-literal">false</span>);\n <span class="hljs-keyword">const</span> <span class="hljs-title function_">onConfirm</span> = (<span class="hljs-params">{ selectedOptions }</span>) => {\n showArea.<span class="hljs-property">value</span> = <span class="hljs-literal">false</span>;\n result.<span class="hljs-property">value</span> = selectedOptions.<span class="hljs-title function_">map</span>(<span class="hljs-function">(<span class="hljs-params">item</span>) =></span> item.<span class="hljs-property">text</span>).<span class="hljs-title function_">join</span>(<span class="hljs-string">'/'</span>);\n };\n\n <span class="hljs-keyword">return</span> {\n result,\n areaList,\n showArea,\n onConfirm,\n };\n },\n};\n</code></pre>\n</div><div class="van-doc-card"><h3 id="field-type---calendar" tabindex="-1">Field Type - Calendar</h3>\n<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-field</span>\n <span class="hljs-attr">v-model</span>=<span class="hljs-string">"result"</span>\n <span class="hljs-attr">is-link</span>\n <span class="hljs-attr">readonly</span>\n <span class="hljs-attr">name</span>=<span class="hljs-string">"calendar"</span>\n <span class="hljs-attr">label</span>=<span class="hljs-string">"Calendar"</span>\n <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Select date"</span>\n @<span class="hljs-attr">click</span>=<span class="hljs-string">"showCalendar = true"</span>\n/></span>\n<span class="hljs-tag"><<span class="hljs-name">van-calendar</span> <span class="hljs-attr">v-model:show</span>=<span class="hljs-string">"showCalendar"</span> @<span class="hljs-attr">confirm</span>=<span class="hljs-string">"onConfirm"</span> /></span>\n</code></pre>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;\n\n<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {\n <span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {\n <span class="hljs-keyword">const</span> result = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);\n <span class="hljs-keyword">const</span> showCalendar = <span class="hljs-title function_">ref</span>(<span class="hljs-literal">false</span>);\n <span class="hljs-keyword">const</span> <span class="hljs-title function_">onConfirm</span> = (<span class="hljs-params">date</span>) => {\n result.<span class="hljs-property">value</span> = <span class="hljs-string">`<span class="hljs-subst">${date.getMonth() + <span class="hljs-number">1</span>}</span>/<span class="hljs-subst">${date.getDate()}</span>`</span>;\n showCalendar.<span class="hljs-property">value</span> = <span class="hljs-literal">false</span>;\n };\n\n <span class="hljs-keyword">return</span> {\n result,\n onConfirm,\n showCalendar,\n };\n },\n};\n</code></pre>\n</div><h2 id="api" tabindex="-1">API</h2>\n<div class="van-doc-card"><h3 id="props" tabindex="-1">Props</h3>\n<table>\n<thead>\n<tr>\n<th>Attribute</th>\n<th>Description</th>\n<th>Type</th>\n<th>Default</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>label-width</td>\n<td>Field label width</td>\n<td><em>number | string</em></td>\n<td><code>6.2em</code></td>\n</tr>\n<tr>\n<td>label-align</td>\n<td>Field label align, can be set to <code>center</code> <code>right</code> <code>top</code></td>\n<td><em>string</em></td>\n<td><code>left</code></td>\n</tr>\n<tr>\n<td>input-align</td>\n<td>Field input align, can be set to <code>center</code> <code>right</code></td>\n<td><em>string</em></td>\n<td><code>left</code></td>\n</tr>\n<tr>\n<td>error-message-align</td>\n<td>Error message align, can be set to <code>center</code> <code>right</code></td>\n<td><em>string</em></td>\n<td><code>left</code></td>\n</tr>\n<tr>\n<td>validate-trigger</td>\n<td>When to validate the form, can be set to <code>onChange</code>\u3001<code>onSubmit</code>, supports using array to set multiple values</td>\n<td><em>string | string[]</em></td>\n<td><code>onBlur</code></td>\n</tr>\n<tr>\n<td>colon</td>\n<td>Whether to display colon after label</td>\n<td><em>boolean</em></td>\n<td><code>false</code></td>\n</tr>\n<tr>\n<td>disabled</td>\n<td>Whether to disable form</td>\n<td><em>boolean</em></td>\n<td><code>false</code></td>\n</tr>\n<tr>\n<td>readonly</td>\n<td>Whether to be readonly</td>\n<td><em>boolean</em></td>\n<td><code>false</code></td>\n</tr>\n<tr>\n<td>required <code>v4.7.3</code></td>\n<td>Whether to show required mark</td>\n<td><em>boolean | \'auto\'</em></td>\n<td><code>null</code></td>\n</tr>\n<tr>\n<td>validate-first</td>\n<td>Whether to stop the validation when a rule fails</td>\n<td><em>boolean</em></td>\n<td><code>false</code></td>\n</tr>\n<tr>\n<td>scroll-to-error</td>\n<td>Whether to scroll to the error field when validation failed</td>\n<td><em>boolean</em></td>\n<td><code>false</code></td>\n</tr>\n<tr>\n<td>show-error</td>\n<td>Whether to highlight input when validation failed</td>\n<td><em>boolean</em></td>\n<td><code>false</code></td>\n</tr>\n<tr>\n<td>show-error-message</td>\n<td>Whether to show error message when validation failed</td>\n<td><em>boolean</em></td>\n<td><code>true</code></td>\n</tr>\n<tr>\n<td>submit-on-enter</td>\n<td>Whether to submit form on enter</td>\n<td><em>boolean</em></td>\n<td><code>true</code></td>\n</tr>\n</tbody>\n</table>\n</div><div class="van-doc-card"><h3 id="data-structure-of-rule" tabindex="-1">Data Structure of Rule</h3>\n<table>\n<thead>\n<tr>\n<th>Key</th>\n<th>Description</th>\n<th>Type</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>required</td>\n<td>Whether to be a required field, the value is not allowed to be empty (empty string, empty array, <code>false</code>, <code>undefined</code>, <code>null</code>)</td>\n<td><em>boolean</em></td>\n</tr>\n<tr>\n<td>message</td>\n<td>Error message, can be a function to dynamically return message content</td>\n<td><em>string | (value, rule) => string</em></td>\n</tr>\n<tr>\n<td>validator</td>\n<td>Custom validator, can return a Promise to validate dynamically</td>\n<td><em>(value, rule) => boolean | string | Promise</em></td>\n</tr>\n<tr>\n<td>pattern</td>\n<td>Regexp pattern, if the regexp cannot match, means that the validation fails</td>\n<td><em>RegExp</em></td>\n</tr>\n<tr>\n<td>trigger</td>\n<td>When to validate the form, priority is higher than the <code>validate-trigger</code> of the Form component, can be set to <code>onChange</code>, <code>onBlur</code>, <code>onSubmit</code></td>\n<td><em>string | string[]</em></td>\n</tr>\n<tr>\n<td>formatter</td>\n<td>Format value before validate</td>\n<td><em>(value, rule) => any</em></td>\n</tr>\n<tr>\n<td>validateEmpty</td>\n<td>Controls whether the <code>validator</code> and <code>pattern</code> options to verify empty values, the default value is <code>true</code>, you can set to <code>false</code> to disable this behavior</td>\n<td><em>boolean</em></td>\n</tr>\n</tbody>\n</table>\n</div><div class="van-doc-card"><h3 id="validate-trigger" tabindex="-1">validate-trigger</h3>\n<table>\n<thead>\n<tr>\n<th>Value</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>onSubmit</td>\n<td>Trigger validation after submitting form</td>\n</tr>\n<tr>\n<td>onBlur</td>\n<td>Trigger validation after submitting form or blurring input</td>\n</tr>\n<tr>\n<td>onChange</td>\n<td>Trigger validation after submitting form or changing input value</td>\n</tr>\n</tbody>\n</table>\n</div><div class="van-doc-card"><h3 id="events" tabindex="-1">Events</h3>\n<table>\n<thead>\n<tr>\n<th>Event</th>\n<th>Description</th>\n<th>Arguments</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>submit</td>\n<td>Emitted after submitting the form and validation passed</td>\n<td><em>values: object</em></td>\n</tr>\n<tr>\n<td>failed</td>\n<td>Emitted after submitting the form and validation failed</td>\n<td><em>errorInfo: { values: object, errors: object[] }</em></td>\n</tr>\n</tbody>\n</table>\n</div><div class="van-doc-card"><h3 id="methods" tabindex="-1">Methods</h3>\n<p>Use <a href="https://vuejs.org/guide/essentials/template-refs.html" target="_blank">ref</a> to get Form instance and call instance methods.</p>\n<table>\n<thead>\n<tr>\n<th>Name</th>\n<th>Description</th>\n<th>Attribute</th>\n<th>Return value</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>submit</td>\n<td>Submit form</td>\n<td>-</td>\n<td>-</td>\n</tr>\n<tr>\n<td>getValues</td>\n<td>Get current form values</td>\n<td>-</td>\n<td><em>Record<string, unknown></em></td>\n</tr>\n<tr>\n<td>validate</td>\n<td>Validate form</td>\n<td><em>name?: string | string[]</em></td>\n<td><em>Promise<void></em></td>\n</tr>\n<tr>\n<td>resetValidation</td>\n<td>Reset validation</td>\n<td><em>name?: string | string[]</em></td>\n<td>-</td>\n</tr>\n<tr>\n<td>getValidationStatus</td>\n<td>Get validation status of all fields\uFF0Cstatus can be <code>passed</code>\u3001<code>failed</code>\u3001<code>unvalidated</code></td>\n<td>-</td>\n<td><em>Record<string, FieldValidationStatus></em></td>\n</tr>\n<tr>\n<td>scrollToField</td>\n<td>Scroll to field</td>\n<td><em>name: string, alignToTop: boolean</em></td>\n<td>-</td>\n</tr>\n</tbody>\n</table>\n</div><div class="van-doc-card"><h3 id="types" tabindex="-1">Types</h3>\n<p>The component exports the following type definitions:</p>\n<pre><code class="language-ts"><span class="hljs-keyword">import</span> <span class="hljs-keyword">type</span> { <span class="hljs-title class_">FormProps</span>, <span class="hljs-title class_">FormInstance</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'vant'</span>;\n</code></pre>\n<p><code>FormInstance</code> is the type of component instance:</p>\n<pre><code class="language-ts"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;\n<span class="hljs-keyword">import</span> <span class="hljs-keyword">type</span> { <span class="hljs-title class_">FormInstance</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'vant'</span>;\n\n<span class="hljs-keyword">const</span> formRef = ref<<span class="hljs-title class_">FormInstance</span>>();\n\nformRef.<span class="hljs-property">value</span>?.<span class="hljs-title function_">submit</span>();\n</code></pre>\n</div><div class="van-doc-card"><h3 id="slots" tabindex="-1">Slots</h3>\n<table>\n<thead>\n<tr>\n<th>Name</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>default</td>\n<td>Form content</td>\n</tr>\n</tbody>\n</table>\n</div>'},null,8,t))}}}]); |