Browse Source

调整步骤表单相关方法

jqh 5 years ago
parent
commit
c0903a0098
5 changed files with 93 additions and 32 deletions
  1. 8 4
      src/Form/Builder.php
  2. 3 2
      src/Form/Concerns/HasSteps.php
  3. 30 6
      src/Form/StepBuilder.php
  4. 48 18
      src/Form/StepForm.php
  5. 4 2
      src/Grid/Filter.php

+ 8 - 4
src/Form/Builder.php

@@ -235,11 +235,11 @@ class Builder
     }
 
     /**
-     * @param \Closure $callback
+     * @param \Closure|StepForm[]|null $builder
      *
      * @return StepBuilder
      */
-    public function multipleSteps(?\Closure $callback = null)
+    public function multipleSteps($builder = null)
     {
         if (! $this->stepBuilder) {
             $this->view = 'admin::form.steps';
@@ -247,8 +247,12 @@ class Builder
             $this->stepBuilder = new StepBuilder($this->form);
         }
 
-        if ($callback) {
-            $callback($this->stepBuilder);
+        if ($builder) {
+            if ($builder instanceof \Closure) {
+                $builder($this->stepBuilder);
+            } elseif (is_array($builder)) {
+                $this->stepBuilder->add($builder);
+            }
         }
 
         return $this->stepBuilder;

+ 3 - 2
src/Form/Concerns/HasSteps.php

@@ -5,6 +5,7 @@ namespace Dcat\Admin\Form\Concerns;
 use Closure;
 use Dcat\Admin\Form\Builder;
 use Dcat\Admin\Form\StepBuilder;
+use Dcat\Admin\Form\StepForm;
 
 /**
  * @property Builder $builder
@@ -12,11 +13,11 @@ use Dcat\Admin\Form\StepBuilder;
 trait HasSteps
 {
     /**
-     * @param Closure|null $builder
+     * @param Closure|StepForm[]|null $builder
      *
      * @return StepBuilder
      */
-    public function multipleSteps(\Closure $builder = null)
+    public function multipleSteps($builder = null)
     {
         return $this->builder->multipleSteps($builder);
     }

+ 30 - 6
src/Form/StepBuilder.php

@@ -49,22 +49,46 @@ class StepBuilder
     }
 
     /**
-     * @param string   $title
-     * @param \Closure $callback
+     * @param string|StepForm|StepForm[] $title
+     * @param \Closure|null              $callback
      *
      * @return $this
      */
-    public function add(string $title, \Closure $callback)
+    public function add($title, ?\Closure $callback = null)
     {
-        $form = new StepForm($this->form, count($this->stepForms), $title);
+        if (is_array($title)) {
+            foreach ($title as $key => $form) {
+                $this->addForm($form, $callback);
+            }
 
-        $this->stepForms[] = $form;
+            return $this;
+        }
+
+        $form = $title instanceof StepForm ? $title : new StepForm($title);
 
-        $callback($form);
+        $this->addForm($form, $callback);
 
         return $this;
     }
 
+    /**
+     * @param StepForm      $form
+     * @param \Closure|null $callback
+     *
+     * @return void
+     */
+    protected function addForm(StepForm $form, ?\Closure $callback = null)
+    {
+        $form->setForm($this->form);
+        $form->setIndex(count($this->stepForms));
+
+        $this->stepForms[] = $form;
+
+        if ($callback) {
+            $callback($form);
+        }
+    }
+
     /**
      * Get all step forms.
      *

+ 48 - 18
src/Form/StepForm.php

@@ -46,52 +46,82 @@ class StepForm extends WidgetForm
      * StepForm constructor.
      *
      * @param Form   $form
-     * @param int    $index
      * @param string $title
+     * @param int    $index
+     */
+    public function __construct(string $title = null, int $index = 0)
+    {
+        $this->initFields();
+
+        $this->setTitle($title);
+        $this->setIndex($index);
+    }
+
+    /**
+     * @param Form $form
+     *
+     * @return $this
      */
-    public function __construct(Form $form, int $index = 0, string $title = null)
+    public function setForm(?Form $form)
     {
         $this->form = $form;
         $this->parent = $form->builder()->stepBuilder();
-        $this->index = $index;
-
-        $this->initFields();
 
-        $this->title($title);
+        return $this;
     }
 
     /**
      * @param string|\Closure $title
      *
-     * @return $this|string
+     * @return $this
      */
-    public function title($title = null)
+    public function setTitle($title)
     {
-        if ($title === null) {
-            return $this->title;
-        }
-
         $this->title = value($title);
 
         return $this;
     }
 
+    /**
+     * @return string
+     */
+    public function title()
+    {
+        return $this->title;
+    }
+
     /**
      * @param string|\Closure $content
      *
-     * @return $this|string
+     * @return $this
      */
-    public function description($content = null)
+    public function setDescription($content)
     {
-        if ($content === null) {
-            return $this->description;
-        }
-
         $this->description = value($content);
 
         return $this;
     }
 
+    /**
+     * @return string
+     */
+    public function description()
+    {
+        return $this->description;
+    }
+
+    /**
+     * @param int $content
+     *
+     * @return $this
+     */
+    public function setIndex(int $index = null)
+    {
+        $this->index = $index;
+
+        return $this;
+    }
+
     /**
      * @return int
      */

+ 4 - 2
src/Grid/Filter.php

@@ -654,8 +654,10 @@ class Filter implements Renderable
      */
     public function urlWithoutFilters()
     {
+        $filters = collect($this->filters);
+
         /** @var Collection $columns */
-        $columns = collect($this->filters)->map->column()->flatten();
+        $columns = $filters->map->column()->flatten();
 
         $pageKey = 'page';
 
@@ -665,7 +667,7 @@ class Filter implements Renderable
 
         $columns->push($pageKey);
 
-        $groupNames = collect($this->filters)->filter(function ($filter) {
+        $groupNames = $filters->filter(function ($filter) {
             return $filter instanceof Group;
         })->map(function (AbstractFilter $filter) {
             return "{$filter->getId()}_group";