Skip to content

Commit 0281239

Browse files
committed
update
1 parent 3d61afc commit 0281239

File tree

3 files changed

+78
-7
lines changed

3 files changed

+78
-7
lines changed

src/Bulma/Input.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ public function renderDom(\DOMDocument $doc): \DOMElement
2929
$this->addClass('button');
3030
$this->addClass('is-primary');
3131
}
32-
$wrapper = $doc->createElement('div');
33-
$wrapper->setAttribute('class', 'control');
3432
$input = parent::renderDom($doc);
35-
$wrapper->appendChild($input);
33+
if ($input->tagName == 'div') {
34+
$wrapper = $input;
35+
} else {
36+
$wrapper = $doc->createElement('div');
37+
$wrapper->appendChild($input);
38+
}
39+
$wrapper->setAttribute('class', 'control');
3640
return $wrapper;
3741
}
3842
}

src/Input.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,15 @@ public function renderDom(DOMDocument $doc): DOMElement
172172
$datalist->setAttribute('id', $this->name . '-options');
173173
foreach ($this->options as $value => $label) {
174174
$option = $doc->createElement('option', $label);
175-
if (is_int($value)) {
176-
$option->setAttribute('value', $label);
177-
} else {
175+
if (!is_int($value)) {
178176
$option->setAttribute('value', $value);
179177
}
180178
$datalist->appendChild($option);
181179
}
182-
$doc->appendChild($datalist);
180+
$wrapper = $doc->createElement('div');
181+
$wrapper->appendChild($input);
182+
$wrapper->appendChild($datalist);
183+
return $wrapper;
183184
}
184185
return $input;
185186
}

tests/Forms/SourceFormTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace MintyPHP\Tests\Forms;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
use MintyPHP\Form\Form;
8+
use MintyPHP\Form\Elements as E;
9+
use MintyPHP\Form\Validator\Validators as V;
10+
11+
class SourceFormTest extends TestCase
12+
{
13+
private function createForm(string $style): Form
14+
{
15+
$sources = [
16+
'Ad',
17+
'Blog',
18+
'Magazine',
19+
'Newspaper',
20+
];
21+
E::$style = $style;
22+
return E::form([
23+
E::field(E::text('sources')->options($sources), E::label('How did you hear about us?'), [V::required('Field cannot be empty')]),
24+
]);
25+
}
26+
27+
public function testRenderForm(): void
28+
{
29+
$form = $this->createForm('none');
30+
$lines = [
31+
'<div>',
32+
' <label for="sources">How did you hear about us?</label>',
33+
' <div>',
34+
' <input id="sources" type="text" name="sources" value="" list="sources-options"/>',
35+
' <datalist id="sources-options">',
36+
' <option>Ad</option>',
37+
' <option>Blog</option>',
38+
' <option>Magazine</option>',
39+
' <option>Newspaper</option>',
40+
' </datalist>',
41+
' </div>',
42+
'</div>',
43+
];
44+
$this->assertEquals(implode("\n", $lines), $form->toString(false, false));
45+
}
46+
47+
public function testRenderBulma(): void
48+
{
49+
$form = $this->createForm('bulma');
50+
$lines = [
51+
'<div class="field">',
52+
' <label class="label" for="sources">How did you hear about us?</label>',
53+
' <div class="control">',
54+
' <input id="sources" class="input" type="text" name="sources" value="" list="sources-options"/>',
55+
' <datalist id="sources-options">',
56+
' <option>Ad</option>',
57+
' <option>Blog</option>',
58+
' <option>Magazine</option>',
59+
' <option>Newspaper</option>',
60+
' </datalist>',
61+
' </div>',
62+
'</div>',
63+
];
64+
$this->assertEquals(implode("\n", $lines), $form->toString(false, false));
65+
}
66+
}

0 commit comments

Comments
 (0)