Calendar 组件

介绍

日历,可平铺/弹窗展示

安装

  1. import { createApp } from 'vue';
  2. import { Calendar } from '@nutui/nutui';
  3. const app = createApp();
  4. app.use(Calendar);

代码演示

基础用法

  1. <nut-cell
  2. :showIcon="true"
  3. title="选择单个日期"
  4. :desc="date ? `${date} ${dateWeek}` : '请选择'"
  5. @click="openSwitch('isVisible')"
  6. >
  7. </nut-cell>
  8. <nut-calendar
  9. v-model:visible="isVisible"
  10. :default-value="date"
  11. @close="closeSwitch('isVisible')"
  12. @choose="setChooseValue"
  13. :start-date="`2019-10-11`"
  14. :end-date="`2022-11-11`"
  15. >
  16. </nut-calendar>
  1. setup() {
  2. const state: TestCalendarState = reactive({
  3. isVisible: false,
  4. date: '',
  5. dateWeek: ''
  6. });
  7. const openSwitch = param => {
  8. state[`${param}`] = true;
  9. };
  10. const closeSwitch = param => {
  11. state[`${param}`] = false;
  12. };
  13. const setChooseValue = param => {
  14. state.date = param[3];
  15. state.dateWeek = param[4];
  16. };
  17. return {
  18. ...toRefs(state),
  19. openSwitch,
  20. closeSwitch,
  21. setChooseValue
  22. };
  23. }

区间选择

  1. <nut-cell
  2. :showIcon="true"
  3. title="选择日期区间"
  4. :desc="date ? `${date[0]}至${date[1]}` : '请选择'"
  5. @click="openSwitch('isVisible')"
  6. >
  7. </nut-cell>
  8. <nut-calendar
  9. v-model:visible="isVisible"
  10. :default-value="date"
  11. type="range"
  12. :start-date="`2019-12-22`"
  13. :end-date="`2021-01-08`"
  14. @close="closeSwitch('isVisible')"
  15. @choose="setChooseValue"
  16. >
  17. </nut-calendar>
  1. setup() {
  2. const state: TestCalendarState = reactive({
  3. date: ['2019-12-23', '2019-12-26'],
  4. isVisible2: true
  5. });
  6. const openSwitch = param => {
  7. state[`${param}`] = true;
  8. };
  9. const closeSwitch = param => {
  10. state[`${param}`] = false;
  11. };
  12. const setChooseValue= param => {
  13. state.date = [...[param[0][3], param[1][3]]];
  14. };
  15. return {
  16. ...toRefs(state),
  17. openSwitch,
  18. closeSwitch,
  19. setChooseValue,
  20. };
  21. }

自定义日历-自动回填

  1. <nut-cell
  2. :showIcon="true"
  3. title="选择日期"
  4. :desc="date ? date : '请选择'"
  5. @click="openSwitch('isVisible')"
  6. >
  7. </nut-cell>
  8. <nut-calendar
  9. v-model:visible="isVisible"
  10. @close="closeSwitch('isVisible')"
  11. @choose="setChooseValue"
  12. :start-date="null"
  13. :end-date="null"
  14. :is-auto-back-fill="true"
  15. >
  16. </nut-calendar>
  1. setup() {
  2. const state: TestCalendarState = reactive({
  3. date: '',
  4. isVisible: false
  5. });
  6. const openSwitch = param => {
  7. state[`${param}`] = true;
  8. };
  9. const closeSwitch = param => {
  10. state[`${param}`] = false;
  11. };
  12. const setChooseValue = param => {
  13. state.date= param[3];
  14. };
  15. return {
  16. ...toRefs(state),
  17. setChooseValue
  18. };
  19. }

平铺展示

  1. <div class="test-calendar-wrapper">
  2. <nut-calendar
  3. :poppable="false"
  4. :is-auto-back-fill="true"
  5. :default-value="date"
  6. @choose="setChooseValue"
  7. >
  8. </nut-calendar
  9. ></div>
  1. setup() {
  2. const state: TestCalendarState = reactive({
  3. date: '2020-07-08'
  4. });
  5. const setChooseValue = param => {
  6. state.date = param[3];
  7. };
  8. return {
  9. ...toRefs(state),
  10. setChooseValue
  11. };
  12. }

基础用法

API

Props

字段说明类型默认值
v-model:visible是否可见Booleanfalse
type类型,日期选择’one’,区间选择’range’String‘one’
poppable是否弹窗状态展示Booleantrue
is-auto-back-fill自动回填Booleanfalse
title显示标题String‘日期选择’
default-value默认值,日期选择 String 格式,区间选择 Array 格式String 、 Arraynull
start-date开始日期, 如果不限制开始日期传 nullString今天
end-date结束日期,如果不限制结束日期传 nullString距离今天 365 天

Events

事件名说明回调参数
choose选择之后或是点击确认按钮触发日期数组(包含年月日和星期)
close关闭时触发-

Calendar  日历 - 图1