form improvements
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\View;
|
||||
|
||||
|
||||
class CheckboxWidget extends Widget
|
||||
{
|
||||
public function checked($condition=true)
|
||||
{
|
||||
$this->value = (bool)$condition;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
+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] . '; ';
|
||||
}, ''))).'"';
|
||||
}
|
||||
|
||||
|
||||
@@ -5,23 +5,48 @@ namespace App\View;
|
||||
|
||||
class WidgetFactory
|
||||
{
|
||||
private $labelCols = 3, $fieldCols = 8;
|
||||
|
||||
/** Configure layout used for future elements */
|
||||
public function setLayout($labelCols, $fieldCols)
|
||||
{
|
||||
$this->labelCols = $labelCols;
|
||||
$this->fieldCols = $fieldCols;
|
||||
}
|
||||
|
||||
private function baseWidget($view, $name, $label)
|
||||
{
|
||||
return (new Widget($view, $name, $label))->layout($this->labelCols, $this->fieldCols);
|
||||
}
|
||||
|
||||
public function text($name, $label)
|
||||
{
|
||||
return new Widget('input', $name, $label);
|
||||
return $this->baseWidget('input', $name, $label);
|
||||
}
|
||||
|
||||
public function password($name, $label)
|
||||
{
|
||||
return $this->baseWidget('input', $name, $label)->type('password');
|
||||
}
|
||||
|
||||
public function number($name, $label)
|
||||
{
|
||||
return new Widget('number', $name, $label);
|
||||
return $this->baseWidget('input', $name, $label)->type('number');
|
||||
}
|
||||
|
||||
public function email($name, $label)
|
||||
{
|
||||
return new Widget('email', $name, $label);
|
||||
return $this->baseWidget('input', $name, $label)->type('email');
|
||||
}
|
||||
|
||||
public function checkbox($name, $label)
|
||||
{
|
||||
return (new CheckboxWidget('checkbox', $name, $label))
|
||||
->layout($this->labelCols, $this->fieldCols);
|
||||
}
|
||||
|
||||
public function textarea($name, $label)
|
||||
{
|
||||
return (new Widget('textarea', $name, $label))->minHeight('4em');
|
||||
return $this->baseWidget('textarea', $name, $label)->minHeight('4em');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user