Repository navigation
Expand file tree
/
Copy pathTextInput.php
More file actions
347 lines (275 loc) · 9.88 KB
/
Copy pathTextInput.php
File metadata and controls
347 lines (275 loc) · 9.88 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
declare(strict_types=1);
namespace NyonCode\WireForms\Components;
use Closure;
use Illuminate\Database\Eloquent\Model;
use NyonCode\WireCore\Foundation\Concerns\CanBeNullable;
use NyonCode\WireCore\Foundation\Concerns\HasExtraInputAttributes;
use NyonCode\WireCore\Foundation\Contracts\DehydratesState;
use NyonCode\WireCore\Foundation\Support\EnumResolver;
use NyonCode\WireForms\Concerns\CanSubmitNatively;
use NyonCode\WireForms\Concerns\HasCharacterLimits;
use NyonCode\WireForms\Contracts\SupportsNativeSubmit;
use NyonCode\WireForms\Exceptions\FormConfigurationException;
use NyonCode\WireForms\Support\FieldBounds;
class TextInput extends Field implements DehydratesState, SupportsNativeSubmit
{
use CanBeNullable;
use CanSubmitNatively;
use HasCharacterLimits;
use HasExtraInputAttributes;
protected string $inputType = 'text';
protected int|float|string|Closure|null $minValue = null;
protected int|float|string|Closure|null $maxValue = null;
protected int|float|string|null $step = null;
protected ?string $mask = null;
protected ?string $dynamicMask = null;
protected ?string $inputMode = null;
protected ?string $autocomplete = null;
/** @var array<int, string>|null */
protected ?array $datalistOptions = null;
protected bool $isRevealable = false;
// ─── Type variants ─────────────────────────────────────────────
/** Set the HTML input type directly. */
public function type(string $type): static
{
$this->inputType = $type;
return $this;
}
/** Type preset: email (sets the matching inputmode). */
public function email(): static
{
$this->inputType = 'email';
$this->inputMode = 'email';
return $this;
}
/** Type preset: password (see `revealable()` for a show/hide toggle). */
public function password(): static
{
$this->inputType = 'password';
return $this;
}
/** Type preset: tel (sets the matching inputmode). */
public function tel(): static
{
$this->inputType = 'tel';
$this->inputMode = 'tel';
return $this;
}
/** Type preset: url (sets the matching inputmode). */
public function url(): static
{
$this->inputType = 'url';
$this->inputMode = 'url';
return $this;
}
/** Type preset: decimal number (type=number, decimal inputmode). */
public function numeric(): static
{
$this->inputType = 'number';
$this->inputMode = 'decimal';
return $this;
}
/** Type preset: whole number (type=number, numeric inputmode, step 1). */
public function integer(): static
{
$this->inputType = 'number';
$this->inputMode = 'numeric';
$this->step = 1;
return $this;
}
/** Type preset: search. */
public function search(): static
{
$this->inputType = 'search';
return $this;
}
// ─── Constraints ───────────────────────────────────────────────
/**
* Set the minimum numeric value (a value or a `$get`-aware Closure).
*
* @throws FormConfigurationException When it exceeds a literal maxValue().
*/
public function minValue(int|float|string|Closure|null $value): static
{
// Only a pair of literal numbers can be compared here. A Closure needs a
// record to evaluate against and a string may be a date or a datetime-local
// bound, which this does not try to parse — those stay the caller's to keep
// consistent, and the browser rejects the obvious cases anyway.
FieldBounds::assertOrdered(
static::class,
'minValue',
self::comparableBound($value),
'maxValue',
self::comparableBound($this->maxValue),
);
$this->minValue = $value;
return $this;
}
/**
* Set the maximum numeric value (a value or a `$get`-aware Closure).
*
* @throws FormConfigurationException When it falls below a literal minValue().
*/
public function maxValue(int|float|string|Closure|null $value): static
{
FieldBounds::assertOrdered(
static::class,
'minValue',
self::comparableBound($this->minValue),
'maxValue',
self::comparableBound($value),
);
$this->maxValue = $value;
return $this;
}
/**
* Set the numeric step increment.
*
* @throws FormConfigurationException When a numeric step is not greater than 0.
*/
public function step(int|float|string|null $step): static
{
// `'any'` is the one non-numeric step HTML defines, and it is the reason
// this setter takes a string at all — so only a numeric one is checked.
if (is_int($step) || is_float($step)) {
FieldBounds::assertPositive(static::class, 'step', $step);
}
$this->step = $step;
return $this;
}
/**
* The bound as a number, or null when it is not one this can compare.
*/
private static function comparableBound(int|float|string|Closure|null $value): int|float|null
{
if (is_int($value) || is_float($value)) {
return $value;
}
return null;
}
// ─── Extras ────────────────────────────────────────────────────
/** Apply an input mask pattern to the field. */
public function mask(?string $mask): static
{
$this->mask = $mask;
return $this;
}
/**
* Apply a mask that is recomputed on every keystroke, written as the Alpine
* expression `x-mask:dynamic` evaluates — `$money($input, ',', ' ')`,
* `$input.startsWith('34') ? '9999 999999 99999' : '9999 9999 9999 9999'`.
*
* A pattern that never changes belongs in {@see mask()}; this one costs an
* evaluation per keypress and takes precedence when both are set.
*/
public function dynamicMask(?string $expression): static
{
$this->dynamicMask = $expression;
return $this;
}
/** Set the HTML `inputmode` (the virtual-keyboard hint on mobile). */
public function inputMode(?string $mode): static
{
$this->inputMode = $mode;
return $this;
}
/** Set the HTML `autocomplete` attribute. */
public function autocomplete(?string $value): static
{
$this->autocomplete = $value;
return $this;
}
/**
* Suggestion list for the input.
*
* Accepts a plain list of suggestion strings, or a backed/unit enum class — in which
* case the enum's case labels (see {@see EnumResolver::options()}) become the suggestions.
*
* @param array<int, string>|class-string $options
*/
public function datalist(array|string $options): static
{
if (is_string($options)) {
$options = EnumResolver::isEnumClass($options) ? array_values(EnumResolver::options($options)) : [];
}
$this->datalistOptions = $options;
return $this;
}
/** Add a show/hide toggle to the field (for `password()` inputs). */
public function revealable(bool $condition = true): static
{
$this->isRevealable = $condition;
return $this;
}
// ─── Getters ───────────────────────────────────────────────────
public function getInputType(): string
{
return $this->inputType;
}
public function getMinValue(): int|float|string|null
{
return $this->evaluate($this->minValue);
}
public function getMaxValue(): int|float|string|null
{
return $this->evaluate($this->maxValue);
}
public function getStep(): int|float|string|null
{
return $this->step;
}
public function getMask(): ?string
{
return $this->mask;
}
public function getDynamicMask(): ?string
{
return $this->dynamicMask;
}
public function getInputMode(): ?string
{
return $this->inputMode;
}
public function getAutocomplete(): ?string
{
return $this->autocomplete;
}
/**
* @return array<int, string>|null
*/
public function getDatalistOptions(): ?array
{
return $this->datalistOptions;
}
public function isRevealable(): bool
{
return $this->isRevealable;
}
// ─── Save path ─────────────────────────────────────────────────
/**
* A cleared input reaches the record as `null` when `''` cannot be what the
* author meant.
*
* A `<input type=number>` submits `''` when it is emptied — there is no
* other value a browser can send — and `''` is not a figure. Postgres and
* strict-mode MySQL reject it on a numeric column; SQLite stores an empty
* string next to the decimals. So a number input nullifies without being
* asked, exactly as {@see Select} does for its empty option.
*
* Every other type has to be told with {@see nullable()}, because on a text
* column `''` is a value an author may well mean, and a `NOT NULL` column
* would reject the null a blanket rule wrote for them.
*/
public function dehydrateState(mixed $state, ?Model $record = null): mixed
{
if ($state === '' && $this->inputType === 'number') {
return null;
}
return $this->nullifyEmptyState($state);
}
protected function viewName(): string
{
return 'wire-forms::components.text-input';
}
}