docs(ActionSheet): use composition api

This commit is contained in:
chenjiahan 2020-12-09 17:48:37 +08:00
parent e026d2d83f
commit 8a7882b0c8
3 changed files with 154 additions and 112 deletions

View File

@ -22,24 +22,27 @@ Use `actions` prop to set options of action-sheet.
``` ```
```js ```js
import { ref } from 'vue';
import { Toast } from 'vant'; import { Toast } from 'vant';
export default { export default {
data() { setup() {
return { const show = ref(false);
show: false, const actions = [
actions: [
{ name: 'Option 1' }, { name: 'Option 1' },
{ name: 'Option 2' }, { name: 'Option 2' },
{ name: 'Option 3' }, { name: 'Option 3' },
], ];
}; const onSelect = (item) => {
}, show.value = false;
methods: {
onSelect(item) {
this.show = false;
Toast(item.name); Toast(item.name);
}, };
return {
show,
actions,
onSelect,
};
}, },
}; };
``` ```
@ -57,23 +60,26 @@ export default {
``` ```
```js ```js
import { ref } from 'vue';
import { Toast } from 'vant'; import { Toast } from 'vant';
export default { export default {
data() { setup() {
return { const show = ref(false);
show: false, const actions = [
actions: [
{ name: 'Option 1' }, { name: 'Option 1' },
{ name: 'Option 2' }, { name: 'Option 2' },
{ name: 'Option 3' }, { name: 'Option 3' },
], ];
}; const onCancel = () => {
},
methods: {
onCancel() {
Toast('cancel'); Toast('cancel');
}, };
return {
show,
actions,
onCancel,
};
}, },
}; };
``` ```
@ -91,15 +97,20 @@ export default {
``` ```
```js ```js
import { ref } from 'vue';
export default { export default {
data() { setup() {
return { const show = ref(false);
show: false, const actions = [
actions: [
{ name: 'Option 1' }, { name: 'Option 1' },
{ name: 'Option 2' }, { name: 'Option 2' },
{ name: 'Option 3', subname: 'Description' }, { name: 'Option 3', subname: 'Description' },
], ];
return {
show,
actions,
}; };
}, },
}; };
@ -117,15 +128,20 @@ export default {
``` ```
```js ```js
import { ref } from 'vue';
export default { export default {
data() { setup() {
return { const show = ref(false);
show: false, const actions = [
actions: [
{ name: 'Colored Option', color: '#ee0a24' }, { name: 'Colored Option', color: '#ee0a24' },
{ name: 'Disabled Option', disabled: true }, { name: 'Disabled Option', disabled: true },
{ name: 'Loading Option', loading: true }, { name: 'Loading Option', loading: true },
], ];
return {
show,
actions,
}; };
}, },
}; };

View File

@ -26,22 +26,29 @@ app.use(ActionSheet);
``` ```
```js ```js
import { ref } from 'vue';
import { Toast } from 'vant'; import { Toast } from 'vant';
export default { export default {
data() { setup() {
return { const show = ref(false);
show: false, const actions = [
actions: [{ name: '选项一' }, { name: '选项二' }, { name: '选项三' }], { name: '选项一' },
}; { name: '选项二' },
}, { name: '选项三' },
methods: { ];
onSelect(item) { const onSelect = (item) => {
// 默认情况下点击选项时不会自动收起 // 默认情况下点击选项时不会自动收起
// 可以通过 close-on-click-action 属性开启自动收起 // 可以通过 close-on-click-action 属性开启自动收起
this.show = false; show.value = false;
Toast(item.name); Toast(item.name);
}, };
return {
show,
actions,
onSelect,
};
}, },
}; };
``` ```
@ -61,19 +68,26 @@ export default {
``` ```
```js ```js
import { ref } from 'vue';
import { Toast } from 'vant'; import { Toast } from 'vant';
export default { export default {
data() { setup() {
return { const show = ref(false);
show: false, const actions = [
actions: [{ name: '选项一' }, { name: '选项二' }, { name: '选项三' }], { name: '选项一' },
}; { name: '选项二' },
}, { name: '选项三' },
methods: { ];
onCancel() { const onCancel = () => {
Toast('取消'); Toast('取消');
}, };
return {
show,
actions,
onCancel,
};
}, },
}; };
``` ```
@ -93,15 +107,20 @@ export default {
``` ```
```js ```js
import { ref } from 'vue';
export default { export default {
data() { setup() {
return { const show = ref(false);
show: false, const actions = [
actions: [
{ name: '选项一' }, { name: '选项一' },
{ name: '选项二' }, { name: '选项二' },
{ name: '选项三', subname: '描述信息' }, { name: '选项三', subname: '描述信息' },
], ];
return {
show,
actions,
}; };
}, },
}; };
@ -121,15 +140,20 @@ export default {
``` ```
```js ```js
import { ref } from 'vue';
export default { export default {
data() { setup() {
return { const show = ref(false);
show: false, const actions = [
actions: [
{ name: '着色选项', color: '#ee0a24' }, { name: '着色选项', color: '#ee0a24' },
{ name: '禁用选项', disabled: true }, { name: '禁用选项', disabled: true },
{ name: '加载选项', loading: true }, { name: '加载选项', loading: true },
], ];
return {
show,
actions,
}; };
}, },
}; };

View File

@ -52,7 +52,9 @@
</template> </template>
<script> <script>
import { RED } from '../../utils/constant'; import { computed, reactive, toRefs } from 'vue';
import { useTranslate } from '../../composables/use-translate';
import Toast from '../../toast';
export default { export default {
i18n: { i18n: {
@ -86,8 +88,9 @@ export default {
}, },
}, },
data() { setup() {
return { const t = useTranslate();
const state = reactive({
show: { show: {
basic: false, basic: false,
cancel: false, cancel: false,
@ -95,44 +98,43 @@ export default {
status: false, status: false,
description: false, description: false,
}, },
}; });
},
computed: { const simpleActions = computed(() => [
simpleActions() { { name: t('option1') },
return [ { name: t('option2') },
{ name: this.t('option1') }, { name: t('option3') },
{ name: this.t('option2') }, ]);
{ name: this.t('option3') },
];
},
actionsWithDescription() { const statusActions = computed(() => [
return [ { name: t('coloredOption'), color: '#ee0a24' },
{ name: this.t('option1') }, { name: t('disabledOption'), disabled: true },
{ name: this.t('option2') },
{ name: this.t('option3'), subname: this.t('subname') },
];
},
statusActions() {
return [
{ name: this.t('coloredOption'), color: RED },
{ name: this.t('disabledOption'), disabled: true },
{ loading: true }, { loading: true },
]; ]);
},
},
methods: { const actionsWithDescription = computed(() => [
onSelect(item) { { name: t('option1') },
this.show.basic = false; { name: t('option2') },
this.$toast(item.name); { name: t('option3'), subname: t('subname') },
}, ]);
onCancel() { const onSelect = (item) => {
this.$toast(this.t('cancel')); state.show.basic = false;
}, Toast(item.name);
};
const onCancel = () => {
Toast(t('cancel'));
};
return {
...toRefs(state),
onSelect,
onCancel,
simpleActions,
statusActions,
actionsWithDescription,
};
}, },
}; };
</script> </script>