jqh 4 years ago
parent
commit
268f013adf
6 changed files with 98 additions and 20 deletions
  1. 0 1
      composer.json
  2. 58 13
      src/Form/Field.php
  3. 23 2
      src/Form/Row.php
  4. 1 1
      src/Grid/Actions/Delete.php
  5. 4 3
      src/Show/Field.php
  6. 12 0
      src/Widgets/Widget.php

+ 0 - 1
composer.json

@@ -15,7 +15,6 @@
         "php": ">=7.1.0",
         "symfony/dom-crawler": "~3.1|~4.0|~5.0",
         "laravel/framework": "~5.5|~6.0|~7.0|~8.0",
-        "doctrine/dbal": "2.*",
         "spatie/eloquent-sortable": "3.*"
     },
     "require-dev": {

+ 58 - 13
src/Form/Field.php

@@ -876,11 +876,13 @@ class Field implements Renderable
     }
 
     /**
+     * @param bool $value
+     *
      * @return $this
      */
-    public function disableHorizontal()
+    public function horizontal(bool $value = true)
     {
-        $this->horizontal = false;
+        $this->horizontal = $value;
 
         return $this;
     }
@@ -1093,25 +1095,40 @@ class Field implements Renderable
         return $this;
     }
 
-    public function setFormGroupClass($labelClass, bool $append = true)
+    /**
+     * @param string|array $class
+     * @param bool         $append
+     *
+     * @return $this
+     */
+    public function setFormGroupClass($class, bool $append = true)
     {
         $this->formGroupClass = $append
-            ? array_unique(array_merge($this->formGroupClass, (array) $labelClass))
-            : (array) $labelClass;
+            ? array_unique(array_merge($this->formGroupClass, (array) $class))
+            : (array) $class;
 
         return $this;
     }
 
+    /**
+     * @return string
+     */
     public function getFormGroupClass()
     {
         return implode(' ', $this->formGroupClass);
     }
 
-    public function setFieldClass($labelClass, bool $append = true)
+    /**
+     * @param string|array $class
+     * @param bool         $append
+     *
+     * @return $this
+     */
+    public function setFieldClass($class, bool $append = true)
     {
         $this->fieldClass = $append
-            ? array_unique(array_merge($this->fieldClass, (array) $labelClass))
-            : (array) $labelClass;
+            ? array_unique(array_merge($this->fieldClass, (array) $class))
+            : (array) $class;
 
         return $this;
     }
@@ -1212,7 +1229,15 @@ class Field implements Renderable
         return $this;
     }
 
-    protected function defaultAttribute($attribute, $value)
+    /**
+     * 设置默认属性.
+     *
+     * @param string $attribute
+     * @param mixed  $value
+     *
+     * @return $this
+     */
+    public function defaultAttribute(string $attribute, $value)
     {
         if (! array_key_exists($attribute, $this->attributes)) {
             $this->attribute($attribute, $value);
@@ -1231,6 +1256,13 @@ class Field implements Renderable
         return $this->display;
     }
 
+    /**
+     * 保存数据为json格式.
+     *
+     * @param int $option
+     *
+     * @return $this
+     */
     public function saveAsJson($option = 0)
     {
         return $this->saving(function ($value) use ($option) {
@@ -1242,10 +1274,15 @@ class Field implements Renderable
         });
     }
 
+    /**
+     * 保存数据为字符串格式.
+     *
+     * @return $this
+     */
     public function saveAsString()
     {
         return $this->saving(function ($value) {
-            if (is_object($value) || is_object($value)) {
+            if (is_object($value) || is_array($value)) {
                 return json_encode($value);
             }
 
@@ -1262,6 +1299,16 @@ class Field implements Renderable
         static::$css && Admin::css(static::$css);
     }
 
+    /**
+     * 设置默认class.
+     */
+    protected function setDefaultClass()
+    {
+        if (is_string($class = $this->getElementClassString())) {
+            $this->defaultAttribute('class', $class);
+        }
+    }
+
     /**
      * Render this filed.
      *
@@ -1273,9 +1320,7 @@ class Field implements Renderable
             return '';
         }
 
-        if (is_string($class = $this->getElementClassString())) {
-            $this->defaultAttribute('class', $class);
-        }
+        $this->setDefaultClass();
 
         $this->callComposing();
 

+ 23 - 2
src/Form/Row.php

@@ -80,7 +80,7 @@ class Row implements Renderable
     /**
      * Fields in this row.
      *
-     * @var array
+     * @var Collection
      */
     protected $fields;
 
@@ -91,6 +91,11 @@ class Row implements Renderable
      */
     protected $defaultFieldWidth = 12;
 
+    /**
+     * @var bool
+     */
+    protected $horizontal = false;
+
     /**
      * Row constructor.
      *
@@ -117,6 +122,22 @@ class Row implements Renderable
         return $this->fields;
     }
 
+    /**
+     * If the form horizontal layout.
+     *
+     * @param bool $value
+     *
+     * @return $this
+     */
+    public function horizontal(bool $value = true)
+    {
+        $this->horizontal = $value;
+
+        $this->fields->each->horizontal($value);
+
+        return $this;
+    }
+
     public function setFields(Collection $collection)
     {
         $this->fields = $collection;
@@ -176,7 +197,7 @@ class Row implements Renderable
     {
         $field = $this->form->__call($method, $arguments);
 
-        $field->disableHorizontal();
+        $field->horizontal($this->horizontal);
 
         $this->fields->push([
             'width'   => $this->defaultFieldWidth,

+ 1 - 1
src/Grid/Actions/Delete.php

@@ -22,7 +22,7 @@ class Delete extends RowAction
             'data-action'  => 'delete',
         ]);
 
-        return parent::render(); // TODO: Change the autogenerated stub
+        return parent::render();
     }
 
     public function url()

+ 4 - 3
src/Show/Field.php

@@ -491,17 +491,18 @@ HTML;
      * Render this column with the given view.
      *
      * @param string $view
+     * @param array  $data
      *
      * @return $this
      */
-    public function view($view)
+    public function view($view, array $data = [])
     {
         $name = $this->name;
 
-        return $this->unescape()->as(function ($value) use ($view, $name) {
+        return $this->unescape()->as(function ($value) use ($view, $name, $data) {
             $model = $this;
 
-            return view($view, compact('model', 'value', 'name'))->render();
+            return view($view, array_merge(compact('model', 'value', 'name'), $data))->render();
         });
     }
 

+ 12 - 0
src/Widgets/Widget.php

@@ -200,6 +200,18 @@ abstract class Widget implements Renderable
         return '.'.$this->getElementClass();
     }
 
+    /**
+     * @param string $elementClass
+     *
+     * @return $this
+     */
+    public function setElementClass(string $elementClass)
+    {
+        $this->elementClass = $elementClass;
+
+        return $this;
+    }
+
     /**
      * @return mixed
      */