docs(Picker): add v-model usage (#10417)

This commit is contained in:
neverland 2022-03-19 20:02:56 +08:00 committed by GitHub
parent dd724fde4d
commit 47a5151a20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 165 additions and 7 deletions

View File

@ -38,7 +38,7 @@ export default {
const columns = [ const columns = [
{ text: 'Delaware', value: 'Delaware' }, { text: 'Delaware', value: 'Delaware' },
{ text: 'Florida', value: 'Florida' }, { text: 'Florida', value: 'Florida' },
{ text: 'Georqia', value: 'Georqia' }, { text: 'Wenzhou', value: 'Wenzhou' },
{ text: 'Indiana', value: 'Indiana' }, { text: 'Indiana', value: 'Indiana' },
{ text: 'Maine', value: 'Maine' }, { text: 'Maine', value: 'Maine' },
]; ];
@ -89,7 +89,7 @@ export default {
const columns = [ const columns = [
{ text: 'Delaware', value: 'Delaware' }, { text: 'Delaware', value: 'Delaware' },
{ text: 'Florida', value: 'Florida' }, { text: 'Florida', value: 'Florida' },
{ text: 'Georqia', value: 'Georqia' }, { text: 'Wenzhou', value: 'Wenzhou' },
{ text: 'Indiana', value: 'Indiana' }, { text: 'Indiana', value: 'Indiana' },
{ text: 'Maine', value: 'Maine' }, { text: 'Maine', value: 'Maine' },
]; ];
@ -111,6 +111,36 @@ export default {
}; };
``` ```
### v-model
Using `v-model` to bind selected values.
```html
<van-picker v-model="selectedValues" title="Title" :columns="columns" />
```
```js
import { Toast } from 'vant';
export default {
setup() {
const columns = [
{ text: 'Delaware', value: 'Delaware' },
{ text: 'Florida', value: 'Florida' },
{ text: 'Wenzhou', value: 'Wenzhou' },
{ text: 'Indiana', value: 'Indiana' },
{ text: 'Maine', value: 'Maine' },
];
const selectedValues = ref(['Wenzhou']);
return {
columns,
selectedValues,
};
},
};
```
### Multiple Columns ### Multiple Columns
```html ```html
@ -213,7 +243,7 @@ export default {
const columns = [ const columns = [
{ text: 'Delaware', value: 'Delaware', disabled: true }, { text: 'Delaware', value: 'Delaware', disabled: true },
{ text: 'Florida', value: 'Florida' }, { text: 'Florida', value: 'Florida' },
{ text: 'Georqia', value: 'Georqia' }, { text: 'Wenzhou', value: 'Wenzhou' },
]; ];
return { columns }; return { columns };
}, },

View File

@ -120,6 +120,38 @@ export default {
}; };
``` ```
### 双向绑定
通过 `v-model` 可以绑定当前选中项的 `values`,修改 `v-model` 绑定的值时Picker 的选中状态也会随之改变。
`v-model` 的值是一个数组,数组的第一位对应第一列选中项的 `value`,第二位对应第二列选中项的 `value`,以此类推。
```html
<van-picker v-model="selectedValues" title="标题" :columns="columns" />
```
```js
import { Toast } from 'vant';
export default {
setup() {
const columns = [
{ text: '杭州', value: 'Hangzhou' },
{ text: '宁波', value: 'Ningbo' },
{ text: '温州', value: 'Wenzhou' },
{ text: '绍兴', value: 'Shaoxing' },
{ text: '湖州', value: 'Huzhou' },
];
const selectedValues = ref(['Wenzhou']);
return {
columns,
selectedValues,
};
},
};
```
### 多列选择 ### 多列选择
`columns` 属性可以通过二维数组的形式配置多列选择。 `columns` 属性可以通过二维数组的形式配置多列选择。

View File

@ -9,7 +9,7 @@ export const basicColumns = {
'en-US': [ 'en-US': [
{ text: 'Delaware', value: 'Delaware' }, { text: 'Delaware', value: 'Delaware' },
{ text: 'Florida', value: 'Florida' }, { text: 'Florida', value: 'Florida' },
{ text: 'Georqia', value: 'Georqia' }, { text: 'Wenzhou', value: 'Wenzhou' },
{ text: 'Indiana', value: 'Indiana' }, { text: 'Indiana', value: 'Indiana' },
{ text: 'Maine', value: 'Maine' }, { text: 'Maine', value: 'Maine' },
], ],
@ -209,6 +209,6 @@ export const disabledColumns = {
'en-US': [ 'en-US': [
{ text: 'Delaware', value: 'Delaware', disabled: true }, { text: 'Delaware', value: 'Delaware', disabled: true },
{ text: 'Florida', value: 'Florida' }, { text: 'Florida', value: 'Florida' },
{ text: 'Georqia', value: 'Georqia' }, { text: 'Wenzhou', value: 'Wenzhou' },
], ],
}; };

View File

@ -1,4 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref } from 'vue';
import WithPopup from './WithPopup.vue'; import WithPopup from './WithPopup.vue';
import VanPicker, { import VanPicker, {
PickerChangeEventParams, PickerChangeEventParams,
@ -17,6 +18,7 @@ import { useTranslate } from '../../../docs/site/use-translate';
const t = useTranslate({ const t = useTranslate({
'zh-CN': { 'zh-CN': {
cascade: '级联选择', cascade: '级联选择',
modelValue: '双向绑定',
showToolbar: '展示顶部栏', showToolbar: '展示顶部栏',
dateColumns: dateColumns['zh-CN'], dateColumns: dateColumns['zh-CN'],
basicColumns: basicColumns['zh-CN'], basicColumns: basicColumns['zh-CN'],
@ -31,6 +33,7 @@ const t = useTranslate({
}, },
'en-US': { 'en-US': {
cascade: 'Cascade', cascade: 'Cascade',
modelValue: 'v-model',
showToolbar: 'Show Toolbar', showToolbar: 'Show Toolbar',
dateColumns: dateColumns['en-US'], dateColumns: dateColumns['en-US'],
basicColumns: basicColumns['en-US'], basicColumns: basicColumns['en-US'],
@ -52,6 +55,8 @@ const customFieldName = {
children: 'cities', children: 'cities',
}; };
const selectedValues = ref(['Wenzhou']);
const onChange1 = ({ selectedValues }: PickerChangeEventParams) => { const onChange1 = ({ selectedValues }: PickerChangeEventParams) => {
Toast(t('toastContent', selectedValues.join(','))); Toast(t('toastContent', selectedValues.join(',')));
}; };
@ -69,12 +74,21 @@ const onCancel = () => Toast(t('cancel'));
:title="t('title')" :title="t('title')"
:columns="t('basicColumns')" :columns="t('basicColumns')"
@change="onChange1" @change="onChange1"
@cancel="onCancel"
@confirm="onConfirm" @confirm="onConfirm"
/> />
</demo-block> </demo-block>
<WithPopup /> <WithPopup />
<demo-block card :title="t('modelValue')">
<van-picker
v-model="selectedValues"
:title="t('title')"
:columns="t('basicColumns')"
/>
</demo-block>
<demo-block card :title="t('multipleColumns')"> <demo-block card :title="t('multipleColumns')">
<van-picker <van-picker
:title="t('title')" :title="t('title')"

View File

@ -49,7 +49,7 @@ exports[`should render demo and match snapshot 1`] = `
class="van-picker-column__item" class="van-picker-column__item"
> >
<div class="van-ellipsis"> <div class="van-ellipsis">
Georqia Wenzhou
</div> </div>
</li> </li>
<li role="button" <li role="button"
@ -114,6 +114,88 @@ exports[`should render demo and match snapshot 1`] = `
<transition-stub> <transition-stub>
</transition-stub> </transition-stub>
</div> </div>
<div>
<div class="van-picker">
<div class="van-picker__toolbar">
<button type="button"
class="van-picker__cancel van-haptics-feedback"
>
Cancel
</button>
<div class="van-picker__title van-ellipsis">
Title
</div>
<button type="button"
class="van-picker__confirm van-haptics-feedback"
>
Confirm
</button>
</div>
<div class="van-picker__columns"
style="height: 264px;"
>
<div class="van-picker-column">
<ul style="transform: translate3d(0, 22px, 0); transition-duration: 0ms; transition-property: none;"
class="van-picker-column__wrapper"
>
<li role="button"
style="height: 44px;"
tabindex="0"
class="van-picker-column__item"
>
<div class="van-ellipsis">
Delaware
</div>
</li>
<li role="button"
style="height: 44px;"
tabindex="0"
class="van-picker-column__item"
>
<div class="van-ellipsis">
Florida
</div>
</li>
<li role="button"
style="height: 44px;"
tabindex="0"
class="van-picker-column__item van-picker-column__item--selected"
>
<div class="van-ellipsis">
Wenzhou
</div>
</li>
<li role="button"
style="height: 44px;"
tabindex="0"
class="van-picker-column__item"
>
<div class="van-ellipsis">
Indiana
</div>
</li>
<li role="button"
style="height: 44px;"
tabindex="0"
class="van-picker-column__item"
>
<div class="van-ellipsis">
Maine
</div>
</li>
</ul>
</div>
<div class="van-picker__mask"
style="background-size: 100% 110px;"
>
</div>
<div class="van-hairline-unset--top-bottom van-picker__frame"
style="height: 44px;"
>
</div>
</div>
</div>
</div>
<div> <div>
<div class="van-picker"> <div class="van-picker">
<div class="van-picker__toolbar"> <div class="van-picker__toolbar">
@ -380,7 +462,7 @@ exports[`should render demo and match snapshot 1`] = `
class="van-picker-column__item" class="van-picker-column__item"
> >
<div class="van-ellipsis"> <div class="van-ellipsis">
Georqia Wenzhou
</div> </div>
</li> </li>
</ul> </ul>