From 0d03666eeb671d0839d1dc89327d8226dfeb3a05 Mon Sep 17 00:00:00 2001 From: wzc520pyfm <1528857653@qq.com> Date: Mon, 23 Jun 2025 22:24:12 +0800 Subject: [PATCH 1/3] feat: support creating different format value by range index --- src/PickerInput/Selector/hooks/useInputProps.ts | 5 +++-- src/interface.tsx | 2 +- src/utils/dateUtil.ts | 4 +++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/PickerInput/Selector/hooks/useInputProps.ts b/src/PickerInput/Selector/hooks/useInputProps.ts index 251777982..8a7d34272 100644 --- a/src/PickerInput/Selector/hooks/useInputProps.ts +++ b/src/PickerInput/Selector/hooks/useInputProps.ts @@ -86,7 +86,8 @@ export default function useInputProps( const firstFormat = format[0]; const getText = React.useCallback( - (date: DateType) => formatValue(date, { locale, format: firstFormat, generateConfig }), + (date: DateType, index: number) => + formatValue(date, { locale, index, format: firstFormat, generateConfig }), [locale, generateConfig, firstFormat], ); @@ -97,7 +98,7 @@ export default function useInputProps( const defaultSize = picker === 'time' ? 8 : 10; const length = typeof firstFormat === 'function' - ? firstFormat(generateConfig.getNow()).length + ? firstFormat(generateConfig.getNow(), 0).length : firstFormat.length; return Math.max(defaultSize, length) + 2; }, [firstFormat, picker, generateConfig]); diff --git a/src/interface.tsx b/src/interface.tsx index 2fe3dae7d..ed8fb82b2 100644 --- a/src/interface.tsx +++ b/src/interface.tsx @@ -281,7 +281,7 @@ export type Components = Partial< >; // ========================= Picker ========================= -export type CustomFormat = (value: DateType) => string; +export type CustomFormat = (value: DateType, index: number) => string; export type FormatType = string | CustomFormat; diff --git a/src/utils/dateUtil.ts b/src/utils/dateUtil.ts index 7508e067e..4a72e1ff9 100644 --- a/src/utils/dateUtil.ts +++ b/src/utils/dateUtil.ts @@ -232,10 +232,12 @@ export function formatValue( generateConfig, locale, format, + index = 0, }: { generateConfig: GenerateConfig; locale: Locale; format: string | CustomFormat; + index?: number; }, ) { if (!value) { @@ -243,7 +245,7 @@ export function formatValue( } return typeof format === 'function' - ? format(value) + ? format(value, index) : generateConfig.locale.format(locale.locale, value, format); } From abd1bba4b8c082b23d0ca83b38b89e822f42c499 Mon Sep 17 00:00:00 2001 From: wzc520pyfm <1528857653@qq.com> Date: Mon, 23 Jun 2025 22:24:32 +0800 Subject: [PATCH 2/3] docs: add custom format demo for range --- docs/examples/range.tsx | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/examples/range.tsx b/docs/examples/range.tsx index 1b137c8b0..4ab06f468 100644 --- a/docs/examples/range.tsx +++ b/docs/examples/range.tsx @@ -202,6 +202,17 @@ export default () => { disabledDate={disabledDate} /> +
+

Custom Format

+ + {...sharedProps} + locale={zhCN} + picker="year" + format={(date, index) => { + return index === 0 ? date.format('YYYY') : date.format('YYYY-MM-DD'); + }} + /> +
); From 82093690e635dbeccdecb7ef3b64ffaed374aaa7 Mon Sep 17 00:00:00 2001 From: wzc520pyfm <1528857653@qq.com> Date: Wed, 25 Jun 2025 21:42:04 +0800 Subject: [PATCH 3/3] fix: fix type err --- src/PickerInput/Selector/hooks/useInputProps.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PickerInput/Selector/hooks/useInputProps.ts b/src/PickerInput/Selector/hooks/useInputProps.ts index 8a7d34272..1574c3980 100644 --- a/src/PickerInput/Selector/hooks/useInputProps.ts +++ b/src/PickerInput/Selector/hooks/useInputProps.ts @@ -86,7 +86,7 @@ export default function useInputProps( const firstFormat = format[0]; const getText = React.useCallback( - (date: DateType, index: number) => + (date: DateType, index?: number) => formatValue(date, { locale, index, format: firstFormat, generateConfig }), [locale, generateConfig, firstFormat], );