Skip to content

Instantly share code, notes, and snippets.

@fczbkk
Created November 23, 2019 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fczbkk/aaf81d402a4609e699fb949f35db00e7 to your computer and use it in GitHub Desktop.
Save fczbkk/aaf81d402a4609e699fb949f35db00e7 to your computer and use it in GitHub Desktop.
Vue custom input
<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>
<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