Repository navigation
Expand file tree
/
Copy pathKeyValue.php
More file actions
148 lines (110 loc) · 3.46 KB
/
Copy pathKeyValue.php
File metadata and controls
148 lines (110 loc) · 3.46 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
<?php
declare(strict_types=1);
namespace NyonCode\WireForms\Components;
use Closure;
/**
* Key-value pair editor for dictionary / map-like data.
*
* State is stored as array<int, array{key: string, value: string}>.
* Use mutateFormDataBeforeSave() to reshape to an associative array if needed.
*/
class KeyValue extends Field
{
protected string|Closure|null $keyLabel = null;
protected string|Closure|null $valueLabel = null;
protected ?string $keyPlaceholder = null;
protected ?string $valuePlaceholder = null;
protected bool $addable = true;
protected bool $deletable = true;
protected bool $reorderable = false;
protected bool $keyEditable = true;
/** Set the header label for the key column. */
public function keyLabel(string|Closure|null $label): static
{
$this->keyLabel = $label;
return $this;
}
/** Set the header label for the value column. */
public function valueLabel(string|Closure|null $label): static
{
$this->valueLabel = $label;
return $this;
}
/** Set the placeholder text for key inputs. */
public function keyPlaceholder(?string $placeholder): static
{
$this->keyPlaceholder = $placeholder;
return $this;
}
/** Set the placeholder text for value inputs. */
public function valuePlaceholder(?string $placeholder): static
{
$this->valuePlaceholder = $placeholder;
return $this;
}
/** Allow the user to add new rows (default true). */
public function addable(bool $condition = true): static
{
$this->addable = $condition;
return $this;
}
/** Allow the user to delete rows (default true). */
public function deletable(bool $condition = true): static
{
$this->deletable = $condition;
return $this;
}
/** Allow the user to drag rows into a new order. */
public function reorderable(bool $condition = true): static
{
$this->reorderable = $condition;
return $this;
}
/** Prevent the user from editing the key column (value-only mode). */
public function keyEditable(bool $condition = true): static
{
$this->keyEditable = $condition;
return $this;
}
// ─── Getters ───────────────────────────────────────────────────
public function getKeyLabel(): string
{
return $this->evaluate($this->keyLabel) ?? __('Key');
}
public function getValueLabel(): string
{
return $this->evaluate($this->valueLabel) ?? __('Value');
}
public function getKeyPlaceholder(): ?string
{
return $this->keyPlaceholder;
}
public function getValuePlaceholder(): ?string
{
return $this->valuePlaceholder;
}
public function isAddable(): bool
{
return $this->addable && ! $this->isDisabled();
}
public function isDeletable(): bool
{
return $this->deletable && ! $this->isDisabled();
}
public function isReorderable(): bool
{
return $this->reorderable && ! $this->isDisabled();
}
public function isKeyEditable(): bool
{
return $this->keyEditable && ! $this->isDisabled();
}
public function getStateType(): string
{
return 'array';
}
protected function viewName(): string
{
return 'wire-forms::components.key-value';
}
}