Skip to content

Commit 4f3ba9f

Browse files
joseraultshafer
authored andcommitted
create datalist form element (#516)
1 parent a89479d commit 4f3ba9f

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/FormBuilder.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,50 @@ public function button($value = null, $options = [])
10121012
return $this->toHtmlString('<button' . $this->html->attributes($options) . '>' . $value . '</button>');
10131013
}
10141014

1015+
/**
1016+
* Create a datalist box field.
1017+
*
1018+
* @param string $id
1019+
* @param array $list
1020+
*
1021+
* @return \Illuminate\Support\HtmlString
1022+
*/
1023+
public function datalist($id, $list = [])
1024+
{
1025+
$this->type = 'datalist';
1026+
1027+
$attributes['id'] = $id;
1028+
1029+
$html = [];
1030+
1031+
if ($this->isAssociativeArray($list)) {
1032+
foreach ($list as $value => $display) {
1033+
$html[] = $this->option($display, $value, null, []);
1034+
}
1035+
} else {
1036+
foreach ($list as $value) {
1037+
$html[] = $this->option($value, $value, null, []);
1038+
}
1039+
}
1040+
1041+
$attributes = $this->html->attributes($attributes);
1042+
1043+
$list = implode('', $html);
1044+
1045+
return $this->toHtmlString("<datalist{$attributes}>{$list}</datalist>");
1046+
}
1047+
1048+
/**
1049+
* Determine if an array is associative.
1050+
*
1051+
* @param array $array
1052+
* @return bool
1053+
*/
1054+
protected function isAssociativeArray($array)
1055+
{
1056+
return (array_values($array) !== $array);
1057+
}
1058+
10151059
/**
10161060
* Parse the form action method.
10171061
*

tests/FormBuilderTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,24 @@ public function testFormColor()
714714
$this->assertEquals('<input class="span2" name="foo" type="color">', $form3);
715715
}
716716

717+
public function testDatalist()
718+
{
719+
// Associative array with string keys.
720+
$genders = ['M' => 'Male', 'F' => 'Female'];
721+
$datalist = $this->formBuilder->datalist('genders', $genders);
722+
$this->assertEquals('<datalist id="genders"><option value="M">Male</option><option value="F">Female</option></datalist>', $datalist);
723+
724+
// Associative array with numeric Keys
725+
$genders = [5 => 'Male', 6 => 'Female'];
726+
$datalist = $this->formBuilder->datalist('genders', $genders);
727+
$this->assertEquals('<datalist id="genders"><option value="5">Male</option><option value="6">Female</option></datalist>', $datalist);
728+
729+
// Not associative array.
730+
$genders = ['Male', 'Female'];
731+
$datalist = $this->formBuilder->datalist('genders', $genders);
732+
$this->assertEquals('<datalist id="genders"><option value="Male">Male</option><option value="Female">Female</option></datalist>', $datalist);
733+
}
734+
717735
public function testBooleanAttributes()
718736
{
719737
$input = $this->formBuilder->text('test', null, ['disabled']);

0 commit comments

Comments
 (0)