Skip to content

Commit 53e20ec

Browse files
committed
input handler
1 parent 58090d3 commit 53e20ec

File tree

3 files changed

+58
-38
lines changed

3 files changed

+58
-38
lines changed

src/Toggle.vue

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
<template>
2-
<div
3-
class="toggle-input"
4-
:class="{'is-active': isOn}"
5-
:style="cssVars"
6-
:id="id"
7-
@click="toggle"
8-
>
9-
<slot name="on">
10-
<span class="toggle-on" v-html="onLabel"></span>
11-
</slot>
12-
<slot name="off">
13-
<span class="toggle-off" v-html="offLabel"></span>
14-
</slot>
2+
<div class="toggle-input" :style="cssVars">
3+
<input
4+
type="checkbox"
5+
:name="name"
6+
:id="id"
7+
:checked="checked"
8+
:trueValue="trueValue"
9+
:falseValue="falseValue"
10+
@input="handleInput"
11+
/>
12+
<label :for="id">
13+
<slot name="on">
14+
<span class="toggle-on" v-html="onLabel"></span>
15+
</slot>
16+
<slot name="off">
17+
<span class="toggle-off" v-html="offLabel"></span>
18+
</slot>
19+
</label>
1520
</div>
1621
</template>
1722

@@ -48,6 +53,11 @@
4853
required: false,
4954
default: 'toggle'
5055
},
56+
name: {
57+
type: [String, Number],
58+
required: false,
59+
default: 'toggle'
60+
},
5161
falseValue: {
5262
type: [String, Number, Boolean],
5363
required: false,
@@ -58,6 +68,16 @@
5868
required: false,
5969
default: true,
6070
},
71+
offLabel: {
72+
type: [String, Object],
73+
required: false,
74+
default: ''
75+
},
76+
onLabel: {
77+
type: [String, Object],
78+
required: false,
79+
default: ''
80+
},
6181
width: {
6282
type: Number,
6383
required: false,
@@ -73,16 +93,6 @@
7393
required: false,
7494
default: 300
7595
},
76-
offLabel: {
77-
type: [String, Object],
78-
required: false,
79-
default: ''
80-
},
81-
onLabel: {
82-
type: [String, Object],
83-
required: false,
84-
default: ''
85-
},
8696
offBackground: {
8797
type: String,
8898
required: false,
@@ -93,17 +103,17 @@
93103
required: false,
94104
default: '#41b883'
95105
},
96-
offColor: {
106+
offTextColor: {
97107
type: String,
98108
required: false,
99109
default: '#888888'
100110
},
101-
onColor: {
111+
onTextColor: {
102112
type: String,
103113
required: false,
104114
default: '#ffffff'
105115
},
106-
handleBackground: {
116+
handleColor: {
107117
type: String,
108118
required: false,
109119
default: '#ffffff'
@@ -116,17 +126,17 @@
116126
},
117127
setup(props, context)
118128
{
119-
// no export
120129
const value = useValue(props, context)
121130
122131
const style = useStyle(props, context)
123132
124133
const toggle = useToggle(props, context, {
125-
value: value.value,
134+
inputValue: value.inputValue,
126135
update: value.update,
127136
})
128137
129138
return {
139+
...value,
130140
...style,
131141
...toggle,
132142
}

src/composables/useToggle.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ export default function useValue (props, context, dependencies)
66

77
// ============ DEPENDENCIES ============
88

9-
const value = dependencies.value
9+
const inputValue = dependencies.inputValue
1010
const update = dependencies.update
1111

1212
// ============== COMPUTED ==============
1313

14-
const isOn = computed(() => {
15-
return value.value === trueValue.value
14+
const checked = computed(() => {
15+
return inputValue.value === trueValue.value
1616
})
1717

1818
// =============== METHODS ==============
1919

2020
const toggle = () => {
21-
update(isOn.value ? falseValue.value : trueValue.value)
21+
update(checked.value ? falseValue.value : trueValue.value)
2222
}
2323

2424
const on = () => {
@@ -30,7 +30,7 @@ export default function useValue (props, context, dependencies)
3030
}
3131

3232
return {
33-
isOn,
33+
checked,
3434
toggle,
3535
on,
3636
off,

src/composables/useValue.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { toRefs } from 'composition-api'
1+
import { toRefs, watch } from 'composition-api'
22

33
export default function useValue (props, context, dependencies)
44
{
5-
const { value: baseValue, modelValue, falseValue } = toRefs(props)
5+
const { value: baseValue, modelValue, falseValue, trueValue } = toRefs(props)
66

77
/* istanbul ignore next */
8-
const value = context.expose !== undefined ? modelValue : baseValue
8+
const inputValue = context.expose !== undefined ? modelValue : baseValue
99

1010
// =============== METHODS ==============
1111

@@ -16,13 +16,23 @@ export default function useValue (props, context, dependencies)
1616
context.emit('change', val)
1717
}
1818

19+
const handleInput = (val) => {
20+
update(val.target.checked ? trueValue.value : falseValue.value)
21+
}
22+
1923
// ================ HOOKS ===============
2024

21-
if ([null, undefined].indexOf(value.value) !== -1) {
25+
if ([null, undefined, false, 0, '0', 'off'].indexOf(inputValue.value) !== -1) {
2226
update(falseValue.value)
2327
}
2428

29+
if ([true, 1, '1', 'on'].indexOf(inputValue.value) !== -1) {
30+
update(trueValue.value)
31+
}
32+
2533
return {
26-
value,
34+
inputValue,
35+
update,
36+
handleInput,
2737
}
2838
}

0 commit comments

Comments
 (0)