diff --git a/src/packages/calendar/__tests__/calendar.spec.tsx b/src/packages/calendar/__tests__/calendar.spec.tsx index b4788aa577..a21f11c3c4 100644 --- a/src/packages/calendar/__tests__/calendar.spec.tsx +++ b/src/packages/calendar/__tests__/calendar.spec.tsx @@ -1,14 +1,10 @@ import * as React from 'react' import { render, fireEvent } from '@testing-library/react' import '@testing-library/jest-dom' +import { CalendarDay } from '@/packages/calendar/types' import { Calendar } from '../calendar' -interface Day { - day: string | number - type: string -} - test('show-title prop', async () => { const { container, rerender } = render( { return
最近七天
} - const renderDay = (date: Day) => { + const renderDay = (date: CalendarDay) => { return custom{date.day} } - const renderDayBottom = (date: Day) => { + const renderDayBottom = (date: CalendarDay) => { return {Number(date.day) <= 10 ? '上旬' : '下旬'} } diff --git a/src/packages/calendar/calendar.taro.tsx b/src/packages/calendar/calendar.taro.tsx index 91cbbac05b..f4fd74bd35 100644 --- a/src/packages/calendar/calendar.taro.tsx +++ b/src/packages/calendar/calendar.taro.tsx @@ -3,13 +3,9 @@ import Popup from '@/packages/popup/index.taro' import CalendarItem from '@/packages/calendaritem/index.taro' import { Utils } from '@/utils/date' import { useConfig } from '@/packages/configprovider/configprovider.taro' -import type { CalendarDay, CalendarType } from './types' +import type { CalendarDay, CalendarType, CalendarRef } from './types' import { ComponentDefaults } from '@/utils/typings' -type CalendarRef = { - scrollToDate: (date: string) => void -} - export interface CalendarProps { type?: CalendarType autoBackfill?: boolean diff --git a/src/packages/calendar/calendar.tsx b/src/packages/calendar/calendar.tsx index 829df9ec34..f17ed9ad5c 100644 --- a/src/packages/calendar/calendar.tsx +++ b/src/packages/calendar/calendar.tsx @@ -3,13 +3,9 @@ import Popup from '@/packages/popup' import CalendarItem from '@/packages/calendaritem' import { Utils } from '@/utils/date' import { useConfig } from '@/packages/configprovider' -import type { CalendarDay, CalendarType } from './types' +import type { CalendarDay, CalendarType, CalendarRef } from './types' import { ComponentDefaults } from '@/utils/typings' -type CalendarRef = { - scrollToDate: (date: string) => void -} - export interface CalendarProps { type?: CalendarType autoBackfill?: boolean diff --git a/src/packages/calendar/demo.taro.tsx b/src/packages/calendar/demo.taro.tsx index 4377695196..7bee1cfbf3 100644 --- a/src/packages/calendar/demo.taro.tsx +++ b/src/packages/calendar/demo.taro.tsx @@ -1,42 +1,23 @@ -import React, { useState, useRef } from 'react' +import React from 'react' import Taro from '@tarojs/taro' import { useTranslate } from '@/sites/assets/locale/taro' -import { Cell, Calendar, DatePicker } from '@/packages/nutui.react.taro' import Header from '@/sites/components/header' -import { Utils } from '@/utils/date' import '@/packages/calendar/demo.scss' -import { padZero } from '@/utils/pad-zero' -interface Day { - day: string | number - type: string -} -interface T { - ce5c5446: string - c38a08ef: string - b840c88f: string - a74a1fd4: string - '8dab2f66': string - '8dab2f67': string - '8dab2f68': string - cfbdc781: string - c3a3a1d2: string - e51e4582: string - '7db1a8b2': string - '7db1a8b3': string - '7db1a8b4': string - '7db1a8b5': string - a52bef0c: string - d04fcbda: string - '0aaad620': string - '60a53514': string - '781b07fd': string - '1076d771': string - '6ab47cd2': string -} +import Demo1 from './demos/taro/demo1' +import Demo2 from './demos/taro/demo2' +import Demo3 from './demos/taro/demo3' +import Demo4 from './demos/taro/demo4' +import Demo5 from './demos/taro/demo5' +import Demo6 from './demos/taro/demo6' +import Demo7 from './demos/taro/demo7' +import Demo8 from './demos/taro/demo8' +import Demo9 from './demos/taro/demo9' +import Demo10 from './demos/taro/demo10' +import Demo11 from './demos/taro/demo11' const CalendarDemo = () => { - const [translated] = useTranslate({ + const [translated] = useTranslate({ 'zh-CN': { ce5c5446: '基础用法', c38a08ef: '选择单个日期', @@ -107,522 +88,40 @@ const CalendarDemo = () => { '6ab47cd2': 'This Month', }, }) - const d = new Date() - const currDay = `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}` - const [date, setDate] = useState(currDay) - const [date1, setDate1] = useState(['2023-01-23', '2023-11-26']) - const [date3, setDate3] = useState('') - const [date4, setDate4] = useState([]) - const [date40, setDate40] = useState([]) - const [date41, setDate41] = useState([]) - const [date42, setDate42] = useState([]) - const [date5, setDate5] = useState(['2023-03-23', '2023-11-26']) - const [date6, setDate6] = useState(['2023-06-12', '2023-06-16']) - const [date7, setDate7] = useState(['2023-07-10', '2023-07-19']) - const [date8, setDate8] = useState(['2023-06-03', '2023-06-16']) - const [dateWeek, setDateWeek] = useState('') - const [isVisible, setIsVisible] = useState(false) - const [isVisible1, setIsVisible1] = useState(false) - const [isVisible3, setIsVisible3] = useState(false) - const [isVisible4, setIsVisible4] = useState(false) - const [isVisible40, setIsVisible40] = useState(false) - const [isVisible41, setIsVisible41] = useState(false) - const [isVisible42, setIsVisible42] = useState(false) - const [isVisible5, setIsVisible5] = useState(false) - const [isVisible6, setIsVisible6] = useState(false) - const [isVisible7, setIsVisible7] = useState(false) - - const calendarRef = useRef(null) - - const openSwitch = () => { - setIsVisible(true) - } - - const openSwitch1 = () => { - setIsVisible1(true) - } - - const openSwitch3 = () => { - setIsVisible3(true) - } - const openSwitch4 = () => { - setIsVisible4(true) - } - const openSwitch40 = () => { - setIsVisible40(true) - } - const openSwitch41 = () => { - setIsVisible41(true) - } - const openSwitch42 = () => { - setIsVisible42(true) - } - const openSwitch5 = () => { - setIsVisible5(true) - } - const openSwitch6 = () => { - setIsVisible6(true) - } - const openSwitch7 = () => { - setIsVisible7(true) - } - const closeSwitch = () => { - setIsVisible(false) - } - - const closeSwitch1 = () => { - setIsVisible1(false) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const closeSwitch4 = () => { - setIsVisible4(false) - } - const closeSwitch40 = () => { - setIsVisible40(false) - } - const closeSwitch41 = () => { - setIsVisible41(false) - } - const closeSwitch42 = () => { - setIsVisible42(false) - } - const closeSwitch5 = () => { - setIsVisible5(false) - } - - const closeSwitch6 = () => { - setIsVisible6(false) - } - - const closeSwitch7 = () => { - setIsVisible7(false) - } - - const setChooseValue = (param: string) => { - setDate(param[3]) - setDateWeek(param[4]) - } - - const setChooseValue1 = (param: string) => { - setDate1([...[param[0][3], param[1][3]]]) - } - const setChooseValue3 = (param: string) => { - setDate3(param[3]) - } - - const setChooseValue4 = (chooseData: any) => { - const dateArr = chooseData.map((item: any) => { - return item[3] - }) - setDate4([...dateArr]) - } - const setChooseValue40 = (chooseData: any) => { - const dateArr = [...[chooseData[0][3], chooseData[1][3]]] - setDate40([...dateArr]) - } - const setChooseValue41 = (chooseData: any) => { - const dateArr = [...[chooseData[0][3], chooseData[1][3]]] - setDate41([...dateArr]) - } - - const setChooseValue42 = (chooseData: any) => { - console.log( - 'setChooseValue42', - [...[chooseData[0][3], chooseData[1][3]]], - chooseData - ) - const dateArr = [...[chooseData[0][3], chooseData[1][3]]] - setDate42([...dateArr]) - } - const setChooseValue5 = (param: string) => { - setDate5([...[param[0][3], param[1][3]]]) - } - - const setChooseValue6 = (param: string) => { - setDate6([...[param[0][3], param[1][3]]]) - } - const setChooseValue7 = (param: string) => { - setDate7([...[param[0][3], param[1][3]]]) - } + return ( + <> +
+
+

{translated.ce5c5446}

- const setChooseValue8 = (param: string) => { - setDate8([...[param[0][3], param[1][3]]]) - } - const select = (param: string) => { - console.log(param) - } + - const yearMonthChange = (param: string) => { - console.log(param) - } + - const goDate = () => { - if (calendarRef.current) { - calendarRef.current.scrollToDate('2023-04-01') - } - } + - const clickBtn = () => { - const date = [Utils.date2Str(new Date()), Utils.getDay(6)] - setDate7(date) - if (calendarRef.current) { - calendarRef.current.scrollToDate(date[0]) - } - } + - const clickBtn1 = () => { - const date = new Date() - const year = date.getFullYear() - let month: any = date.getMonth() + 1 - month = month < 10 ? `0${month}` : `${month}` - const yearMonth = `${year}-${month}` - const currMonthDays = Utils.getMonthDays(`${year}`, `${month}`) - setDate7([`${yearMonth}-01`, `${yearMonth}-${currMonthDays}`]) - if (calendarRef.current) { - calendarRef.current.scrollToDate(`${yearMonth}-01`) - } - } - const disableDate = (date: Day) => { - return date.day === 25 || date.day === 20 || date.day === 22 - } + - const renderDay = (date: Day) => { - return <>{padZero(date.day)} - } - const renderDayTop = (date: Day) => { - let currDate = '' - if (date && date.day === 10) { - currDate = '☺' - } - return {currDate} - } + - const renderDayBottom = (date: Day) => { - let currDate = '' - if (date && date.day === 10) { - currDate = '纪念日' - } - return {currDate} - } +

{translated.cfbdc781}

- const renderHeaderButtons = () => { - return ( -
-
- - {translated['781b07fd']} - -
-
- - {translated['1076d771']} - -
-
- - {translated['6ab47cd2']} - -
-
- ) - } + - const [show1, setShow1] = useState(false) - const [dpAbled, setDatePickerAbled] = useState([false, false]) - const [desc1, setDesc1] = useState('10:00:00') - const [desc2, setDesc2] = useState('20:00:00') - const desc = useRef(0) - const confirm1 = (values: (string | number)[], options: any[]) => { - if (desc.current === 1) { - setDesc1( - options.map((option) => padZero(parseInt(option.text))).join(':') - ) - } else { - setDesc2( - options.map((option) => padZero(parseInt(option.text))).join(':') - ) - } - } - const showDatePicker = (e: any, index: number) => { - if (dpAbled[index - 1]) { - e.stopPropagation() - setShow1(true) - desc.current = index - } - } - return ( - <> -
-
-

{translated.ce5c5446}

- - - - - - - - + - - +

{translated.d04fcbda}

- -
- {date42 && date42.length - ? `${date42[0]} ${desc1}` - : translated['8dab2f67']} -
-
-
-
- {date42 && date42.length - ? `${date42[1]} ${desc2}` - : translated['8dab2f68']} -
-
- } - /> - { - let d = [false, false] - if (date.length > 1) { - d = [true, true] - } else if (date.length > 0) { - d = [true, false] - } - setDatePickerAbled(d) - }} - onClose={closeSwitch42} - onConfirm={setChooseValue42} - > -
-
{ - showDatePicker(e, 1) - }} - > - 开始时间:{desc1} -
- - -
{ - showDatePicker(e, 2) - }} - > - 结束时间:{desc2} -
-
- setShow1(false)} - onConfirm={(options, values) => confirm1(values, options)} - /> -
+ -

{translated.cfbdc781}

- - - - -

{translated.d04fcbda}

- - + - -

{translated.e51e4582}

-
- test
} - endText="leave" - autoBackfill - onConfirm={setChooseValue8} - onPageChange={yearMonthChange} - /> -
+ + ) diff --git a/src/packages/calendar/demo.tsx b/src/packages/calendar/demo.tsx index b5cd4081aa..03d93d8cb3 100644 --- a/src/packages/calendar/demo.tsx +++ b/src/packages/calendar/demo.tsx @@ -1,43 +1,20 @@ -import React, { useState, useRef } from 'react' -import { Calendar } from './calendar' -import { Cell } from '@/packages/cell/cell' -import DatePicker from '@/packages/datepicker/index' -import { Utils } from '@/utils/date' -import { useTranslate } from '../../sites/assets/locale' - +import React from 'react' +import { useTranslate } from '@/sites/assets/locale' import './demo.scss' -import { padZero } from '@/utils/pad-zero' - -interface Day { - day: string | number - type: string -} -interface T { - ce5c5446: string - c38a08ef: string - b840c88f: string - a74a1fd4: string - '8dab2f66': string - '8dab2f67': string - '8dab2f68': string - cfbdc781: string - c3a3a1d2: string - e51e4582: string - '7db1a8b2': string - '7db1a8b3': string - '7db1a8b4': string - '7db1a8b5': string - a52bef0c: string - d04fcbda: string - '0aaad620': string - '60a53514': string - '781b07fd': string - '1076d771': string - '6ab47cd2': string -} +import Demo1 from './demos/h5/demo1' +import Demo2 from './demos/h5/demo2' +import Demo3 from './demos/h5/demo3' +import Demo4 from './demos/h5/demo4' +import Demo5 from './demos/h5/demo5' +import Demo6 from './demos/h5/demo6' +import Demo7 from './demos/h5/demo7' +import Demo8 from './demos/h5/demo8' +import Demo9 from './demos/h5/demo9' +import Demo10 from './demos/h5/demo10' +import Demo11 from './demos/h5/demo11' const CalendarDemo = () => { - const [translated] = useTranslate({ + const [translated] = useTranslate({ 'zh-CN': { ce5c5446: '基础用法', c38a08ef: '选择单个日期', @@ -108,526 +85,39 @@ const CalendarDemo = () => { '6ab47cd2': 'This Month', }, }) - const d = new Date() - const currDay = `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}` - const [date, setDate] = useState(currDay) - const [date1, setDate1] = useState(['2023-01-23', '2023-11-26']) - const [date3, setDate3] = useState('') - const [date4, setDate4] = useState([]) - const [date40, setDate40] = useState([]) - const [date41, setDate41] = useState([]) - const [date42, setDate42] = useState([]) - const [date5, setDate5] = useState(['2023-03-23', '2023-11-26']) - const [date6, setDate6] = useState(['2023-06-12', '2023-06-16']) - const [date7, setDate7] = useState(['2023-07-10', '2023-07-19']) - const [date8, setDate8] = useState(['2023-06-03', '2023-06-16']) - const [dateWeek, setDateWeek] = useState('') - const [isVisible, setIsVisible] = useState(false) - const [isVisible1, setIsVisible1] = useState(false) - const [isVisible3, setIsVisible3] = useState(false) - const [isVisible4, setIsVisible4] = useState(false) - const [isVisible40, setIsVisible40] = useState(false) - const [isVisible41, setIsVisible41] = useState(false) - const [isVisible42, setIsVisible42] = useState(false) - const [isVisible5, setIsVisible5] = useState(false) - const [isVisible6, setIsVisible6] = useState(false) - const [isVisible7, setIsVisible7] = useState(false) - - const calendarRef = useRef(null) - - const openSwitch = () => { - setIsVisible(true) - } - - const openSwitch1 = () => { - setIsVisible1(true) - } - - const openSwitch3 = () => { - setIsVisible3(true) - } - const openSwitch4 = () => { - setIsVisible4(true) - } - const openSwitch40 = () => { - setIsVisible40(true) - } - const openSwitch41 = () => { - setIsVisible41(true) - } - const openSwitch42 = () => { - setIsVisible42(true) - } - const openSwitch5 = () => { - setIsVisible5(true) - } - const openSwitch6 = () => { - setIsVisible6(true) - } - const openSwitch7 = () => { - setIsVisible7(true) - } - const closeSwitch = () => { - setIsVisible(false) - } - - const closeSwitch1 = () => { - setIsVisible1(false) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const closeSwitch4 = () => { - setIsVisible4(false) - } - const closeSwitch40 = () => { - setIsVisible40(false) - } - const closeSwitch41 = () => { - setIsVisible41(false) - } - const closeSwitch42 = () => { - setIsVisible42(false) - } - const closeSwitch5 = () => { - setIsVisible5(false) - } - - const closeSwitch6 = () => { - setIsVisible6(false) - } - - const closeSwitch7 = () => { - setIsVisible7(false) - } - - const setChooseValue = (param: string) => { - setDate(param[3]) - setDateWeek(param[4]) - } - - const setChooseValue1 = (param: string) => { - setDate1([...[param[0][3], param[1][3]]]) - } - const setChooseValue3 = (param: string) => { - setDate3(param[3]) - } - - const setChooseValue4 = (chooseData: any) => { - const dateArr = chooseData.map((item: any) => { - return item[3] - }) - setDate4([...dateArr]) - } - const setChooseValue40 = (chooseData: any) => { - const dateArr = [...[chooseData[0][3], chooseData[1][3]]] - setDate40([...dateArr]) - } - const setChooseValue41 = (chooseData: any) => { - const dateArr = [...[chooseData[0][3], chooseData[1][3]]] - setDate41([...dateArr]) - } - - const setChooseValue42 = (chooseData: any) => { - console.log( - 'setChooseValue42', - [...[chooseData[0][3], chooseData[1][3]]], - chooseData - ) - const dateArr = [...[chooseData[0][3], chooseData[1][3]]] - setDate42([...dateArr]) - } - const setChooseValue5 = (param: string) => { - setDate5([...[param[0][3], param[1][3]]]) - } - const setChooseValue6 = (param: string) => { - setDate6([...[param[0][3], param[1][3]]]) - } - - const setChooseValue7 = (param: string) => { - setDate7([...[param[0][3], param[1][3]]]) - } - - const setChooseValue8 = (param: string) => { - setDate8([...[param[0][3], param[1][3]]]) - } - const select = (param: string) => { - console.log(param) - } - - const yearMonthChange = (param: string) => { - console.log(param) - } - - const goDate = () => { - if (calendarRef.current) { - calendarRef.current.scrollToDate('2023-04-01') - } - } - - const clickBtn = () => { - const date = [Utils.date2Str(new Date()), Utils.getDay(6)] - setDate7(date) - if (calendarRef.current) { - calendarRef.current.scrollToDate(date[0]) - } - } - - const clickBtn1 = () => { - const date = new Date() - const year = date.getFullYear() - let month: any = date.getMonth() + 1 - month = month < 10 ? `0${month}` : `${month}` - const yearMonth = `${year}-${month}` - const currMonthDays = Utils.getMonthDays(`${year}`, `${month}`) - setDate7([`${yearMonth}-01`, `${yearMonth}-${currMonthDays}`]) - if (calendarRef.current) { - calendarRef.current.scrollToDate(`${yearMonth}-01`) - } - } - const disableDate = (date: Day) => { - return date.day === 25 || date.day === 20 || date.day === 22 - } - - const renderDay = (date: Day) => { - return <>{padZero(date.day)} - } - const renderDayTop = (date: Day) => { - let currDate = '' - if (date && date.day === 10) { - currDate = '☺' - } - return {currDate} - } - - const renderDayBottom = (date: Day) => { - let currDate = '' - if (date && date.day === 10) { - currDate = '纪念日' - } - return {currDate} - } - - const renderHeaderButtons = () => { - return ( -
-
- - {translated['781b07fd']} - -
-
- - {translated['1076d771']} - -
-
- - {translated['6ab47cd2']} - -
-
- ) - } - - const [show1, setShow1] = useState(false) - const [dpAbled, setDatePickerAbled] = useState([false, false]) - const [desc1, setDesc1] = useState('10:00:00') - const [desc2, setDesc2] = useState('20:00:00') - const desc = useRef(0) - const confirm1 = (values: (string | number)[], options: any[]) => { - if (desc.current === 1) { - setDesc1( - options.map((option) => padZero(parseInt(option.text))).join(':') - ) - } else { - setDesc2( - options.map((option) => padZero(parseInt(option.text))).join(':') - ) - } - } - const showDatePicker = (e: any, index: number) => { - if (dpAbled[index - 1]) { - e.stopPropagation() - setShow1(true) - desc.current = index - } - } return ( <>

{translated.ce5c5446}

- - + - - + - - + - - + - - + - -
- {date42 && date42.length - ? `${date42[0]} ${desc1}` - : translated['8dab2f67']} -
-
-
-
- {date42 && date42.length - ? `${date42[1]} ${desc2}` - : translated['8dab2f68']} -
-
- } - /> - { - let d = [false, false] - if (date.length > 1) { - d = [true, true] - } else if (date.length > 0) { - d = [true, false] - } - setDatePickerAbled(d) - }} - onClose={closeSwitch42} - onConfirm={setChooseValue42} - > -
-
{ - showDatePicker(e, 1) - }} - > - 开始时间:{desc1} -
- - -
{ - showDatePicker(e, 2) - }} - > - 结束时间:{desc2} -
-
- setShow1(false)} - onConfirm={(options, values) => confirm1(values, options)} - /> -
+

{translated.cfbdc781}

- - - - + + + + +

{translated.d04fcbda}

- - - - + + +

{translated.e51e4582}

-
- test
} - endText="leave" - autoBackfill - onConfirm={setChooseValue8} - onPageChange={yearMonthChange} - /> - + + ) diff --git a/src/packages/calendar/demos/h5/demo1.tsx b/src/packages/calendar/demos/h5/demo1.tsx new file mode 100644 index 0000000000..a5235e190c --- /dev/null +++ b/src/packages/calendar/demos/h5/demo1.tsx @@ -0,0 +1,46 @@ +import React, { useState } from 'react' +import { Cell, Calendar } from '@nutui/nutui-react' + +const Demo1 = () => { + const d = new Date() + const currDay = `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}` + const [date, setDate] = useState(currDay) + const [isVisible, setIsVisible] = useState(false) + const [dateWeek, setDateWeek] = useState('') + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (param: string) => { + setDate(param[3]) + setDateWeek(param[4]) + } + + const select = (param: string) => { + console.log(param) + } + + return ( + <> + + + + ) +} +export default Demo1 diff --git a/src/packages/calendar/demos/h5/demo10.tsx b/src/packages/calendar/demos/h5/demo10.tsx new file mode 100644 index 0000000000..780b921c4d --- /dev/null +++ b/src/packages/calendar/demos/h5/demo10.tsx @@ -0,0 +1,147 @@ +import React, { useState, useRef } from 'react' +import { Cell, Calendar } from '@nutui/nutui-react' + +function isLeapYear(y: number): boolean { + return (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0 +} + +function getMonthDays(year: string, month: string): number { + if (/^0/.test(month)) { + month = month.split('')[1] + } + return ( + [ + 0, + 31, + isLeapYear(Number(month)) ? 29 : 28, + 31, + 30, + 31, + 30, + 31, + 31, + 30, + 31, + 30, + 31, + ] as number[] + )[month as any] +} + +const padZero = (num: number | string, targetLength = 2) => { + let str = `${num}` + while (str.length < targetLength) { + str = `0${str}` + } + return str +} + +function date2Str(date: Date, split?: string): string { + split = split || '-' + const y = date.getFullYear() + const m = padZero(date.getMonth() + 1) + const d = padZero(date.getDate()) + return [y, m, d].join(split) +} + +function getDay(i: number): string { + i = i || 0 + let date = new Date() + const diff = i * (1000 * 60 * 60 * 24) + date = new Date(date.getTime() + diff) + return date2Str(date) +} + +const Demo10 = () => { + const [date, setDate] = useState(['2023-07-10', '2023-07-19']) + + const [isVisible, setIsVisible] = useState(false) + + const calendarRef = useRef(null) + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (param: string) => { + setDate([...[param[0][3], param[1][3]]]) + } + + const goDate = () => { + if (calendarRef.current) { + calendarRef.current.scrollToDate('2023-04-01') + } + } + + const clickBtn = () => { + const date = [date2Str(new Date()), getDay(6)] + setDate(date) + if (calendarRef.current) { + calendarRef.current.scrollToDate(date[0]) + } + } + + const clickBtn1 = () => { + const date = new Date() + const year = date.getFullYear() + let month: any = date.getMonth() + 1 + month = month < 10 ? `0${month}` : `${month}` + const yearMonth = `${year}-${month}` + const currMonthDays = getMonthDays(`${year}`, `${month}`) + setDate([`${yearMonth}-01`, `${yearMonth}-${currMonthDays}`]) + if (calendarRef.current) { + calendarRef.current.scrollToDate(`${yearMonth}-01`) + } + } + + const renderHeaderButtons = () => { + return ( +
+
+ + {/* {translated['781b07fd']} */} + 去某个月 + +
+
+ + {/* {translated['1076d771']} */} + 最近七天 + +
+
+ + {/* {translated['6ab47cd2']} */} + 当月 + +
+
+ ) + } + + return ( + <> + + + + ) +} +export default Demo10 diff --git a/src/packages/calendar/demos/h5/demo11.tsx b/src/packages/calendar/demos/h5/demo11.tsx new file mode 100644 index 0000000000..ac63bea957 --- /dev/null +++ b/src/packages/calendar/demos/h5/demo11.tsx @@ -0,0 +1,42 @@ +import React, { useState } from 'react' +import { Calendar } from '@nutui/nutui-react' + +const Demo11 = () => { + const [date, setDate] = useState(['2023-06-03', '2023-06-16']) + + const setChooseValue = (param: string) => { + setDate([...[param[0][3], param[1][3]]]) + } + + const yearMonthChange = (param: string) => { + console.log(param) + } + + return ( + <> +
+ test
} + endText="leave" + autoBackfill + onConfirm={setChooseValue} + onPageChange={yearMonthChange} + /> + + + ) +} +export default Demo11 diff --git a/src/packages/calendar/demos/h5/demo2.tsx b/src/packages/calendar/demos/h5/demo2.tsx new file mode 100644 index 0000000000..03fe66f43e --- /dev/null +++ b/src/packages/calendar/demos/h5/demo2.tsx @@ -0,0 +1,45 @@ +import React, { useState } from 'react' +import { Cell, Calendar } from '@nutui/nutui-react' + +const Demo2 = () => { + const [date, setDate] = useState(['2023-01-23', '2023-11-26']) + const [isVisible, setIsVisible] = useState(false) + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (param: string) => { + setDate([...[param[0][3], param[1][3]]]) + } + + const select = (param: string) => { + console.log(param) + } + + return ( + <> + + + + + ) +} +export default Demo2 diff --git a/src/packages/calendar/demos/h5/demo3.tsx b/src/packages/calendar/demos/h5/demo3.tsx new file mode 100644 index 0000000000..98b634f31a --- /dev/null +++ b/src/packages/calendar/demos/h5/demo3.tsx @@ -0,0 +1,42 @@ +import React, { useState } from 'react' +import { Cell, Calendar } from '@nutui/nutui-react' + +const Demo3 = () => { + const [date, setDate] = useState([]) + const [isVisible, setIsVisible] = useState(false) + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (chooseData: any) => { + const dateArr = chooseData.map((item: any) => { + return item[3] + }) + setDate([...dateArr]) + } + + return ( + <> + + + + ) +} +export default Demo3 diff --git a/src/packages/calendar/demos/h5/demo4.tsx b/src/packages/calendar/demos/h5/demo4.tsx new file mode 100644 index 0000000000..4568248eb8 --- /dev/null +++ b/src/packages/calendar/demos/h5/demo4.tsx @@ -0,0 +1,40 @@ +import React, { useState } from 'react' +import { Cell, Calendar } from '@nutui/nutui-react' + +const Demo4 = () => { + const [date, setDate] = useState([]) + const [isVisible, setIsVisible] = useState(false) + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (chooseData: any) => { + const dateArr = [...[chooseData[0][3], chooseData[1][3]]] + setDate([...dateArr]) + } + return ( + <> + + + + ) +} +export default Demo4 diff --git a/src/packages/calendar/demos/h5/demo5.tsx b/src/packages/calendar/demos/h5/demo5.tsx new file mode 100644 index 0000000000..db857934a6 --- /dev/null +++ b/src/packages/calendar/demos/h5/demo5.tsx @@ -0,0 +1,48 @@ +import React, { useState } from 'react' +import { Cell, Calendar, CalendarDay } from '@nutui/nutui-react' + +const Demo5 = () => { + const [date, setDate] = useState([]) + const [isVisible, setIsVisible] = useState(false) + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (chooseData: any) => { + const dateArr = [...[chooseData[0][3], chooseData[1][3]]] + setDate([...dateArr]) + } + + const disableDate = (date: CalendarDay) => { + return date.day === 25 || date.day === 20 || date.day === 22 + } + + return ( + <> + + + + + ) +} + +export default Demo5 diff --git a/src/packages/calendar/demos/h5/demo6.tsx b/src/packages/calendar/demos/h5/demo6.tsx new file mode 100644 index 0000000000..37ae07619b --- /dev/null +++ b/src/packages/calendar/demos/h5/demo6.tsx @@ -0,0 +1,130 @@ +import React, { useRef, useState } from 'react' +import { Cell, Calendar, DatePicker, CalendarDay } from '@nutui/nutui-react' + +const padZero = (num: number | string, targetLength = 2) => { + let str = `${num}` + while (str.length < targetLength) { + str = `0${str}` + } + return str +} + +const Demo6 = () => { + const [date, setDate] = useState([]) + const [isVisible, setIsVisible] = useState(false) + + const disableDate = (date: CalendarDay) => { + return date.day === 25 || date.day === 20 || date.day === 22 + } + + const [show, setShow] = useState(false) + const [dpAbled, setDatePickerAbled] = useState([false, false]) + const [desc1, setDesc1] = useState('10:00:00') + const [desc2, setDesc2] = useState('20:00:00') + const desc = useRef(0) + + const setChooseValue = (chooseData: any) => { + console.log( + 'setChooseValue', + [...[chooseData[0][3], chooseData[1][3]]], + chooseData + ) + const dateArr = [...[chooseData[0][3], chooseData[1][3]]] + setDate([...dateArr]) + } + const confirm = (values: (string | number)[], options: any[]) => { + if (desc.current === 1) { + setDesc1( + options.map((option) => padZero(parseInt(option.text))).join(':') + ) + } else { + setDesc2( + options.map((option) => padZero(parseInt(option.text))).join(':') + ) + } + } + + const showDatePicker = (e: any, index: number) => { + if (dpAbled[index - 1]) { + e.stopPropagation() + setShow(true) + desc.current = index + } + } + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + return ( + <> + +
+ {date && date.length ? `${date[0]} ${desc1}` : '请选择起始时间'} +
+
-
+
+ {date && date.length ? `${date[1]} ${desc2}` : '请选择截止时间'} +
+ + } + /> + { + let d = [false, false] + if (date.length > 1) { + d = [true, true] + } else if (date.length > 0) { + d = [true, false] + } + setDatePickerAbled(d) + }} + onClose={closeSwitch} + onConfirm={setChooseValue} + > +
+
{ + showDatePicker(e, 1) + }} + > + 开始时间:{desc1} +
+ - +
{ + showDatePicker(e, 2) + }} + > + 结束时间:{desc2} +
+
+ setShow(false)} + onConfirm={(options, values) => confirm(values, options)} + /> +
+ + ) +} +export default Demo6 diff --git a/src/packages/calendar/demos/h5/demo7.tsx b/src/packages/calendar/demos/h5/demo7.tsx new file mode 100644 index 0000000000..8024cd6362 --- /dev/null +++ b/src/packages/calendar/demos/h5/demo7.tsx @@ -0,0 +1,39 @@ +import React, { useState } from 'react' +import { Cell, Calendar } from '@nutui/nutui-react' + +const Demo7 = () => { + const [date, setDate] = useState('') + const [isVisible, setIsVisible] = useState(false) + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (param: string) => { + setDate(param[3]) + } + + return ( + <> + + + + ) +} +export default Demo7 diff --git a/src/packages/calendar/demos/h5/demo8.tsx b/src/packages/calendar/demos/h5/demo8.tsx new file mode 100644 index 0000000000..8b730b1e91 --- /dev/null +++ b/src/packages/calendar/demos/h5/demo8.tsx @@ -0,0 +1,45 @@ +import React, { useState } from 'react' +import { Cell, Calendar } from '@nutui/nutui-react' + +const Demo8 = () => { + const [date, setDate] = useState(['2023-03-23', '2023-11-26']) + const [isVisible, setIsVisible] = useState(false) + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (param: string) => { + setDate([...[param[0][3], param[1][3]]]) + } + + const select = (param: string) => { + console.log(param) + } + + return ( + <> + + + + ) +} +export default Demo8 diff --git a/src/packages/calendar/demos/h5/demo9.tsx b/src/packages/calendar/demos/h5/demo9.tsx new file mode 100644 index 0000000000..0a9d84415e --- /dev/null +++ b/src/packages/calendar/demos/h5/demo9.tsx @@ -0,0 +1,75 @@ +import React, { useState } from 'react' +import { Cell, Calendar, CalendarDay } from '@nutui/nutui-react' + +const padZero = (num: number | string, targetLength = 2) => { + let str = `${num}` + while (str.length < targetLength) { + str = `0${str}` + } + return str +} + +const Demo9 = () => { + const [date, setDate] = useState(['2023-06-12', '2023-06-16']) + + const [isVisible, setIsVisible] = useState(false) + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (param: string) => { + setDate([...[param[0][3], param[1][3]]]) + } + + const renderDay = (date: CalendarDay) => { + return <>{padZero(date.day)} + } + + const renderDayTop = (date: CalendarDay) => { + let currDate = '' + if (date && date.day === 10) { + currDate = '☺' + } + return {currDate} + } + + const renderDayBottom = (date: CalendarDay) => { + let currDate = '' + if (date && date.day === 10) { + currDate = '纪念日' + } + return {currDate} + } + + return ( + <> + + + + ) +} +export default Demo9 diff --git a/src/packages/calendar/demos/taro/demo1.tsx b/src/packages/calendar/demos/taro/demo1.tsx new file mode 100644 index 0000000000..baefc59d7d --- /dev/null +++ b/src/packages/calendar/demos/taro/demo1.tsx @@ -0,0 +1,46 @@ +import React, { useState } from 'react' +import { Cell, Calendar } from '@nutui/nutui-react-taro' + +const Demo1 = () => { + const d = new Date() + const currDay = `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}` + const [date, setDate] = useState(currDay) + const [isVisible, setIsVisible] = useState(false) + const [dateWeek, setDateWeek] = useState('') + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (param: string) => { + setDate(param[3]) + setDateWeek(param[4]) + } + + const select = (param: string) => { + console.log(param) + } + + return ( + <> + + + + ) +} +export default Demo1 diff --git a/src/packages/calendar/demos/taro/demo10.tsx b/src/packages/calendar/demos/taro/demo10.tsx new file mode 100644 index 0000000000..35f31a0c15 --- /dev/null +++ b/src/packages/calendar/demos/taro/demo10.tsx @@ -0,0 +1,144 @@ +import React, { useState, useRef } from 'react' +import { Cell, Calendar } from '@nutui/nutui-react-taro' + +function isLeapYear(y: number): boolean { + return (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0 +} + +function getMonthDays(year: string, month: string): number { + if (/^0/.test(month)) { + month = month.split('')[1] + } + return ( + [ + 0, + 31, + isLeapYear(Number(month)) ? 29 : 28, + 31, + 30, + 31, + 30, + 31, + 31, + 30, + 31, + 30, + 31, + ] as number[] + )[month as any] +} + +const padZero = (num: number | string, targetLength = 2) => { + let str = `${num}` + while (str.length < targetLength) { + str = `0${str}` + } + return str +} + +function date2Str(date: Date, split?: string): string { + split = split || '-' + const y = date.getFullYear() + const m = padZero(date.getMonth() + 1) + const d = padZero(date.getDate()) + return [y, m, d].join(split) +} + +function getDay(i: number): string { + i = i || 0 + let date = new Date() + const diff = i * (1000 * 60 * 60 * 24) + date = new Date(date.getTime() + diff) + return date2Str(date) +} + +const Demo10 = () => { + const [date, setDate] = useState(['2023-07-10', '2023-07-19']) + + const [isVisible, setIsVisible] = useState(false) + + const calendarRef = useRef(null) + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (param: string) => { + setDate([...[param[0][3], param[1][3]]]) + } + + const goDate = () => { + if (calendarRef.current) { + calendarRef.current.scrollToDate('2023-04-01') + } + } + + const clickBtn = () => { + const date = [date2Str(new Date()), getDay(6)] + setDate(date) + if (calendarRef.current) { + calendarRef.current.scrollToDate(date[0]) + } + } + + const clickBtn1 = () => { + const date = new Date() + const year = date.getFullYear() + let month: any = date.getMonth() + 1 + month = month < 10 ? `0${month}` : `${month}` + const yearMonth = `${year}-${month}` + const currMonthDays = getMonthDays(`${year}`, `${month}`) + setDate([`${yearMonth}-01`, `${yearMonth}-${currMonthDays}`]) + if (calendarRef.current) { + calendarRef.current.scrollToDate(`${yearMonth}-01`) + } + } + + const renderHeaderButtons = () => { + return ( +
+
+ + 去某个月 + +
+
+ + 最近七天 + +
+
+ + 当月 + +
+
+ ) + } + + return ( + <> + + + + ) +} +export default Demo10 diff --git a/src/packages/calendar/demos/taro/demo11.tsx b/src/packages/calendar/demos/taro/demo11.tsx new file mode 100644 index 0000000000..cd73ba8365 --- /dev/null +++ b/src/packages/calendar/demos/taro/demo11.tsx @@ -0,0 +1,42 @@ +import React, { useState } from 'react' +import { Calendar } from '@nutui/nutui-react-taro' + +const Demo11 = () => { + const [date, setDate] = useState(['2023-06-03', '2023-06-16']) + + const setChooseValue = (param: string) => { + setDate([...[param[0][3], param[1][3]]]) + } + + const yearMonthChange = (param: string) => { + console.log(param) + } + + return ( + <> +
+ test
} + endText="leave" + autoBackfill + onConfirm={setChooseValue} + onPageChange={yearMonthChange} + /> + + + ) +} +export default Demo11 diff --git a/src/packages/calendar/demos/taro/demo2.tsx b/src/packages/calendar/demos/taro/demo2.tsx new file mode 100644 index 0000000000..8936f209be --- /dev/null +++ b/src/packages/calendar/demos/taro/demo2.tsx @@ -0,0 +1,45 @@ +import React, { useState } from 'react' +import { Cell, Calendar } from '@nutui/nutui-react-taro' + +const Demo2 = () => { + const [date, setDate] = useState(['2023-01-23', '2023-11-26']) + const [isVisible, setIsVisible] = useState(false) + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (param: string) => { + setDate([...[param[0][3], param[1][3]]]) + } + + const select = (param: string) => { + console.log(param) + } + + return ( + <> + + + + + ) +} +export default Demo2 diff --git a/src/packages/calendar/demos/taro/demo3.tsx b/src/packages/calendar/demos/taro/demo3.tsx new file mode 100644 index 0000000000..f9683f6c13 --- /dev/null +++ b/src/packages/calendar/demos/taro/demo3.tsx @@ -0,0 +1,42 @@ +import React, { useState } from 'react' +import { Cell, Calendar } from '@nutui/nutui-react-taro' + +const Demo3 = () => { + const [date, setDate] = useState([]) + const [isVisible, setIsVisible] = useState(false) + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (chooseData: any) => { + const dateArr = chooseData.map((item: any) => { + return item[3] + }) + setDate([...dateArr]) + } + + return ( + <> + + + + ) +} +export default Demo3 diff --git a/src/packages/calendar/demos/taro/demo4.tsx b/src/packages/calendar/demos/taro/demo4.tsx new file mode 100644 index 0000000000..f6b2e549b4 --- /dev/null +++ b/src/packages/calendar/demos/taro/demo4.tsx @@ -0,0 +1,40 @@ +import React, { useState } from 'react' +import { Cell, Calendar } from '@nutui/nutui-react-taro' + +const Demo4 = () => { + const [date, setDate] = useState([]) + const [isVisible, setIsVisible] = useState(false) + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (chooseData: any) => { + const dateArr = [...[chooseData[0][3], chooseData[1][3]]] + setDate([...dateArr]) + } + return ( + <> + + + + ) +} +export default Demo4 diff --git a/src/packages/calendar/demos/taro/demo5.tsx b/src/packages/calendar/demos/taro/demo5.tsx new file mode 100644 index 0000000000..18c0b2977c --- /dev/null +++ b/src/packages/calendar/demos/taro/demo5.tsx @@ -0,0 +1,47 @@ +import React, { useState } from 'react' +import { Cell, Calendar, CalendarDay } from '@nutui/nutui-react-taro' + +const Demo5 = () => { + const [date, setDate] = useState([]) + const [isVisible, setIsVisible] = useState(false) + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (chooseData: any) => { + const dateArr = [...[chooseData[0][3], chooseData[1][3]]] + setDate([...dateArr]) + } + + const disableDate = (date: CalendarDay) => { + return date.day === 25 || date.day === 20 || date.day === 22 + } + + return ( + <> + + + + + ) +} +export default Demo5 diff --git a/src/packages/calendar/demos/taro/demo6.tsx b/src/packages/calendar/demos/taro/demo6.tsx new file mode 100644 index 0000000000..45ac299d91 --- /dev/null +++ b/src/packages/calendar/demos/taro/demo6.tsx @@ -0,0 +1,130 @@ +import React, { useRef, useState } from 'react' +import { + Cell, + Calendar, + DatePicker, + CalendarDay, +} from '@nutui/nutui-react-taro' + +const padZero = (num: number | string, targetLength = 2) => { + let str = `${num}` + while (str.length < targetLength) { + str = `0${str}` + } + return str +} + +const Demo6 = () => { + const [date, setDate] = useState([]) + const [isVisible, setIsVisible] = useState(false) + + const disableDate = (date: CalendarDay) => { + return date.day === 25 || date.day === 20 || date.day === 22 + } + + const [show, setShow] = useState(false) + const [dpAbled, setDatePickerAbled] = useState([false, false]) + const [desc1, setDesc1] = useState('10:00:00') + const [desc2, setDesc2] = useState('20:00:00') + const desc = useRef(0) + + const setChooseValue = (chooseData: any) => { + const dateArr = [...[chooseData[0][3], chooseData[1][3]]] + setDate([...dateArr]) + } + const confirm = (values: (string | number)[], options: any[]) => { + if (desc.current === 1) { + setDesc1( + options.map((option) => padZero(parseInt(option.text))).join(':') + ) + } else { + setDesc2( + options.map((option) => padZero(parseInt(option.text))).join(':') + ) + } + } + + const showDatePicker = (e: any, index: number) => { + if (dpAbled[index - 1]) { + e.stopPropagation() + setShow(true) + desc.current = index + } + } + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + return ( + <> + +
+ {date && date.length ? `${date[0]} ${desc1}` : '请选择起始时间'} +
+
-
+
+ {date && date.length ? `${date[1]} ${desc2}` : '请选择截止时间'} +
+ + } + /> + { + let d = [false, false] + if (date.length > 1) { + d = [true, true] + } else if (date.length > 0) { + d = [true, false] + } + setDatePickerAbled(d) + }} + onClose={closeSwitch} + onConfirm={setChooseValue} + > +
+
{ + showDatePicker(e, 1) + }} + > + 开始时间:{desc1} +
+ - +
{ + showDatePicker(e, 2) + }} + > + 结束时间:{desc2} +
+
+ setShow(false)} + onConfirm={(options, values) => confirm(values, options)} + /> +
+ + ) +} +export default Demo6 diff --git a/src/packages/calendar/demos/taro/demo7.tsx b/src/packages/calendar/demos/taro/demo7.tsx new file mode 100644 index 0000000000..3b0d46a131 --- /dev/null +++ b/src/packages/calendar/demos/taro/demo7.tsx @@ -0,0 +1,39 @@ +import React, { useState } from 'react' +import { Cell, Calendar } from '@nutui/nutui-react-taro' + +const Demo7 = () => { + const [date, setDate] = useState('') + const [isVisible, setIsVisible] = useState(false) + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (param: string) => { + setDate(param[3]) + } + + return ( + <> + + + + ) +} +export default Demo7 diff --git a/src/packages/calendar/demos/taro/demo8.tsx b/src/packages/calendar/demos/taro/demo8.tsx new file mode 100644 index 0000000000..b1e33441a0 --- /dev/null +++ b/src/packages/calendar/demos/taro/demo8.tsx @@ -0,0 +1,45 @@ +import React, { useState } from 'react' +import { Cell, Calendar } from '@nutui/nutui-react-taro' + +const Demo8 = () => { + const [date, setDate] = useState(['2023-03-23', '2023-11-26']) + const [isVisible, setIsVisible] = useState(false) + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (param: string) => { + setDate([...[param[0][3], param[1][3]]]) + } + + const select = (param: string) => { + console.log(param) + } + + return ( + <> + + + + ) +} +export default Demo8 diff --git a/src/packages/calendar/demos/taro/demo9.tsx b/src/packages/calendar/demos/taro/demo9.tsx new file mode 100644 index 0000000000..6b8b6b493b --- /dev/null +++ b/src/packages/calendar/demos/taro/demo9.tsx @@ -0,0 +1,75 @@ +import React, { useState } from 'react' +import { Cell, Calendar, CalendarDay } from '@nutui/nutui-react-taro' + +const padZero = (num: number | string, targetLength = 2) => { + let str = `${num}` + while (str.length < targetLength) { + str = `0${str}` + } + return str +} + +const Demo9 = () => { + const [date, setDate] = useState(['2023-06-12', '2023-06-16']) + + const [isVisible, setIsVisible] = useState(false) + + const openSwitch = () => { + setIsVisible(true) + } + + const closeSwitch = () => { + setIsVisible(false) + } + + const setChooseValue = (param: string) => { + setDate([...[param[0][3], param[1][3]]]) + } + + const renderDay = (date: CalendarDay) => { + return <>{padZero(date.day)} + } + + const renderDayTop = (date: CalendarDay) => { + let currDate = '' + if (date && date.day === 10) { + currDate = '☺' + } + return {currDate} + } + + const renderDayBottom = (date: CalendarDay) => { + let currDate = '' + if (date && date.day === 10) { + currDate = '纪念日' + } + return {currDate} + } + + return ( + <> + + + + ) +} +export default Demo9 diff --git a/src/packages/calendar/doc.en-US.md b/src/packages/calendar/doc.en-US.md index 6224ed9218..f0b30e5ba0 100644 --- a/src/packages/calendar/doc.en-US.md +++ b/src/packages/calendar/doc.en-US.md @@ -16,44 +16,7 @@ import { Calendar } from '@nutui/nutui-react'; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date, setDate] = useState(''); - const [isVisible, setIsVisible] = useState(false); - const [dateWeek, setDateWeek] = useState(''); - - const openSwitch = () => { - setIsVisible(true); - } - - const closeSwitch = () => { - setIsVisible(false); - } - - const setChooseValue = (param: string) => { - setDate(param[3]); - setDateWeek(param[4]); - } - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -61,44 +24,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date1, setDate1] = useState(['2019-12-23', '2019-12-26']) - const [isVisible1, setIsVisible1] = useState(false) - - const openSwitch1 = () => { - setIsVisible1(true) - } - - const closeSwitch1 = () => { - setIsVisible1(false) - } - - const setChooseValue1 = (param: string) => { - setDate1([...[param[0][3], param[1][3]]]) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -106,44 +32,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - setDate3(param[3]) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -151,45 +40,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - const dateArr = [...[param[0][3], param[1][3]]] - setDate3([...dateArr]) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -197,50 +48,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - const dateArr = [...[param[0][3], param[1][3]]] - setDate3([...dateArr]) - } - - const disableDate = (date: CalendarDay) => { - return date.day === 25 - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -248,124 +56,7 @@ export default App; :::demo -```tsx -import React, {useRef, useState } from "react"; -import { Cell, Calendar, DatePicker } from '@nutui/nutui-react'; - -const App = () => { - const openSwitch42 = () => { - setIsVisible42(true) - } - const [date42, setDate42] = useState([]) - const [isVisible42, setIsVisible42] = useState(false) - const disableDate = (date: CalendarDay) => { - return date.day === 25 || date.day === 20 || date.day === 22 - } - const [show1, setShow1] = useState(false) - const [dpAbled, setDatePickerAbled] = useState([false, false]) - const [desc1, setDesc1] = useState('10:00:00') - const [desc2, setDesc2] = useState('20:00:00') - const desc = useRef(0) - const padZero = (d: number | string) => { - return d <= 9 ? `0${d}` : d - } - const setChooseValue42 = (chooseData: any) => { - const dateArr = [...[chooseData[0][3], chooseData[1][3]]] - setDate42([...dateArr]) - } - const confirm1 = (values: (string | number)[], options: any[]) => { - if (desc.current === 1) { - setDesc1( - options.map((option) => padZero(parseInt(option.text))).join(':') - ) - } else { - setDesc2( - options.map((option) => padZero(parseInt(option.text))).join(':') - ) - } - } - const showDatePicker = (e: any, index: number) => { - if (dpAbled[index - 1]) { - e.stopPropagation() - setShow1(true) - desc.current = index - } - } - - return ( - <> - -
- {date42 && date42.length - ? `${date42[0]} ${desc1}` - : 'Select start Time'} -
-
-
-
- {date42 && date42.length - ? `${date42[1]} ${desc2}` - : 'Select end time'} -
- - } - /> - { - let d = [false, false] - if (date.length > 1) { - d = [true, true] - } else if (date.length > 0) { - d = [true, false] - } - setDatePickerAbled(d) - }} - onClose={closeSwitch42} - onConfirm={setChooseValue42} - > -
-
{ - showDatePicker(e, 1) - }} - > - Start Time: {desc1} -
- - -
{ - showDatePicker(e, 2) - }} - > - End Time: {desc2} -
-
- setShow1(false)} - onConfirm={(options, values) => confirm1(values, options)} - /> -
- - ); -}; -export default App; - -``` + ::: @@ -373,44 +64,15 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - setDate3(param[3]) - } - - return ( - <> - - - - ); -}; -export default App; + -``` +::: + +### Quick selection - Interval selection + +:::demo + + ::: @@ -418,69 +80,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -interface CalendarDay { - day: string | number; - type: string; -} - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - setDate3([...[param[0][3], param[1][3]]]) - } - - const renderDay = (date: CalendarDay) => { - return ( - { date.day <= 9 ? `0${ date.day}` : date.day } - ) - } - - const renderDayBottom = (date: CalendarDay) => { - return ( - { - date ? (date.day <= 10 ? '' : date.day <= 20 ? 'mid' : '') : '' - } - ) - } - - return ( - <> - - - - - ); -}; -export default App; - -``` + ::: @@ -488,144 +88,7 @@ export default App; :::demo -```tsx -import React, { useState, useRef } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const Utils = { - date2Str(date: Date, split?: string): string { - split = split || '-' - const y = date.getFullYear() - const m = this.getNumTwoBit(date.getMonth() + 1) - const d = this.getNumTwoBit(date.getDate()) - return [y, m, d].join(split) - }, - getDay(i: number): string { - i = i || 0 - let date = new Date() - const diff = i * (1000 * 60 * 60 * 24) - date = new Date(date.getTime() + diff) - return this.date2Str(date) - }, - getNumTwoBit(n: number): string { - n = Number(n) - return (n > 9 ? '' : '0') + n - }, - date2Str(date: Date, split?: string): string { - split = split || '-' - const y = date.getFullYear() - const m = this.getNumTwoBit(date.getMonth() + 1) - const d = this.getNumTwoBit(date.getDate()) - return [y, m, d].join(split) - }, - getMonthDays(year: string, month: string): number { - if (/^0/.test(month)) { - month = month.split('')[1] - } - return ( - [ - 0, - 31, - this.isLeapYear(Number(year)) ? 29 : 28, - 31, - 30, - 31, - 30, - 31, - 31, - 30, - 31, - 30, - 31, - ] as number[] - )[month as any] - }, - isLeapYear(y: number): boolean { - return (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0 - }, -}; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const calendarRef = useRef(null); - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - setDate3([...[param[0][3], param[1][3]]]) - } - - const goDate = () => { - if (calendarRef.current) { - calendarRef.current.scrollToDate('2023-04-01'); - } - }; - - const clickBtn = () => { - const date = [Utils.date2Str(new Date()), Utils.getDay(6)]; - setDate3(date); - if (calendarRef.current) { - calendarRef.current.scrollToDate(date[0]) - } - } - - const clickBtn1 = () => { - const date = new Date(); - const year = date.getFullYear(); - let month: any = date.getMonth() + 1; - month = month < 10 ? `0${ month}` : `${month }`; - const yearMonth = `${year}-${month}`; - const currMonthDays = Utils.getMonthDays(`${year }`, `${month }`); - setDate3([`${yearMonth}-01`, `${yearMonth}-${currMonthDays}`]); - if (calendarRef.current) { - calendarRef.current.scrollToDate(`${yearMonth}-01`) - } - } - - const renderHeaderButtons = () => { - return ( -
-
- to a certain month -
-
- last seven days -
-
- current month -
-
- ) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -633,33 +96,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date2, setDate2] = useState('2020-07-08') - - const setChooseValue2 = (param: string) => { - setDate2(param[3]) - } - - return ( - <> -
- -
- - ); -}; -export default App; - -``` + ::: diff --git a/src/packages/calendar/doc.md b/src/packages/calendar/doc.md index 23d694eb41..733a0502b5 100644 --- a/src/packages/calendar/doc.md +++ b/src/packages/calendar/doc.md @@ -16,44 +16,7 @@ import { Calendar } from '@nutui/nutui-react'; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date, setDate] = useState(''); - const [isVisible, setIsVisible] = useState(false); - const [dateWeek, setDateWeek] = useState(''); - - const openSwitch = () => { - setIsVisible(true); - } - - const closeSwitch = () => { - setIsVisible(false); - } - - const setChooseValue = (param: string) => { - setDate(param[3]); - setDateWeek(param[4]); - } - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -61,44 +24,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date1, setDate1] = useState(['2019-12-23', '2019-12-26']) - const [isVisible1, setIsVisible1] = useState(false) - - const openSwitch1 = () => { - setIsVisible1(true) - } - - const closeSwitch1 = () => { - setIsVisible1(false) - } - - const setChooseValue1 = (param: string) => { - setDate1([...[param[0][3], param[1][3]]]) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -106,44 +32,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - setDate3(param[3]) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -151,45 +40,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - const dateArr = [...[param[0][3], param[1][3]]] - setDate3([...dateArr]) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -197,50 +48,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - const dateArr = [...[param[0][3], param[1][3]]] - setDate3([...dateArr]) - } - - const disableDate = (date: CalendarDay) => { - return date.day === 25 - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -248,124 +56,7 @@ export default App; :::demo -```tsx -import React, {useRef, useState } from "react"; -import { Cell, Calendar, DatePicker } from '@nutui/nutui-react'; - -const App = () => { - const openSwitch42 = () => { - setIsVisible42(true) - } - const [date42, setDate42] = useState([]) - const [isVisible42, setIsVisible42] = useState(false) - const disableDate = (date: CalendarDay) => { - return date.day === 25 || date.day === 20 || date.day === 22 - } - const [show1, setShow1] = useState(false) - const [dpAbled, setDatePickerAbled] = useState([false, false]) - const [desc1, setDesc1] = useState('10:00:00') - const [desc2, setDesc2] = useState('20:00:00') - const desc = useRef(0) - const padZero = (d: number | string) => { - return d <= 9 ? `0${d}` : d - } - const setChooseValue42 = (chooseData: any) => { - const dateArr = [...[chooseData[0][3], chooseData[1][3]]] - setDate42([...dateArr]) - } - const confirm1 = (values: (string | number)[], options: any[]) => { - if (desc.current === 1) { - setDesc1( - options.map((option) => padZero(parseInt(option.text))).join(':') - ) - } else { - setDesc2( - options.map((option) => padZero(parseInt(option.text))).join(':') - ) - } - } - const showDatePicker = (e: any, index: number) => { - if (dpAbled[index - 1]) { - e.stopPropagation() - setShow1(true) - desc.current = index - } - } - - return ( - <> - -
- {date42 && date42.length - ? `${date42[0]} ${desc1}` - : '请选择起始时间'} -
-
-
-
- {date42 && date42.length - ? `${date42[1]} ${desc2}` - : '请选择截止时间'} -
- - } - /> - { - let d = [false, false] - if (date.length > 1) { - d = [true, true] - } else if (date.length > 0) { - d = [true, false] - } - setDatePickerAbled(d) - }} - onClose={closeSwitch42} - onConfirm={setChooseValue42} - > -
-
{ - showDatePicker(e, 1) - }} - > - 开始时间:{desc1} -
- - -
{ - showDatePicker(e, 2) - }} - > - 结束时间:{desc2} -
-
- setShow1(false)} - onConfirm={(options, values) => confirm1(values, options)} - /> -
- - ); -}; -export default App; - -``` + ::: @@ -373,44 +64,15 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - setDate3(param[3]) - } - - return ( - <> - - - - ); -}; -export default App; + -``` +::: + +### 快捷选择-日期区间 + +:::demo + + ::: @@ -418,68 +80,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -interface CalendarDay { - day: string | number; - type: string; -} - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - setDate3([...[param[0][3], param[1][3]]]) - } - - const renderDay = (date: CalendarDay) => { - return ( - { date.day <= 9 ? `0${ date.day}` : date.day } - ) - } - - const renderDayBottom = (date: CalendarDay) => { - return ( - { - date ? (date.day <= 10 ? '' : date.day <= 20 ? 'mid' : '') : '' - } - ) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -487,143 +88,7 @@ export default App; :::demo -```tsx -import React, { useState, useRef } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const Utils = { - date2Str(date: Date, split?: string): string { - split = split || '-' - const y = date.getFullYear() - const m = this.getNumTwoBit(date.getMonth() + 1) - const d = this.getNumTwoBit(date.getDate()) - return [y, m, d].join(split) - }, - getDay(i: number): string { - i = i || 0 - let date = new Date() - const diff = i * (1000 * 60 * 60 * 24) - date = new Date(date.getTime() + diff) - return this.date2Str(date) - }, - getNumTwoBit(n: number): string { - n = Number(n) - return (n > 9 ? '' : '0') + n - }, - date2Str(date: Date, split?: string): string { - split = split || '-' - const y = date.getFullYear() - const m = this.getNumTwoBit(date.getMonth() + 1) - const d = this.getNumTwoBit(date.getDate()) - return [y, m, d].join(split) - }, - getMonthDays(year: string, month: string): number { - if (/^0/.test(month)) { - month = month.split('')[1] - } - return ( - [ - 0, - 31, - this.isLeapYear(Number(year)) ? 29 : 28, - 31, - 30, - 31, - 30, - 31, - 31, - 30, - 31, - 30, - 31, - ] as number[] - )[month as any] - }, - isLeapYear(y: number): boolean { - return (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0 - }, -}; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - const calendarRef = useRef(null); - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - setDate3([...[param[0][3], param[1][3]]]) - } - - const goDate = () => { - if (calendarRef.current) { - calendarRef.current.scrollToDate('2023-04-01'); - } - }; - - const clickBtn = () => { - const date = [Utils.date2Str(new Date()), Utils.getDay(6)]; - setDate3(date); - if (calendarRef.current) { - calendarRef.current.scrollToDate(date[0]) - } - } - - const clickBtn1 = () => { - const date = new Date(); - const year = date.getFullYear(); - let month: any = date.getMonth() + 1; - month = month < 10 ? `0${ month}` : `${month }`; - const yearMonth = `${year}-${month}`; - const currMonthDays = Utils.getMonthDays(`${year }`, `${month }`); - setDate3([`${yearMonth}-01`, `${yearMonth}-${currMonthDays}`]); - if (calendarRef.current) { - calendarRef.current.scrollToDate(`${yearMonth}-01`) - } - } - - const renderHeaderButtons = () => { - return ( -
-
- 去某个月 -
-
- 最近七天 -
-
- 当月 -
-
- ) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -631,32 +96,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date2, setDate2] = useState('2020-07-08') - const setChooseValue2 = (param: string) => { - setDate2(param[3]) - } - - return ( - <> -
- -
- - ); -}; -export default App; - -``` + ::: diff --git a/src/packages/calendar/doc.taro.md b/src/packages/calendar/doc.taro.md index 14ca0fa640..d67e113984 100644 --- a/src/packages/calendar/doc.taro.md +++ b/src/packages/calendar/doc.taro.md @@ -16,44 +16,7 @@ import { Calendar } from '@nutui/nutui-react-taro'; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react-taro'; - -const App = () => { - const [date, setDate] = useState(''); - const [isVisible, setIsVisible] = useState(false); - const [dateWeek, setDateWeek] = useState(''); - - const openSwitch = () => { - setIsVisible(true); - } - - const closeSwitch = () => { - setIsVisible(false); - } - - const setChooseValue = (param: string) => { - setDate(param[3]); - setDateWeek(param[4]); - } - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -61,44 +24,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react-taro'; - -const App = () => { - const [date1, setDate1] = useState(['2019-12-23', '2019-12-26']) - const [isVisible1, setIsVisible1] = useState(false) - - const openSwitch1 = () => { - setIsVisible1(true) - } - - const closeSwitch1 = () => { - setIsVisible1(false) - } - - const setChooseValue1 = (param: string) => { - setDate1([...[param[0][3], param[1][3]]]) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -106,44 +32,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react-taro'; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - setDate3(param[3]) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -151,45 +40,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react-taro'; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - const dateArr = [...[param[0][3], param[1][3]]] - setDate3([...dateArr]) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -197,50 +48,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react-taro'; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - const dateArr = [...[param[0][3], param[1][3]]] - setDate3([...dateArr]) - } - - const disableDate = (date: CalendarDay) => { - return date.day === 25 - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -248,127 +56,7 @@ export default App; :::demo -```tsx -import React, {useRef, useState } from "react"; -import { Cell, Calendar, DatePicker } from '@nutui/nutui-react-taro'; - -const App = () => { - const openSwitch42 = () => { - setIsVisible42(true) - } - const [date42, setDate42] = useState([]) - const [isVisible42, setIsVisible42] = useState(false) - const disableDate = (date: CalendarDay) => { - return date.day === 25 || date.day === 20 || date.day === 22 - } - - const [show1, setShow1] = useState(false) - const [dpAbled, setDatePickerAbled] = useState([false, false]) - const [desc1, setDesc1] = useState('10:00:00') - const [desc2, setDesc2] = useState('20:00:00') - const desc = useRef(0) - const padZero = (d: number | string) => { - return d <= 9 ? `0${d}` : d - } - const setChooseValue42 = (chooseData: any) => { - const dateArr = [...[chooseData[0][3], chooseData[1][3]]] - setDate42([...dateArr]) - } - const confirm1 = (values: (string | number)[], options: any[]) => { - if (desc.current === 1) { - setDesc1( - options.map((option) => padZero(parseInt(option.text))).join(':') - ) - } else { - setDesc2( - options.map((option) => padZero(parseInt(option.text))).join(':') - ) - } - } - const showDatePicker = (e: any, index: number) => { - if (dpAbled[index - 1]) { - e.stopPropagation() - setShow1(true) - desc.current = index - } - } - - - - return ( - <> - -
- {date42 && date42.length - ? `${date42[0]} ${desc1}` - : '请选择起始时间'} -
-
-
-
- {date42 && date42.length - ? `${date42[1]} ${desc2}` - : '请选择截止时间'} -
- - } - /> - { - let d = [false, false] - if (date.length > 1) { - d = [true, true] - } else if (date.length > 0) { - d = [true, false] - } - setDatePickerAbled(d) - }} - onClose={closeSwitch42} - onConfirm={setChooseValue42} - > -
-
{ - showDatePicker(e, 1) - }} - > - 开始时间:{desc1} -
- - -
{ - showDatePicker(e, 2) - }} - > - 结束时间:{desc2} -
-
- setShow1(false)} - onConfirm={(options, values) => confirm1(values, options)} - /> -
- - ); -}; -export default App; - -``` + ::: @@ -376,45 +64,15 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react-taro'; - - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - setDate3(param[3]) - } - - return ( - <> - - - - ); -}; -export default App; + -``` +::: + +### 快捷选择-日期区间 + +:::demo + + ::: @@ -422,68 +80,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react-taro'; - -interface CalendarDay { - day: string | number; - type: string; -} - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - setDate3([...[param[0][3], param[1][3]]]) - } - - const renderDay = (date: CalendarDay) => { - return ( - { date.day <= 9 ? `0${ date.day}` : date.day } - ) - } - - const renderDayBottom = (date: CalendarDay) => { - return ( - { - date ? (date.day <= 10 ? '' : date.day <= 20 ? 'mid' : '') : '' - } - ) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -491,143 +88,7 @@ export default App; :::demo -```tsx -import React, { useState, useRef } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react-taro'; - -const Utils = { - date2Str(date: Date, split?: string): string { - split = split || '-' - const y = date.getFullYear() - const m = this.getNumTwoBit(date.getMonth() + 1) - const d = this.getNumTwoBit(date.getDate()) - return [y, m, d].join(split) - }, - getDay(i: number): string { - i = i || 0 - let date = new Date() - const diff = i * (1000 * 60 * 60 * 24) - date = new Date(date.getTime() + diff) - return this.date2Str(date) - }, - getNumTwoBit(n: number): string { - n = Number(n) - return (n > 9 ? '' : '0') + n - }, - date2Str(date: Date, split?: string): string { - split = split || '-' - const y = date.getFullYear() - const m = this.getNumTwoBit(date.getMonth() + 1) - const d = this.getNumTwoBit(date.getDate()) - return [y, m, d].join(split) - }, - getMonthDays(year: string, month: string): number { - if (/^0/.test(month)) { - month = month.split('')[1] - } - return ( - [ - 0, - 31, - this.isLeapYear(Number(year)) ? 29 : 28, - 31, - 30, - 31, - 30, - 31, - 31, - 30, - 31, - 30, - 31, - ] as number[] - )[month as any] - }, - isLeapYear(y: number): boolean { - return (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0 - }, -}; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - const calendarRef = useRef(null); - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - setDate3([...[param[0][3], param[1][3]]]) - } - - const goDate = () => { - if (calendarRef.current) { - calendarRef.current.scrollToDate('2023-04-01'); - } - }; - - const clickBtn = () => { - const date = [Utils.date2Str(new Date()), Utils.getDay(6)]; - setDate3(date); - if (calendarRef.current) { - calendarRef.current.scrollToDate(date[0]) - } - } - - const clickBtn1 = () => { - const date = new Date(); - const year = date.getFullYear(); - let month: any = date.getMonth() + 1; - month = month < 10 ? `0${ month}` : `${month }`; - const yearMonth = `${year}-${month}`; - const currMonthDays = Utils.getMonthDays(`${year }`, `${month }`); - setDate3([`${yearMonth}-01`, `${yearMonth}-${currMonthDays}`]); - if (calendarRef.current) { - calendarRef.current.scrollToDate(`${yearMonth}-01`) - } - } - - const renderHeaderButtons = () => { - return ( -
-
- 去某个月 -
-
- 最近七天 -
-
- 当月 -
-
- ) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -635,32 +96,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Calendar } from '@nutui/nutui-react-taro'; - -const App = () => { - const [date2, setDate2] = useState('2020-07-08') - const setChooseValue2 = (param: string) => { - setDate2(param[3]) - } - - return ( - <> -
- -
- - ); -}; -export default App; - -``` + ::: diff --git a/src/packages/calendar/doc.zh-TW.md b/src/packages/calendar/doc.zh-TW.md index 41a4933a64..85f4bcbe5a 100644 --- a/src/packages/calendar/doc.zh-TW.md +++ b/src/packages/calendar/doc.zh-TW.md @@ -16,44 +16,7 @@ import { Calendar } from '@nutui/nutui-react'; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date, setDate] = useState(''); - const [isVisible, setIsVisible] = useState(false); - const [dateWeek, setDateWeek] = useState(''); - - const openSwitch = () => { - setIsVisible(true); - } - - const closeSwitch = () => { - setIsVisible(false); - } - - const setChooseValue = (param: string) => { - setDate(param[3]); - setDateWeek(param[4]); - } - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -61,44 +24,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date1, setDate1] = useState(['2019-12-23', '2019-12-26']) - const [isVisible1, setIsVisible1] = useState(false) - - const openSwitch1 = () => { - setIsVisible1(true) - } - - const closeSwitch1 = () => { - setIsVisible1(false) - } - - const setChooseValue1 = (param: string) => { - setDate1([...[param[0][3], param[1][3]]]) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -106,44 +32,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - setDate3(param[3]) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -151,45 +40,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - const dateArr = [...[param[0][3], param[1][3]]] - setDate3([...dateArr]) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -197,175 +48,15 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - const dateArr = [...[param[0][3], param[1][3]]] - setDate3([...dateArr]) - } - - const disableDate = (date: CalendarDay) => { - return date.day === 25 - } - - return ( - <> - - - - ); -}; -export default App; + -``` ::: - ### 和Datepicker 聯動 :::demo -```tsx -import React, {useRef, useState } from "react"; -import { Cell, Calendar, DatePicker } from '@nutui/nutui-react'; - -const App = () => { - const openSwitch42 = () => { - setIsVisible42(true) - } - const [date42, setDate42] = useState([]) - const [isVisible42, setIsVisible42] = useState(false) - const disableDate = (date: CalendarDay) => { - return date.day === 25 || date.day === 20 || date.day === 22 - } - const [show1, setShow1] = useState(false) - const [dpAbled, setDatePickerAbled] = useState([false, false]) - const [desc1, setDesc1] = useState('10:00:00') - const [desc2, setDesc2] = useState('20:00:00') - const desc = useRef(0) - const padZero = (d: number | string) => { - return d <= 9 ? `0${d}` : d - } - const setChooseValue42 = (chooseData: any) => { - const dateArr = [...[chooseData[0][3], chooseData[1][3]]] - setDate42([...dateArr]) - } - const confirm1 = (values: (string | number)[], options: any[]) => { - if (desc.current === 1) { - setDesc1( - options.map((option) => padZero(parseInt(option.text))).join(':') - ) - } else { - setDesc2( - options.map((option) => padZero(parseInt(option.text))).join(':') - ) - } - } - const showDatePicker = (e: any, index: number) => { - if (dpAbled[index - 1]) { - e.stopPropagation() - setShow1(true) - desc.current = index - } - } - - return ( - <> - -
- {date42 && date42.length - ? `${date42[0]} ${desc1}` - : '請選擇起始時間'} -
-
-
-
- {date42 && date42.length - ? `${date42[1]} ${desc2}` - : '請選擇截止時間'} -
- - } - /> - { - let d = [false, false] - if (date.length > 1) { - d = [true, true] - } else if (date.length > 0) { - d = [true, false] - } - setDatePickerAbled(d) - }} - onClose={closeSwitch42} - onConfirm={setChooseValue42} - > -
-
{ - showDatePicker(e, 1) - }} - > - 開始時間:{desc1} -
- - -
{ - showDatePicker(e, 2) - }} - > - 結束時間:{desc2} -
-
- setShow1(false)} - onConfirm={(options, values) => confirm1(values, options)} - /> -
- - ); -}; -export default App; - -``` + ::: @@ -373,44 +64,15 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - setDate3(param[3]) - } - - return ( - <> - - - - ); -}; -export default App; + -``` +::: + +### 快捷選擇-日期區間 + +:::demo + + ::: @@ -418,68 +80,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -interface CalendarDay { - day: string | number; - type: string; -} - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - setDate3([...[param[0][3], param[1][3]]]) - } - - const renderDay = (date: CalendarDay) => { - return ( - { date.day <= 9 ? `0${ date.day}` : date.day } - ) - } - - const renderDayBottom = (date: CalendarDay) => { - return ( - { - date ? (date.day <= 10 ? '' : date.day <= 20 ? 'mid' : '') : '' - } - ) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -487,143 +88,7 @@ export default App; :::demo -```tsx -import React, { useState, useRef } from "react"; -import { Cell, Calendar } from '@nutui/nutui-react'; - -const Utils = { - date2Str(date: Date, split?: string): string { - split = split || '-' - const y = date.getFullYear() - const m = this.getNumTwoBit(date.getMonth() + 1) - const d = this.getNumTwoBit(date.getDate()) - return [y, m, d].join(split) - }, - getDay(i: number): string { - i = i || 0 - let date = new Date() - const diff = i * (1000 * 60 * 60 * 24) - date = new Date(date.getTime() + diff) - return this.date2Str(date) - }, - getNumTwoBit(n: number): string { - n = Number(n) - return (n > 9 ? '' : '0') + n - }, - date2Str(date: Date, split?: string): string { - split = split || '-' - const y = date.getFullYear() - const m = this.getNumTwoBit(date.getMonth() + 1) - const d = this.getNumTwoBit(date.getDate()) - return [y, m, d].join(split) - }, - getMonthDays(year: string, month: string): number { - if (/^0/.test(month)) { - month = month.split('')[1] - } - return ( - [ - 0, - 31, - this.isLeapYear(Number(year)) ? 29 : 28, - 31, - 30, - 31, - 30, - 31, - 31, - 30, - 31, - 30, - 31, - ] as number[] - )[month as any] - }, - isLeapYear(y: number): boolean { - return (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0 - }, -}; - -const App = () => { - const [date3, setDate3] = useState('') - const [isVisible3, setIsVisible3] = useState(false) - const calendarRef = useRef(null); - - const openSwitch3 = () => { - setIsVisible3(true) - } - - const closeSwitch3 = () => { - setIsVisible3(false) - } - - const setChooseValue3 = (param: string) => { - setDate3([...[param[0][3], param[1][3]]]) - } - - const goDate = () => { - if (calendarRef.current) { - calendarRef.current.scrollToDate('2023-04-01'); - } - }; - - const clickBtn = () => { - const date = [Utils.date2Str(new Date()), Utils.getDay(6)]; - setDate3(date); - if (calendarRef.current) { - calendarRef.current.scrollToDate(date[0]) - } - } - - const clickBtn1 = () => { - const date = new Date(); - const year = date.getFullYear(); - let month: any = date.getMonth() + 1; - month = month < 10 ? `0${ month}` : `${month }`; - const yearMonth = `${year}-${month}`; - const currMonthDays = Utils.getMonthDays(`${year }`, `${month }`); - setDate3([`${yearMonth}-01`, `${yearMonth}-${currMonthDays}`]); - if (calendarRef.current) { - calendarRef.current.scrollToDate(`${yearMonth}-01`) - } - } - - const renderHeaderButtons = () => { - return ( -
-
- 去某個月 -
-
- 最近七天 -
-
- 當月 -
-
- ) - } - - return ( - <> - - - - ); -}; -export default App; - -``` + ::: @@ -631,32 +96,7 @@ export default App; :::demo -```tsx -import React, { useState } from "react"; -import { Calendar } from '@nutui/nutui-react'; - -const App = () => { - const [date2, setDate2] = useState('2020-07-08') - const setChooseValue2 = (param: string) => { - setDate2(param[3]) - } - - return ( - <> -
- -
- - ); -}; -export default App; - -``` + ::: diff --git a/src/packages/calendar/types.ts b/src/packages/calendar/types.ts index 4030c73958..27d3a485c2 100644 --- a/src/packages/calendar/types.ts +++ b/src/packages/calendar/types.ts @@ -14,3 +14,7 @@ export interface CalendarMonthInfo { export type CalendarValue = string | string[] export type CalendarType = 'single' | 'range' | 'multiple' | 'week' + +export type CalendarRef = { + scrollToDate: (date: string) => void +}