Skip to content

Commit

Permalink
feat(date-picker): add footerRender api (#2630)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyprepare committed Oct 20, 2023
1 parent 28ef7b9 commit ddb8d0c
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-cameras-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/date-picker": minor
---

feat: add footerRender api
5 changes: 5 additions & 0 deletions .changeset/red-carrots-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

DatePicker feat: add footerRender api
5 changes: 5 additions & 0 deletions packages/ui/date-picker/src/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const DatePicker = forwardRef<HTMLDivElement | null, DatePickerProps>(
invalid = false,
onClose,
cellRender,
footerRender,
...otherProps
},
ref
Expand All @@ -86,13 +87,15 @@ export const DatePicker = forwardRef<HTMLDivElement | null, DatePickerProps>(
const cacheDate = useRef<(moment.Moment | null)[]>([])
const [inputFocus, setInputFocus] = useState(false)
const [type, setType] = useState<DatePickerTypeEnum>(propType)

useEffect(() => {
moment.locale(locale === 'en-US' ? 'en' : 'zh-CN')
// V4: 不使用 weekOffset 判断国际化语言
// if (weekOffset !== undefined) {
// moment.locale(weekOffset === 0 ? 'en' : 'zh-CN')
// }
}, [locale, weekOffset])

useEffect(() => {
setType(propType)
}, [propType])
Expand Down Expand Up @@ -354,6 +357,7 @@ export const DatePicker = forwardRef<HTMLDivElement | null, DatePickerProps>(
/>
)
}

return (
<div className={popperCls}>
{type.includes('range') || type === 'timeperiod' ? (
Expand Down Expand Up @@ -416,6 +420,7 @@ export const DatePicker = forwardRef<HTMLDivElement | null, DatePickerProps>(
isInDateRangeTimeMode,
size,
cellRender,
footerRender,
}}
>
<div className={cx(prefixCls, className)} {...otherProps}>
Expand Down
18 changes: 12 additions & 6 deletions packages/ui/date-picker/src/components/date-range-time-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface DateRangeTimePanelProps {

// 选择日期范围,并且希望选择时间
export const DateRangeTimePanel = (props: DateRangeTimePanelProps) => {
const { outDate, onPick, disabledDate, prefixCls } = useContext(DPContext)
const { outDate, onPick, disabledDate, prefixCls, footerRender } = useContext(DPContext)
const { nowIndex, onChangeNowIndex } = props

const [range, setRange] = useState<CalenderSelectedRange>({
Expand Down Expand Up @@ -92,6 +92,16 @@ export const DateRangeTimePanel = (props: DateRangeTimePanelProps) => {
return nowIndex === 0 ? !range.start : !range.end
}, [nowIndex, range])

const footer = useMemo(() => {
const sureActionContent = (
<HiButton type="primary" disabled={isConfirmButtonDisabled} onClick={onConfirmButtonClick}>
确认
</HiButton>
)

return typeof footerRender === 'function' ? footerRender(sureActionContent) : sureActionContent
}, [footerRender, isConfirmButtonDisabled, onConfirmButtonClick])

return (
<React.Fragment>
<Panel
Expand All @@ -100,11 +110,7 @@ export const DateRangeTimePanel = (props: DateRangeTimePanelProps) => {
outDate={panelData}
range={range}
/>
<div className={`${prefixCls}__date-range-time-bottom`}>
<HiButton type="primary" disabled={isConfirmButtonDisabled} onClick={onConfirmButtonClick}>
确认
</HiButton>
</div>
<div className={`${prefixCls}__date-range-time-bottom`}>{footer}</div>
</React.Fragment>
)
}
4 changes: 4 additions & 0 deletions packages/ui/date-picker/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,8 @@ export interface DatePickerProps extends Omit<HiBaseHTMLProps<'div'>, 'placehold
* 自定义单元格内容
*/
cellRender?: (colInfo: CalendarColInfo) => React.ReactNode
/**
* 自定义渲染页脚
*/
footerRender?: (...actionContents: React.ReactElement[]) => React.ReactNode
}
40 changes: 40 additions & 0 deletions packages/ui/date-picker/stories/footer-render.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react'
import DatePicker from '../src'
import Button from '@hi-ui/button'

/**
* @title 自定义渲染页脚
*/
export const FooterRender = () => {
return (
<>
<h1>FooterRender</h1>
<div className="date-picker-footer-render__wrap">
<DatePicker
style={{ width: 240 }}
type="daterange"
showTime
footerRender={(sureActionContent) => (
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
width: '100%',
}}
>
<Button type="secondary" appearance="link">
自定义操作
</Button>
{sureActionContent}
</div>
)}
onChange={(date, dateStr) => {
console.log('onChange', date, dateStr)
}}
onSelect={console.log}
/>
</div>
</>
)
}
1 change: 1 addition & 0 deletions packages/ui/date-picker/stories/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './range.stories'
export * from './shortcut.stories'
export * from './lunar.stories'
export * from './size.stories'
export * from './footer-render.stories'

export default {
title: 'Data Input/DatePicker',
Expand Down

0 comments on commit ddb8d0c

Please sign in to comment.