Grapesjs: [BUG]: Some form components not working well with Preset Webpage Plugin

Created on 3 Jul 2020  路  8Comments  路  Source: artf/grapesjs

Hi, I've noticed that some form components (like checkbox, radio and select) are not working when gjs is initialized with the Preset Webpage Plugin like in the example below:
You can confirm this issue with the online Webpage Demo too https://grapesjs.com/demo.html

Expected Behavior: can select all of them normally (only in preview mode)
gif1

Current Behavior: can't select anything (not even in preview mode)
gif2

BTW: when I click in the select it shows the options far from its parent (in the first gif) but this is due to this gif when recording it because actually it shows right below the select as the normal behavior, so just ignore that.

All 8 comments

I think it's just because the webpage preset uses an old version of plugin-forms, so it should be updated

@artf I tried the grapes-plugin-forms itself (that is more updated throughout your repository), still not works, I even update all the grapes-plugin-forms dependencies to the latest ones and it still have the same behavior

in that case, what were you using in the Expected Behavior gif?

In those gifs I'm using the same version directly from the cdn with only custom code plugin:
image

In the first one:
<script src="https://unpkg.com/grapesjs"></script>

In the second one (grapesjs-preset-webpage - 0.1.11):

<script src="https://unpkg.com/grapesjs"></script>
<script src="dist/grapesjs-preset-webpage.min.js"></script>

So, basically, this is what I'm using, just the basic:
Codepen GrapesJS Demo

Ah ok, now I got it. Yeah, forms plugin avoids some default input behavior, so it's intended. I agree that in the preview mode those behaviors could be re-enabled but I think that part might be integrated as a plugin (eg. by using event listeners on the preview command)

Hum...ok, I'll try to do that, thanks for the help :+1:

Hi @artf , so I've tried to create a listenner like you suggest, the thing is, the editor wont listen those changes after initialize and somehow selecting a checkbox/radio input makes the editor put the style="display: none" to them hiding from the user.
So, I've searched in the forms plugin and I find the code that's preventing the default behavior in those components:
grapesjs-plugin-forms-master > src > components.js

Line 278 (for select component):

...
    view: preventDefaultClick(),
...

Line 309 (for checkbox and radio components):

...
    view: defaultView.extend({
        events: {
            'click': 'handleClick',
        },

        handleClick(e) {
            e.preventDefault();
        },

        init() {
            this.listenTo(this.model, 'change:attributes:checked', this.handleChecked);
        },

        handleChecked() {
            this.el.checked = !!this.model.get('attributes').checked;
        },
    }),
...

As you say, the editor prevents the default behavior of those components, so this issue can be resolved changing the view property value on both lines to the default behavior, like this:

...
    view: defaultView,
...

After rebuild the plugin the behavior works as expected, so problem resolved.
I just want to ask if you think that this changes may cause any other issue that I'm not seeing right now, otherwise I keep the changes.

After rebuild the plugin the behavior works as expected, so problem resolved.

Maybe there is a reason why I've done that?! 馃槄

Via plugin, you can simply extend those components and override handleClick function with your logic, where you prevent only if the editor is not in the preview (editor.Commands.isActive('preview'))

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andre2 picture andre2  路  3Comments

nojacko picture nojacko  路  3Comments

YashPrince picture YashPrince  路  3Comments

tribulant picture tribulant  路  3Comments

kawika-connell picture kawika-connell  路  3Comments