rguliev / conditionize2.js

jQuery plugin for conditioning things
https://rguliev.github.io/conditionize2.js/demo/index.html
MIT License
22 stars 6 forks source link

Feature request: disable fields action handler #12

Open Seb33300 opened 3 years ago

Seb33300 commented 3 years ago

Hello,

I would like to see a new action handler disableFields very similar to the clearFields handler.

Instead of clearing fields, I would like to disable them.

It's quite similar to the clearFields because disabled field are not submitted. But the advantage is that if the user hide the field, and then display it again, all the values he started to enter are not lost. Its also possible to set default values that will not be cleared and not submitted if the fields are hidden.

Thank you

rguliev commented 3 years ago

Hello,

Thanks for your suggestion. I am not sure if I got your idea. It would be nice if you could provide a PR or, at least, an example.

Another thing is that for disableFields it is not enough to have one action, so one will need also to un-disable fields. So, it looks like it is simpler to write your custom ifTrue and ifFalse functions.

Seb33300 commented 1 year ago

Oups, sorry I never answered this. Here is how I fixed it:

$('...').conditionize({
    ifTrue: [
        // ...
        function ($s) {
            if ($s.is('option')) {
                $s.prop('disabled', false).insertAfter($s.parent().find(':not([disabled]):last'));
            } else {
                $s.find('input, select, textarea, button').prop('disabled', false);
            }
        }
    ],
    ifFalse: [
        // ...
        function ($s) {
            if ($s.is('option')) {
                if ($s.is(':selected')) {
                    $s.closest('select').val(null);
                }
                $s.prop('disabled', true).appendTo($s.parent());
            } else {
                $s.find('input, select, textarea, button').prop('disabled', true);
            }
        }
    ]
});