Skip to content
This repository was archived by the owner on Dec 6, 2021. It is now read-only.

Commit 4129d4d

Browse files
committed
feat(switch): add v-model support
1 parent bb419e0 commit 4129d4d

File tree

3 files changed

+109
-88
lines changed

3 files changed

+109
-88
lines changed

src/components/switch/index.ts

+19-20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { h, defineComponent, computed, mergeProps, PropType } from 'vue'
22
import { Switch, View } from '@tarojs/components'
33
import { CommonEvent } from '@tarojs/components/types/common'
44
import { AtSwitchProps } from 'types/switch'
5+
import { useModelValue } from '../../composables/model'
56

67
const AtSwitch = defineComponent({
78

@@ -16,42 +17,40 @@ const AtSwitch = defineComponent({
1617
type: String,
1718
default: '#6190e8'
1819
},
19-
checked: {
20-
type: Boolean,
21-
default: false
22-
},
23-
disabled: {
24-
type: Boolean,
25-
default: false
26-
},
27-
border: {
28-
type: Boolean,
29-
default: false
30-
},
20+
checked: Boolean,
21+
disabled: Boolean,
22+
border: Boolean,
3123
onChange: Function as PropType<AtSwitchProps['onChange']>,
3224
},
3325

34-
setup(props: AtSwitchProps, { attrs, slots }) {
26+
setup(props: AtSwitchProps, { attrs, emit }) {
3527

36-
const rootClass = computed(() => ({
28+
const modelChecked = useModelValue(props, emit, 'checked')
29+
30+
const rootClasses = computed(() => ({
3731
'at-switch': true,
38-
'at-switch--without-border': !props.border
32+
'at-switch--without-border': !Boolean(props.border)
3933
}))
4034

41-
const containerClass = computed(() => ({
35+
const containerClasses = computed(() => ({
4236
'at-switch__container': true,
4337
'at-switch--disabled': props.disabled
4438
}))
4539

4640
function handleChange(event: CommonEvent): void {
4741
const { value, checked } = event.detail
4842
const state = typeof value === 'undefined' ? checked : value
49-
props.onChange && props.onChange(state)
43+
44+
if (attrs['onUpdate:checked']) {
45+
modelChecked.value = state
46+
} else {
47+
props.onChange && props.onChange(state)
48+
}
5049
}
5150

5251
return () => (
5352
h(View, mergeProps(attrs, {
54-
class: rootClass.value
53+
class: rootClasses.value
5554
}), {
5655
default: () => [
5756
// title
@@ -61,7 +60,7 @@ const AtSwitch = defineComponent({
6160

6261
// container
6362
h(View, {
64-
class: containerClass.value
63+
class: containerClasses.value
6564
}, {
6665
default: () => [
6766
// mask
@@ -70,7 +69,7 @@ const AtSwitch = defineComponent({
7069
// switch
7170
h(Switch, {
7271
class: 'at-switch__switch',
73-
checked: props.checked,
72+
checked: modelChecked.value,
7473
color: props.color,
7574
onChange: handleChange,
7675
})

src/pages/form/switch/index.ts

-68
This file was deleted.

src/pages/form/switch/index.vue

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<template>
2+
<page header-title="Switch 开关">
3+
<panel
4+
title="基础用法"
5+
no-padding
6+
>
7+
<example-item>
8+
<at-switch
9+
title="开启中"
10+
checked
11+
/>
12+
<at-switch
13+
title="已关闭"
14+
border
15+
/>
16+
</example-item>
17+
</panel>
18+
19+
<panel
20+
title="使用 v-model 或 onChange 获取开关状态"
21+
no-padding
22+
>
23+
<example-item>
24+
<at-switch
25+
:title="`绑定 checked + onChange:${'&nbsp;'.repeat(6)}${switchValue ? '开启中' : '已关闭'}`"
26+
:checked="switchValue"
27+
@change="handleChange"
28+
/>
29+
<at-switch
30+
:title="`使用 v-model:checked:${'&nbsp;'.repeat(13)}${switchValue2 ? '开启中' : '已关闭'}`"
31+
v-model:checked="switchValue2"
32+
border
33+
/>
34+
</example-item>
35+
</panel>
36+
37+
<panel
38+
title="禁用状态"
39+
no-padding
40+
>
41+
<example-item>
42+
<at-switch
43+
title="不可点击: 无 border"
44+
checked
45+
disabled
46+
/>
47+
<at-switch
48+
title="不可点击: 有 border"
49+
border
50+
disabled
51+
/>
52+
</example-item>
53+
</panel>
54+
</page>
55+
56+
</template>
57+
58+
<script lang="ts">
59+
import { defineComponent, ref } from 'vue'
60+
import { AtSwitch } from '../../../index'
61+
import { Page, Panel, ExampleItem } from '../../components/demo-page'
62+
import './index.scss'
63+
64+
export default defineComponent({
65+
name: "SwitchDemo",
66+
67+
components: {
68+
AtSwitch,
69+
Page,
70+
Panel,
71+
ExampleItem
72+
},
73+
74+
setup() {
75+
const switchValue = ref(true)
76+
const switchValue2 = ref(true)
77+
78+
const handleChange = (value: boolean): void => {
79+
switchValue.value = value
80+
}
81+
82+
return {
83+
switchValue,
84+
switchValue2,
85+
handleChange
86+
}
87+
}
88+
})
89+
90+
</script>

0 commit comments

Comments
 (0)