Skip to content

Commit 16b4ad0

Browse files
committed
TermInput: Allow to enable read-only terms and style them
1 parent a5f8542 commit 16b4ad0

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed

asset/css/compat.less

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,13 @@ form.icinga-form .control-group {
8080
width: 0;
8181
}
8282

83-
input[type="text"] {
83+
input {
8484
flex: unset;
8585
width: 100%;
86+
87+
&[type="button"] {
88+
background-color: @default-input-bg;
89+
}
8690
}
8791
}
8892
}

asset/css/search-base.less

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,28 @@
209209
outline-width: 3px;
210210
outline-offset: ~"calc(-@{labelPad} + 3px)";
211211
}
212+
213+
&.read-only {
214+
label {
215+
position: relative;
216+
217+
input[type="button"] {
218+
padding-left: 1.5em;
219+
220+
+ i {
221+
position: absolute;
222+
display: none;
223+
top: .5em;
224+
left: .5em;
225+
}
226+
}
227+
228+
input[type="button"]:hover + i,
229+
input[type="button"]:focus + i {
230+
display: revert;
231+
}
232+
}
233+
}
212234
}
213235

214236
.search-suggestions {

src/FormElement/TermInput.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class TermInput extends FieldsetElement
4040
/** @var bool Whether term direction is vertical */
4141
protected $verticalTermDirection = false;
4242

43+
/** @var bool Whether registered terms are read-only */
44+
protected $readOnly = false;
45+
4346
/** @var array Changes to transmit to the client */
4447
protected $changes = [];
4548

@@ -103,6 +106,30 @@ public function getTermDirection(): ?string
103106
return $this->verticalTermDirection ? 'vertical' : null;
104107
}
105108

109+
/**
110+
* Set whether registered terms are read-only
111+
*
112+
* @param bool $state
113+
*
114+
* @return $this
115+
*/
116+
public function setReadOnly(bool $state = true): self
117+
{
118+
$this->readOnly = $state;
119+
120+
return $this;
121+
}
122+
123+
/**
124+
* Get whether registered terms are read-only
125+
*
126+
* @return bool
127+
*/
128+
public function getReadOnly(): bool
129+
{
130+
return $this->readOnly;
131+
}
132+
106133
/**
107134
* Set terms
108135
*
@@ -415,6 +442,7 @@ public function getValueAttribute()
415442
'data-with-multi-completion' => true,
416443
'data-no-auto-submit-on-remove' => true,
417444
'data-term-direction' => $this->getTermDirection(),
445+
'data-read-only-terms' => $this->getReadOnly(),
418446
'data-data-input' => '#' . $dataInputId,
419447
'data-term-input' => '#' . $termInputId,
420448
'data-term-container' => '#' . $termContainer->getAttribute('id')->getValue(),
@@ -436,7 +464,11 @@ public function getValueAttribute()
436464

437465
$mainInput->prependWrapper((new HtmlElement(
438466
'div',
439-
Attributes::create(['class' => ['term-input-area', $this->getTermDirection()]]),
467+
Attributes::create(['class' => [
468+
'term-input-area',
469+
$this->getTermDirection(),
470+
$this->getReadOnly() ? 'read-only' : null
471+
]]),
440472
$termContainer,
441473
new HtmlElement('label', null, $mainInput)
442474
)));

src/FormElement/TermInput/TermContainer.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use ipl\Html\BaseHtmlElement;
77
use ipl\Html\HtmlElement;
88
use ipl\Web\FormElement\TermInput;
9+
use ipl\Web\Widget\Icon;
910

1011
class TermContainer extends BaseHtmlElement
1112
{
@@ -28,27 +29,33 @@ public function __construct(TermInput $input)
2829

2930
protected function assemble()
3031
{
32+
$inputType = $this->input->getReadOnly() ? 'button' : 'text';
3133
foreach ($this->input->getTerms() as $i => $term) {
32-
$label = $term->getLabel() ?: $term->getSearchValue();
34+
$value = $term->getLabel() ?: $term->getSearchValue();
3335

34-
$this->addHtml(new HtmlElement(
36+
$label = new HtmlElement(
3537
'label',
3638
Attributes::create([
3739
'class' => $term->getClass(),
3840
'data-search' => $term->getSearchValue(),
39-
'data-label' => $label,
41+
'data-label' => $value,
4042
'data-index' => $i
4143
]),
4244
new HtmlElement(
4345
'input',
4446
Attributes::create([
45-
'type' => 'text',
46-
'value' => $label,
47+
'type' => $inputType,
48+
'value' => $value,
4749
'pattern' => $term->getPattern(),
4850
'data-invalid-msg' => $term->getMessage()
4951
])
5052
)
51-
));
53+
);
54+
if ($inputType === 'button') {
55+
$label->addHtml(new Icon('trash'));
56+
}
57+
58+
$this->addHtml($label);
5259
}
5360
}
5461
}

0 commit comments

Comments
 (0)