diff --git a/.changeset/hot-fans-beg.md b/.changeset/hot-fans-beg.md new file mode 100644 index 000000000..971b6c242 --- /dev/null +++ b/.changeset/hot-fans-beg.md @@ -0,0 +1,5 @@ +--- +"@hi-ui/hiui": patch +--- + +feat(date-picker): 新增自定义触发器 diff --git a/.changeset/metal-squids-unite.md b/.changeset/metal-squids-unite.md new file mode 100644 index 000000000..8eecf3c0e --- /dev/null +++ b/.changeset/metal-squids-unite.md @@ -0,0 +1,5 @@ +--- +"@hi-ui/date-picker": minor +--- + +feat: 新增自定义触发器 diff --git a/packages/ui/date-picker/src/DatePicker.tsx b/packages/ui/date-picker/src/DatePicker.tsx index 8930de21b..c975ca2e0 100644 --- a/packages/ui/date-picker/src/DatePicker.tsx +++ b/packages/ui/date-picker/src/DatePicker.tsx @@ -78,6 +78,7 @@ export const DatePicker = forwardRef( cellRender, footerRender, strideSelectMode = 'auto', + customRender, prefix, needConfirm: needConfirmProp = false, onConfirm, @@ -85,7 +86,8 @@ export const DatePicker = forwardRef( }, ref ) => { - const i18n = useLocaleContext() + const i18n = useLocaleConte + xt() const locale = i18n.locale const [dateRangeTimePanelNow, setDateRangeTimePanelNow] = useState(0) @@ -461,6 +463,7 @@ export const DatePicker = forwardRef( setAttachEl={setAttachEl} dateRangeTimePanelNow={dateRangeTimePanelNow} invalid={invalid} + customRender={customRender} prefix={prefix} /> void @@ -24,6 +25,7 @@ const Root = ({ inputChangeEvent: InputChangeEvent dateRangeTimePanelNow: number invalid: boolean + customRender?: React.ReactNode | ((option: (Date | undefined)[]) => React.ReactNode) prefix: React.ReactNode }) => { const { @@ -135,7 +137,22 @@ const Root = ({ (invalid || !isValueValid) && `${prefixCls}__picker--invalid` ) - return ( + return customRender ? ( +
{ + if (renderRange) { + onPickerClickEvent(1) + } else { + onPickerClickEvent(0) + } + }} + > + {typeof customRender === 'function' + ? customRender(inputData.map((item) => item?.toDate())) + : customRender} +
+ ) : (
{prefix ? {prefix} : null} diff --git a/packages/ui/date-picker/src/types.ts b/packages/ui/date-picker/src/types.ts index baf728b1b..1bfa56dca 100644 --- a/packages/ui/date-picker/src/types.ts +++ b/packages/ui/date-picker/src/types.ts @@ -284,6 +284,9 @@ export interface DatePickerProps extends Omit, 'placehold */ strideSelectMode?: 'auto' | 'fixed' /** + * 自定义触发器 + */ + customRender?: React.ReactNode | ((option: (Date | undefined)[]) => React.ReactNode) * 选择框前置内容 */ prefix?: React.ReactNode diff --git a/packages/ui/date-picker/stories/custom-render.stories.tsx b/packages/ui/date-picker/stories/custom-render.stories.tsx new file mode 100644 index 000000000..c2d62756e --- /dev/null +++ b/packages/ui/date-picker/stories/custom-render.stories.tsx @@ -0,0 +1,62 @@ +import React from 'react' +import DatePicker from '../src' +import Input from '@hi-ui/input' + +/** + * @title 自定义触发器 + */ +export const CustomRender = () => { + return ( + <> +

日期

+
+

基础

+ { + console.log('onChange', date, dateStr) + }} + onSelect={console.log} + customRender={(data) => { + return + }} + /> +

日期时间范围

+ { + console.log('onChange', date, dateStr) + }} + defaultValue={[new Date(), new Date()]} + customRender={(data) => { + const date1 = new Date(data[0]) + const date2 = new Date(data[1]) + const year1 = date1.getFullYear() + const year2 = date2.getFullYear() + const month1 = (date1.getMonth() + 1).toString().padStart(2, '0') + const month2 = (date2.getMonth() + 1).toString().padStart(2, '0') + const day1 = date1.getDate().toString().padStart(2, '0') + const day2 = date2.getDate().toString().padStart(2, '0') + const hours1 = date1.getHours().toString().padStart(2, '0') + const hours2 = date2.getHours().toString().padStart(2, '0') + const minutes1 = date1.getMinutes().toString().padStart(2, '0') + const minutes2 = date2.getMinutes().toString().padStart(2, '0') + const seconds1 = date1.getSeconds().toString().padStart(2, '0') + const seconds2 = date2.getSeconds().toString().padStart(2, '0') + const formattedDateTime1 = `${year1}-${month1}-${day1} ${hours1}:${minutes1}:${seconds1}` + const formattedDateTime2 = `${year2}-${month2}-${day2} ${hours2}:${minutes2}:${seconds2}` + return ( + + ) + }} + /> +
+ + ) +} diff --git a/packages/ui/date-picker/stories/index.stories.tsx b/packages/ui/date-picker/stories/index.stories.tsx index 2de81f5a9..185bb2dc2 100644 --- a/packages/ui/date-picker/stories/index.stories.tsx +++ b/packages/ui/date-picker/stories/index.stories.tsx @@ -11,6 +11,7 @@ export * from './shortcut.stories' export * from './lunar.stories' export * from './size.stories' export * from './footer-render.stories' +export * from './custom-render.stories' export * from './addon.stories' export * from './need-confirm.stories'