Repository navigation
Expand file tree
/
Copy pathToggle.php
More file actions
154 lines (118 loc) · 3.67 KB
/
Copy pathToggle.php
File metadata and controls
154 lines (118 loc) · 3.67 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
<?php
declare(strict_types=1);
namespace NyonCode\WireForms\Components;
use Closure;
use NyonCode\WireCore\Foundation\Colors\Color;
use NyonCode\WireCore\Foundation\Concerns\HasColor;
use NyonCode\WireCore\Foundation\Concerns\HasExtraInputAttributes;
use NyonCode\WireCore\Foundation\Icons\Icon;
/**
* Toggle switch field with customizable colors, icons, and labels.
*/
class Toggle extends Field
{
use HasExtraInputAttributes;
protected string|Closure|null $onLabel = null;
protected string|Closure|null $offLabel = null;
protected string $onColor = 'primary';
protected string $offColor = 'gray';
protected ?string $onIcon = null;
protected ?string $offIcon = null;
protected bool $inline = true;
/** Set the label shown when the toggle is on. */
public function onLabel(string|Closure|null $label): static
{
$this->onLabel = $label;
return $this;
}
/** Set the label shown when the toggle is off. */
public function offLabel(string|Closure|null $label): static
{
$this->offLabel = $label;
return $this;
}
/** Set the track color when the toggle is on. */
public function onColor(string|Color $color): static
{
$this->onColor = $color instanceof Color ? $color->value : $color;
return $this;
}
/** Set the track color when the toggle is off. */
public function offColor(string|Color $color): static
{
$this->offColor = $color instanceof Color ? $color->value : $color;
return $this;
}
/** Set the icon shown on the thumb when the toggle is on. */
public function onIcon(string|Icon|null $icon): static
{
$this->onIcon = $icon instanceof Icon ? $icon->value() : $icon;
return $this;
}
/** Set the icon shown on the thumb when the toggle is off. */
public function offIcon(string|Icon|null $icon): static
{
$this->offIcon = $icon instanceof Icon ? $icon->value() : $icon;
return $this;
}
/** Render the label inline with the toggle instead of above it (default true). */
public function inline(bool $condition = true): static
{
$this->inline = $condition;
return $this;
}
public function getOnLabel(): ?string
{
return $this->evaluate($this->onLabel);
}
public function getOffLabel(): ?string
{
return $this->evaluate($this->offLabel);
}
public function getOnColor(): string
{
return $this->onColor;
}
public function getOffColor(): string
{
return $this->offColor;
}
public function getOnIcon(): ?string
{
return $this->onIcon;
}
public function getOffIcon(): ?string
{
return $this->offIcon;
}
/**
* Background utility class for the "on" track. Delegates to the canonical
* solid-fill resolver in Foundation {@see HasColor::getSolidBgClass()} so the
* toggle shares one palette with every other solid surface instead of
* re-encoding hue rules locally.
*/
public function getOnColorClasses(): string
{
return HasColor::getSolidBgClass($this->onColor);
}
/**
* Background utility class for the "off" track. Delegates to the canonical
* soft-fill resolver {@see HasColor::getSoftBgClass()}.
*/
public function getOffColorClasses(): string
{
return HasColor::getSoftBgClass($this->offColor);
}
public function isInline(): bool
{
return $this->inline;
}
public function getStateType(): string
{
return 'bool';
}
protected function viewName(): string
{
return 'wire-forms::components.toggle';
}
}