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(CalendarPickerView): add renderTop and renderBottom hide logic #6735

Merged
merged 7 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ Only the simplest content area is shown here, and other more usages can be consu
| max | Maximum value of a selectable range. | `Date` | - |
| min | Minimum value of a selectable range. | `Date` | - | - |
| onChange | Trigger when selected date changes. | `(val: Date \| null) => void` when selection mode is "single". `(val: [Date, Date] \| null) => void` when selection mode is "range". | - |
| renderTop | The top information of date render function. | `(date: Date) => ReactNode \| null \| undefined` | - |
| renderBottom | The bottom information of date render function. | `(date: Date) => ReactNode \| null \| undefined` | - |
| renderTop | The top information of date render function. | `((date: Date) => ReactNode \| null \| undefined) \| false` | - | `false`: 5.38.0 |
| renderBottom | The bottom information of date render function. | `((date: Date) => ReactNode \| null \| undefined) \| false` | - | `false`: 5.38.0 |
| selectionMode | The selection mode. Disable selection when this prop is not set. | `'single' \| 'range'` | - |
| shouldDisableDate | Set whether the date is disable selection. The min and max Settings are ignored | `(date: Date) => boolean` | - |
| title | The title of calendar | `React.ReactNode` | `Date selection` |
Expand Down
43 changes: 30 additions & 13 deletions src/components/calendar-picker-view/calendar-picker-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@
getDateRange: () => DateRange
}

export type CalendarPickerViewColumRender = (date: Date) => ReactNode

export type CalendarPickerViewProps = {
title?: React.ReactNode | false
confirmText?: string
weekStartsOn?: 'Monday' | 'Sunday'
renderTop?: (date: Date) => React.ReactNode
renderDate?: (date: Date) => React.ReactNode
renderBottom?: (date: Date) => React.ReactNode
renderTop?: CalendarPickerViewColumRender | false
renderDate?: CalendarPickerViewColumRender
renderBottom?: CalendarPickerViewColumRender | false
allowClear?: boolean
max?: Date
min?: Date
Expand Down Expand Up @@ -243,26 +245,45 @@
(minDay && d.isBefore(minDay, 'day'))

const renderTop = () => {
if (props.renderTop === false) return null

const contentWrapper = (content: ReactNode) => (
<div className={`${classPrefix}-cell-top`}>{content}</div>
)

const top = props.renderTop?.(d.toDate())

if (top) {
return top
return contentWrapper(top)
}

if (props.selectionMode === 'range') {
if (isBegin) {
return locale.Calendar.start
return contentWrapper(locale.Calendar.start)
}

if (isEnd) {
return locale.Calendar.end
return contentWrapper(locale.Calendar.end)
}
}

if (d.isSame(today, 'day') && !isSelect) {
return locale.Calendar.today
return contentWrapper(locale.Calendar.today)
}

return contentWrapper(null)
}

const renderBottom = () => {
if (props.renderBottom === false) return null

return (
<div className={`${classPrefix}-cell-bottom`}>
{props.renderBottom?.(d.toDate())}
</div>
)
}

return (
<div
key={d.valueOf()}
Expand Down Expand Up @@ -316,17 +337,13 @@
}
}}
>
<div className={`${classPrefix}-cell-top`}>
{renderTop()}
</div>
{renderTop()}
<div className={`${classPrefix}-cell-date`}>
{props.renderDate
? props.renderDate(d.toDate())
: d.date()}
</div>
<div className={`${classPrefix}-cell-bottom`}>
{props.renderBottom?.(d.toDate())}
</div>
{renderBottom()}

Check warning on line 346 in src/components/calendar-picker-view/calendar-picker-view.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/calendar-picker-view/calendar-picker-view.tsx#L346

Added line #L346 was not covered by tests
</div>
)
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ CalendarPickerView 是 [CalendarPicker](/zh/components/calendar-picker) 的内
| max | 可选择范围的最大值 | `Date` | - |
| min | 可选择范围的最小值 | `Date` | - |
| onChange | 选择日期变化时触发 | 单选模式下为 `(val: Date \| null) => void`,多选模式下为 `(val: [Date, Date] \| null) => void` | - |
| renderTop | 日期顶部信息的渲染函数 | `(date: Date) => ReactNode \| null \| undefined` | - |
| renderBottom | 日期底部信息的渲染函数 | `(date: Date) => ReactNode \| null \| undefined` | - |
| renderTop | 日期顶部信息的渲染函数 | `((date: Date) => ReactNode \| null \| undefined) \| false` | - | `false`: 5.38.0 |
| renderBottom | 日期底部信息的渲染函数 | `((date: Date) => ReactNode \| null \| undefined) \| false` | - | `false`: 5.38.0 |
| selectionMode | 选择模式,不设置的话表示不支持选择 | `'single' \| 'range'` | - |
| shouldDisableDate | 判断日期是否可选,使用后会忽略 min 和 max 设置 | `(date: Date) => boolean` | - |
| title | 日期选择器的标题 | `React.ReactNode` | `日期选择` |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ describe('Calendar', () => {
expect(document.querySelector(`.${classPrefix}-header`)).toBeNull()
})

test('renderTop hidden', () => {
render(<CalendarPickerView renderTop={false} />)

expect(document.querySelector(`.${classPrefix}-cell-top`)).toBeNull()
})

test('renderBottom hidden', () => {
render(<CalendarPickerView renderBottom={false} />)

expect(document.querySelector(`.${classPrefix}-cell-bottom`)).toBeNull()
})

test('not fill empty cells if unnecessary', () => {
const { container } = render(
<CalendarPickerView
Expand Down
Loading