1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Formularium\Frontend\Bulma\Element; |
4
|
|
|
|
5
|
|
|
use Formularium\Element; |
6
|
|
|
use Formularium\Exception\Exception; |
7
|
|
|
use Formularium\HTMLNode; |
8
|
|
|
use Formularium\Frontend\HTML\Element\Button as HTMLButton; |
9
|
|
|
use Formularium\Metadata; |
10
|
|
|
use Formularium\MetadataParameter; |
11
|
|
|
|
12
|
|
|
class Button extends Element |
13
|
|
|
{ |
14
|
|
|
public function render(array $parameters, HTMLNode $previous): HTMLNode |
15
|
|
|
{ |
16
|
|
|
$previous->addAttribute('class', 'button'); |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
$color = $parameters[HTMLButton::COLOR] ?? ''; |
20
|
|
|
$colorMap = [ |
21
|
|
|
HTMLButton::COLOR_PRIMARY => 'is-primary', |
22
|
|
|
HTMLButton::COLOR_LINK => 'is-link', |
23
|
|
|
HTMLButton::COLOR_INFO => 'is-info', |
24
|
|
|
HTMLButton::COLOR_SUCCESS => 'is-success', |
25
|
|
|
HTMLButton::COLOR_WARNING => 'is-warning', |
26
|
|
|
HTMLButton::COLOR_ERROR => 'is-error', |
27
|
|
|
]; |
28
|
|
|
if (array_key_exists($color, $colorMap)) { |
29
|
|
|
$color = $colorMap[$color]; |
30
|
|
|
} else { |
31
|
|
|
$colors = [ |
32
|
|
|
'is-primary', |
33
|
|
|
'is-success', |
34
|
|
|
'is-danger', |
35
|
|
|
'is-warning', |
36
|
|
|
'is-info', |
37
|
|
|
'is-light', |
38
|
|
|
'is-dark', |
39
|
|
|
'is-link', |
40
|
|
|
'is-black', |
41
|
|
|
'is-white', |
42
|
|
|
]; |
43
|
|
|
if (!in_array($color, $colors)) { |
44
|
|
|
throw new Exception('Invalid button color.'); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
$previous->addAttribute('class', $color); |
48
|
|
|
|
49
|
|
|
$size = $parameters[self::SIZE] ?? ''; |
50
|
|
|
switch ($size) { |
51
|
|
|
case self::SIZE_LARGE: |
52
|
|
|
$previous->addAttribute('class', 'is-large'); |
53
|
|
|
break; |
54
|
|
|
case self::SIZE_SMALL: |
55
|
|
|
$previous->addAttribute('class', 'is-small'); |
56
|
|
|
break; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$icon = $parameters[self::ICON] ?? ''; |
60
|
|
|
if ($icon) { |
61
|
|
|
$iconData = []; |
62
|
|
|
$iconPack = $parameters[self::ICON_PACK] ?? ''; |
63
|
|
|
if ($iconPack) { |
64
|
|
|
$iconData[] = $iconPack; |
65
|
|
|
} |
66
|
|
|
$iconData[] = $icon; |
67
|
|
|
$iconElement = HTMLNode::factory( |
68
|
|
|
'span', |
69
|
|
|
[ |
70
|
|
|
'class' => "icon is-small is-left", |
71
|
|
|
'aria-hidden' => "true" |
72
|
|
|
], |
73
|
|
|
[ |
74
|
|
|
HTMLNode::factory( |
75
|
|
|
'i', |
76
|
|
|
[ 'class' => $iconData ], |
77
|
|
|
[] |
78
|
|
|
) |
79
|
|
|
] |
80
|
|
|
); |
81
|
|
|
$previous->addAttribute('class', 'has-icons-left'); |
82
|
|
|
$previous->appendContent($iconElement); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $previous; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public static function getMetadata(): Metadata |
89
|
|
|
{ |
90
|
|
|
$metadata = HTMLButton::getMetadata(); |
91
|
|
|
$metadata->appendParameter( |
92
|
|
|
new MetadataParameter( |
93
|
|
|
HTMLButton::COLOR, |
94
|
|
|
'string', |
95
|
|
|
'Button color. Supports HTMLButton::COLOR_PRIMARY, HTMLButton::COLOR_LINK, HTMLButton::COLOR_INFO, HTMLButton::COLOR_SUCCESS, HTMLButton::COLOR_WARNING, HTMLButton::COLOR_ERROR and any Bulma button color classes. Default: primary.' |
96
|
|
|
) |
97
|
|
|
); |
98
|
|
|
return $metadata; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|