mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
379 lines
42 KiB
JavaScript
379 lines
42 KiB
JavaScript
import{o as a,a as n,y as l}from"./vue-libs.b44bc779.js";const t={class:"van-doc-markdown-body"},p=l(`<h1>Form</h1><div class="van-doc-card"><h3 id="intro" tabindex="-1">Intro</h3><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></div><div class="van-doc-card"><h3 id="install" tabindex="-1">Install</h3><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><pre><code class="language-js"><span class="hljs-keyword">import</span> { createApp } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;
|
|
<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>;
|
|
|
|
<span class="hljs-keyword">const</span> app = <span class="hljs-title function_">createApp</span>();
|
|
app.<span class="hljs-title function_">use</span>(<span class="hljs-title class_">Form</span>);
|
|
app.<span class="hljs-title function_">use</span>(<span class="hljs-title class_">Field</span>);
|
|
app.<span class="hljs-title function_">use</span>(<span class="hljs-title class_">CellGroup</span>);
|
|
</code></pre></div><h2 id="usage" tabindex="-1">Usage</h2><div class="van-doc-card"><h3 id="basic-usage" tabindex="-1">Basic Usage</h3><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>
|
|
<span class="hljs-tag"><<span class="hljs-name">van-cell-group</span> <span class="hljs-attr">inset</span>></span>
|
|
<span class="hljs-tag"><<span class="hljs-name">van-field</span>
|
|
<span class="hljs-attr">v-model</span>=<span class="hljs-string">"username"</span>
|
|
<span class="hljs-attr">name</span>=<span class="hljs-string">"Username"</span>
|
|
<span class="hljs-attr">label</span>=<span class="hljs-string">"Username"</span>
|
|
<span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Username"</span>
|
|
<span class="hljs-attr">:rules</span>=<span class="hljs-string">"[{ required: true, message: 'Username is required' }]"</span>
|
|
/></span>
|
|
<span class="hljs-tag"><<span class="hljs-name">van-field</span>
|
|
<span class="hljs-attr">v-model</span>=<span class="hljs-string">"password"</span>
|
|
<span class="hljs-attr">type</span>=<span class="hljs-string">"password"</span>
|
|
<span class="hljs-attr">name</span>=<span class="hljs-string">"Password"</span>
|
|
<span class="hljs-attr">label</span>=<span class="hljs-string">"Password"</span>
|
|
<span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Password"</span>
|
|
<span class="hljs-attr">:rules</span>=<span class="hljs-string">"[{ required: true, message: 'Password is required' }]"</span>
|
|
/></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-cell-group</span>></span>
|
|
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"margin: 16px;"</span>></span>
|
|
<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>
|
|
Submit
|
|
<span class="hljs-tag"></<span class="hljs-name">van-button</span>></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-form</span>></span>
|
|
</code></pre><pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;
|
|
|
|
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
|
|
<span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {
|
|
<span class="hljs-keyword">const</span> username = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);
|
|
<span class="hljs-keyword">const</span> password = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);
|
|
<span class="hljs-keyword">const</span> <span class="hljs-title function_">onSubmit</span> = (<span class="hljs-params">values</span>) => {
|
|
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'submit'</span>, values);
|
|
};
|
|
|
|
<span class="hljs-keyword">return</span> {
|
|
username,
|
|
password,
|
|
onSubmit,
|
|
};
|
|
},
|
|
};
|
|
</code></pre></div><div class="van-doc-card"><h3 id="validate-rules" tabindex="-1">Validate Rules</h3><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>
|
|
<span class="hljs-tag"><<span class="hljs-name">van-cell-group</span> <span class="hljs-attr">inset</span>></span>
|
|
<span class="hljs-tag"><<span class="hljs-name">van-field</span>
|
|
<span class="hljs-attr">v-model</span>=<span class="hljs-string">"value1"</span>
|
|
<span class="hljs-attr">name</span>=<span class="hljs-string">"pattern"</span>
|
|
<span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Use pattern"</span>
|
|
<span class="hljs-attr">:rules</span>=<span class="hljs-string">"[{ pattern, message: 'Error message' }]"</span>
|
|
/></span>
|
|
<span class="hljs-tag"><<span class="hljs-name">van-field</span>
|
|
<span class="hljs-attr">v-model</span>=<span class="hljs-string">"value2"</span>
|
|
<span class="hljs-attr">name</span>=<span class="hljs-string">"validator"</span>
|
|
<span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Use validator"</span>
|
|
<span class="hljs-attr">:rules</span>=<span class="hljs-string">"[{ validator, message: 'Error message' }]"</span>
|
|
/></span>
|
|
<span class="hljs-tag"><<span class="hljs-name">van-field</span>
|
|
<span class="hljs-attr">v-model</span>=<span class="hljs-string">"value3"</span>
|
|
<span class="hljs-attr">name</span>=<span class="hljs-string">"validatorMessage"</span>
|
|
<span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Use validator to return message"</span>
|
|
<span class="hljs-attr">:rules</span>=<span class="hljs-string">"[{ validator: validatorMessage }]"</span>
|
|
/></span>
|
|
<span class="hljs-tag"><<span class="hljs-name">van-field</span>
|
|
<span class="hljs-attr">v-model</span>=<span class="hljs-string">"value4"</span>
|
|
<span class="hljs-attr">name</span>=<span class="hljs-string">"asyncValidator"</span>
|
|
<span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Use async validator"</span>
|
|
<span class="hljs-attr">:rules</span>=<span class="hljs-string">"[{ validator: asyncValidator, message: 'Error message' }]"</span>
|
|
/></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-cell-group</span>></span>
|
|
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"margin: 16px;"</span>></span>
|
|
<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>
|
|
Submit
|
|
<span class="hljs-tag"></<span class="hljs-name">van-button</span>></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-form</span>></span>
|
|
</code></pre><pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;
|
|
<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Toast</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'vant'</span>;
|
|
|
|
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
|
|
<span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {
|
|
<span class="hljs-keyword">const</span> value1 = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);
|
|
<span class="hljs-keyword">const</span> value2 = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);
|
|
<span class="hljs-keyword">const</span> value3 = <span class="hljs-title function_">ref</span>(<span class="hljs-string">'abc'</span>);
|
|
<span class="hljs-keyword">const</span> value4 = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);
|
|
<span class="hljs-keyword">const</span> pattern = <span class="hljs-regexp">/\\d{6}/</span>;
|
|
|
|
<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);
|
|
|
|
<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>;
|
|
|
|
<span class="hljs-keyword">const</span> <span class="hljs-title function_">asyncValidator</span> = (<span class="hljs-params">val</span>) =>
|
|
<span class="hljs-keyword">new</span> <span class="hljs-title class_">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve</span>) =></span> {
|
|
<span class="hljs-title class_">Toast</span>.<span class="hljs-title function_">loading</span>(<span class="hljs-string">'Validating...'</span>);
|
|
|
|
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
|
|
<span class="hljs-title class_">Toast</span>.<span class="hljs-title function_">clear</span>();
|
|
<span class="hljs-title function_">resolve</span>(val === <span class="hljs-string">'1234'</span>);
|
|
}, <span class="hljs-number">1000</span>);
|
|
});
|
|
|
|
<span class="hljs-keyword">const</span> <span class="hljs-title function_">onFailed</span> = (<span class="hljs-params">errorInfo</span>) => {
|
|
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'failed'</span>, errorInfo);
|
|
};
|
|
|
|
<span class="hljs-keyword">return</span> {
|
|
value1,
|
|
value2,
|
|
value3,
|
|
value4,
|
|
pattern,
|
|
onFailed,
|
|
validator,
|
|
asyncValidator,
|
|
validatorMessage,
|
|
};
|
|
},
|
|
};
|
|
</code></pre></div><div class="van-doc-card"><h3 id="field-type---switch" tabindex="-1">Field Type - Switch</h3><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>
|
|
<span class="hljs-tag"><<span class="hljs-name">template</span> #<span class="hljs-attr">input</span>></span>
|
|
<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 class="hljs-attr">size</span>=<span class="hljs-string">"20"</span> /></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">template</span>></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-field</span>></span>
|
|
</code></pre><pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;
|
|
|
|
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
|
|
<span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {
|
|
<span class="hljs-keyword">const</span> checked = <span class="hljs-title function_">ref</span>(<span class="hljs-literal">false</span>);
|
|
<span class="hljs-keyword">return</span> { checked };
|
|
},
|
|
};
|
|
</code></pre></div><div class="van-doc-card"><h3 id="field-type---checkbox" tabindex="-1">Field Type - Checkbox</h3><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>
|
|
<span class="hljs-tag"><<span class="hljs-name">template</span> #<span class="hljs-attr">input</span>></span>
|
|
<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>
|
|
<span class="hljs-tag"></<span class="hljs-name">template</span>></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-field</span>></span>
|
|
<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>
|
|
<span class="hljs-tag"><<span class="hljs-name">template</span> #<span class="hljs-attr">input</span>></span>
|
|
<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>
|
|
<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>
|
|
<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>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-checkbox-group</span>></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">template</span>></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-field</span>></span>
|
|
</code></pre><pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;
|
|
|
|
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
|
|
<span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {
|
|
<span class="hljs-keyword">const</span> checked = <span class="hljs-title function_">ref</span>(<span class="hljs-literal">false</span>);
|
|
<span class="hljs-keyword">const</span> groupChecked = <span class="hljs-title function_">ref</span>([]);
|
|
<span class="hljs-keyword">return</span> {
|
|
checked,
|
|
groupChecked,
|
|
};
|
|
},
|
|
};
|
|
</code></pre></div><div class="van-doc-card"><h3 id="field-type---radio" tabindex="-1">Field Type - Radio</h3><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>
|
|
<span class="hljs-tag"><<span class="hljs-name">template</span> #<span class="hljs-attr">input</span>></span>
|
|
<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>
|
|
<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>
|
|
<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>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-radio-group</span>></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">template</span>></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-field</span>></span>
|
|
</code></pre><pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;
|
|
|
|
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
|
|
<span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {
|
|
<span class="hljs-keyword">const</span> checked = <span class="hljs-title function_">ref</span>(<span class="hljs-string">'1'</span>);
|
|
<span class="hljs-keyword">return</span> { checked };
|
|
},
|
|
};
|
|
</code></pre></div><div class="van-doc-card"><h3 id="field-type---stepper" tabindex="-1">Field Type - Stepper</h3><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>
|
|
<span class="hljs-tag"><<span class="hljs-name">template</span> #<span class="hljs-attr">input</span>></span>
|
|
<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>
|
|
<span class="hljs-tag"></<span class="hljs-name">template</span>></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-field</span>></span>
|
|
</code></pre><pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;
|
|
|
|
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
|
|
<span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {
|
|
<span class="hljs-keyword">const</span> value = <span class="hljs-title function_">ref</span>(<span class="hljs-number">1</span>);
|
|
<span class="hljs-keyword">return</span> { value };
|
|
},
|
|
};
|
|
</code></pre></div><div class="van-doc-card"><h3 id="field-type---rate" tabindex="-1">Field Type - Rate</h3><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>
|
|
<span class="hljs-tag"><<span class="hljs-name">template</span> #<span class="hljs-attr">input</span>></span>
|
|
<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>
|
|
<span class="hljs-tag"></<span class="hljs-name">template</span>></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-field</span>></span>
|
|
</code></pre><pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;
|
|
|
|
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
|
|
<span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {
|
|
<span class="hljs-keyword">const</span> value = <span class="hljs-title function_">ref</span>(<span class="hljs-number">3</span>);
|
|
<span class="hljs-keyword">return</span> { value };
|
|
},
|
|
};
|
|
</code></pre></div><div class="van-doc-card"><h3 id="field-type---slider" tabindex="-1">Field Type - Slider</h3><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>
|
|
<span class="hljs-tag"><<span class="hljs-name">template</span> #<span class="hljs-attr">input</span>></span>
|
|
<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>
|
|
<span class="hljs-tag"></<span class="hljs-name">template</span>></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-field</span>></span>
|
|
</code></pre><pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;
|
|
|
|
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
|
|
<span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {
|
|
<span class="hljs-keyword">const</span> value = <span class="hljs-title function_">ref</span>(<span class="hljs-number">50</span>);
|
|
<span class="hljs-keyword">return</span> { value };
|
|
},
|
|
};
|
|
</code></pre></div><div class="van-doc-card"><h3 id="field-type---uploader" tabindex="-1">Field Type - Uploader</h3><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>
|
|
<span class="hljs-tag"><<span class="hljs-name">template</span> #<span class="hljs-attr">input</span>></span>
|
|
<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>
|
|
<span class="hljs-tag"></<span class="hljs-name">template</span>></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-field</span>></span>
|
|
</code></pre><pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;
|
|
|
|
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
|
|
<span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {
|
|
<span class="hljs-keyword">const</span> value = <span class="hljs-title function_">ref</span>([
|
|
{ <span class="hljs-attr">url</span>: <span class="hljs-string">'https://fastly.jsdelivr.net/npm/@vant/assets/leaf.jpeg'</span> },
|
|
]);
|
|
<span class="hljs-keyword">return</span> { value };
|
|
},
|
|
};
|
|
</code></pre></div><div class="van-doc-card"><h3 id="field-type---picker" tabindex="-1">Field Type - Picker</h3><pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-field</span>
|
|
<span class="hljs-attr">v-model</span>=<span class="hljs-string">"result"</span>
|
|
<span class="hljs-attr">is-link</span>
|
|
<span class="hljs-attr">readonly</span>
|
|
<span class="hljs-attr">name</span>=<span class="hljs-string">"picker"</span>
|
|
<span class="hljs-attr">label</span>=<span class="hljs-string">"Picker"</span>
|
|
<span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Select city"</span>
|
|
@<span class="hljs-attr">click</span>=<span class="hljs-string">"showPicker = true"</span>
|
|
/></span>
|
|
<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>
|
|
<span class="hljs-tag"><<span class="hljs-name">van-picker</span>
|
|
<span class="hljs-attr">:columns</span>=<span class="hljs-string">"columns"</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>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-popup</span>></span>
|
|
</code></pre><pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;
|
|
|
|
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
|
|
<span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {
|
|
<span class="hljs-keyword">const</span> result = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);
|
|
<span class="hljs-keyword">const</span> showPicker = <span class="hljs-title function_">ref</span>(<span class="hljs-literal">false</span>);
|
|
<span class="hljs-keyword">const</span> columns = [<span class="hljs-string">'Delaware'</span>, <span class="hljs-string">'Florida'</span>, <span class="hljs-string">'Georqia'</span>, <span class="hljs-string">'Indiana'</span>, <span class="hljs-string">'Maine'</span>];
|
|
|
|
<span class="hljs-keyword">const</span> <span class="hljs-title function_">onConfirm</span> = (<span class="hljs-params">value</span>) => {
|
|
result.<span class="hljs-property">value</span> = value;
|
|
showPicker.<span class="hljs-property">value</span> = <span class="hljs-literal">false</span>;
|
|
};
|
|
|
|
<span class="hljs-keyword">return</span> {
|
|
result,
|
|
columns,
|
|
onConfirm,
|
|
showPicker,
|
|
};
|
|
},
|
|
};
|
|
</code></pre></div><div class="van-doc-card"><h3 id="field-type---datetimepicker" tabindex="-1">Field Type - DatetimePicker</h3><pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-field</span>
|
|
<span class="hljs-attr">v-model</span>=<span class="hljs-string">"result"</span>
|
|
<span class="hljs-attr">is-link</span>
|
|
<span class="hljs-attr">readonly</span>
|
|
<span class="hljs-attr">name</span>=<span class="hljs-string">"datetimePicker"</span>
|
|
<span class="hljs-attr">label</span>=<span class="hljs-string">"Datetime Picker"</span>
|
|
<span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Select time"</span>
|
|
@<span class="hljs-attr">click</span>=<span class="hljs-string">"showPicker = true"</span>
|
|
/></span>
|
|
<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>
|
|
<span class="hljs-tag"><<span class="hljs-name">van-datetime-picker</span>
|
|
<span class="hljs-attr">type</span>=<span class="hljs-string">"time"</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>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-popup</span>></span>
|
|
</code></pre><pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;
|
|
|
|
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
|
|
<span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {
|
|
<span class="hljs-keyword">const</span> result = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);
|
|
<span class="hljs-keyword">const</span> showPicker = <span class="hljs-title function_">ref</span>(<span class="hljs-literal">false</span>);
|
|
|
|
<span class="hljs-keyword">const</span> <span class="hljs-title function_">onConfirm</span> = (<span class="hljs-params">value</span>) => {
|
|
result.<span class="hljs-property">value</span> = value;
|
|
showPicker.<span class="hljs-property">value</span> = <span class="hljs-literal">false</span>;
|
|
};
|
|
|
|
<span class="hljs-keyword">return</span> {
|
|
result,
|
|
onConfirm,
|
|
showPicker,
|
|
};
|
|
},
|
|
};
|
|
</code></pre></div><div class="van-doc-card"><h3 id="field-type---area" tabindex="-1">Field Type - Area</h3><pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-field</span>
|
|
<span class="hljs-attr">v-model</span>=<span class="hljs-string">"result"</span>
|
|
<span class="hljs-attr">is-link</span>
|
|
<span class="hljs-attr">readonly</span>
|
|
<span class="hljs-attr">name</span>=<span class="hljs-string">"area"</span>
|
|
<span class="hljs-attr">label</span>=<span class="hljs-string">"Area Picker"</span>
|
|
<span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Select area"</span>
|
|
@<span class="hljs-attr">click</span>=<span class="hljs-string">"showArea = true"</span>
|
|
/></span>
|
|
<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>
|
|
<span class="hljs-tag"><<span class="hljs-name">van-area</span>
|
|
<span class="hljs-attr">:area-list</span>=<span class="hljs-string">"areaList"</span>
|
|
@<span class="hljs-attr">confirm</span>=<span class="hljs-string">"onConfirm"</span>
|
|
@<span class="hljs-attr">cancel</span>=<span class="hljs-string">"showArea = false"</span>
|
|
/></span>
|
|
<span class="hljs-tag"></<span class="hljs-name">van-popup</span>></span>
|
|
</code></pre><pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;
|
|
<span class="hljs-keyword">import</span> { areaList } <span class="hljs-keyword">from</span> <span class="hljs-string">'@vant/area-data'</span>;
|
|
|
|
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
|
|
<span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {
|
|
<span class="hljs-keyword">const</span> result = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);
|
|
<span class="hljs-keyword">const</span> showArea = <span class="hljs-title function_">ref</span>(<span class="hljs-literal">false</span>);
|
|
<span class="hljs-keyword">const</span> <span class="hljs-title function_">onConfirm</span> = (<span class="hljs-params">areaValues</span>) => {
|
|
showArea.<span class="hljs-property">value</span> = <span class="hljs-literal">false</span>;
|
|
result.<span class="hljs-property">value</span> = areaValues
|
|
.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">item</span>) =></span> !!item)
|
|
.<span class="hljs-title function_">map</span>(<span class="hljs-function">(<span class="hljs-params">item</span>) =></span> item.<span class="hljs-property">name</span>)
|
|
.<span class="hljs-title function_">join</span>(<span class="hljs-string">'/'</span>);
|
|
};
|
|
|
|
<span class="hljs-keyword">return</span> {
|
|
result,
|
|
areaList,
|
|
showArea,
|
|
onConfirm,
|
|
};
|
|
},
|
|
};
|
|
</code></pre></div><div class="van-doc-card"><h3 id="field-type---calendar" tabindex="-1">Field Type - Calendar</h3><pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">van-field</span>
|
|
<span class="hljs-attr">v-model</span>=<span class="hljs-string">"result"</span>
|
|
<span class="hljs-attr">is-link</span>
|
|
<span class="hljs-attr">readonly</span>
|
|
<span class="hljs-attr">name</span>=<span class="hljs-string">"calendar"</span>
|
|
<span class="hljs-attr">label</span>=<span class="hljs-string">"Calendar"</span>
|
|
<span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Select date"</span>
|
|
@<span class="hljs-attr">click</span>=<span class="hljs-string">"showCalendar = true"</span>
|
|
/></span>
|
|
<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>
|
|
</code></pre><pre><code class="language-js"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;
|
|
|
|
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
|
|
<span class="hljs-title function_">setup</span>(<span class="hljs-params"></span>) {
|
|
<span class="hljs-keyword">const</span> result = <span class="hljs-title function_">ref</span>(<span class="hljs-string">''</span>);
|
|
<span class="hljs-keyword">const</span> showCalendar = <span class="hljs-title function_">ref</span>(<span class="hljs-literal">false</span>);
|
|
<span class="hljs-keyword">const</span> <span class="hljs-title function_">onConfirm</span> = (<span class="hljs-params">date</span>) => {
|
|
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>;
|
|
showCalendar.<span class="hljs-property">value</span> = <span class="hljs-literal">false</span>;
|
|
};
|
|
|
|
<span class="hljs-keyword">return</span> {
|
|
result,
|
|
onConfirm,
|
|
showCalendar,
|
|
};
|
|
},
|
|
};
|
|
</code></pre></div><h2 id="api" tabindex="-1">API</h2><div class="van-doc-card"><h3 id="props" tabindex="-1">Props</h3><table><thead><tr><th>Attribute</th><th>Description</th><th>Type</th><th>Default</th></tr></thead><tbody><tr><td>label-width</td><td>Field label width</td><td><em>number | string</em></td><td><code>6.2em</code></td></tr><tr><td>label-align</td><td>Field label align, can be set to <code>center</code> <code>right</code></td><td><em>string</em></td><td><code>left</code></td></tr><tr><td>input-align</td><td>Field input align, can be set to <code>center</code> <code>right</code></td><td><em>string</em></td><td><code>left</code></td></tr><tr><td>error-message-align</td><td>Error message align, can be set to <code>center</code> <code>right</code></td><td><em>string</em></td><td><code>left</code></td></tr><tr><td>validate-trigger</td><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><td><em>string | string[]</em></td><td><code>onBlur</code></td></tr><tr><td>colon</td><td>Whether to display colon after label</td><td><em>boolean</em></td><td><code>false</code></td></tr><tr><td>disabled</td><td>Whether to disable form</td><td><em>boolean</em></td><td><code>false</code></td></tr><tr><td>readonly</td><td>Whether to be readonly</td><td><em>boolean</em></td><td><code>false</code></td></tr><tr><td>validate-first</td><td>Whether to stop the validation when a rule fails</td><td><em>boolean</em></td><td><code>false</code></td></tr><tr><td>scroll-to-error</td><td>Whether to scroll to the error field when validation failed</td><td><em>boolean</em></td><td><code>false</code></td></tr><tr><td>show-error</td><td>Whether to highlight input when validation failed</td><td><em>boolean</em></td><td><code>false</code></td></tr><tr><td>show-error-message</td><td>Whether to show error message when validation failed</td><td><em>boolean</em></td><td><code>true</code></td></tr><tr><td>submit-on-enter</td><td>Whether to submit form on enter</td><td><em>boolean</em></td><td><code>true</code></td></tr></tbody></table></div><div class="van-doc-card"><h3 id="data-structure-of-rule" tabindex="-1">Data Structure of Rule</h3><table><thead><tr><th>Key</th><th>Description</th><th>Type</th></tr></thead><tbody><tr><td>required</td><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><td><em>boolean</em></td></tr><tr><td>message</td><td>Error message, can be a function to dynamically return message content</td><td><em>string | (value, rule) => string</em></td></tr><tr><td>validator</td><td>Custom validator, can return a Promise to validate dynamically</td><td><em>(value, rule) => boolean | string | Promise</em></td></tr><tr><td>pattern</td><td>Regexp pattern, if the regexp cannot match, means that the validation fails</td><td><em>RegExp</em></td></tr><tr><td>trigger</td><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><td><em>string | string[]</em></td></tr><tr><td>formatter</td><td>Format value before validate</td><td><em>(value, rule) => any</em></td></tr><tr><td>validateEmpty <code>v3.6.0</code></td><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><td><em>boolean</em></td></tr></tbody></table></div><div class="van-doc-card"><h3 id="validate-trigger" tabindex="-1">validate-trigger</h3><table><thead><tr><th>Value</th><th>Description</th></tr></thead><tbody><tr><td>onSubmit</td><td>Trigger validation after submitting form</td></tr><tr><td>onBlur</td><td>Trigger validation after submitting form or blurring input</td></tr><tr><td>onChange</td><td>Trigger validation after submitting form or changing input value</td></tr></tbody></table></div><div class="van-doc-card"><h3 id="events" tabindex="-1">Events</h3><table><thead><tr><th>Event</th><th>Description</th><th>Arguments</th></tr></thead><tbody><tr><td>submit</td><td>Emitted after submitting the form and validation passed</td><td><em>values: object</em></td></tr><tr><td>failed</td><td>Emitted after submitting the form and validation failed</td><td><em>errorInfo: { values: object, errors: object[] }</em></td></tr></tbody></table></div><div class="van-doc-card"><h3 id="methods" tabindex="-1">Methods</h3><p>Use <a href="https://v3.vuejs.org/guide/component-template-refs.html" target="_blank">ref</a> to get Form instance and call instance methods.</p><table><thead><tr><th>Name</th><th>Description</th><th>Attribute</th><th>Return value</th></tr></thead><tbody><tr><td>submit</td><td>Submit form</td><td>-</td><td>-</td></tr><tr><td>getValues <code>v3.4.8</code></td><td>Get current form values</td><td>-</td><td><em>Record<string, unknown></em></td></tr><tr><td>validate</td><td>Validate form</td><td><em>name?: string | string[]</em></td><td><em>Promise<void></em></td></tr><tr><td>resetValidation</td><td>Reset validation</td><td><em>name?: string | string[]</em></td><td>-</td></tr><tr><td>getValidationStatus <code>v3.5.0</code></td><td>Get validation status of all fields\uFF0Cstatus can be <code>passed</code>\u3001<code>failed</code>\u3001<code>unvalidated</code></td><td>-</td><td><em>Record<string, FieldValidationStatus></em></td></tr><tr><td>scrollToField</td><td>Scroll to field</td><td><em>name: string, alignToTop: boolean</em></td><td>-</td></tr></tbody></table></div><div class="van-doc-card"><h3 id="types" tabindex="-1">Types</h3><p>The component exports the following type definitions:</p><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>;
|
|
</code></pre><p><code>FormInstance</code> is the type of component instance:</p><pre><code class="language-ts"><span class="hljs-keyword">import</span> { ref } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;
|
|
<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>;
|
|
|
|
<span class="hljs-keyword">const</span> formRef = ref<<span class="hljs-title class_">FormInstance</span>>();
|
|
|
|
formRef.<span class="hljs-property">value</span>?.<span class="hljs-title function_">submit</span>();
|
|
</code></pre></div><div class="van-doc-card"><h3 id="slots" tabindex="-1">Slots</h3><table><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td>default</td><td>Form content</td></tr></tbody></table></div>`,25),e=[p],i={__name:"README",setup(c,{expose:s}){return s({frontmatter:{}}),(o,h)=>(a(),n("div",t,e))}};export{i as default};
|