I am trying to make an editable component with Vue 2. It should use the contenteditable attribute in any tag, replacing normal input. I want to give it placeholder functionality to show the value when the user has not provided to anyone, but I cannot get it to work.
I look at the current value of the component and set data.isEmpty to true when user content is missing. Then the component should show a placeholder value, but currently it doesn't show anything.
If I console.log result of the render method, it will show that the child placeholder node was created correctly, but for some reason it just wonโt display in the final HTML text.
Here's JSFiddle: https://jsfiddle.net/dy27fa8t/
And the inline snippet for those who prefer:
Vue.component('editable-content', { props: { initial: { type: String }, placeholder: { type: String, required: false } }, data() { return { value: this.initial, isEmpty: this.initial === '' } }, render: function(createElement) { const self = this return createElement( 'div', { attrs: { contenteditable: true }, on: { input: function(event) { self.value = event.target.innerHTML self.$emit('edited', event.target.value) } } }, this.isEmpty ? this.placeholder : this.value ) }, watch: { value(to, from) { this.isEmpty = to === '' } } }) new Vue({ el: '#app', components: [ 'editable-content' ] })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.0/vue.min.js"></script> <div id="app"> <editable-content initial="Initial value" placeholder="Placeholder" /> </div>
source share