Skip to content

Commit

Permalink
feat: (Calendar) add allowClear prop
Browse files Browse the repository at this point in the history
  • Loading branch information
awmleer committed Mar 16, 2022
1 parent 2f6a928 commit d4513d0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/components/calendar/calendar.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
| onPageChange | Trigger when changed year or month. | `(year: number, month: number) => void` | - |
| weekStartsOn | Week starts on which day. | `'Monday' \| 'Sunday'` | `'Sunday'` |
| renderLabel | The label render function. | `(date: Date) => string \| null \| undefined` | - |
| allowClear | Whether to allow clearing after another click. | `boolean` | `true` |

### CSS Variables

Expand Down
23 changes: 17 additions & 6 deletions src/components/calendar/calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ export type CalenderRef = {
}

export type CalendarProps = {
onPageChange?: (year: number, month: number) => void
weekStartsOn?: 'Monday' | 'Sunday'
renderLabel?: (date: Date) => string | null | undefined
onPageChange?: (year: number, month: number) => void
allowClear?: boolean
} & (
| {
selectionMode?: undefined
Expand All @@ -56,6 +57,7 @@ export type CalendarProps = {
const defaultProps = {
weekStartsOn: 'Sunday',
defaultValue: null,
allowClear: true,
}

export const Calendar = forwardRef<CalenderRef, CalendarProps>((p, ref) => {
Expand Down Expand Up @@ -194,16 +196,28 @@ export const Calendar = forwardRef<CalenderRef, CalendarProps>((p, ref) => {
onClick={() => {
if (!props.selectionMode) return
const date = d.toDate()
if (!inThisMonth) {
setCurrent(d.clone().date(1))
}
function shouldClear() {
if (!props.allowClear) return false
if (!dateRange) return false
const [begin, end] = dateRange
return d.isSame(begin, 'date') && d.isSame(end, 'day')
}
if (props.selectionMode === 'single') {
if (props.allowClear && shouldClear()) {
setDateRange(null)
return
}
setDateRange([date, date])
} else if (props.selectionMode === 'range') {
if (!dateRange) {
setDateRange([date, date])
setIntermediate(true)
return
}
const [begin, end] = dateRange
if (d.isSame(begin, 'date') && d.isSame(end, 'day')) {
if (shouldClear()) {
setDateRange(null)
setIntermediate(false)
return
Expand All @@ -217,9 +231,6 @@ export const Calendar = forwardRef<CalenderRef, CalendarProps>((p, ref) => {
setIntermediate(true)
}
}
if (!inThisMonth) {
setCurrent(d.clone().date(1))
}
}}
>
<div className={`${classPrefix}-cell-top`}>{d.date()}</div>
Expand Down
1 change: 1 addition & 0 deletions src/components/calendar/calendar.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
| onPageChange | 切换月或年时触发 | `(year: number, month: number) => void` | - |
| weekStartsOn | 每周以周几作为第一天 | `'Monday' \| 'Sunday'` | `'Sunday'` |
| renderLabel | 标注信息的渲染函数 | `(date: Date) => string \| null \| undefined` | - |
| allowClear | 是否允许再次点击后清除 | `boolean` | `true` |

### CSS 变量

Expand Down

0 comments on commit d4513d0

Please sign in to comment.