|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Formularium\Frontend\Buefy\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->setTag('b-button'); |
|
17
|
|
|
|
|
18
|
|
|
$color = $parameters[HTMLButton::COLOR] ?? ''; |
|
19
|
|
|
$colorMap = [ |
|
20
|
|
|
HTMLButton::COLOR_PRIMARY => 'is-primary', |
|
21
|
|
|
HTMLButton::COLOR_LINK => 'is-link', |
|
22
|
|
|
HTMLButton::COLOR_INFO => 'is-info', |
|
23
|
|
|
HTMLButton::COLOR_SUCCESS => 'is-success', |
|
24
|
|
|
HTMLButton::COLOR_WARNING => 'is-warning', |
|
25
|
|
|
HTMLButton::COLOR_ERROR => 'is-error', |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
if (array_key_exists($color, $colorMap)) { |
|
29
|
|
|
$color = $parameters[HTMLButton::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('type', $color); |
|
48
|
|
|
|
|
49
|
|
|
$size = $parameters[self::SIZE] ?? ''; |
|
50
|
|
|
switch ($size) { |
|
51
|
|
|
case self::SIZE_LARGE: |
|
52
|
|
|
$previous->addAttribute('size', 'is-large'); |
|
53
|
|
|
break; |
|
54
|
|
|
case self::SIZE_SMALL: |
|
55
|
|
|
$previous->addAttribute('size', 'is-small'); |
|
56
|
|
|
break; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$icon = $parameters[self::ICON] ?? ''; |
|
60
|
|
|
if ($icon) { |
|
61
|
|
|
$previous->addAttribute('icon-left', $icon); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $previous; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public static function getMetadata(): Metadata |
|
68
|
|
|
{ |
|
69
|
|
|
$metadata = HTMLButton::getMetadata(); |
|
70
|
|
|
$metadata->appendParameter( |
|
71
|
|
|
new MetadataParameter( |
|
72
|
|
|
HTMLButton::COLOR, |
|
73
|
|
|
'string', |
|
74
|
|
|
'Button color. Supports HTMLButton::COLOR_PRIMARY, HTMLButton::COLOR_LINK, HTMLButton::COLOR_INFO, HTMLButton::COLOR_SUCCESS, HTMLButton::COLOR_WARNING, HTMLButton::COLOR_ERROR. Default: primary.' |
|
75
|
|
|
), |
|
76
|
|
|
); |
|
77
|
|
|
return $metadata; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|