Quellcode durchsuchen

Grid\Column\Label支持传颜色数组

jqh vor 5 Jahren
Ursprung
Commit
e1e01ef474
4 geänderte Dateien mit 88 neuen und 37 gelöschten Zeilen
  1. 3 3
      src/Grid/Column.php
  2. 28 10
      src/Grid/Displayers/Chip.php
  3. 26 5
      src/Grid/Displayers/Label.php
  4. 31 19
      src/Show/Field.php

+ 3 - 3
src/Grid/Column.php

@@ -19,11 +19,11 @@ use Illuminate\Support\Traits\Macroable;
  * @method $this switch(string $color = '')
  * @method $this switchGroup($columns = [], string $color = '')
  * @method $this image($server = '', int $width = 200, int $height = 200)
- * @method $this label($style = 'success', int $max = null)
- * @method $this chip($style = 'success', int $max = null)
+ * @method $this label($style = 'primary', int $max = null)
+ * @method $this chip($style = 'primary', int $max = null)
  * @method $this button($style = 'success');
  * @method $this link($href = '', $target = '_blank');
- * @method $this badge($style = 'success', int $max = null);
+ * @method $this badge($style = 'primary', int $max = null);
  * @method $this progressBar($style = 'primary', $size = 'sm', $max = 100)
  * @method $this checkbox($options = [])
  * @method $this radio($options = [])

+ 28 - 10
src/Grid/Displayers/Chip.php

@@ -2,25 +2,29 @@
 
 namespace Dcat\Admin\Grid\Displayers;
 
+use Dcat\Admin\Admin;
 use Dcat\Admin\Support\Helper;
 
 class Chip extends AbstractDisplayer
 {
     public function display($style = 'primary', $max = null)
     {
-        $class = $style;
-        $background = '';
-        $textColor = '';
-
-        if (strpos($style, '#') === 0 || strpos($style, '(') !== false) {
-            $class = '';
-            $background = "style='background:{$style}'";
-            $textColor = 'text-white';
+        if (! $value = $this->value($max)) {
+            return;
         }
 
-        return collect($this->value($max))->map(function ($name) use ($class, $background, $textColor) {
+        $original = $this->column->getOriginal();
+        $defaultStyle = is_array($style) ? ($style['default'] ?? 'primary') : 'primary';
+
+        [$background, $textColor] = $this->formatStyle(
+            is_array($style) ?
+                (is_scalar($original) ? ($style[$original] ?? $defaultStyle) : current($style))
+                : $style
+        );
+
+        return collect($value)->map(function ($name) use ($background, $textColor) {
             return <<<HTML
-<div class="chip chip-{$class}" {$background}>
+<div class="chip" {$background}>
   <div class="chip-body">
     <div class="chip-text {$textColor}">{$name}</div>
   </div>
@@ -29,6 +33,20 @@ HTML;
         })->implode('&nbsp;');
     }
 
+    protected function formatStyle($style)
+    {
+        $background = '';
+        $textColor = '';
+
+        if ($style !== 'default') {
+            $style = Admin::color()->get($style);
+            $background = "style='background:{$style}'";
+            $textColor = 'text-white';
+        }
+
+        return [$background, $textColor];
+    }
+
     protected function value($max)
     {
         $values = Helper::array($this->value);

+ 26 - 5
src/Grid/Displayers/Label.php

@@ -2,6 +2,7 @@
 
 namespace Dcat\Admin\Grid\Displayers;
 
+use Dcat\Admin\Admin;
 use Dcat\Admin\Support\Helper;
 
 class Label extends AbstractDisplayer
@@ -11,17 +12,37 @@ class Label extends AbstractDisplayer
 
     public function display($style = 'primary', $max = null)
     {
-        $class = $style;
+        if (! $value = $this->value($max)) {
+            return;
+        }
+
+        $original = $this->column->getOriginal();
+        $defaultStyle = is_array($style) ? ($style['default'] ?? 'default') : 'default';
+
+        [$class, $background] = $this->formatStyle(
+            is_array($style) ?
+                (is_scalar($original) ? ($style[$original] ?? $defaultStyle) : current($style))
+                : $style
+        );
+
+        return collect($value)->map(function ($name) use ($class, $background) {
+            return "<span class='{$this->baseClass} {$this->stylePrefix}-{$class}' {$background}>$name</span>";
+        })->implode('&nbsp;');
+    }
+
+    protected function formatStyle($style)
+    {
+        $class = 'default';
         $background = '';
 
-        if (strpos($style, '#') === 0 || strpos($style, '(') !== false) {
+        if ($style !== 'default') {
             $class = '';
+
+            $style = Admin::color()->get($style);
             $background = "style='background:{$style}'";
         }
 
-        return collect($this->value($max))->map(function ($name) use ($class, $background) {
-            return "<span class='{$this->baseClass} {$this->stylePrefix}-{$class}' {$background}>$name</span>";
-        })->implode('&nbsp;');
+        return [$class, $background];
     }
 
     protected function value($max)

+ 31 - 19
src/Show/Field.php

@@ -2,6 +2,7 @@
 
 namespace Dcat\Admin\Show;
 
+use Dcat\Admin\Admin;
 use Dcat\Admin\Show;
 use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Traits\HasBuilderEvents;
@@ -321,14 +322,10 @@ HTML;
      */
     public function label($style = 'primary')
     {
-        return $this->unescape()->as(function ($value) use ($style) {
-            $class = $style;
-            $background = '';
+        $self = $this;
 
-            if (strpos($style, '#') === 0 || strpos($style, '(') !== false) {
-                $class = '';
-                $background = "style='background:{$style}'";
-            }
+        return $this->unescape()->as(function ($value) use ($self, $style) {
+            [$class, $background] = $self->formatStyle($style);
 
             return collect($value)->map(function ($name) use ($class, $background) {
                 return "<span class='label bg-{$class}' $background>$name</span>";
@@ -345,14 +342,10 @@ HTML;
      */
     public function badge($style = 'blue')
     {
-        return $this->unescape()->as(function ($value) use ($style) {
-            $class = $style;
-            $background = '';
+        $self = $this;
 
-            if (strpos($style, '#') === 0 || strpos($style, '(') !== false) {
-                $class = '';
-                $background = "style='background:{$style}'";
-            }
+        return $this->unescape()->as(function ($value) use ($self, $style) {
+            [$class, $background] = $self->formatStyle($style);
 
             return collect($value)->map(function ($name) use ($class, $background) {
                 return "<span class='badge bg-{$class}' $background>$name</span>";
@@ -368,19 +361,18 @@ HTML;
     public function chip($style = 'primary')
     {
         return $this->unescape()->as(function ($value) use ($style) {
-            $class = $style;
             $background = '';
             $textColor = '';
 
-            if (strpos($style, '#') === 0 || strpos($style, '(') !== false) {
-                $class = '';
+            if ($style !== 'default') {
+                $style = Admin::color()->get($style);
                 $background = "style='background:{$style}'";
                 $textColor = 'text-white';
             }
 
-            return collect($value)->map(function ($name) use ($class, $background, $textColor) {
+            return collect($value)->map(function ($name) use ($background, $textColor) {
                 return <<<HTML
-<div class="chip chip-{$class}" {$background}>
+<div class="chip" {$background}>
   <div class="chip-body">
     <div class="chip-text {$textColor}">{$name}</div>
   </div>
@@ -390,6 +382,26 @@ HTML;
         });
     }
 
+    /**
+     * @param $style
+     *
+     * @return array
+     */
+    public function formatStyle($style)
+    {
+        $class = 'default';
+        $background = '';
+
+        if ($style !== 'default') {
+            $class = '';
+
+            $style = Admin::color()->get($style);
+            $background = "style='background:{$style}'";
+        }
+
+        return [$class, $background];
+    }
+
     /**
      * Show field as json code.
      *