Issue #937Opened March 8, 2018by nojacko0 reactions

[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)

artfMarch 9, 20180 reactions

Unfortunately, the current implementation of traits doesn't allow extending its types

lock[bot]September 18, 20190 reactions

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.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.