Completed
Push — master ( 82be35...1537c0 )
by Song
02:28
created

src/Form/Field/Text.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Encore\Admin\Form\Field;
6
7
class Text extends Field
8
{
9
    use PlainInput, HasValuePicker;
10
11
    /**
12
     * @var string
13
     */
14
    protected $icon = 'fa-pencil';
15
16
    /**
17
     * @var bool
18
     */
19
    protected $withoutIcon = false;
20
21
    /**
22
     * Set custom fa-icon.
23
     *
24
     * @param string $icon
25
     *
26
     * @return $this
27
     */
28
    public function icon($icon)
29
    {
30
        $this->icon = $icon;
31
32
        return $this;
33
    }
34
35
    /**
36
     * Render this filed.
37
     *
38
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
39
     */
40
    public function render()
41
    {
42
        $this->initPlainInput();
43
44
        if (!$this->withoutIcon) {
45
            $this->prepend('<i class="fa '.$this->icon.' fa-fw"></i>');
46
        }
47
        $this->defaultAttribute('type', 'text')
48
            ->defaultAttribute('id', $this->id)
49
            ->defaultAttribute('name', $this->elementName ?: $this->formatName($this->column))
50
            ->defaultAttribute('value', old($this->elementName ?: $this->column, $this->value()))
51
            ->defaultAttribute('class', 'form-control '.$this->getElementClassString())
52
            ->defaultAttribute('placeholder', $this->getPlaceholder())
53
            ->mountPicker()
54
            ->addVariables([
55
                'prepend' => $this->prepend,
56
                'append'  => $this->append,
57
            ]);
58
59
        return parent::render();
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::render(); of type string|Illuminate\View\V...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 59 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
60
    }
61
62
    /**
63
     * Add inputmask to an elements.
64
     *
65
     * @param array $options
66
     *
67
     * @return $this
68
     */
69
    public function inputmask($options)
70
    {
71
        $options = json_encode_options($options);
72
73
        $this->script = "$('{$this->getElementClassSelector()}').inputmask($options);";
74
75
        return $this;
76
    }
77
78
    /**
79
     * Add datalist element to Text input.
80
     *
81
     * @param array $entries
82
     *
83
     * @return $this
84
     */
85
    public function datalist($entries = [])
86
    {
87
        $this->defaultAttribute('list', "list-{$this->id}");
88
89
        $datalist = "<datalist id=\"list-{$this->id}\">";
90
        foreach ($entries as $k => $v) {
91
            $datalist .= "<option value=\"{$k}\">{$v}</option>";
92
        }
93
        $datalist .= '</datalist>';
94
95
        return $this->append($datalist);
96
    }
97
98
    /**
99
     * show no icon in font of input.
100
     *
101
     * @return $this
102
     */
103
    public function withoutIcon()
104
    {
105
        $this->withoutIcon = true;
106
107
        return $this;
108
    }
109
}
110