-
Notifications
You must be signed in to change notification settings - Fork 147
/
InputgroupTrait.php
166 lines (144 loc) · 5.05 KB
/
InputgroupTrait.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
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
155
156
157
158
159
160
161
162
163
164
165
166
<?php
declare(strict_types=1);
namespace BootstrapUI\View\Widget;
use BootstrapUI\View\Helper\OptionsAwareTrait;
use Cake\View\Form\ContextInterface;
trait InputgroupTrait
{
use OptionsAwareTrait;
/**
* Render a widget with input group wrapper if requried.
*
* Apart from the standard data keys used by a widget you can use following
* extra keys:
*
* - `append` Append addon to input.
* - `prepend` Prepend addon to input.
* - `size` Append/Prepend can have option size (sm/lg) for wrapping container
* - `input` If set it will be used as input tag between append and prepend addons.
* By default parent::render() will be called to get input tag.
*
* @param array $data The data to build an input with.
* @param \Cake\View\Form\ContextInterface $context The current form context.
* @return string
*/
protected function _withInputGroup(array $data, ContextInterface $context): string
{
$data += [
'type' => null,
'prepend' => null,
'append' => null,
'injectFormControl' => true,
'injectErrorClass' => null,
'input' => null,
];
if ($data['injectFormControl'] && $data['type'] !== 'hidden') {
$data = $this->injectClasses('form-control', $data);
}
$prepend = $data['prepend'];
$append = $data['append'];
$errorClass = $data['injectErrorClass'];
unset($data['append'], $data['prepend'], $data['injectFormControl'], $data['injectErrorClass']);
if (isset($data['input'])) {
$input = $data['input'];
unset($data['input']);
} else {
$input = parent::render($data, $context);
}
$attrs = [];
if ($prepend) {
$prepend = $this->_checkForOptions($prepend);
$attrs = $this->_processOptions($prepend, $attrs);
$data['inputClass'] = 'input-group-prepend';
$prepend = $this->_addon($prepend['content'], $data);
}
if ($append) {
$append = $this->_checkForOptions($append);
$attrs = $this->_processOptions($append, $attrs);
$data['inputClass'] = 'input-group-append';
$append = $this->_addon($append['content'], $data);
}
if ($prepend || $append) {
if (
$errorClass &&
$context->hasError($data['fieldName'])
) {
$attrs['class'][] = $errorClass;
}
$input = $this->_templates->format('inputGroupContainer', [
'attrs' => $this->_templates->formatAttributes($attrs),
'append' => $append,
'prepend' => $prepend,
'content' => $input,
'templateVars' => $data['templateVars'],
]);
}
return $input;
}
/**
* Get addon HTML.
*
* @param string $addon Addon content.
* @param array $data Widget data.
* @return string
*/
protected function _addon(string $addon, array $data): string
{
if ($this->_isButton($addon)) {
$element = $addon;
} else {
$element = $this->_templates->format('inputGroupText', [
'content' => $addon,
]);
}
$addon = $this->_templates->format('inputGroupAddon', [
'class' => $data['inputClass'],
'content' => $element,
'templateVars' => $data['templateVars'],
]);
return $addon;
}
/**
* Checks if an HTML markup is for a button.
*
* @param string $html Markup to check.
* @return bool TRUE if it's a button.
*/
protected function _isButton(string $html): bool
{
return strpos($html, '<button') !== false || strpos($html, 'type="submit"') !== false;
}
/**
* Checks, if prepend/append has options and formats them
*
* @param array|string $attachment prepend or append element. Can be a string or array, if contains options
* @return array
*/
protected function _checkForOptions($attachment): array
{
$ret = [];
if (is_array($attachment)) {
$ret['content'] = $attachment[0];
$ret['options'] = $attachment[1];
} else {
$ret['content'] = $attachment;
}
return $ret;
}
/**
* Processes options given by prepend/append and adds them to wrapping container
*
* @param array $attachment prepend/append content and options
* @param array $attrs of wrapping container
* @return array attrs with classes
*/
protected function _processOptions(array $attachment, array $attrs): array
{
$attrs['class'][] = 'input-group';
if (isset($attachment['options']['size']) && $attachment['options']['size'] != null) {
$attrs['class'][] = $this->genClassName('input-group', $attachment['options']['size']);
$attrs['class'] = $this->_toClassArray($attrs['class']);
}
return $attrs;
}
}