File tree Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -1012,6 +1012,50 @@ public function button($value = null, $options = [])
1012
1012
return $ this ->toHtmlString ('<button ' . $ this ->html ->attributes ($ options ) . '> ' . $ value . '</button> ' );
1013
1013
}
1014
1014
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
+
1015
1059
/**
1016
1060
* Parse the form action method.
1017
1061
*
Original file line number Diff line number Diff line change @@ -714,6 +714,24 @@ public function testFormColor()
714
714
$ this ->assertEquals ('<input class="span2" name="foo" type="color"> ' , $ form3 );
715
715
}
716
716
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
+
717
735
public function testBooleanAttributes ()
718
736
{
719
737
$ input = $ this ->formBuilder ->text ('test ' , null , ['disabled ' ]);
You can’t perform that action at this time.
0 commit comments