Code Duplication    Length = 39-46 lines in 2 locations

src/Form/Field/CheckboxButton.php 1 location

@@ 7-52 (lines=46) @@
4
5
use Encore\Admin\Admin;
6
7
class CheckboxButton extends Checkbox
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $cascadeEvent = 'change';
13
14
    protected function addScript()
15
    {
16
        $script = <<<'SCRIPT'
17
$('.checkbox-group-toggle label').click(function(e) {
18
    e.stopPropagation();
19
    e.preventDefault();
20
21
    if ($(this).hasClass('active')) {
22
        $(this).removeClass('active');
23
        $(this).find('input').prop('checked', false);
24
    } else {
25
        $(this).addClass('active');
26
        $(this).find('input').prop('checked', true);
27
    }
28
29
    $(this).find('input').trigger('change');
30
});
31
SCRIPT;
32
33
        Admin::script($script);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function render()
40
    {
41
        $this->addScript();
42
43
        $this->addCascadeScript();
44
45
        $this->addVariables([
46
            'options' => $this->options,
47
            'checked' => $this->checked,
48
        ]);
49
50
        return parent::fieldRender();
51
    }
52
}
53

src/Form/Field/RadioButton.php 1 location

@@ 7-45 (lines=39) @@
4
5
use Encore\Admin\Admin;
6
7
class RadioButton extends Radio
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $cascadeEvent = 'change';
13
14
    protected function addScript()
15
    {
16
        $script = <<<'SCRIPT'
17
$('.radio-group-toggle label').click(function() {
18
    $(this).parent().children().removeClass('active');
19
    $(this).addClass('active');
20
});
21
SCRIPT;
22
23
        Admin::script($script);
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function render()
30
    {
31
        $this->addScript();
32
33
        $this->addCascadeScript();
34
35
        $this->addVariables([
36
            'options' => $this->options,
37
            'checked' => $this->checked,
38
        ]);
39
40
        return parent::fieldRender();
41
    }
42
}
43