-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCheckbox.php
113 lines (84 loc) · 2.19 KB
/
Checkbox.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
<?php
namespace Laravolt\SemanticForm\Elements;
class Checkbox extends Input
{
protected $attributes = [
'type' => 'checkbox',
];
private $checked;
protected $fieldLabel;
public function __construct($name, $value = 1)
{
parent::__construct($name);
$this->setValue($value);
}
public function render()
{
if ($this->label || $this->fieldLabel) {
$element = clone $this;
$element->label = false;
$element->fieldLabel = false;
$items = [];
if (is_string($this->fieldLabel)) {
$items[] = new Label($this->fieldLabel);
}
if ($this->label) {
$items[] = new CheckboxWrapper($element, $this->label);
}
return $this->decorateField(new Field($items))->render();
}
$result = '<input';
$result .= $this->renderAttributes();
$result .= '>';
return $result;
}
public function defaultToChecked()
{
if (!isset($this->checked)) {
$this->check();
}
return $this;
}
public function defaultToUnchecked()
{
if (!isset($this->checked)) {
$this->uncheck();
}
return $this;
}
public function defaultCheckedState($state)
{
$state ? $this->defaultToChecked() : $this->defaultToUnchecked();
return $this;
}
public function check()
{
$this->setChecked(true);
return $this;
}
public function uncheck()
{
$this->setChecked(false);
return $this;
}
public function fieldLabel($label)
{
$this->fieldLabel = $label;
return $this;
}
public function displayValue()
{
if ($this->checked) {
return '<div class="ui tiny label basic green">Ya</div>';
}
return '<div class="ui tiny label basic red">Tidak</div>';
}
public function setChecked($checked = true)
{
$this->checked = $checked;
$this->removeAttribute('checked');
if ($checked) {
$this->setAttribute('checked', 'checked');
}
}
}