new table form html, css
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\View;
|
||||
|
||||
/**
|
||||
* Widget
|
||||
*
|
||||
* @property-read string $name
|
||||
* @property-read string $id
|
||||
* @property-read string $label
|
||||
* @property-read string|null $help
|
||||
* @property-read string|null $value
|
||||
* @property-read string $attributes
|
||||
*/
|
||||
class Widget
|
||||
{
|
||||
private $attributes = [],
|
||||
$id, $name, $label, $value,
|
||||
$viewName,
|
||||
$help,
|
||||
$style = [];
|
||||
|
||||
public function __construct($viewName, $name, $label)
|
||||
{
|
||||
$this->id = 'field-'.$name;
|
||||
$this->name = $name;
|
||||
$this->label = $label;
|
||||
$this->viewName = $viewName;
|
||||
}
|
||||
|
||||
// setting attributes via magic method
|
||||
public function __call($method, $args)
|
||||
{
|
||||
static $cssProps = [
|
||||
'height', 'minHeight', 'maxHeight'
|
||||
];
|
||||
|
||||
if (empty($args)) $args = [''];
|
||||
|
||||
$arg = $args[0];
|
||||
|
||||
// css
|
||||
$lccamel = $method;
|
||||
if (in_array($lccamel, $cssProps)) {
|
||||
return $this->css(kebab_case($lccamel), $arg);
|
||||
}
|
||||
|
||||
if (property_exists($this, $method)) {
|
||||
$this->$method = $arg;
|
||||
}
|
||||
else {
|
||||
$this->attributes[$method] = $arg;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function css($prop, $val)
|
||||
{
|
||||
$this->style[$prop] = $val;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function __get($name)
|
||||
{
|
||||
if ($name == 'attributes') {
|
||||
return $this->compileAttribs();
|
||||
}
|
||||
|
||||
if ($name == 'value') {
|
||||
return old($this->name, $this->value);
|
||||
}
|
||||
|
||||
if (property_exists($this, $name)) {
|
||||
return $this->$name;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function compileAttribs()
|
||||
{
|
||||
// compile attribs string
|
||||
$attribs_s = array_reduce(array_keys($this->attributes), function ($carry, $key) {
|
||||
return $carry . ' ' . $key . '="' . e($this->attributes[$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] . '; ';
|
||||
}, ''))).'"';
|
||||
}
|
||||
|
||||
return trim($attribs_s);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('form.'.$this->viewName)->with(['w' => $this]);
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return (string) $this->render();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\View;
|
||||
|
||||
class WidgetFactory
|
||||
{
|
||||
public function text($name, $label)
|
||||
{
|
||||
return new Widget('input', $name, $label);
|
||||
}
|
||||
|
||||
public function number($name, $label)
|
||||
{
|
||||
return new Widget('number', $name, $label);
|
||||
}
|
||||
|
||||
public function email($name, $label)
|
||||
{
|
||||
return new Widget('email', $name, $label);
|
||||
}
|
||||
|
||||
public function textarea($name, $label)
|
||||
{
|
||||
return (new Widget('textarea', $name, $label))->minHeight('4em');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user