Slider
简介
滑块(Slider)组件允许你通过滑动选择一个或多个数值:
use Filament\Forms\Components\Slider;
Slider::make('slider')

该组件使用了 noUiSlider 包,其中的很多 API 也是基于该库。
NOTE
由于其性质,滑块字段永远不会为空。字段值永远不能为 null 或空数组。如果滑块字段为空,则用户将没有能在轨道上拖动的控制柄。
因此,滑块字段有一个开箱即用的默认值,该值设置为滑块的范围中允许的最小值。默认值在表单为空时使用,例如在资源的“新建”页面上。要了解有关默认值的更多信息,请查看 default() 文档。
控制滑块范围
滑块默认可选的最小值和最大值为 0 和 100。Filament 将会自动应用验证规则以确保用户不会超出这些值。你可以使用 range() 方法对其进行修改:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 40, maxValue: 80)
除了静态值外,range() 方法同时也接受通过函数动态设置其值。你可以将各种 utility 作为参数注入到该函数中。
Learn more about utility injection.| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |

控制步长
默认情况下,用户可以选择最小值和最大值之间的任何十进制值。你可以使用 step() 方法将值限制为特定的步长。Filament 将自动应用验证规则,以确保用户不能偏离此步长:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->step(10)
除了静态值外,step() 方法同时也接受通过函数动态设置其值。你可以将各种 utility 作为参数注入到该函数中。
Learn more about utility injection.| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |

