@@ -103,9 +103,7 @@ public static function generateId(string $prefix = 'i'): string
103103 $ counter = ++static ::$ generateIdCounter [$ prefix ];
104104 } else {
105105 $ counter = 1 ;
106- static ::$ generateIdCounter = [
107- $ prefix => $ counter ,
108- ];
106+ static ::$ generateIdCounter = [$ prefix => $ counter ];
109107 }
110108 return $ prefix . $ counter ;
111109 }
@@ -792,6 +790,9 @@ public static function checkbox(string $name, bool $checked = false, array $opti
792790 * When this option is specified, the checkbox will be enclosed by a label tag.
793791 * - labelOptions: array, the HTML attributes for the label tag. Do not set this option unless you set the "label"
794792 * option.
793+ * - wrapInput: bool, use when has label.
794+ * if `wrapInput` is true result will be `<label><input> Label</label>`,
795+ * else `<input> <label>Label</label>`
795796 *
796797 * The rest of the options will be rendered as the attributes of the resulting checkbox tag. The values will be
797798 * HTML-encoded using {@see encode()}. If a value is null, the corresponding attribute will not be rendered.
@@ -823,15 +824,27 @@ private static function booleanInput(string $type, string $name, bool $checked,
823824 $ hidden = '' ;
824825 }
825826
826- if (isset ($ options ['label ' ])) {
827- $ label = $ options ['label ' ];
828- $ labelOptions = $ options ['labelOptions ' ] ?? [];
829- unset($ options ['label ' ], $ options ['labelOptions ' ]);
830- $ content = static ::label (static ::input ($ type , $ name , $ value , $ options ) . ' ' . $ label , null , $ labelOptions );
831- return $ hidden . $ content ;
827+ $ label = $ options ['label ' ] ?? null ;
828+ $ labelOptions = $ options ['labelOptions ' ] ?? [];
829+ $ wrapInput = $ options ['wrapInput ' ] ?? true ;
830+ unset($ options ['label ' ], $ options ['labelOptions ' ], $ options ['wrapInput ' ]);
831+
832+ if (empty ($ label )) {
833+ return $ hidden . static ::input ($ type , $ name , $ value , $ options );
834+ }
835+
836+ if ($ wrapInput ) {
837+ $ input = static ::input ($ type , $ name , $ value , $ options );
838+ return $ hidden . static ::label ($ input . ' ' . $ label , null , $ labelOptions );
832839 }
833840
834- return $ hidden . static ::input ($ type , $ name , $ value , $ options );
841+ if (!isset ($ options ['id ' ])) {
842+ $ options ['id ' ] = static ::generateId ();
843+ }
844+ return $ hidden .
845+ static ::input ($ type , $ name , $ value , $ options ) .
846+ ' ' .
847+ static ::label ($ label , $ options ['id ' ], $ labelOptions );
835848 }
836849
837850 /**
0 commit comments