Skip to content

Commit 0d03666

Browse files
committed
feat: support creating different format value by range index
1 parent d3dc263 commit 0d03666

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

src/PickerInput/Selector/hooks/useInputProps.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ export default function useInputProps<DateType extends object = any>(
8686
const firstFormat = format[0];
8787

8888
const getText = React.useCallback(
89-
(date: DateType) => formatValue(date, { locale, format: firstFormat, generateConfig }),
89+
(date: DateType, index: number) =>
90+
formatValue(date, { locale, index, format: firstFormat, generateConfig }),
9091
[locale, generateConfig, firstFormat],
9192
);
9293

@@ -97,7 +98,7 @@ export default function useInputProps<DateType extends object = any>(
9798
const defaultSize = picker === 'time' ? 8 : 10;
9899
const length =
99100
typeof firstFormat === 'function'
100-
? firstFormat(generateConfig.getNow()).length
101+
? firstFormat(generateConfig.getNow(), 0).length
101102
: firstFormat.length;
102103
return Math.max(defaultSize, length) + 2;
103104
}, [firstFormat, picker, generateConfig]);

src/interface.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export type Components<DateType extends object = any> = Partial<
281281
>;
282282

283283
// ========================= Picker =========================
284-
export type CustomFormat<DateType> = (value: DateType) => string;
284+
export type CustomFormat<DateType> = (value: DateType, index: number) => string;
285285

286286
export type FormatType<DateType = any> = string | CustomFormat<DateType>;
287287

src/utils/dateUtil.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,20 @@ export function formatValue<DateType>(
232232
generateConfig,
233233
locale,
234234
format,
235+
index = 0,
235236
}: {
236237
generateConfig: GenerateConfig<DateType>;
237238
locale: Locale;
238239
format: string | CustomFormat<DateType>;
240+
index?: number;
239241
},
240242
) {
241243
if (!value) {
242244
return '';
243245
}
244246

245247
return typeof format === 'function'
246-
? format(value)
248+
? format(value, index)
247249
: generateConfig.locale.format(locale.locale, value, format);
248250
}
249251

0 commit comments

Comments
 (0)