cmontellanob / livewire-calendar
Laravel Livewire 日历组件分支
Requires
- php: ^8.0
- livewire/livewire: ^2.1
Requires (Dev)
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-16 23:30:09 UTC
README
此包允许您构建一个 Livewire 月历网格来显示每天的事件。事件可以从组件内部加载,并会根据事件的日期在每天显示。
预览
安装
您可以通过 composer 安装此包
composer require asantibanez/livewire-calendar
要求
此包在内部使用 livewire/livewire
(https://laravel-livewire.com/)。
它还使用 TailwindCSS (https://tailwind.org.cn/) 进行基本样式。
请确保在使用此组件之前包含这两个依赖项。
用法
为了使用此组件,您必须创建一个新的 Livewire 组件,该组件扩展自 LivewireCalendar
您可以使用 make:livewire
创建新组件。例如。
php artisan make:livewire AppointmentsCalendar
在 AppointmentsCalendar
类中,不要从基本 Component
Livewire 类扩展,而是从 LivewireCalendar
扩展。还要删除 render
方法。您将有一个类似于以下片段的类。
class AppointmentsCalendar extends LivewireCalendar { // }
在此类中,您必须覆盖以下方法
public function events() : Collection { // must return a Laravel collection }
在 events()
方法中,返回一个包含将在日历上显示的事件的集合。事件必须是至少包含以下键的键数组:id
、title
、description
、date
(《date》必须是 Carbon\Carbon
实例)。
示例
public function events() : Collection { return collect([ [ 'id' => 1, 'title' => 'Breakfast', 'description' => 'Pancakes! 🥞', 'date' => Carbon::today(), ], [ 'id' => 2, 'title' => 'Meeting with Pamela', 'description' => 'Work stuff', 'date' => Carbon::tomorrow(), ], ]); }
将使用 date
值来确定事件属于哪一天。要在 events()
方法中加载值,您可以使用以下组件属性来过滤事件。
startsAt
:月份的开始日期endsAt
:月份的结束日期gridStartsAt
:日历网格的开始日期。可以是上个月的日期。endingStartsAt
:日历网格的结束日期。可以是下个月的日期。
示例
public function events(): Collection { return Model::query() ->whereDate('scheduled_at', '>=', $this->gridStartsAt) ->whereDate('scheduled_at', '<=', $this->gridEndsAt) ->get() ->map(function (Model $model) { return [ 'id' => $model->id, 'title' => $model->title, 'description' => $model->notes, 'date' => $model->scheduled_at, ]; }); }
现在,我们可以在任何视图中包含我们的组件。
示例
<livewire:appointments-calendar/>
这将渲染一个日历网格。
默认情况下,组件将渲染当前月份。如果要将起始月份更改为其他月份,您可以设置 year
和 month
props。
示例
<livewire:appointments-calendar year="2019" month="12" />
您应该包含带有 @livewireCalendarScripts
的脚本以启用拖放,这默认是开启的。您必须在 @livewireScripts
之后包含它们
@livewireScripts @livewireCalendarScripts
组件有 3 个公共方法,可以帮助在月份之间导航
goToPreviousMonth
goToNextMonth
goToCurrentMonth
您可以在下面解释的额外视图中使用这些方法 before-calendar-view
或 after-calendar-view
。
高级用法
UI 定制
当在视图中渲染时,您可以使用以下属性来定制组件的行为
-
week-starts-at
,可以是 0 到 6 的数字,根据 Carbon 的工作日来表示日历应该从星期几开始渲染。 -
event-view
,可以是任何blade.php
视图,将用于渲染事件卡。此视图将注入$event
变量,其中包含其数据。 -
before-calendar-view
和after-calendar-view
可以是任何在日历本身之前或之后渲染的blade.php
视图。这些可以通过 Livewire 添加额外的功能到你的组件。 -
drag-and-drop-classes
可以是用于在日历中拖拽事件时渲染悬停效果的任何 CSS 类。默认值为border border-blue-400 border-4
-
day-of-week-view
可以是任何用于渲染每个日历日头的blade.php
视图。此视图将注入day
属性,它是一个表示周几的 Carbon 实例。
<livewire:appointments-grid week-starts-at="0, 1, 2, 3, 4, 5 or 6. 0 stands for Sunday" event-view="path/to/view/starting/from/views/folder" day-of-week-view="path/to/view/starting/from/views/folder" before-calendar-view="path/to/view/starting/from/views/folder" after-calendar-view="path/to/view/starting/from/views/folder" drag-and-drop-classes="css classes" />
高级 UI 自定义
(这些选项应使用基于组件默认视图的 blade 视图来使用)
要使用这些选项,建议发布组件使用的基 blade 视图,并根据您的喜好扩展其行为和样式。为此,运行 php artisan vendor:publish
并导出 livewire-calendar
标签。
-
calendar-view
可以是任何渲染整个组件的blade.php
视图。建议用修改后的基础calendar-view
覆盖此视图,例如添加一个与组件并排的视图。 -
day-view
可以是任何用于渲染每月每一天的blade.php
视图。此视图将注入以下属性:componentId
(Livewire 组件的 id),day
(作为 Carbon 实例的月份中的某一天),dayInMonth
(该天是否属于该月),isToday
(该天是否为今天),events
(与该天相对应的事件集合)
示例
<livewire:appointments-grid calendar-view="path/to/view/starting/from/views/folder" day-view="path/to/view/starting/from/views/folder" />
交互自定义
你可以覆盖以下方法来给你的组件添加交互性
public function onDayClick($year, $month, $day) { // This event is triggered when a day is clicked // You will be given the $year, $month and $day for that day } public function onEventClick($eventId) { // This event is triggered when an event card is clicked // You will be given the event id that was clicked } public function onEventDropped($eventId, $year, $month, $day) { // This event will fire when an event is dragged and dropped into another calendar day // You will get the event id, year, month and day where it was dragged to }
自动轮询
如果你需要,也可以使用 pollMillis
参数添加自动轮询。你可以结合使用 pollAction
来在期望的轮询间隔内调用组件中的特定操作。
禁用交互
默认情况下,点击和拖拽事件是启用的。要禁用它们,你可以使用以下参数在渲染组件时使用
<livewire:appointments-grid :day-click-enabled="false" :event-click-enabled="false" :drag-and-drop-enabled="false" />
测试
composer test
变更日志
请参阅 变更日志 了解最近发生了哪些变化。
贡献
请参阅 贡献指南 了解详细信息。
安全
如果你发现任何安全相关的问题,请通过电子邮件发送到 santibanez.andres@gmail.com,而不是使用问题跟踪器。
鸣谢
许可
MIT 许可证(MIT)。请参阅 许可文件 了解更多信息。