form improvements
This commit is contained in:
+26
-10
@@ -12,14 +12,17 @@ namespace App\View;
|
||||
* @property-read string|null $help
|
||||
* @property-read string|null $value
|
||||
* @property-read string $attributes
|
||||
* @property-read int $labelCols
|
||||
* @property-read int $fieldCols
|
||||
*/
|
||||
class Widget
|
||||
{
|
||||
private $attributes = [],
|
||||
protected $attributesArray = [],
|
||||
$id, $name, $label, $value,
|
||||
$viewName,
|
||||
$help,
|
||||
$style = [];
|
||||
$labelCols, $fieldCols,
|
||||
$styleArray = [];
|
||||
|
||||
public function __construct($viewName, $name, $label)
|
||||
{
|
||||
@@ -36,7 +39,7 @@ class Widget
|
||||
'height', 'minHeight', 'maxHeight'
|
||||
];
|
||||
|
||||
if (empty($args)) $args = [''];
|
||||
if (empty($args)) $args = [null];
|
||||
|
||||
$arg = $args[0];
|
||||
|
||||
@@ -50,7 +53,7 @@ class Widget
|
||||
$this->$method = $arg;
|
||||
}
|
||||
else {
|
||||
$this->attributes[$method] = $arg;
|
||||
$this->attributesArray[$method] = $arg;
|
||||
}
|
||||
|
||||
return $this;
|
||||
@@ -58,7 +61,15 @@ class Widget
|
||||
|
||||
public function css($prop, $val)
|
||||
{
|
||||
$this->style[$prop] = $val;
|
||||
$this->styleArray[$prop] = $val;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** Apply a given layout (bootstrap grid, 12 cols total) */
|
||||
public function layout($labelCols, $fieldCols)
|
||||
{
|
||||
$this->labelCols = $labelCols;
|
||||
$this->fieldCols = $fieldCols;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -82,14 +93,19 @@ class Widget
|
||||
public function compileAttribs()
|
||||
{
|
||||
// compile attribs string
|
||||
$attribs_s = array_reduce(array_keys($this->attributes), function ($carry, $key) {
|
||||
return $carry . ' ' . $key . '="' . e($this->attributes[$key]) . '"';
|
||||
$attribs_s = array_reduce(array_keys($this->attributesArray), function ($carry, $key) {
|
||||
if ($this->attributesArray[$key] === null) {
|
||||
// simple attribs like 'required'
|
||||
return $carry . ' ' . $key;
|
||||
} else {
|
||||
return $carry . ' ' . $key . '="' . e($this->attributesArray[$key]) . '"';
|
||||
}
|
||||
}, '');
|
||||
|
||||
// add a compiled list of styles
|
||||
if (!empty($this->style)) {
|
||||
$attribs_s .= 'style="'.trim(e(array_reduce(array_keys($this->style), function ($carry, $key) {
|
||||
return $carry . $key . ': ' . $this->style[$key] . '; ';
|
||||
if (!empty($this->styleArray)) {
|
||||
$attribs_s .= 'style="'.trim(e(array_reduce(array_keys($this->styleArray), function ($carry, $key) {
|
||||
return $carry . $key . ': ' . $this->styleArray[$key] . '; ';
|
||||
}, ''))).'"';
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user