Closed Sudhin35 closed 2 years ago
Chrome
Not there
How to reproduce the bug?
What is the expected behavior?
What is the current behavior? My Options ( in traits ) for select component gets resetted every time after each reload of page
If is necessary to execute some code in order to reproduce the bug, paste it here below:
![options_issue](https://user-images.githubusercontent.com/50030377/141947836-5ff7009b-93ae-455b-8135-5a0a4ea91269.png) const trm = editor.TraitManager; trm.addType('select-options', { events:{ 'keyup': 'onChange', }, onValueChange: function () { var optionsStr = this.model.get('value').trim(); var options = optionsStr.split('\n'); var optComps = []; for (var i = 0; i < options.length; i++) { var optionStr = options[i]; var option = optionStr.split('::'); var opt = { tagName: 'option', attributes: {} }; if(option[1]) { opt.content = option[1]; opt.attributes.value = option[0]; } else { opt.content = option[0]; opt.attributes.value = option[0]; } optComps.push(opt); } var comps = this.target.get('components'); comps.reset(optComps); this.target.view.render(); }, getInputEl: function() { if (!this.$input) { var md = this.model; var trg = this.target; var name = md.get('name'); var optionsStr = ''; var opts = {placeholder: ''}; var options = trg.get('components'); for (var i = 0; i < options.length; i++) { var option = options.models[i]; var optAttr = option.get('attributes'); var optValue = optAttr.value || ''; optionsStr += `${optValue}::${option.get('content')}\n`; } this.$input = document.createElement('textarea'); this.$input.value = optionsStr; } return this.$input; }, }); editor.Components.addType('input-select', { isComponent: el => el.tagName === 'SELECT', // Model definition model: { defaults: { tagName: 'select', components:{options: [ // Array of options { 'opt1':'Option 1'}, ]}, //draggable: 'form, form *', // Can be dropped only inside `form` elements droppable: false, // Can't drop other elements inside attributes: { // Default attributes type: 'select-options', name: 'select-name', class:'form-control' }, traits: [{ type: 'select-options' // built-in types: text, number, select, checkbox, color }], // Add some style, just to make the component visible style: { } } } });
Hi @Sudhin35 please follow this API for your custom traits and I'd highly recommend storing your options as a component property instead of using inner components
GrapesJS version
What browser are you using?
Chrome
Reproducible demo link
Not there
Describe the bug
How to reproduce the bug?
What is the expected behavior?
What is the current behavior? My Options ( in traits ) for select component gets resetted every time after each reload of page
If is necessary to execute some code in order to reproduce the bug, paste it here below:
Code of Conduct