1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Formularium\Frontend\Bootstrapvue\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 => 'primary', |
21
|
|
|
HTMLButton::COLOR_LINK => 'link', |
22
|
|
|
HTMLButton::COLOR_INFO => 'info', |
23
|
|
|
HTMLButton::COLOR_SUCCESS => 'success', |
24
|
|
|
HTMLButton::COLOR_WARNING => 'warning', |
25
|
|
|
HTMLButton::COLOR_ERROR => 'error', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
if (array_key_exists($color, $colorMap)) { |
29
|
|
|
$color = $parameters[HTMLButton::COLOR]; |
30
|
|
|
} else { |
31
|
|
|
$colors = [ |
32
|
|
|
'primary', |
33
|
|
|
'success', |
34
|
|
|
'danger', |
35
|
|
|
'warning', |
36
|
|
|
'info', |
37
|
|
|
'link', |
38
|
|
|
'light', |
39
|
|
|
'dark', |
40
|
|
|
]; |
41
|
|
|
if (!in_array($color, $colors)) { |
42
|
|
|
throw new Exception('Invalid button color.'); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
$previous->addAttribute('variant', $color); |
46
|
|
|
|
47
|
|
|
$size = $parameters[self::SIZE] ?? ''; |
48
|
|
|
switch ($size) { |
49
|
|
|
case self::SIZE_LARGE: |
50
|
|
|
$previous->addAttribute('size', 'lg'); |
51
|
|
|
break; |
52
|
|
|
case self::SIZE_SMALL: |
53
|
|
|
$previous->addAttribute('size', 'sm'); |
54
|
|
|
break; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// TODO $icon = $parameters[self::ICON] ?? ''; |
58
|
|
|
|
59
|
|
|
return $previous; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function getMetadata(): Metadata |
63
|
|
|
{ |
64
|
|
|
$metadata = HTMLButton::getMetadata(); |
65
|
|
|
$metadata->appendParameter( |
66
|
|
|
new MetadataParameter( |
67
|
|
|
HTMLButton::COLOR, |
68
|
|
|
'string', |
69
|
|
|
'Button color. Supports HTMLButton::COLOR_PRIMARY, HTMLButton::COLOR_LINK, HTMLButton::COLOR_INFO, HTMLButton::COLOR_SUCCESS, HTMLButton::COLOR_WARNING, HTMLButton::COLOR_ERROR. Default: primary.' |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
return $metadata; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|