Skip to content

Commit e2a0bfc

Browse files
author
Julian Kempff
committed
fix: only append onInput handler when modelValue or value are given
1 parent 22db4d8 commit e2a0bfc

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

example/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,15 @@ <h1>Basic Example</h1>
160160
</wrapper>
161161
<w-2>
162162
<h-2 :animate="animated2"> {{text2}} </h-2>
163-
<styled-input v-model:value="text2" />
163+
<styled-input v-model="text2" />
164164
<super-btn visible @click="animated2 = !animated2"> An extension of Styled Button </super-btn>
165165
</w-2>
166166
<w-3
167167
bg="#c6f7e6"
168168
>
169169
<custom-title :visible="true">Change Rendered Elements with "as" prop.</custom-title>
170170
<h3>Enter any HTML element and see the element rendered by the styled component change 👇🏽</h3>
171-
<styled-input @keydown.enter="updateElement" placeholder="Enter an HTML element! Like 'button' or 'section'" v-model:value="element" />
171+
<styled-input @keydown.enter="updateElement" placeholder="Enter an HTML element! Like 'button' or 'section'" v-model="element" />
172172
<custom-btn
173173
visible
174174
:as="setElement"

src/models/StyledComponent.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default (ComponentStyle) => {
1818
props: {
1919
as: [String, Object],
2020
value: null,
21+
modelValue: null,
2122

2223
...currentProps,
2324
...prevProps
@@ -31,7 +32,7 @@ export default (ComponentStyle) => {
3132

3233
data () {
3334
return {
34-
localValue: this.value
35+
localValue: this.modelValue || this.value
3536
}
3637
},
3738

@@ -41,17 +42,35 @@ export default (ComponentStyle) => {
4142
if (propName === 'as' && isStringTarget) {
4243
return props
4344
}
44-
if (this.$props[propName]) {
45+
if (this.$props[propName] !== undefined) {
4546
props[propName] = this.$props[propName]
4647
}
4748
return props
4849
}, {})
50+
},
51+
valueProps () {
52+
if (this.modelValue === undefined && this.value === undefined) {
53+
return {}
54+
}
55+
56+
return {
57+
value: this.localValue,
58+
onInput: event => {
59+
if (event && event.target) {
60+
this.localValue = event.target.value
61+
}
62+
}
63+
}
4964
}
5065
},
5166

5267
watch: {
5368
localValue (val) {
54-
this.$emit('update:value', val)
69+
if (this.modelValue !== undefined) {
70+
this.$emit('update:modelValue', val)
71+
} else {
72+
this.$emit('update:value', val)
73+
}
5574
}
5675
},
5776

@@ -62,13 +81,9 @@ export default (ComponentStyle) => {
6281
isVueComponent(target) ? target : this.$props.as || target,
6382
{
6483
...this.passProps,
65-
class: [styleClass],
66-
value: this.localValue,
67-
onInput: event => {
68-
if (event && event.target) {
69-
this.localValue = event.target.value
70-
}
71-
}
84+
...this.valueProps,
85+
...this.$attrs,
86+
class: [styleClass]
7287
},
7388
this.$slots
7489
)

0 commit comments

Comments
 (0)