技能图谱SKILLSGROVE
技能图谱/效率工具/jiaiyan/naive-ui-skills/n-calendar

n-calendar

安装

$npx skills add https://github.com/jiaiyan/naive-ui-skills --skill n-calendar
$git clone --depth 1 https://github.com/jiaiyan/naive-ui-skills.git /tmp/naive-ui-skills && mkdir -p ~/.claude/skills && cp -r /tmp/naive-ui-skills/components/n-calendar ~/.claude/skills/n-calendar
请帮我安装这个 Agent Skill:https://github.com/jiaiyan/naive-ui-skills/tree/fe21b15d95ae1a6f798b8e19d96653bc76d80db6/components/n-calendar ,先阅读它的 SKILL.md 和脚本,确认安全后再安装到我的技能目录。

简介

原文描述 · MITCalendar component for displaying and selecting dates. Invoke when user needs to implement date selection, date display, or calendar-based interfaces in Naive UI.
以下 SKILL.md 内容转载自 jiaiyan/naive-ui-skills(提交 fe21b15),依据 MIT 许可证转载正文(文字未改动;仅省略文件头元数据、将图片显示为链接,超长内容会截断);版权归 2024 Naive UI Skills Library 所有。许可证:LICENSE。仅展示说明文件,附带脚本请在原仓库查看。

Naive UI Calendar Component

Calendar component for displaying dates with customizable content and date selection.

When to Use

Use this component when:

  • Date selection: Allow users to select a specific date
  • Date display: Show calendar views with custom content per date
  • Scheduling interfaces: Display events or schedules on a calendar
  • Date-based navigation: Navigate through dates in applications

When to Invoke

Invoke this skill when:

  • User needs to implement a calendar with date selection
  • User wants to display custom content for each date cell
  • User needs to disable specific dates
  • User wants to customize the calendar header
  • User asks about handling date changes or panel changes

Features

  • Date Selection: Select a single date with v-model binding
  • Custom Cell Content: Render custom content in each date cell
  • Date Validation: Disable specific dates using validation function
  • Custom Header: Customize the calendar header
  • Panel Navigation: Navigate between months and years
  • Timestamp-based: Uses timestamps for date values

API Reference

Calendar Props

Name Type Default Description
default-value number null Default selected date's timestamp.
is-date-disabled (timestamp: number) => boolean undefined Validator of the date.
value number | null undefined Selected date's timestamp.
on-panel-change (info: { year: number, month: number }) undefined Callback on panel content is changed.
on-update:value (timestamp: number, info: { year: number, month: number, date: number }) => void undefined Callback on date is selected. month starts from 1.

Calendar Slots

Name Parameters Description
default ({ year: number, month: number, date: number }) Content to be rendered in each date cell.
header (props: { year: number, month: number }) Header of the calendar. month starts from 1.

Basic Usage

Basic Calendar

<template>
  <n-calendar
    v-model:value="value"
    #="{ year, month, date }"
    :is-date-disabled="isDateDisabled"
    @update:value="handleUpdateValue"
  >
    {{ year }}-{{ month }}-{{ date }}
  </n-calendar>
</template>

<script setup>
import { ref } from 'vue'

const value = ref(Date.now())

const isDateDisabled = (timestamp) => {
  if (timestamp < Date.now()) {
    return true
  }
  return false
}

const handleUpdateValue = (timestamp, { year, month, date }) => {
  console.log(`Selected: ${year}-${month}-${date}`)
}
</script>

Calendar with Custom Header

<template>
  <n-calendar v-model:value="value">
    <template #header="{ year, month }">
      <div style="display: flex; justify-content: space-between; align-items: center;">
        <n-button @click="handlePrevMonth">Previous</n-button>
        <span>{{ year }} - {{ month }}</span>
        <n-button @click="handleNextMonth">Next</n-button>
      </div>
    </template>
    <template #default="{ year, month, date }">
      <div v-if="hasEvent(year, month, date)">
        <n-badge dot />
      </div>
      {{ date }}
    </template>
  </n-calendar>
</template>

<script setup>
import { ref } from 'vue'

const value = ref(Date.now())

const hasEvent = (year, month, date) => {
  return false
}

const handlePrevMonth = () => {}
const handleNextMonth = () => {}
</script>

Common Patterns

Disable Past Dates

<template>
  <n-calendar
    v-model:value="value"
    :is-date-disabled="isDateDisabled"
  />
</template>

<script setup>
import { ref } from 'vue'

const value = ref(Date.now())

const isDateDisabled = (timestamp) => {
  const today = new Date()
  today.setHours(0, 0, 0, 0)
  return timestamp < today.getTime()
}
</script>

Disable Weekends

<template>
  <n-calendar
    v-model:value="value"
    :is-date-disabled="isDateDisabled"
  />
</template>

<script setup>
import { ref } from 'vue'

const value = ref(Date.now())

const isDateDisabled = (timestamp) => {
  const date = new Date(timestamp)
  const day = date.getDay()
  return day === 0 || day === 6
}
</script>

Display Events on Calendar

<template>
  <n-calendar v-model:value="value">
    <template #default="{ year, month, date }">
      <div class="calendar-cell">
        <span>{{ date }}</span>
        <div v-for="event in getEvents(year, month, date)" :key="event.id">
          <n-tag size="small" :type="event.type">
            {{ event.title }}
          </n-tag>
        </div>
      </div>
    </template>
  </n-calendar>
</template>

<script setup>
import { ref } from 'vue'

const value = ref(Date.now())

const events = [
  { id: 1, date: '2024-01-15', title: 'Meeting', type: 'info' },
  { id: 2, date: '2024-01-20', title: 'Deadline', type: 'warning' }
]

const getEvents = (year, month, date) => {
  const dateStr = `${year}-${String(month).padStart(2, '0')}-${String(date).padStart(2, '0')}`
  return events.filter(e => e.date === dateStr)
}
</script>

Track Panel Changes

<template>
  <n-calendar
    v-model:value="value"
    @panel-change="handlePanelChange"
  />
</template>

<script setup>
import { ref } from 'vue'

const value = ref(Date.now())

const handlePanelChange = ({ year, month }) => {
  console.log(`Viewing: ${year}-${month}`)
}
</script>

Best Practices

  1. Use timestamp for date values: The calendar uses timestamps for all date operations

    <n-calendar v-model:value="selectedTimestamp" />
    
  2. Disable dates efficiently: Use the is-date-disabled prop for validation

    <n-calendar :is-date-disabled="(ts) => ts < Date.now()" />
    
  3. Custom cell content: Use the default slot for rich date cell content

    <n-calendar #default="{ date }">
      {{ date }}
    </n-calendar>
    
  4. Handle date selection: Use on-update:value for side effects

    <n-calendar @update:value="handleDateSelect" />
    
  5. Remember month indexing: The month parameter starts from 1, not 0

查看许可证全文(MIT)
MIT License

Copyright (c) 2024 Naive UI Skills Library

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.