Forráskód Böngészése

增加表单多列布局 Form::cloumn

jqh 5 éve
szülő
commit
c147702a88

+ 2 - 0
resources/views/form/container.blade.php

@@ -16,6 +16,8 @@
                         {!! $row->render() !!}
                     @endforeach
                 </div>
+            @elseif($form->layout()->hasColumns())
+                {!! $form->layout()->build() !!}
             @else
                 @foreach($form->fields() as $field)
                     {!! $field->render() !!}

+ 2 - 0
resources/views/widgets/form.blade.php

@@ -22,6 +22,8 @@
                         @endif
                     @endforeach
                 </div>
+            @elseif($layout)
+                {!! $layout->build() !!}
             @else
                 @foreach($fields as $field)
                     {!! $field->render() !!}

+ 16 - 0
src/Form.php

@@ -297,6 +297,7 @@ class Form implements Renderable
         $field->setForm($this);
 
         $this->builder->fields()->push($field);
+        $this->builder->layout()->addField($field);
 
         $width = $this->builder->getWidth();
 
@@ -1604,6 +1605,21 @@ class Form implements Renderable
         return $this;
     }
 
+    /**
+     * @param int|float $width
+     * @param Closure   $callback
+     *
+     * @return $this
+     */
+    public function column($width, \Closure $callback)
+    {
+        $this->builder->layout()->onlyColumn($width, function () use ($callback) {
+            $callback($this);
+        });
+
+        return $this;
+    }
+
     /**
      * @param int $width
      *

+ 11 - 5
src/Form/Builder.php

@@ -843,13 +843,19 @@ class Builder
             'steps'      => $this->stepBuilder,
         ];
 
-        $this->layout->prepend(
-            $this->defaultBlockWidth,
-            $this->doWrap(view($this->view, $data))
-        );
+        if ($this->layout->hasColumns()) {
+            $content = $this->doWrap(view($this->view, $data));
+        } else {
+            $this->layout->prepend(
+                $this->defaultBlockWidth,
+                $this->doWrap(view($this->view, $data))
+            );
+
+            $content = $this->layout->build();
+        }
 
         return <<<EOF
-{$open} {$this->layout->build()} {$this->close()}
+{$open} {$content} {$this->close()}
 EOF;
     }
 

+ 43 - 5
src/Form/Layout.php

@@ -2,13 +2,14 @@
 
 namespace Dcat\Admin\Form;
 
+use Dcat\Admin\Widgets\Form as WidgetForm;
 use Dcat\Admin\Form;
 use Dcat\Admin\Layout\Column;
 
 class Layout
 {
     /**
-     * @var Form
+     * @var Form|WidgetForm
      */
     protected $form;
 
@@ -17,32 +18,69 @@ class Layout
      */
     protected $columns = [];
 
-    public function __construct(Form $form)
+    /**
+     * @var array
+     */
+    protected $currentFields = [];
+
+    /**
+     * @var bool
+     */
+    protected $hasColumn = false;
+
+    public function __construct($form)
     {
         $this->form = $form;
     }
 
+    public function addField(Field $field)
+    {
+        $this->currentFields[] = $field;
+    }
+
+    public function hasColumns()
+    {
+        return $this->hasColumn;
+    }
+
     /**
      * @param int   $width   1~12
      * @param mixed $content
      */
-    public function column(int $width, $content)
+    public function onlyColumn($width, $content)
     {
         $width = $width < 1 ? round(12 * $width) : $width;
 
+        $this->hasColumn = true;
+
+        $this->currentFields = [];
+
         $column = new Column($content, $width);
 
         $this->columns[] = $column;
+
+        foreach ($this->currentFields as $field) {
+            $column->append($field);
+        }
     }
 
     /**
-     * @param int   $width
+     * @param int   $width   1~12
      * @param mixed $content
      */
-    public function prepend(int $width, $content)
+    public function column($width, $content)
     {
         $width = $width < 1 ? round(12 * $width) : $width;
 
+        $this->columns[] = new Column($content, $width);
+    }
+
+    /**
+     * @param int   $width
+     * @param mixed $content
+     */
+    public function prepend(int $width, $content)
+    {
         $column = new Column($content, $width);
 
         array_unshift($this->columns, $column);

+ 34 - 0
src/Widgets/Form.php

@@ -8,6 +8,7 @@ use Dcat\Admin\Form\Concerns\HandleCascadeFields;
 use Dcat\Admin\Form\Concerns\HasRows;
 use Dcat\Admin\Form\Concerns\HasTabs;
 use Dcat\Admin\Form\Field;
+use Dcat\Admin\Form\Layout;
 use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Traits\HasAuthorization;
 use Dcat\Admin\Traits\HasFormResponse;
@@ -102,6 +103,11 @@ class Form implements Renderable
      */
     protected $fields;
 
+    /**
+     * @var Layout
+     */
+    protected $layout;
+
     /**
      * @var array
      */
@@ -318,6 +324,30 @@ class Form implements Renderable
         return $this->fields;
     }
 
+
+    /**
+     * @param int|float $width
+     * @param Closure   $callback
+     *
+     * @return $this
+     */
+    public function column($width, \Closure $callback)
+    {
+        $this->layout()->onlyColumn($width, function () use ($callback) {
+            $callback($this);
+        });
+
+        return $this;
+    }
+
+    /**
+     * @return Layout
+     */
+    public function layout()
+    {
+        return $this->layout ?: ($this->layout = new Layout($this));
+    }
+
     /**
      * Validate this form fields.
      *
@@ -466,6 +496,9 @@ class Form implements Renderable
     public function pushField(Field $field)
     {
         $this->fields->push($field);
+        if ($this->layout) {
+            $this->layout->addField($field);
+        }
 
         $field->setForm($this);
         $field->width($this->width['field'], $this->width['label']);
@@ -501,6 +534,7 @@ class Form implements Renderable
             'method'  => $this->getHtmlAttribute('method'),
             'buttons' => $this->buttons,
             'rows'    => $this->rows(),
+            'layout'  => $this->layout,
         ], $this->variables);
     }