it seems should be a simple solution but something went wrong...
I need to conditionally remove contenteditable attribute from element:
<!-- first state -->
<div contenteditable="true"></div>
<!-- second state -->
<div></div>
I've tried:
data() {
return {
contenteditable: true,
title: "Title",
};
},
methods: {
handleClick() {
this.contenteditable = null;
this.title = null;
},
},
render(createElement) {
const options = {
attrs: {
id: "ID",
title: this.title,
contenteditable: this.contenteditable,
// Not working either
//...(this.contenteditable && { contenteditable: true }),
},
domProps: {
innerHTML: "<p>Test</p>",
},
on: {
click: this.handleClick,
},
};
return createElement("div", options);
},
Result is: <div contenteditable="false"></div>, but I need to remove attribute completely. For example title attribute works as expected, it not renders if it's value was set to null. I've also tried to make attrs object fully reactive, like:
data() {
return {
attrs: {
title: 'Title',
contenteditable: true,
}
}
},
methods: {
handleClick() {
Vue.delete(this.attrs, 'contenteditable')
Vue.delete(this.attrs, 'title')
},
},
render(createElement) {
const options = {
attrs: this.attrs,
domProps: {
innerHTML: "<p>Test</p>",
},
on: {
click: this.handleClick,
},
};
return createElement("div", options);
},
...but with no luck. Any ideas how to solve this?
Thanks!
Vue 2 treats the
contenteditableattribute as a special case here, it will not remove the attribute if the new attribute value isundefined. This is fixed (or planned) in Vue 3.You can bypass this behavior and set it with
domPropsinstead:Here is a demo: