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(date-picker): add footerRender api (#2630) #2634

Merged
merged 2 commits into from
Oct 23, 2023
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
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,
strideSelectMode = 'auto',
...otherProps
},
Expand All @@ -87,13 +88,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 @@ -355,6 +358,7 @@ export const DatePicker = forwardRef<HTMLDivElement | null, DatePickerProps>(
/>
)
}

return (
<div className={popperCls}>
{type.includes('range') || type === 'timeperiod' ? (
Expand Down Expand Up @@ -417,6 +421,7 @@ export const DatePicker = forwardRef<HTMLDivElement | null, DatePickerProps>(
isInDateRangeTimeMode,
size,
cellRender,
footerRender,
strideSelectMode,
}}
>
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 @@ -262,6 +262,10 @@ export interface DatePickerProps extends Omit<HiBaseHTMLProps<'div'>, 'placehold
* 自定义单元格内容
*/
cellRender?: (colInfo: CalendarColInfo) => React.ReactNode
/**
* 自定义渲染页脚
*/
footerRender?: (...actionContents: React.ReactElement[]) => React.ReactNode
/**
* 跨月选择模式
* 'auto' 自动切换模式,跨月选择时自动切换到跨月的日期选择面板;
Expand Down
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