danidoble/livewire-calendar

一个简单的Laravel包骨架开发环境

v3.0.1 2024-05-20 23:08 UTC

This package is auto-updated.

Last update: 2024-09-20 23:49:08 UTC


README

注意

本包基于原始包asantibanez/livewire-calendar

本包允许您构建一个Livewire月历网格,以显示每一天的事件。事件可以由组件内部加载,并将根据事件的日期在每天显示。

安装

您可以通过composer安装此包

composer require danidoble/livewire-calendar

要求

  • Laravel 10或更高版本
  • Livewire 3.4或更高版本

此包在底层使用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()方法中,返回一个包含将在日历上显示的事件的集合。事件必须是包含以下键的最少键的键数组:idtitledescriptiondatedate必须是一个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/>

这将渲染一个日历网格。

默认情况下,组件将渲染当前月份。如果您想更改起始月份,您可以设置yearmonth属性。

示例

<livewire:appointments-calendar
   initialYear="2019"
   initialMonth="12"
/>

如果您想渲染包含月份和年份的标题,可以使用default-heading属性。

示例

<livewire:appointments-calendar
    default-heading="true"
/>

您应包含带有@livewireCalendarScripts的脚本以启用默认启用的拖放。您必须在@livewireScripts之后包含它们

@livewireScripts
@livewireCalendarScripts

该组件有3个公共方法,可以帮助在月份之间导航

  • goToPreviousMonth
  • goToNextMonth
  • goToCurrentMonth

您可以在下面的before-calendar-viewafter-calendar-view中使用这些方法,如下所述。

高级用法

UI自定义

在视图中渲染时,您可以使用以下属性来自定义组件的行为

  • week-starts-at,可以是0到6的数字,根据Carbon星期几表示日历应该以星期几开始。

  • event-view 可以是任何用于渲染事件卡的 blade.php 视图。此视图将被注入一个包含其数据的 $event 变量。

  • before-calendar-viewafter-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)。有关更多信息,请参阅 许可文件