/
TextField.php
155 lines (132 loc) · 4.04 KB
/
TextField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\fieldlayoutelements;
use Craft;
use craft\base\ElementInterface;
/**
* TextField represents a text field that can be included in field layouts.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 4.0.0
*/
class TextField extends BaseNativeField
{
/**
* @var string|null The input type
* @since 4.5.12
*/
public ?string $inputType = null;
/**
* @var string The input type
* @deprecated in 4.5.12. [[$inputType]] should be used instead.
*/
public string $type = 'text';
/**
* @var string|bool|null The input’s `autocomplete` attribute value.
*
* This can be set to `true` (`"on"`), `false` ("off")`, or any other allowed `autocomplete` value.
*
* If this is explicitly set to `null`, the input won’t get an `autocomplete` attribute.
*/
public string|bool|null $autocomplete = false;
/**
* @var string|string[]|null The input’s `class` attribute value.
*/
public string|array|null $class = null;
/**
* @var int|null The input’s `size` attribute value.
*/
public ?int $size = null;
/**
* @var string|null The input’s `name` attribute value.
*
* If this is not set, [[attribute()]] will be used by default.
*/
public ?string $name = null;
/**
* @var int|null The input’s `maxlength` attribute value.
*/
public ?int $maxlength = null;
/**
* @var bool Whether the input should get an `autofocus` attribute.
*/
public bool $autofocus = false;
/**
* @var bool Whether the input should support autocorrect.
*/
public bool $autocorrect = true;
/**
* @var bool Whether the input should support auto-capitalization.
*/
public bool $autocapitalize = true;
/**
* @var bool Whether the input should get a `disabled` attribute.
*/
public bool $disabled = false;
/**
* @var bool Whether the input should get a `readonly` attribute.
*/
public bool $readonly = false;
/**
* @var string|null The input’s `title` attribute value.
*/
public ?string $title = null;
/**
* @var string|null The input’s `placeholder` attribute value.
*/
public ?string $placeholder = null;
/**
* @var int|null The input’s `step` attribute value.
*/
public ?int $step = null;
/**
* @var int|null The input’s `min` attribute value.
*/
public ?int $min = null;
/**
* @var int|null The input’s `max` attribute value.
*/
public ?int $max = null;
/**
* @inheritdoc
*/
public function fields(): array
{
$fields = parent::fields();
// Don't include the value
unset($fields['value']);
return $fields;
}
/**
* @inheritdoc
*/
protected function inputHtml(?ElementInterface $element = null, bool $static = false): ?string
{
return Craft::$app->getView()->renderTemplate('_includes/forms/text.twig', [
'type' => $this->inputType ?? $this->type,
'autocomplete' => $this->autocomplete,
'class' => $this->class,
'id' => $this->id(),
'describedBy' => $this->describedBy($element, $static),
'size' => $this->size,
'name' => $this->name ?? $this->attribute(),
'value' => $this->value($element),
'maxlength' => $this->maxlength,
'autofocus' => $this->autofocus,
'autocorrect' => $this->autocorrect,
'autocapitalize' => $this->autocapitalize,
'disabled' => $static || $this->disabled,
'readonly' => $this->readonly,
'required' => !$static && $this->required,
'title' => $this->title,
'placeholder' => $this->placeholder,
'step' => $this->step,
'min' => $this->min,
'max' => $this->max,
]);
}
}