Skip to content

Commit e339afc

Browse files
committed
feat(calendar): calendar增加disabledDate属性支持自定义禁用
1 parent cc82c1f commit e339afc

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

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

+7-8
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export default class Index extends React.Component<IndexProps, IndexState> {
3232
super(props)
3333
this.state = {
3434
now: Date.now(),
35-
minDate: '2018/06/11',
36-
maxDate: '2020/12/12',
35+
minDate: dayjs().startOf('month').format('YYYY-MM-DD'),
36+
maxDate: dayjs().endOf('month').format('YYYY-MM-DD'),
3737
multiCurentDate: {
3838
start: Date.now()
3939
},
@@ -212,13 +212,12 @@ export default class Index extends React.Component<IndexProps, IndexState> {
212212
<View className='panel__title'>禁用日期</View>
213213
<View className='panel__content'>
214214
<AtCalendar
215+
minDate={minDate}
216+
maxDate={maxDate}
215217
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-
)
218+
// 禁用周末
219+
const currentDayOfWeek = dayjs(currentDate).day()
220+
return [0, 6].includes(currentDayOfWeek)
222221
}}
223222
/>
224223
</View>

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

+20
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ import { AtCalendar } from "taro-ui"
105105

106106
:::
107107

108+
## 不可选择的日期
109+
110+
可以与`minDate``maxDate`一起使用
111+
112+
:::demo
113+
114+
```html
115+
<AtCalendar
116+
minDate={minDate}
117+
maxDate={maxDate}
118+
disabledDate={(currentDate: dayjs.Dayjs) => {
119+
// 禁用周末
120+
const currentDayOfWeek = dayjs(currentDate).day();
121+
return [0, 6].includes(currentDayOfWeek)
122+
}}
123+
/>
124+
```
125+
126+
:::
127+
108128
## AtCalendar 参数
109129

110130
```ts

packages/taro-ui/config/rollup.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import iconsMaker from './iconsMaker.js'
99

1010
iconsMaker('../rn/assets/iconfont.svg')
1111

12-
const resolveFile = path => NodePath.resolve(__dirname, '..', path)
12+
const resolveFile = path =>
13+
NodePath.resolve(__dirname, '..', path).split(NodePath.sep).join('/')
1314

1415
const externalPackages = [
1516
'react',

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

+7-27
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,18 @@ export function handleDisabled(
8787
item: Calendar.Item
8888
): Calendar.Item {
8989
const { options } = args
90-
const { _value } = item
91-
const { minDate, maxDate } = options
90+
const { _value, value } = item
91+
const { minDate, maxDate, disabledDate } = options
92+
93+
const dayjsItemDate = dayjs(value)
9294

9395
const dayjsMinDate = dayjs(minDate)
9496
const dayjsMaxDate = dayjs(maxDate)
9597

9698
item.isDisabled =
9799
!!(minDate && _value?.isBefore(dayjsMinDate)) ||
98-
!!(maxDate && _value?.isAfter(dayjsMaxDate))
99-
100-
return item
101-
}
102-
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-
}
100+
!!(maxDate && _value?.isAfter(dayjsMaxDate)) ||
101+
(disabledDate && disabledDate(dayjsItemDate))
116102

117103
return item
118104
}
@@ -138,10 +124,4 @@ export function handleValid(
138124
return item
139125
}
140126

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

0 commit comments

Comments
 (0)