跳到主要内容
版本:5.x

标注框

简介​

标注框(Callout)用于高亮显示重要信息或提示。它们常用于警示、通知或提示信息。你可以使用 Callout 组件来创建标注:

use Filament\Schemas\Components\Callout;

Callout::make('New version available')
->description('Filament v4 has been released with exciting new features and improvements.')
->info()
除了允许静态值之外,make() 和 description() 方法也接受函数动态计算他们的值。你可以将各种 utility 作为参数注入函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ComponentFilament\Schemas\Components\Component$componentThe current component instance.
Get functionFilament\Schemas\Components\Utilities\Get$getA function for retrieving values from the current schema data. Validation is not run on form fields.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current schema.
Operationstring$operationThe current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current schema.
Callout

使用状态变体​

标注框(Callout)内置了状态变体,可自动设置相应的图标、图标颜色及背景颜色。你可以使用 danger()、info()、success() 或 warning() 方法:

use Filament\Schemas\Components\Callout;

Callout::make('Payment successful')
->description('Your order has been confirmed and is being processed.')
->success()

Callout::make('Session expiring soon')
->description('Your session will expire in 5 minutes. Save your work to avoid losing changes.')
->warning()

Callout::make('Connection failed')
->description('Unable to connect to the server. Please check your internet connection.')
->danger()
Callout statuses

状态变体的可访问性​

由于标注框的状态是通过图标和背景颜色以视觉方式传达的,Filament 也会将其朗读给屏幕阅读器。当使用状态变体时,标注框标题内会渲染一个视觉上隐藏的严重性前缀(Error:、Note:、Success: 或 Warning:),以便辅助技术用户在听到消息之前了解严重性程度。标题中的图标被标记为装饰性图标(aria-hidden),因此不会单独朗读。

标注框会渲染为普通的 <div>,这对于页面加载时已存在的内容来说是正确的。如果你动态显示标注(例如,在操作完成后),并且希望屏幕阅读器朗读它,请使用 extraAttributes() 选择启用实时区域:

use Filament\Schemas\Components\Callout;

Callout::make('Changes saved')
->success()
->extraAttributes(['role' => 'status']) // Use `alert` instead of `status` for urgent messages

移除背景色​

默认情况下,状态标注带有背景色。你可以使用 color(null) 移除背景色,同时保留状态图标及其颜色:

use Filament\Schemas\Components\Callout;

Callout::make('Scheduled maintenance')
->description('The system will be unavailable on Sunday from 2:00 AM to 4:00 AM.')
->warning()
->color(null)
除了允许静态值之外,color() 方法也接受函数动态计算其值。你可以将多个 utility 作为参数注入函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ComponentFilament\Schemas\Components\Component$componentThe current component instance.
Get functionFilament\Schemas\Components\Utilities\Get$getA function for retrieving values from the current schema data. Validation is not run on form fields.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current schema.
Operationstring$operationThe current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current schema.
Callout without background

添加自定义图标​

你可以使用 icon() 方法为标注框添加自定义图标:

use Filament\Schemas\Components\Callout;
use Filament\Support\Icons\Heroicon;

Callout::make('Pro tip')
->description('You can use keyboard shortcuts to navigate faster. Press ? to see all available shortcuts.')
->icon(Heroicon::OutlinedLightBulb)
除了允许静态值之外,icon() 方法也接受函数动态计算其值。你可以将多个 utility 作为参数注入函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ComponentFilament\Schemas\Components\Component$componentThe current component instance.
Get functionFilament\Schemas\Components\Utilities\Get$getA function for retrieving values from the current schema data. Validation is not run on form fields.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current schema.
Operationstring$operationThe current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current schema.

更改图标颜色​

你可以使用 iconColor() 方法更改图标颜色:

use Filament\Schemas\Components\Callout;
use Filament\Support\Icons\Heroicon;

Callout::make('Pro tip')
->description('You can use keyboard shortcuts to navigate faster. Press ? to see all available shortcuts.')
->icon(Heroicon::OutlinedLightBulb)
->iconColor('primary')
除了允许静态值之外,iconColor() 方法也接受函数动态计算其值。你可以将多个 utility 作为参数注入函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ComponentFilament\Schemas\Components\Component$componentThe current component instance.
Get functionFilament\Schemas\Components\Utilities\Get$getA function for retrieving values from the current schema data. Validation is not run on form fields.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current schema.
Operationstring$operationThe current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current schema.
Callout with custom icon

更改图标大小​

默认情况下,图标大小为“大”(large)。你可以使用 iconSize() 方法将其更改为“小”(small)或“中”(medium):

use Filament\Schemas\Components\Callout;
use Filament\Support\Enums\IconSize;

Callout::make('Quick note')
->description('This callout has a smaller icon.')
->info()
->iconSize(IconSize::Small)
除了允许静态值之外,iconSize() 方法也接受函数动态计算其值。你可以将多个 utility 作为参数注入函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ComponentFilament\Schemas\Components\Component$componentThe current component instance.
Get functionFilament\Schemas\Components\Utilities\Get$getA function for retrieving values from the current schema data. Validation is not run on form fields.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current schema.
Operationstring$operationThe current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current schema.
Callout with small icon

使用自定义背景颜色​

你可以使用 color() 方法设置自定义背景颜色:

use Filament\Schemas\Components\Callout;
use Filament\Support\Icons\Heroicon;

Callout::make('Pro tip')
->description('You can use keyboard shortcuts to navigate faster. Press ? to see all available shortcuts.')
->color('primary')
->icon(Heroicon::OutlinedLightBulb)
->iconColor('primary')
Callout with custom color

向标注框页脚添加 Action​

你可以使用 actions() 方法向标注框页脚添加操作:

use Filament\Actions\Action;
use Filament\Schemas\Components\Callout;

Callout::make('Your trial ends in 3 days')
->description('Upgrade now to keep access to all premium features.')
->warning()
->actions([
Action::make('upgrade')
->label('Upgrade to Pro')
->button(),
Action::make('compare')
->label('Compare plans'),
])
除了允许静态值之外,actions() 方法也接受函数动态计算其值。你可以将多个 utility 作为参数注入函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ComponentFilament\Schemas\Components\Component$componentThe current component instance.
Get functionFilament\Schemas\Components\Utilities\Get$getA function for retrieving values from the current schema data. Validation is not run on form fields.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current schema.
Operationstring$operationThe current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current schema.
Callout with actions

更改页脚操作的对齐方式​

默认情况下,Action 对齐到起始位置。你可以使用 footerActionsAlignment() 方法来更改对齐方式:

use Filament\Actions\Action;
use Filament\Schemas\Components\Callout;
use Filament\Support\Enums\Alignment;

Callout::make('Updates available')
->description('New features and improvements are ready to install.')
->info()
->actions([
Action::make('install')->label('Install Now'),
Action::make('later')->label('Remind Me Later'),
])
->footerActionsAlignment(Alignment::End)

可用的对齐选项有:Alignment::Start、Alignment::Center、Alignment::End 和 Alignment::Between。

除了允许静态值之外,footerActionsAlignment() 方法也接受函数动态计算其值。你可以将多个 utility 作为参数注入函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ComponentFilament\Schemas\Components\Component$componentThe current component instance.
Get functionFilament\Schemas\Components\Utilities\Get$getA function for retrieving values from the current schema data. Validation is not run on form fields.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current schema.
Operationstring$operationThe current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current schema.
Callout with actions aligned to the end

添加自定义页脚内容​

你可以使用 footer() 方法向页脚添加自定义内容。该方法接受一个由 Schema 组件组成的数组:

use Filament\Actions\Action;
use Filament\Schemas\Components\Callout;
use Filament\Schemas\Components\Text;

Callout::make('Backup complete')
->description('Your data has been successfully backed up to the cloud.')
->success()
->footer([
Text::make('Last backup: 5 minutes ago')
->color('gray'),
Action::make('viewBackups')
->label('View All Backups')
->button(),
])
除了允许静态值之外,footer() 方法也接受函数动态计算其值。你可以将多个 utility 作为参数注入函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ComponentFilament\Schemas\Components\Component$componentThe current component instance.
Get functionFilament\Schemas\Components\Utilities\Get$getA function for retrieving values from the current schema data. Validation is not run on form fields.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current schema.
Operationstring$operationThe current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current schema.
Callout with custom footer content

添加自定义控件内容​

你可以使用 controls() 方法向控件区域(右上角)添加自定义内容。该方法接收一个 Schema 组件数组:

use Filament\Actions\Action;
use Filament\Schemas\Components\Callout;

Callout::make('Backup complete')
->description('Your data has been successfully backed up to the cloud.')
->success()
->controls([
Action::make('dismiss')
->icon('heroicon-m-x-mark')
->iconButton()
->color('gray'),
])
除了允许静态值之外,controls() 方法也接受函数动态计算其值。你可以将多个 utility 作为参数注入函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ComponentFilament\Schemas\Components\Component$componentThe current component instance.
Get functionFilament\Schemas\Components\Utilities\Get$getA function for retrieving values from the current schema data. Validation is not run on form fields.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current schema.
Operationstring$operationThe current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current schema.

为标注框添加控制操作​

你可以使用 controlActions() 方法在标注框的右上角添加控制操作。例如,你可以添加一个“关闭”按钮,用于在当前用户会话期间隐藏该标注:

use Filament\Actions\Action;
use Filament\Schemas\Components\Callout;
use Filament\Support\Icons\Heroicon;

Callout::make('New version available')
->description('Filament v4 has been released with exciting new features and improvements.')
->info()
->controlActions([
Action::make('dismiss')
->icon(Heroicon::XMark)
->iconButton()
->color('gray')
->action(fn () => session()->put('new-version-callout-dismissed', true)),
])
->visible(fn (): bool => ! session()->get('new-version-callout-dismissed'))
Callout with control actions