Skip to content

Commit

Permalink
refactor: optimize code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Layouwen committed Oct 23, 2024
1 parent 7b5bf48 commit 5866c28
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions src/components/calendar-picker-view/calendar-picker-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ export const CalendarPickerView = forwardRef<
)

const showHeader = props.title !== false
const showTop = props.renderTop !== false
const showBottom = props.renderBottom !== false

// =============================== Scroll ===============================
const context = useContext(Context)
Expand Down Expand Up @@ -247,33 +245,41 @@ export const CalendarPickerView = forwardRef<
(minDay && d.isBefore(minDay, 'day'))

const renderTop = () => {
const top = (
props.renderTop as CalendarPickerViewColumRender
)?.(d.toDate())
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
contentWrapper(locale.Calendar.start)
}

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

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

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

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

return (
Expand Down Expand Up @@ -329,21 +335,13 @@ export const CalendarPickerView = forwardRef<
}
}}
>
{showTop && (
<div className={`${classPrefix}-cell-top`}>
{renderTop()}
</div>
)}
{renderTop()}
<div className={`${classPrefix}-cell-date`}>
{props.renderDate
? props.renderDate(d.toDate())
: d.date()}
</div>
{showBottom && (
<div className={`${classPrefix}-cell-bottom`}>
{renderBottom()}
</div>
)}
{renderBottom()}
</div>
)
})}
Expand Down

0 comments on commit 5866c28

Please sign in to comment.