[Question] Is it it possible to extend the select trait?
Question
I'm looking to create a trait called colours. This trait has a dropdown of colours that can be selected. When a colour is selected, I'd like to modified the class of the element to include the selected colour and remove other colours. The modifying of the class should be simple enough but I'm looking to extend the default select trait's behaviour instead of having to duplicate it for traits based around a select input.
I've tried a few things.
Extending the select type when adding the new type to the trait manager.
const selectTrait = editor.TraitManager.getType('select');
trm.addType('colours', selectTrait.extend({
options: [
{value: '', name: 'default'},
{value: 'black', name: 'Black'},
{value: 'blue', name: 'Blue'},
// etc ...
],
onValueChange: function () {
console.log('onValueChange', this.model.get('value'));
}
}));
And, I've attempted to change the onValueChange function (I know it's private 😉 ) when creating the component.
traits: [
{
type: 'select',
label: 'colours',
options: [
{value: '', name: 'default'},
{value: 'black', name: 'Black'},
{value: 'blue', name: 'Blue'},
// etc ...
],
/**
* On change callback
* @private
*/
onValueChange(model, value, opts = {}) {
const mod = this.model;
const trg = this.target;
const name = mod.get('name');
console.log(mod);
console.log(trg);
console.log(name);
if (opts.fromTarget) {
this.setInputValue(mod.get('value'));
} else {
const value = this.getValueForTarget();
mod.setTargetValue(value);
}
},
}
Neither seem to call my onValueChange function. Any help would be greatly appreciated. Thanks
Answers (2)
Unfortunately, the current implementation of traits doesn't allow extending its types
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #869
[Question] Should traits be disabled / hidden when no component is selected?
The style manager seems to have a nice solution for when no component is selected by showing a "Select an element before using Style Manage...
Issue #921
[QUESTION] Select and focus an html element from a panel button
Hi everyone, Is it possible to set focus in a section html with Grapes, like as an anchor? I have a panel button and it just select the sec...
Issue #987
[BUG/Question] Dropdown indicator not displayed for a custom trait
Hello, the dropdown (select) element arrow indicator icon is not rendered when adding a custom trait to a component: I'm using following co...
Issue #1886
How to create Drop down for the button inside a panel?
I'm trying to create a dropdown while clicking a button. I can append the data using ul, li to a button. Other than that any other way is t...
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.