[Question] Cannot add new trait
Question
I am trying to add a new trait but it is not working. I have done quite a bit of research and tried multiple ways but it still does not seem to be working. Can you please guide what I may be doing wrong.
Option 1:
export default function (editor, opt = {}) {
const c = opt;
const pfx = c.formClsPfx;
const FORMIO_TYPE = 'jmi_form';
editor.DomComponents.addType(FORMIO_TYPE, {
model: {
defaults: {
formURL: c.formURL,
droppable: false,
traits: [
// Strings are automatically converted to text types
'name', // Same as: { type: 'text', name: 'name' }
],
}
},
isComponent(el) {
if (el.getAttribute &&
el.getAttribute('data-gjs-type') === FORMIO_TYPE) {
return {
type: FORMIO_TYPE
};
}
},
view: {
init() {
const comps = this.model.get('components');
// Add a basic dynamic_form template if it's not yet initialized
if (!comps.length) {
comps.reset();
//Note that we have out "data-gjs-editable="false" below in the div tag
//Refer to: https://github.com/artf/grapesjs/issues/422
comps.add(`
<div data-gjs-editable="false" data-js="jmi_form" className="${pfx}"> This is test component </div>
`);
}
} //End of init
},
}); //End of domc.addType
} //End of export function
Option2: Trying to add programatically. I am following doc from: https://grapesjs.com/docs/modules/Traits.html#add-traits-to-components
export default function (editor, opt = {}) {
const c = opt;
const pfx = c.formClsPfx;
const FORMIO_TYPE = 'jmi_form';
editor.DomComponents.addType(FORMIO_TYPE, {
model: {
defaults: {
formURL: c.formURL,
droppable: false,
traits(component) {
const result = [];
result.push('name');
console.log("in here##########")
return result;
},
}
},
isComponent(el) {
if (el.getAttribute &&
el.getAttribute('data-gjs-type') === FORMIO_TYPE) {
return {
type: FORMIO_TYPE
};
}
},
view: {
init() {
const comps = this.model.get('components');
// Add a basic dynamic_form template if it's not yet initialized
if (!comps.length) {
comps.reset();
//Note that we have out "data-gjs-editable="false" below in the div tag
//Refer to: https://github.com/artf/grapesjs/issues/422
comps.add(`
<div data-gjs-editable="false" data-js="jmi_form" className="${pfx}"> This is test component </div>
`);
}
} //End of init
},
}); //End of domc.addType
} //End of export function
This option 2 is giving compilation error.
Answers (2)
Hi there! i do not see any reason why your first approach is not working! did you try this one? cheers.
This is working now. The original code is working.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #2043
[QUESTIONS] Adding custom component does not work
I have done a lot of research but still could not add a custom component. I tried the example in official docs (to modify behavior of "inpu...
Issue #2738
[QUESTION] Keep Hover state when amending the child
I'm trying to edit a dropdown list option, and it's quite difficult to style it because you cannot move your mouse to the Style Manager sin...
Issue #2189
[Question] How to add "Ordered/Unordered List" option in rte/custom block
Hi I'm trying to create an ordered list with grapesjs. I tried adding the following to the rte: And this works if my page is pre-filled wit...
Issue #3010
Asset Manager Configuration Is not Working
Hi, I tried all possible ways to change asset manager configuration, but it is not working as expected. Even I create a sample plugin but i...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.