diff --git a/.changeset/silver-ads-jog.md b/.changeset/silver-ads-jog.md new file mode 100644 index 000000000..35eab000f --- /dev/null +++ b/.changeset/silver-ads-jog.md @@ -0,0 +1,5 @@ +--- +"@hi-ui/date-picker": minor +--- + +feat: Add needConfirm and onConfirm api diff --git a/.changeset/slow-bottles-talk.md b/.changeset/slow-bottles-talk.md new file mode 100644 index 000000000..693f7c86b --- /dev/null +++ b/.changeset/slow-bottles-talk.md @@ -0,0 +1,5 @@ +--- +"@hi-ui/hiui": patch +--- + +feat(date-picker): Add needConfirm and onConfirm api diff --git a/packages/ui/date-picker/src/DatePicker.tsx b/packages/ui/date-picker/src/DatePicker.tsx index cdda14389..8930de21b 100644 --- a/packages/ui/date-picker/src/DatePicker.tsx +++ b/packages/ui/date-picker/src/DatePicker.tsx @@ -79,6 +79,8 @@ export const DatePicker = forwardRef( footerRender, strideSelectMode = 'auto', prefix, + needConfirm: needConfirmProp = false, + onConfirm, ...otherProps }, ref @@ -92,6 +94,14 @@ export const DatePicker = forwardRef( const [inputFocus, setInputFocus] = useState(false) const [type, setType] = useState(propType) + const needConfirm = useMemo(() => { + // 如果是日期时间范围选择,则默认返回 true + if (type === 'daterange' && showTime) { + return true + } + return needConfirmProp + }, [needConfirmProp, showTime, type]) + useEffect(() => { moment.locale(locale === 'en-US' ? 'en' : 'zh-CN') // V4: 不使用 weekOffset 判断国际化语言 @@ -301,12 +311,10 @@ export const DatePicker = forwardRef( setInputFocus(false) }, []) - const isInDateRangeTimeMode = useMemo(() => type === 'daterange' && showTime, [type, showTime]) - const onPopperClose = useCallback(() => { resetStatus() - if (!isInDateRangeTimeMode) { + if (!needConfirm) { const outDateValue = outDate[0] const isValid = moment(outDateValue).isValid() // @ts-ignore @@ -322,7 +330,7 @@ export const DatePicker = forwardRef( } onClose?.() - }, [resetStatus, outDate, isInDateRangeTimeMode, onClose, max, min, callback, changeOutDate]) + }, [callback, changeOutDate, max, min, needConfirm, onClose, outDate, resetStatus]) const onClear = () => { resetStatus() @@ -349,8 +357,11 @@ export const DatePicker = forwardRef( type === 'timeperiod' && `${prefixCls}__popper--timeperiod`, shortcuts && `${prefixCls}__popper--shortcuts` ) + const [attachEl, setAttachEl] = useState(null) + const isInDateRangeTimeMode = useMemo(() => type === 'daterange' && showTime, [type, showTime]) + const popContent = useMemo(() => { // 日期时间范围选择器特殊处理 if (isInDateRangeTimeMode) { @@ -367,7 +378,13 @@ export const DatePicker = forwardRef( {type.includes('range') || type === 'timeperiod' ? ( ) : ( - + )} ) @@ -378,6 +395,8 @@ export const DatePicker = forwardRef( onPick, outDate, disabledDate, + needConfirm, + onConfirm, dateRangeTimePanelNow, ]) 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 36b834a21..4b988259c 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 @@ -1,11 +1,10 @@ import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react' -import { useLocaleContext } from '@hi-ui/core' -import HiButton from '@hi-ui/button' import DPContext, { DPContextData } from '../context' import Panel from './panel' import { CalenderSelectedRange } from '../hooks/useCalenderData' import moment from 'moment' import { CalendarViewEnum } from '../types' +import { Footer } from './footer' interface DateRangeTimePanelProps { nowIndex: number @@ -14,8 +13,7 @@ interface DateRangeTimePanelProps { // 选择日期范围,并且希望选择时间 export const DateRangeTimePanel = (props: DateRangeTimePanelProps) => { - const i18n = useLocaleContext() - const { outDate, onPick, disabledDate, prefixCls, footerRender } = useContext(DPContext) + const { outDate, onPick, disabledDate } = useContext(DPContext) const { nowIndex, onChangeNowIndex } = props const [range, setRange] = useState({ @@ -94,16 +92,6 @@ export const DateRangeTimePanel = (props: DateRangeTimePanelProps) => { return nowIndex === 0 ? !range.start : !range.end }, [nowIndex, range]) - const footer = useMemo(() => { - const sureActionContent = ( - - {i18n.get('datePicker.ok')} - - ) - - return typeof footerRender === 'function' ? footerRender(sureActionContent) : sureActionContent - }, [footerRender, i18n, isConfirmButtonDisabled, onConfirmButtonClick]) - return ( { outDate={panelData} range={range} /> -
{footer}
+