Skip to content

Commit cc82c1f

Browse files
committed
feat(calendar): add disabledDate attribute to the calendar component
1 parent 7f6c0fb commit cc82c1f

File tree

7 files changed

+75
-25
lines changed

7 files changed

+75
-25
lines changed

packages/taro-ui-demo/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"@tarojs/runtime": "3.6.6",
6060
"@tarojs/shared": "3.6.6",
6161
"@tarojs/taro": "3.6.6",
62+
"dayjs": "^1.7.7",
6263
"taro-ui": "workspace:^"
6364
},
6465
"devDependencies": {

packages/taro-ui-demo/src/pages/advanced/calendar/index.tsx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22
import { AtButton, AtCalendar } from 'taro-ui'
3+
import dayjs from 'dayjs'
34
import { View } from '@tarojs/components'
45
import Taro from '@tarojs/taro'
56
import DocsHeader from '../../components/doc-header'
@@ -21,7 +22,8 @@ interface IndexState {
2122
[key: string]: any
2223
}
2324

24-
export default class Index extends React.Component<{}, IndexState> {
25+
interface IndexProps {}
26+
export default class Index extends React.Component<IndexProps, IndexState> {
2527
public config: Taro.PageConfig = {
2628
navigationBarTitleText: 'Taro日历组件展示'
2729
}
@@ -86,14 +88,8 @@ export default class Index extends React.Component<{}, IndexState> {
8688
}
8789

8890
public render(): JSX.Element {
89-
const {
90-
now,
91-
minDate,
92-
maxDate,
93-
mark,
94-
multiCurentDate,
95-
validDates
96-
} = this.state
91+
const { now, minDate, maxDate, mark, multiCurentDate, validDates } =
92+
this.state
9793
return (
9894
<View className='page calendar-page'>
9995
<DocsHeader title='Calendar 日历' />
@@ -211,6 +207,22 @@ export default class Index extends React.Component<{}, IndexState> {
211207
<AtCalendar validDates={validDates} />
212208
</View>
213209
</View>
210+
211+
<View className='panel'>
212+
<View className='panel__title'>禁用日期</View>
213+
<View className='panel__content'>
214+
<AtCalendar
215+
disabledDate={(currentDate: dayjs.Dayjs) => {
216+
const yesterday = dayjs().subtract(1, 'day')
217+
const tomorrow = dayjs().add(1, 'day')
218+
return (
219+
dayjs(currentDate).isSame(yesterday, 'day') ||
220+
dayjs(currentDate).isSame(tomorrow, 'day')
221+
)
222+
}}
223+
/>
224+
</View>
225+
</View>
214226
</View>
215227
</View>
216228
)

packages/taro-ui-docs/markdown/calendar.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,20 @@ interface SelectDate {
116116
}
117117
```
118118

119-
| 参数 | 说明 | 类型 | 默认值 |
120-
| ------------- | -------------- | ------------------------------- | ------------ |
121-
| currentDate | 当前的时间 | `DateArg | SelectDate` | `Date.now()` |
122-
| minDate | 最小的可选时间 | `DateArg` | - |
123-
| maxDate | 最大的可选时间 | `DateArg` | - |
124-
| isSwiper | 是否可以滑动 | `boolean` | `true` |
125-
| marks | 需要标记的时间 | `Array<{'{ value: DateArg }'}>` | `[]` |
126-
| validDates | 需要标记的有效时间 | `Array<{'{ value: DateArg }'}>` | `[]` |
127-
| format | 日期格式 | `string` | `YYYY-MM-DD` |
128-
| monthFormat | 月份格式 | `string` | `YYYY年MM月` |
129-
| hideArrow | 是否隐藏箭头 | `boolean` | `false` |
130-
| isVertical | 是否垂直滑动 | `boolean` | `false` |
131-
| isMultiSelect | 是否范围选择 | `boolean` | `false` |
119+
| 参数 | 说明 | 类型 | 默认值 |
120+
| ------------- |---------------| ------------------------------- |--------------|
121+
| currentDate | 当前的时间 | `DateArg | SelectDate` | `Date.now()` |
122+
| minDate | 最小的可选时间 | `DateArg` | - |
123+
| maxDate | 最大的可选时间 | `DateArg` | - |
124+
| disabledDate | 不可选择的日期 | `(currentDate: dayjs.Dayjs) => boolean` | - |
125+
| isSwiper | 是否可以滑动 | `boolean` | `true` |
126+
| marks | 需要标记的时间 | `Array<{'{ value: DateArg }'}>` | `[]` |
127+
| validDates | 需要标记的有效时间 | `Array<{'{ value: DateArg }'}>` | `[]` |
128+
| format | 日期格式 | `string` | `YYYY-MM-DD` |
129+
| monthFormat | 月份格式 | `string` | `YYYY年MM月` |
130+
| hideArrow | 是否隐藏箭头 | `boolean` | `false` |
131+
| isVertical | 是否垂直滑动 | `boolean` | `false` |
132+
| isMultiSelect | 是否范围选择 | `boolean` | `false` |
132133

133134
## AtCalendar 事件
134135

packages/taro-ui/src/components/calendar/body/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export default class AtCalendarBody extends React.Component<
3939
public constructor(props: AtCalendarBodyProps) {
4040
super(props)
4141
const {
42+
disabledDate,
4243
validDates,
4344
marks,
4445
format,
@@ -50,6 +51,7 @@ export default class AtCalendarBody extends React.Component<
5051
} = props
5152

5253
this.generateFunc = generateCalendarGroup({
54+
disabledDate,
5355
validDates,
5456
format,
5557
minDate,
@@ -76,6 +78,7 @@ export default class AtCalendarBody extends React.Component<
7678
nextProps: AtCalendarBodyProps
7779
): void {
7880
const {
81+
disabledDate,
7982
validDates,
8083
marks,
8184
format,
@@ -87,6 +90,7 @@ export default class AtCalendarBody extends React.Component<
8790
} = nextProps
8891

8992
this.generateFunc = generateCalendarGroup({
93+
disabledDate,
9094
validDates,
9195
format,
9296
minDate,

packages/taro-ui/src/components/calendar/common/plugins.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,23 @@ export function handleDisabled(
100100
return item
101101
}
102102

103+
export function handleDisabledDate(
104+
args: PluginArg,
105+
item: Calendar.Item
106+
): Calendar.Item {
107+
const { options } = args
108+
const { value } = item
109+
const { disabledDate } = options
110+
111+
const curDate = dayjs(value)
112+
113+
if (value && disabledDate) {
114+
item.isDisabled = disabledDate(curDate)
115+
}
116+
117+
return item
118+
}
119+
103120
export function handleValid(
104121
args: PluginArg,
105122
item: Calendar.Item
@@ -121,4 +138,10 @@ export function handleValid(
121138
return item
122139
}
123140

124-
export default [handleActive, handleMarks, handleDisabled, handleValid]
141+
export default [
142+
handleActive,
143+
handleMarks,
144+
handleDisabled,
145+
handleValid,
146+
handleDisabledDate
147+
]

packages/taro-ui/src/components/calendar/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const defaultProps: AtCalendarDefaultProps = {
2323
isMultiSelect: false,
2424
format: 'YYYY-MM-DD',
2525
currentDate: Date.now(),
26-
monthFormat: 'YYYY年MM月'
26+
monthFormat: 'YYYY年MM月',
27+
disabledDate: () => false
2728
}
2829

2930
export default class AtCalendar extends React.Component<
@@ -283,7 +284,8 @@ export default class AtCalendar extends React.Component<
283284
hideArrow,
284285
isVertical,
285286
monthFormat,
286-
selectedDates
287+
selectedDates,
288+
disabledDate
287289
} = this.props as AtCalendarPropsWithDefaults
288290

289291
return (
@@ -299,6 +301,7 @@ export default class AtCalendar extends React.Component<
299301
onSelectDate={this.handleSelectDate}
300302
/>
301303
<AtCalendarBody
304+
disabledDate={disabledDate}
302305
validDates={validDates}
303306
marks={marks}
304307
format={format}

packages/taro-ui/types/calendar.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ declare namespace Calendar {
6464
minDate?: DateArg
6565

6666
maxDate?: DateArg
67+
68+
disabledDate?: (currentDate: dayjs.Dayjs) => boolean
6769
}
6870

6971
export type List<T> = Array<T>
@@ -150,6 +152,8 @@ export interface AtCalendarDefaultProps {
150152
isMultiSelect: boolean
151153

152154
selectedDates: Array<Calendar.SelectedDate>
155+
156+
disabledDate: (currentDate: dayjs.Dayjs) => boolean
153157
}
154158

155159
export interface AtCalendarState {
@@ -190,6 +194,8 @@ export type AtCalendarBodyListGroup = Array<Calendar.ListInfo<Calendar.Item>>
190194
export interface AtCalendarBodyProps {
191195
format: string
192196

197+
disabledDate: (currentDate: dayjs.Dayjs) => boolean
198+
193199
validDates: Array<Calendar.ValidDate>
194200

195201
marks: Array<Calendar.Mark>

0 commit comments

Comments
 (0)