refactor: demo using script setup (#9235)

This commit is contained in:
neverland
2021-08-11 10:55:10 +08:00
committed by GitHub
parent 010238b63c
commit 4c41908ac1
86 changed files with 4277 additions and 5151 deletions
+135 -148
View File
@@ -1,113 +1,8 @@
<template>
<demo-block :title="t('basicUsage')">
<van-checkbox v-model="checkbox1">{{ t('checkbox') }}</van-checkbox>
</demo-block>
<demo-block :title="t('disabled')">
<van-checkbox :model-value="false" disabled>
{{ t('checkbox') }}
</van-checkbox>
<van-checkbox :model-value="true" disabled>
{{ t('checkbox') }}
</van-checkbox>
</demo-block>
<demo-block :title="t('customShape')">
<van-checkbox v-model="checkboxShape" shape="square">
{{ t('customShape') }}
</van-checkbox>
</demo-block>
<demo-block :title="t('customColor')">
<van-checkbox v-model="checkbox2" checked-color="#ee0a24">
{{ t('customColor') }}
</van-checkbox>
</demo-block>
<demo-block :title="t('customIconSize')">
<van-checkbox v-model="checboxIcon" icon-size="24px">
{{ t('customIconSize') }}
</van-checkbox>
</demo-block>
<demo-block :title="t('customIcon')">
<van-checkbox v-model="checkbox3">
{{ t('customIcon') }}
<template #icon="{ checked }">
<img :src="checked ? activeIcon : inactiveIcon" />
</template>
</van-checkbox>
</demo-block>
<demo-block :title="t('disableLabel')">
<van-checkbox v-model="checkboxLabel" label-disabled>
{{ t('checkbox') }}
</van-checkbox>
</demo-block>
<demo-block :title="t('title3')">
<van-checkbox-group v-model="result">
<van-checkbox name="a">{{ t('checkbox') }} a</van-checkbox>
<van-checkbox name="b">{{ t('checkbox') }} b</van-checkbox>
</van-checkbox-group>
</demo-block>
<demo-block v-if="!isWeapp" :title="t('horizontal')">
<van-checkbox-group v-model="horizontalResult" direction="horizontal">
<van-checkbox name="a">{{ t('checkbox') }} a</van-checkbox>
<van-checkbox name="b">{{ t('checkbox') }} b</van-checkbox>
</van-checkbox-group>
</demo-block>
<demo-block :title="t('title4')">
<van-checkbox-group v-model="result2" :max="2">
<van-checkbox name="a">{{ t('checkbox') }} a</van-checkbox>
<van-checkbox name="b">{{ t('checkbox') }} b</van-checkbox>
<van-checkbox name="c">{{ t('checkbox') }} c</van-checkbox>
</van-checkbox-group>
</demo-block>
<demo-block v-if="!isWeapp" :title="t('toggleAll')">
<van-checkbox-group v-model="checkAllResult" ref="group">
<van-checkbox name="a">{{ t('checkbox') }} a</van-checkbox>
<van-checkbox name="b">{{ t('checkbox') }} b</van-checkbox>
<van-checkbox name="c">{{ t('checkbox') }} c</van-checkbox>
</van-checkbox-group>
<div class="demo-checkbox-buttons">
<van-button type="primary" @click="checkAll">
{{ t('checkAll') }}
</van-button>
<van-button type="primary" @click="toggleAll">
{{ t('inverse') }}
</van-button>
</div>
</demo-block>
<demo-block :title="t('title5')">
<van-checkbox-group v-model="result3">
<van-cell-group>
<van-cell
v-for="(item, index) in list"
clickable
:key="index"
:title="`${t('checkbox')} ${item}`"
@click="toggle(index)"
>
<template #right-icon>
<van-checkbox :ref="setRefs(index)" :name="item" @click.stop />
</template>
</van-cell>
</van-cell-group>
</van-checkbox-group>
</demo-block>
</template>
<script lang="ts">
import { ref, reactive, toRefs } from 'vue';
<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useTranslate } from '@demo/use-translate';
import { useRefs } from '../../composables/use-refs';
import { ComponentInstance } from 'src/utils';
import { ComponentInstance } from '../../utils';
const i18n = {
'zh-CN': {
@@ -142,54 +37,146 @@ const i18n = {
},
};
export default {
setup() {
const t = useTranslate(i18n);
const state = reactive({
checkbox1: true,
checkbox2: true,
checkbox3: true,
checkboxShape: true,
checkboxLabel: true,
checboxIcon: true,
list: ['a', 'b'],
result: ['a', 'b'],
result2: [],
result3: [],
checkAllResult: [],
horizontalResult: [],
});
const t = useTranslate(i18n);
const state = reactive({
checkbox1: true,
checkbox2: true,
checkbox3: true,
checkboxShape: true,
checkboxLabel: true,
checboxIcon: true,
list: ['a', 'b'],
result: ['a', 'b'],
result2: [],
result3: [],
checkAllResult: [],
horizontalResult: [],
});
const group = ref<ComponentInstance>();
const [refs, setRefs] = useRefs<ComponentInstance>();
const activeIcon = 'https://img.yzcdn.cn/vant/user-active.png';
const inactiveIcon = 'https://img.yzcdn.cn/vant/user-inactive.png';
const toggle = (index: number) => {
refs.value[index].toggle();
};
const group = ref<ComponentInstance>();
const [refs, setRefs] = useRefs<ComponentInstance>();
const checkAll = () => {
group.value?.toggleAll(true);
};
const toggle = (index: number) => {
refs.value[index].toggle();
};
const toggleAll = () => {
group.value?.toggleAll();
};
const checkAll = () => {
group.value?.toggleAll(true);
};
return {
...toRefs(state),
t,
group,
toggle,
setRefs,
checkAll,
toggleAll,
activeIcon: 'https://img.yzcdn.cn/vant/user-active.png',
inactiveIcon: 'https://img.yzcdn.cn/vant/user-inactive.png',
};
},
const toggleAll = () => {
group.value?.toggleAll();
};
</script>
<template>
<demo-block :title="t('basicUsage')">
<van-checkbox v-model="state.checkbox1">{{ t('checkbox') }}</van-checkbox>
</demo-block>
<demo-block :title="t('disabled')">
<van-checkbox :model-value="false" disabled>
{{ t('checkbox') }}
</van-checkbox>
<van-checkbox :model-value="true" disabled>
{{ t('checkbox') }}
</van-checkbox>
</demo-block>
<demo-block :title="t('customShape')">
<van-checkbox v-model="state.checkboxShape" shape="square">
{{ t('customShape') }}
</van-checkbox>
</demo-block>
<demo-block :title="t('customColor')">
<van-checkbox v-model="state.checkbox2" checked-color="#ee0a24">
{{ t('customColor') }}
</van-checkbox>
</demo-block>
<demo-block :title="t('customIconSize')">
<van-checkbox v-model="state.checboxIcon" icon-size="24px">
{{ t('customIconSize') }}
</van-checkbox>
</demo-block>
<demo-block :title="t('customIcon')">
<van-checkbox v-model="state.checkbox3">
{{ t('customIcon') }}
<template #icon="{ checked }">
<img :src="checked ? activeIcon : inactiveIcon" />
</template>
</van-checkbox>
</demo-block>
<demo-block :title="t('disableLabel')">
<van-checkbox v-model="state.checkboxLabel" label-disabled>
{{ t('checkbox') }}
</van-checkbox>
</demo-block>
<demo-block :title="t('title3')">
<van-checkbox-group v-model="state.result">
<van-checkbox name="a">{{ t('checkbox') }} a</van-checkbox>
<van-checkbox name="b">{{ t('checkbox') }} b</van-checkbox>
</van-checkbox-group>
</demo-block>
<demo-block v-if="!isWeapp" :title="t('horizontal')">
<van-checkbox-group v-model="state.horizontalResult" direction="horizontal">
<van-checkbox name="a">{{ t('checkbox') }} a</van-checkbox>
<van-checkbox name="b">{{ t('checkbox') }} b</van-checkbox>
</van-checkbox-group>
</demo-block>
<demo-block :title="t('title4')">
<van-checkbox-group v-model="state.result2" :max="2">
<van-checkbox name="a">{{ t('checkbox') }} a</van-checkbox>
<van-checkbox name="b">{{ t('checkbox') }} b</van-checkbox>
<van-checkbox name="c">{{ t('checkbox') }} c</van-checkbox>
</van-checkbox-group>
</demo-block>
<demo-block v-if="!isWeapp" :title="t('toggleAll')">
<van-checkbox-group v-model="state.checkAllResult" ref="group">
<van-checkbox name="a">{{ t('checkbox') }} a</van-checkbox>
<van-checkbox name="b">{{ t('checkbox') }} b</van-checkbox>
<van-checkbox name="c">{{ t('checkbox') }} c</van-checkbox>
</van-checkbox-group>
<div class="demo-checkbox-buttons">
<van-button type="primary" @click="checkAll">
{{ t('checkAll') }}
</van-button>
<van-button type="primary" @click="toggleAll">
{{ t('inverse') }}
</van-button>
</div>
</demo-block>
<demo-block :title="t('title5')">
<van-checkbox-group v-model="state.result3">
<van-cell-group>
<van-cell
v-for="(item, index) in state.list"
clickable
:key="index"
:title="`${t('checkbox')} ${item}`"
@click="toggle(index)"
>
<template #right-icon>
<van-checkbox :ref="setRefs(index)" :name="item" @click.stop />
</template>
</van-cell>
</van-cell-group>
</van-checkbox-group>
</demo-block>
</template>
<style lang="less">
@import '../../style/var';