Skip to content

Commit 0312237

Browse files
Redesign class
1 parent 32a0bb2 commit 0312237

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

doc/fields/ChoiceField.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,14 @@ pages (``index`` and ``detail``)::
9090

9191
The built-in badge styles are the same as Bootstrap: ``'success'``,
9292
``'warning'``, ``'danger'``, ``'info'``, ``'primary'``, ``'secondary'``,
93-
``'light'``, ``'dark'``, but you can also pass a custom full 6-digit hexadecimal background color::
93+
``'light'``, ``'dark'``, but you can also pass a custom
94+
``EasyCorp\Bundle\EasyAdminBundle\Field\Style\BadgeStyle`` instance::
9495

9596
yield ChoiceField::new('...')->renderAsBadges([
9697
// $value => $badgeStyleName
97-
'paid' => BadgeStyle::fromBgColor('#00FF00'),
98-
'pending' => BadgeStyle::fromBgColor('#FFFF00'),
99-
'refunded' => BadgeStyle::fromBgColor('#FF0000'),
98+
'paid' => BadgeStyle::new()->withBgColor('#00FF00'),
99+
'pending' => BadgeStyle::new()->withBgColor('#FFFF00'),
100+
'refunded' => BadgeStyle::new()->withBgColor('#FF0000'),
100101
]);
101102

102103
renderAsNativeWidget

src/Field/Style/BadgeStyle.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,38 @@ final class BadgeStyle
88
* @param array<string> $classes
99
* @param array<string, string> $style
1010
*/
11-
private function __construct(private readonly array $classes, private readonly array $style)
11+
private function __construct(private array $classes, private array $style)
1212
{
1313
}
1414

15-
public static function fromBgColor(string $backgroundColor, ?string $textColor = null): self
15+
public static function new(): self
16+
{
17+
return new self([], []);
18+
}
19+
20+
public function withBgColor(string $backgroundColor, bool $autoTextContrast = true): self
1621
{
1722
if (!self::isSupportedColor($backgroundColor)) {
1823
throw new \InvalidArgumentException(sprintf('The background color must be a full 6-digit hexadecimal color ("%s" given).', $backgroundColor));
1924
}
2025

21-
$classes = [];
22-
$styleProperties = ['background-color' => $backgroundColor];
26+
$this->style['background-color'] = $backgroundColor;
27+
if ($autoTextContrast) {
28+
$this->classes[] = self::generateTextClassFromBackgroundColor($backgroundColor);
29+
}
30+
31+
return $this;
32+
}
2333

24-
if (null === $textColor) {
25-
$classes[] = self::computeTextClass($backgroundColor);
26-
} elseif (self::isSupportedColor($textColor)) {
27-
$styleProperties['color'] = $textColor;
28-
} else {
34+
public function withTextColor(string $textColor): self
35+
{
36+
if (!self::isSupportedColor($textColor)) {
2937
throw new \InvalidArgumentException(sprintf('The text color must be a full 6-digit hexadecimal color ("%s" given).', $textColor));
3038
}
3139

32-
return new self($classes, $styleProperties);
40+
$this->style['color'] = $textColor;
41+
42+
return $this;
3343
}
3444

3545
public function getClasses(): string
@@ -60,7 +70,7 @@ private static function isSupportedColor(string $color): bool
6070
return 1 === preg_match('/^#[0-9a-f]{6}$/iD', $color);
6171
}
6272

63-
private static function computeTextClass(string $backgroundColor): string
73+
private static function generateTextClassFromBackgroundColor(string $backgroundColor): string
6474
{
6575
[$r, $g, $b] = [
6676
hexdec(substr($backgroundColor, 1, 2)),

tests/Field/ChoiceFieldTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,16 @@ public function testBadges()
173173
$field->setValue([1, 3])->renderAsBadges(function ($value) { return $value > 1 ? 'success' : 'primary'; });
174174
self::assertSame('<span class="badge badge-primary">a</span><span class="badge badge-success">c</span>', (string) $this->configure($field)->getFormattedValue());
175175

176-
$field->setValue(1)->renderAsBadges([1 => BadgeStyle::fromBgColor('#123456'), '3' => BadgeStyle::fromBgColor('#AAAAAA')]);
176+
$field->setValue(1)->renderAsBadges([1 => BadgeStyle::new()->withBgColor('#123456'), '3' => BadgeStyle::new()->withBgColor('#AAAAAA')]);
177177
self::assertSame('<span class="badge text-light" style="background-color:#123456;">a</span>', (string) $this->configure($field)->getFormattedValue());
178178

179-
$field->setValue([1, 3])->renderAsBadges([1 => BadgeStyle::fromBgColor('#123456'), '3' => BadgeStyle::fromBgColor('#AAAAAA')]);
179+
$field->setValue([1, 3])->renderAsBadges([1 => BadgeStyle::new()->withBgColor('#123456'), '3' => BadgeStyle::new()->withBgColor('#AAAAAA')]);
180180
self::assertSame('<span class="badge text-light" style="background-color:#123456;">a</span><span class="badge text-dark" style="background-color:#AAAAAA;">c</span>', (string) $this->configure($field)->getFormattedValue());
181181

182-
$field->setValue(1)->renderAsBadges(function ($value) { return $value > 1 ? BadgeStyle::fromBgColor('#AAAAAA') : BadgeStyle::fromBgColor('#123456'); });
182+
$field->setValue(1)->renderAsBadges(function ($value) { return $value > 1 ? BadgeStyle::new()->withBgColor('#AAAAAA') : BadgeStyle::new()->withBgColor('#123456'); });
183183
self::assertSame('<span class="badge text-light" style="background-color:#123456;">a</span>', (string) $this->configure($field)->getFormattedValue());
184184

185-
$field->setValue([1, 3])->renderAsBadges(function ($value) { return $value > 1 ? BadgeStyle::fromBgColor('#AAAAAA') : BadgeStyle::fromBgColor('#123456'); });
185+
$field->setValue([1, 3])->renderAsBadges(function ($value) { return $value > 1 ? BadgeStyle::new()->withBgColor('#AAAAAA') : BadgeStyle::new()->withBgColor('#123456'); });
186186
self::assertSame('<span class="badge text-light" style="background-color:#123456;">a</span><span class="badge text-dark" style="background-color:#AAAAAA;">c</span>', (string) $this->configure($field)->getFormattedValue());
187187
}
188188
}

0 commit comments

Comments
 (0)