Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/examples/range.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,17 @@ export default () => {
disabledDate={disabledDate}
/>
</div>
<div style={{ margin: '0 8px' }}>
<h3>Custom Format</h3>
<RangePicker<Moment>
{...sharedProps}
locale={zhCN}
picker="year"
format={(date, index) => {
return index === 0 ? date.format('YYYY') : date.format('YYYY-MM-DD');
}}
/>
</div>
</div>
</div>
);
Expand Down
5 changes: 3 additions & 2 deletions src/PickerInput/Selector/hooks/useInputProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export default function useInputProps<DateType extends object = any>(
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],
);

Expand All @@ -97,7 +98,7 @@ export default function useInputProps<DateType extends object = any>(
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]);
Expand Down
2 changes: 1 addition & 1 deletion src/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export type Components<DateType extends object = any> = Partial<
>;

// ========================= Picker =========================
export type CustomFormat<DateType> = (value: DateType) => string;
export type CustomFormat<DateType> = (value: DateType, index: number) => string;

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

Expand Down
4 changes: 3 additions & 1 deletion src/utils/dateUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,20 @@ export function formatValue<DateType>(
generateConfig,
locale,
format,
index = 0,
}: {
generateConfig: GenerateConfig<DateType>;
locale: Locale;
format: string | CustomFormat<DateType>;
index?: number;
},
) {
if (!value) {
return '';
}

return typeof format === 'function'
? format(value)
? format(value, index)
: generateConfig.locale.format(locale.locale, value, format);
}

Expand Down