filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
19.28k stars 2.96k forks source link

Method Filament\Tables\Columns\TextColumn::enum does not exist #7122

Closed wivaku closed 1 year ago

wivaku commented 1 year ago

Package

filament/filament

Package Version

v3.0.0-beta3

Laravel Version

v10.15.0

Livewire Version

No response

PHP Version

PHP 8.2.8

Problem description

filament/tables/src/Columns/Concerns/CanFormatState.php had enum method in v2.

As seen in the documentation for BadgeColumn (in v3: TextColumn(...)->badge()

Not available in v3.

Expected behavior

Being able to still use TextColumn(...)->enum([...])

Steps to reproduce

See example in v2 documentation: https://filamentphp.com/docs/2.x/tables/columns/badge

Reproduction repository

N/A

Relevant log output

No response

github-actions[bot] commented 1 year ago

Hey @wivaku! We're sorry to hear that you've hit this issue. 💛

However, it looks like you forgot to fill in the reproduction repository URL. Can you edit your original post and then we'll look at your issue?

We need a public GitHub repository which contains a Laravel app with the minimal amount of Filament code to reproduce the problem. Please do not link to your actual project, what we need instead is a minimal reproduction in a fresh project without any unnecessary code. This means it doesn't matter if your real project is private / confidential, since we want a link to a separate, isolated reproduction. That would allow us to download it and review your bug much easier, so it can be fixed quicker. Please make sure to include a database seeder with everything we need to set the app up quickly.

zepfietje commented 1 year ago

Enums are automatically detected in v3.

wivaku commented 1 year ago

great! Is there a way to mark it as deprecated (so existing code can still work) or mention it in the upgrade notes?

zepfietje commented 1 year ago

Not sure if we can keep the old API for BC. Probably just need some docs for it if it hasn't been documented yet. CC @danharrin

FlorianW208 commented 1 year ago

@zepfietje How are Enums automatically detected, Method Filament\Tables\Columns\TextColumn::enum does not exist

My table column looks like: Tables\Columns\TextColumn::make('source')->enum(Source::keyText())

In the model there is this cast: protected $casts = [ 'source' => Source::class,

zepfietje commented 1 year ago

@FlorianW208, you don't need the enum method as a cast on your model should suffice. Make sure to implement the Filament\Support\Contracts\HasLabel interface if you want your enum to automatically map to a text value in the column.

FlorianW208 commented 1 year ago

i've added the interface to my enum: enum Salutation: int implements HasLabel

but the table shows in the column only the integer value...

zepfietje commented 1 year ago

You should of course implement the getLabel() method and return the correct strings using a match expression for example.

Please create a help thread in our Discord server if you need additional help. 🙂

alexanderchr commented 1 year ago

It's a bit annoying that backwards compatbility with old array syntax for enums is not kept. I have an app that does not use enum casts and while I'd like to use them in the future it's not an option right now. Would be nice if there was some escape path to use enums on TextColumns without casting.

TextColumn::make('source')->state(fn (Order $resource) => Order::sourceOptions()[$resource->source])

works but it's a bit of a mouthful compared to

TextColumn::make('source')->enum(Order::sourceOptions())

Especially since

Select::make('source')
    ->label(__('Source'))
    ->options(Order::sourceOptions())

still works, it would make a lot of sense if it also worked in tables

danharrin commented 1 year ago

You can make a macro on that column class if you want, to restore the feature

danharrin commented 1 year ago

The reason why we completely removed it is that the feature is named in a confusing way, it is incompatible with other features that also use formatStateUsing(), and we want to push everyone to refactor their enums into real enum classes which greatly improves code quality

alexanderchr commented 1 year ago

Thanks, appreciate the explanation. I think it'd have been nicer to keep and deprecate and provide incentives for refactoring rather than pulling the rug since it's a nice feature, but understand your reasoning.

Should at least be mentioned in the upgrade guide, right?

https://filamentphp.com/docs/3.x/panels/upgrade-guide

danharrin commented 1 year ago

https://filamentphp.com/docs/3.x/tables/upgrade-guide#badgecolumnenum-removed