Skip to content

Commit

Permalink
Text column
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Jan 30, 2025
1 parent 74cb825 commit b6ee723
Show file tree
Hide file tree
Showing 20 changed files with 150 additions and 71 deletions.
2 changes: 1 addition & 1 deletion docs-assets/app/public/css/filament/support/support.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/support/dist/index.css

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
}

&.fi-color {
@apply text-color-600 dark:text-color-400;
@apply text-(--text) dark:text-(--dark-text);

&li::marker {
@apply text-gray-950 dark:text-white;
Expand Down
2 changes: 1 addition & 1 deletion packages/support/src/SupportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function packageBooted(): void
return preg_replace('/\s*@trim\s*/m', '', $view);
});

ComponentAttributeBag::macro('color', function (string | HasColor $component, string $color): ComponentAttributeBag {
ComponentAttributeBag::macro('color', function (string | HasColor $component, ?string $color): ComponentAttributeBag {
return $this->class(FilamentColor::getComponentClasses($component, $color));
});

Expand Down
84 changes: 17 additions & 67 deletions packages/tables/src/Columns/TextColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@
use Filament\Support\Enums\FontFamily;
use Filament\Support\Enums\FontWeight;
use Filament\Support\Enums\IconPosition;
use Filament\Support\View\Components\Badge;
use Filament\Tables\Columns\TextColumn\Enums\TextColumnSize;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\View\Components\Columns\TextColumn\Item;
use Filament\Tables\View\Components\Columns\TextColumn\Item\Icon;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Js;
use Illuminate\View\ComponentAttributeBag;
use stdClass;

use function Filament\Support\generate_icon_html;
use function Filament\Support\get_color_css_variables;

class TextColumn extends Column implements HasEmbeddedView
{
Expand Down Expand Up @@ -261,37 +263,7 @@ public function toEmbeddedHtml(): string
$iconColor = $this->getIconColor($stateItem);

$iconHtml = generate_icon_html($this->getIcon($stateItem), attributes: (new ComponentAttributeBag)
->class([
match ($iconColor) {
null, 'gray' => null,
default => 'fi-color',
} => filled($iconColor),
is_string($iconColor) ? "fi-color-{$iconColor}" : null,
])
->style([
...(
$isBadge
? [
get_color_css_variables(
$color,
shades: [500],
alias: 'badge.icon',
) => $color !== 'gray',
]
: []
),
...(
((! $isBadge) && $iconColor)
? [
get_color_css_variables(
$iconColor,
shades: [400, 500],
alias: 'tables::columns.text-column.item.icon',
) => ! in_array($iconColor, [null, 'gray']),
]
: []
),
]))?->toHtml();
->color(Icon::class, $iconColor))?->toHtml();

$isCopyable = $this->isCopyable($stateItem);

Expand Down Expand Up @@ -322,50 +294,28 @@ public function toEmbeddedHtml(): string
], escape: false)
->class([
'fi-ta-text-item',
...((! $isBadge) ? [
match ($color) {
null, 'gray' => null,
default => 'fi-color',
},
is_string($color) ? "fi-color-{$color}" : null,
(($size = $this->getSize($stateItem)) instanceof TextColumnSize) ? "fi-size-{$size->value}" : $size,
(($weight = $this->getWeight($stateItem)) instanceof FontWeight) ? "fi-font-{$weight->value}" : (is_string($weight) ? $weight : ''),
] : []),
(($fontFamily = $this->getFontFamily($stateItem)) instanceof FontFamily) ? "fi-font-{$fontFamily->value}" : (is_string($fontFamily) ? $fontFamily : ''),
'fi-copyable' => $isCopyable,
])
->style([
...((! $isBadge) ? [
get_color_css_variables(
$color,
shades: [400, 600],
alias: 'tables::columns.text-column.item',
) => ! in_array($color, [null, 'gray']),
"--line-clamp: {$lineClamp}" => $lineClamp,
] : []),
]),
->when(
! $isBadge,
fn (ComponentAttributeBag $attributes) => $attributes
->class([
(($size = $this->getSize($stateItem)) instanceof TextColumnSize) ? "fi-size-{$size->value}" : $size,
(($weight = $this->getWeight($stateItem)) instanceof FontWeight) ? "fi-font-{$weight->value}" : (is_string($weight) ? $weight : ''),
])
->style([
"--line-clamp: {$lineClamp}" => $lineClamp,
])
->color(Item::class, $color)
),
'badgeAttributes' => $isBadge
? (new ComponentAttributeBag)
->class([
'fi-badge',
match ($color ?? 'primary') {
'gray' => null,
default => 'fi-color',
},
is_string($color) ? "fi-color-{$color}" : null,
(($size = $this->getSize($stateItem)) instanceof TextColumnSize) ? "fi-size-{$size->value}" : $size,
])
->style([
get_color_css_variables(
$color,
shades: [
50,
400,
600,
],
alias: 'badge',
) => $color !== 'gray',
])
->color(Badge::class, $color ?? 'primary')
: null,
'iconAfterHtml' => ($iconPosition === IconPosition::After) ? $iconHtml : '',
'iconBeforeHtml' => ($iconPosition === IconPosition::Before) ? $iconHtml : '',
Expand Down
55 changes: 55 additions & 0 deletions packages/tables/src/View/Components/Columns/TextColumn/Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Filament\Tables\View\Components\Columns\TextColumn;

use Filament\Support\Colors\Color;
use Filament\Support\Facades\FilamentColor;
use Filament\Support\View\Components\Contracts\HasColor;
use Filament\Support\View\Components\Contracts\HasDefaultGrayColor;

class Item implements HasColor, HasDefaultGrayColor
{
/**
* @param array<int, string> $color
* @return array<string>
*/
public function getColorClasses(array $color): array
{
$gray = FilamentColor::getColor('gray');

ksort($color);

foreach (array_keys($color) as $shade) {
if (Color::isTextContrastRatioAccessible('oklch(1 0 0)', $color[$shade])) {
$text = $shade;

break;
}
}

$text ??= 900;

krsort($color);

$lightestDarkGrayBg = $gray[800];

foreach (array_keys($color) as $shade) {
if ($shade > 600) {
continue;
}

if (Color::isTextContrastRatioAccessible($lightestDarkGrayBg, $color[$shade])) {
$darkText = $shade;

break;
}
}

$darkText ??= 200;

return [
"fi-text-color-{$text}",
"dark:fi-text-color-{$darkText}",
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Filament\Tables\View\Components\Columns\TextColumn\Item;

use Filament\Support\View\Components\Contracts\HasColor;
use Filament\Support\View\Components\Contracts\HasDefaultGrayColor;

class Icon implements HasColor, HasDefaultGrayColor
{
/**
* @param array<int, string> $color
* @return array<string>
*/
public function getColorClasses(array $color): array
{
return [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
"fi-color",
"fi-color-danger",
"fi-text-color-600",
"dark:fi-text-color-400"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
"fi-color",
"fi-color-info",
"fi-text-color-600",
"dark:fi-text-color-400"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
"fi-color",
"fi-color-primary",
"fi-text-color-700",
"dark:fi-text-color-600"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
"fi-color",
"fi-color-success",
"fi-text-color-700",
"dark:fi-text-color-600"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
"fi-color",
"fi-color-warning",
"fi-text-color-700",
"dark:fi-text-color-600"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"fi-color",
"fi-color-danger"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"fi-color",
"fi-color-info"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"fi-color",
"fi-color-primary"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"fi-color",
"fi-color-success"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"fi-color",
"fi-color-warning"
]
4 changes: 4 additions & 0 deletions tests/src/Support/ColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Filament\Support\View\Components\Toggle;
use Filament\Tables\View\Components\Columns\IconColumn\Icon as TableIconColumnIcon;
use Filament\Tables\View\Components\Columns\Summarizers\Count\Icon as TableColumnCountSummarizerIcon;
use Filament\Tables\View\Components\Columns\TextColumn\Item as TableTextColumnItem;
use Filament\Tables\View\Components\Columns\TextColumn\Item\Icon as TableTextColumnItemIcon;
use Filament\Tests\TestCase;
use Filament\Widgets\View\Components\ChartWidget;
use Filament\Widgets\View\Components\StatsOverviewWidget\Stat\Description as StatsOverviewWidgetStatDescription;
Expand Down Expand Up @@ -79,6 +81,8 @@
'stats overview widget stat chart' => StatsOverviewWidgetStatChart::class,
'table column count summarizer icon' => TableColumnCountSummarizerIcon::class,
'table icon column icon' => TableIconColumnIcon::class,
'table text column item' => TableTextColumnItem::class,
'table text column item icon' => TableTextColumnItemIcon::class,
'toggle' => Toggle::class,
])
->with(fn (): array => array_keys(app(ColorManager::class)->getColors()));

0 comments on commit b6ee723

Please sign in to comment.