From ddb8d0c8b8140f4984b17cf92ac58aed5e8b4ca4 Mon Sep 17 00:00:00 2001 From: zhouyun1 Date: Fri, 20 Oct 2023 18:09:08 +0800 Subject: [PATCH] feat(date-picker): add footerRender api (#2630) --- .changeset/popular-cameras-burn.md | 5 +++ .changeset/red-carrots-jam.md | 5 +++ packages/ui/date-picker/src/DatePicker.tsx | 5 +++ .../src/components/date-range-time-panel.tsx | 18 ++++++--- packages/ui/date-picker/src/types.ts | 4 ++ .../stories/footer-render.stories.tsx | 40 +++++++++++++++++++ .../ui/date-picker/stories/index.stories.tsx | 1 + 7 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 .changeset/popular-cameras-burn.md create mode 100644 .changeset/red-carrots-jam.md create mode 100644 packages/ui/date-picker/stories/footer-render.stories.tsx diff --git a/.changeset/popular-cameras-burn.md b/.changeset/popular-cameras-burn.md new file mode 100644 index 000000000..a925baa8a --- /dev/null +++ b/.changeset/popular-cameras-burn.md @@ -0,0 +1,5 @@ +--- +"@hi-ui/date-picker": minor +--- + +feat: add footerRender api diff --git a/.changeset/red-carrots-jam.md b/.changeset/red-carrots-jam.md new file mode 100644 index 000000000..35f846dd8 --- /dev/null +++ b/.changeset/red-carrots-jam.md @@ -0,0 +1,5 @@ +--- +"@hi-ui/hiui": patch +--- + +DatePicker feat: add footerRender api diff --git a/packages/ui/date-picker/src/DatePicker.tsx b/packages/ui/date-picker/src/DatePicker.tsx index 4a9a61045..52f48951d 100644 --- a/packages/ui/date-picker/src/DatePicker.tsx +++ b/packages/ui/date-picker/src/DatePicker.tsx @@ -74,6 +74,7 @@ export const DatePicker = forwardRef( invalid = false, onClose, cellRender, + footerRender, ...otherProps }, ref @@ -86,6 +87,7 @@ export const DatePicker = forwardRef( const cacheDate = useRef<(moment.Moment | null)[]>([]) const [inputFocus, setInputFocus] = useState(false) const [type, setType] = useState(propType) + useEffect(() => { moment.locale(locale === 'en-US' ? 'en' : 'zh-CN') // V4: 不使用 weekOffset 判断国际化语言 @@ -93,6 +95,7 @@ export const DatePicker = forwardRef( // moment.locale(weekOffset === 0 ? 'en' : 'zh-CN') // } }, [locale, weekOffset]) + useEffect(() => { setType(propType) }, [propType]) @@ -354,6 +357,7 @@ export const DatePicker = forwardRef( /> ) } + return (
{type.includes('range') || type === 'timeperiod' ? ( @@ -416,6 +420,7 @@ export const DatePicker = forwardRef( isInDateRangeTimeMode, size, cellRender, + footerRender, }} >
diff --git a/packages/ui/date-picker/src/components/date-range-time-panel.tsx b/packages/ui/date-picker/src/components/date-range-time-panel.tsx index e2a6b317a..b795e6cbb 100644 --- a/packages/ui/date-picker/src/components/date-range-time-panel.tsx +++ b/packages/ui/date-picker/src/components/date-range-time-panel.tsx @@ -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({ @@ -92,6 +92,16 @@ export const DateRangeTimePanel = (props: DateRangeTimePanelProps) => { return nowIndex === 0 ? !range.start : !range.end }, [nowIndex, range]) + const footer = useMemo(() => { + const sureActionContent = ( + + 确认 + + ) + + return typeof footerRender === 'function' ? footerRender(sureActionContent) : sureActionContent + }, [footerRender, isConfirmButtonDisabled, onConfirmButtonClick]) + return ( { outDate={panelData} range={range} /> -
- - 确认 - -
+
{footer}
) } diff --git a/packages/ui/date-picker/src/types.ts b/packages/ui/date-picker/src/types.ts index c71e040e7..29b3b36b6 100644 --- a/packages/ui/date-picker/src/types.ts +++ b/packages/ui/date-picker/src/types.ts @@ -261,4 +261,8 @@ export interface DatePickerProps extends Omit, 'placehold * 自定义单元格内容 */ cellRender?: (colInfo: CalendarColInfo) => React.ReactNode + /** + * 自定义渲染页脚 + */ + footerRender?: (...actionContents: React.ReactElement[]) => React.ReactNode } diff --git a/packages/ui/date-picker/stories/footer-render.stories.tsx b/packages/ui/date-picker/stories/footer-render.stories.tsx new file mode 100644 index 000000000..e11ab41d9 --- /dev/null +++ b/packages/ui/date-picker/stories/footer-render.stories.tsx @@ -0,0 +1,40 @@ +import React from 'react' +import DatePicker from '../src' +import Button from '@hi-ui/button' + +/** + * @title 自定义渲染页脚 + */ +export const FooterRender = () => { + return ( + <> +

FooterRender

+
+ ( +
+ + {sureActionContent} +
+ )} + onChange={(date, dateStr) => { + console.log('onChange', date, dateStr) + }} + onSelect={console.log} + /> +
+ + ) +} diff --git a/packages/ui/date-picker/stories/index.stories.tsx b/packages/ui/date-picker/stories/index.stories.tsx index 839c882c3..3ec21ef85 100644 --- a/packages/ui/date-picker/stories/index.stories.tsx +++ b/packages/ui/date-picker/stories/index.stories.tsx @@ -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',