Skip to content

Commit

Permalink
feat(date-picker): cellRender增加date参数 (#2834)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyprepare authored Jul 15, 2024
1 parent 8f888cb commit 969abb8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-monkeys-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

feat(date-picker): cellRender 增加 date 参数
5 changes: 5 additions & 0 deletions .changeset/kind-bikes-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/date-picker": minor
---

feat: cellRender 增加 date 参数
66 changes: 49 additions & 17 deletions packages/ui/date-picker/src/components/calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,19 @@ const Calendar = ({
return _week
}

const onTableClick = (e: React.MouseEvent<HTMLTableElement, MouseEvent>) => {
const td = e.target as HTMLTableElement
const value = td.getAttribute('value') as string
const isBelongFullOurOfRange = td.getAttribute('belong-full-out-of-range') === 'true'

if (!['SPAN', 'DIV'].includes(td.nodeName) || td.getAttribute('type') === 'disabled') {
return false
}

const clickVal = parseInt(value)
const getDateByValue = ({
value,
cellType,
cellWeekType,
isBelongFullOurOfRange,
}: {
value: string | number
cellType: string
cellWeekType: string
isBelongFullOurOfRange: boolean
}) => {
const _date = moment(renderDate) as any
const cellType = td.getAttribute('type')
const cellWeekType = td.getAttribute('weektype')
const _value = typeof value === 'string' ? parseInt(value) : value

if (type !== 'weekrange' || isBelongFullOurOfRange) {
if (cellType === 'prev' || cellWeekType === 'prev') {
Expand All @@ -136,10 +136,10 @@ const Calendar = ({
}

if (type.includes('quarter')) {
_date.quarter(clickVal)
_date.quarter(_value)
}

_date[view](clickVal)
_date[view](_value)
} else {
// 点击的上个月的部分,鼠标还在本月的panel上,则视作,鼠标正在本月第一天
if (cellType === 'prev') {
Expand All @@ -149,11 +149,33 @@ const Calendar = ({
else if (cellType === 'next') {
_date.set('date', _date.endOf('month').date())
} else {
_date[view](clickVal)
_date[view](_value)
}
}

onPick(_date)
return _date
}

const onTableClick = (e: React.MouseEvent<HTMLTableElement, MouseEvent>) => {
const td = e.target as HTMLTableElement
const value = td.getAttribute('value') as string
const isBelongFullOurOfRange = td.getAttribute('belong-full-out-of-range') === 'true'

if (!['SPAN', 'DIV'].includes(td.nodeName) || td.getAttribute('type') === 'disabled') {
return false
}

const cellType = td.getAttribute('type')!
const cellWeekType = td.getAttribute('weektype')!

const parsedDate = getDateByValue({
value,
cellType,
cellWeekType,
isBelongFullOurOfRange,
})

onPick(parsedDate)
}

const onTableMouseMove = (e: React.MouseEvent<HTMLTableElement, MouseEvent>) => {
Expand Down Expand Up @@ -357,7 +379,17 @@ const Calendar = ({
// @ts-ignore
belong-full-out-of-range={isBelongFullOutOfRange}
>
{cellRender ? cellRender(cell) : cellValue}
{cellRender
? cellRender(
cell,
getDateByValue({
value: cellValue,
cellType: cell.type,
cellWeekType: cell.weekType,
isBelongFullOurOfRange: isBelongFullOutOfRange === 'true',
})
)
: cellValue}
</span>
{renderAltCalendar(cell, isBelongFullOutOfRange)}
</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/date-picker/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export interface DatePickerProps extends Omit<HiBaseHTMLProps<'div'>, 'placehold
/**
* 自定义单元格内容
*/
cellRender?: (colInfo: CalendarColInfo) => React.ReactNode
cellRender?: (colInfo: CalendarColInfo, date: Moment) => React.ReactNode
/**
* 自定义渲染页脚
*/
Expand Down

0 comments on commit 969abb8

Please sign in to comment.