Created
November 23, 2019 09:06
-
-
Save fczbkk/aaf81d402a4609e699fb949f35db00e7 to your computer and use it in GitHub Desktop.
Vue custom input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<p> | |
First name:<br> | |
<input v-model="first" @input="handleNameChange"> | |
</p> | |
<p> | |
Last name:<br> | |
<input v-model="last" @input="handleNameChange"> | |
</p> | |
</div> | |
</template> | |
<script> | |
export default { | |
name: 'FullNameEditor', | |
data () { | |
return this.splitName() | |
}, | |
props: { | |
value: { | |
type: String, | |
required: false, | |
default: 'John Doe' | |
} | |
}, | |
methods: { | |
splitName () { | |
const [first = '', last = ''] = this.value.split(' ') | |
return {first, last} | |
}, | |
joinName () { | |
return [this.first, this.last].join(' ').trim() | |
}, | |
handleNameChange () { | |
this.$emit('input', this.joinName()) | |
} | |
}, | |
watch: { | |
value () { | |
const {first, last} = this.splitName() | |
this.first = first | |
this.last = last | |
} | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<p> | |
Full name<br> | |
<input v-model="fullName"> | |
</p> | |
<FullNameEditor v-model="fullName" /> | |
</div> | |
</template> | |
<script> | |
import FullNameEditor from './FullNameEditor' | |
export default { | |
components: { FullNameEditor }, | |
data () { | |
return { | |
fullName: 'Riki Fridrich' | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment