|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Enjoys\Forms\Elements; |
|
6
|
|
|
|
|
7
|
|
|
use Enjoys\Forms\AttributeFactory; |
|
8
|
|
|
use Enjoys\Forms\Element; |
|
9
|
|
|
use Enjoys\Forms\Interfaces\Descriptionable; |
|
10
|
|
|
use Enjoys\Forms\Interfaces\Fillable; |
|
11
|
|
|
use Enjoys\Forms\Interfaces\Ruleable; |
|
12
|
|
|
use Enjoys\Forms\Traits\Description; |
|
13
|
|
|
use Enjoys\Forms\Traits\Fill; |
|
14
|
|
|
use Enjoys\Forms\Traits\Rules; |
|
15
|
|
|
|
|
16
|
|
|
class Datalist extends Element implements Fillable, Ruleable, Descriptionable |
|
17
|
|
|
{ |
|
18
|
|
|
use Fill; |
|
19
|
|
|
use Description; |
|
20
|
|
|
use Rules; |
|
21
|
|
|
|
|
22
|
|
|
protected string $type = 'option'; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
4 |
|
public function __construct(string $name, ?string $title = null) |
|
26
|
|
|
{ |
|
27
|
4 |
|
parent::__construct($name, $title); |
|
28
|
4 |
|
$this->setAttribute(AttributeFactory::create('list', $name . '-list')); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @psalm-suppress PossiblyNullReference |
|
33
|
|
|
*/ |
|
34
|
3 |
|
public function baseHtml(): string |
|
35
|
|
|
{ |
|
36
|
3 |
|
$return = sprintf( |
|
37
|
3 |
|
"<input%s>\n<datalist id='%s'>\n", |
|
38
|
3 |
|
$this->getAttributesString(), |
|
39
|
3 |
|
$this->getAttribute('list')->getValueString() |
|
40
|
3 |
|
); |
|
41
|
|
|
|
|
42
|
3 |
|
foreach ($this->getElements() as $data) { |
|
43
|
|
|
//$return .= "<option value=\"{$data->getLabel()}\">"; |
|
44
|
3 |
|
$data->setAttribute(AttributeFactory::create('value', $data->getLabel())); |
|
45
|
3 |
|
$data->setLabel(null); |
|
46
|
3 |
|
$return .= $data->baseHtml() . PHP_EOL; |
|
47
|
|
|
} |
|
48
|
3 |
|
$return .= "</datalist>"; |
|
49
|
3 |
|
return $return; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|