Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(calendar): add viewmode #2985

Open
wants to merge 26 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions src/packages/calendar/calendar.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import '../popup/popup.scss';
@import './calendarviewmode.scss';

.nut-calendar {
position: relative;
Expand Down Expand Up @@ -62,6 +63,10 @@
height: 36px;
border-radius: 0px 0px 12px 12px;
box-shadow: 0px 4px 10px 0px rgba($color: #000000, $alpha: 0.06);

&-shrink {
padding-left: 10%;
}
}

&-week-item {
Expand Down Expand Up @@ -111,6 +116,22 @@
margin: 8px 0;
}

&-weeknumber {
width: 35%;
&-index {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
position: relative;
height: $calendar-day-height;
font-weight: 400;
font-size: 14px;
margin-bottom: 4px;
color: $color-text-help;
}
}

&-days {
overflow: hidden;
}
Expand Down Expand Up @@ -205,6 +226,10 @@
}
}

.shrink {
display: flex;
}

// 底部导航
&-footer {
display: flex;
Expand Down
105 changes: 69 additions & 36 deletions src/packages/calendar/calendar.taro.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import React, { useRef, ReactNode } from 'react'
import Popup from '@/packages/popup/index.taro'
import CalendarItem from '@/packages/calendaritem/index.taro'
import CalendarViewModeItem from './calendarviewmodeitem.taro'
import { getDateString } from '@/utils/date'
import { useConfig } from '@/packages/configprovider/configprovider.taro'
import type { CalendarDay, CalendarType, CalendarRef } from './types'
import { ComponentDefaults } from '@/utils/typings'

export interface CalendarProps {
type?: CalendarType
viewMode: 'day' | 'month' | 'quarter'
autoBackfill?: boolean
popup?: boolean
visible?: boolean
title?: string
value?: string
defaultValue?: string | string[]
startDate?: string
endDate?: string
Expand All @@ -21,6 +24,7 @@ export interface CalendarProps {
confirmText?: ReactNode
showTitle?: boolean
showSubTitle?: boolean
showMonthNumber: boolean
scrollAnimation?: boolean
firstDayOfWeek: number
closeIcon?: ReactNode
Expand All @@ -33,16 +37,19 @@ export interface CalendarProps {
onClose?: () => void
onConfirm?: (param: string) => void
onDayClick?: (data: string) => void
onItemClick: (param: string) => void
onPageChange?: (param: string) => void
}

const defaultProps = {
...ComponentDefaults,
type: 'single',
viewMode: 'day',
autoBackfill: false,
popup: true,
visible: false,
title: '',
value: '',
defaultValue: '',
startDate: getDateString(0),
endDate: getDateString(365),
Expand All @@ -52,6 +59,7 @@ const defaultProps = {
confirmText: '',
showTitle: true,
showSubTitle: true,
showMonthNumber: false,
scrollAnimation: true,
firstDayOfWeek: 0,
disableDate: (date: CalendarDay) => false,
Expand All @@ -62,6 +70,7 @@ const defaultProps = {
onClose: () => {},
onConfirm: (param: string) => {},
onDayClick: (data: string) => {},
onItemClick: () => {},
onPageChange: (param: string) => {},
} as CalendarProps

Expand All @@ -77,8 +86,10 @@ export const Calendar = React.forwardRef<
popup,
visible,
type,
viewMode,
autoBackfill,
title,
value,
defaultValue,
startDate,
endDate,
Expand All @@ -88,6 +99,7 @@ export const Calendar = React.forwardRef<
confirmText,
showTitle,
showSubTitle,
showMonthNumber,
scrollAnimation,
firstDayOfWeek,
closeIcon,
Expand All @@ -100,6 +112,7 @@ export const Calendar = React.forwardRef<
onClose,
onConfirm,
onDayClick,
onItemClick,
onPageChange,
} = { ...defaultProps, ...props }

Expand All @@ -117,10 +130,6 @@ export const Calendar = React.forwardRef<
close()
}

const select = (param: string) => {
onDayClick && onDayClick(param)
}

const scrollToDate = (date: string) => {
calendarRef.current?.scrollToDate(date)
}
Expand All @@ -135,53 +144,77 @@ export const Calendar = React.forwardRef<

const renderItem = () => {
return (
<CalendarItem
ref={calendarRef}
style={style}
className={className}
children={children}
type={type}
autoBackfill={autoBackfill}
popup={popup}
title={title || locale.calendaritem.title}
defaultValue={defaultValue}
startDate={startDate}
endDate={endDate}
showToday={showToday}
startText={startText || locale.calendaritem.start}
endText={endText || locale.calendaritem.end}
confirmText={confirmText || locale.calendaritem.confirm}
showTitle={showTitle}
showSubTitle={showSubTitle}
scrollAnimation={scrollAnimation}
firstDayOfWeek={firstDayOfWeek}
disableDate={disableDate}
renderHeaderButtons={renderHeaderButtons}
renderBottomButton={renderBottomButton}
renderDay={renderDay}
renderDayTop={renderDayTop}
renderDayBottom={renderDayBottom}
onConfirm={choose}
onDayClick={select}
onPageChange={yearMonthChange}
/>
<>
{viewMode !== 'day' ? (
<CalendarViewModeItem
ref={calendarRef}
style={style}
className={className}
children={children}
type={type}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

需要修正 children 属性的传递方式

直接通过 prop 传递 children 不符合 React 的最佳实践。

建议修改如下:

- <CalendarViewModeItem
-   children={children}
+ <CalendarViewModeItem>
+   {children}
  ...
- />
+ </CalendarViewModeItem>

- <CalendarItem
-   children={children}
+ <CalendarItem>
+   {children}
  ...
- />
+ </CalendarItem>

Also applies to: 172-173

🧰 Tools
🪛 Biome (1.9.4)

[error] 153-153: Avoid passing children using a prop

The canonical way to pass children in React is to use JSX elements

(lint/correctness/noChildrenProp)

viewMode={viewMode}
title={title || locale.calendaritem.title}
value={value}
defaultValue={defaultValue}
startDate={startDate}
endDate={endDate}
showTitle={showTitle}
scrollAnimation={scrollAnimation}
renderDay={renderDay}
onItemClick={onItemClick}
onPageChange={yearMonthChange}
/>
) : (
<CalendarItem
ref={calendarRef}
style={style}
className={className}
children={children}
type={type}
autoBackfill={autoBackfill}
popup={popup}
title={title || locale.calendaritem.title}
defaultValue={defaultValue}
startDate={startDate}
endDate={endDate}
showToday={showToday}
startText={startText || locale.calendaritem.start}
endText={endText || locale.calendaritem.end}
confirmText={confirmText || locale.calendaritem.confirm}
showTitle={showTitle}
showSubTitle={showSubTitle}
showMonthNumber={showMonthNumber}
scrollAnimation={scrollAnimation}
firstDayOfWeek={firstDayOfWeek}
disableDate={disableDate}
renderHeaderButtons={renderHeaderButtons}
renderBottomButton={renderBottomButton}
renderDay={renderDay}
renderDayTop={renderDayTop}
renderDayBottom={renderDayBottom}
onConfirm={choose}
onDayClick={onDayClick}
onPageChange={yearMonthChange}
/>
)}
</>
)
}

return (
<>
{popup ? (
{popup && viewMode === 'day' ? (
<Popup
className="nut-calendar-popup"
visible={visible}
position="bottom"
round
closeable
closeIcon={closeIcon}
destroyOnClose
onOverlayClick={closePopup}
onCloseIconClick={closePopup}
style={{ height: '83%' }}
closeIcon={closeIcon}
>
{renderItem()}
</Popup>
Expand Down
Loading
Loading