Skip to content

feat: date picker custom format #2276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
2 changes: 1 addition & 1 deletion components/date-picker/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const PickerProps = () => ({
transitionName: PropTypes.string,
prefixCls: PropTypes.string,
inputPrefixCls: PropTypes.string,
format: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
format: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.func]),
disabled: PropTypes.bool,
allowClear: PropTypes.bool,
suffixIcon: PropTypes.any,
Expand Down
12 changes: 12 additions & 0 deletions components/date-picker/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
export function formatDate(value, format) {
const isFunction = function(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
};

if (!value) {
return '';
}
if (Array.isArray(format)) {
format = format[0];
}
if (isFunction(format)) {
const result = format(value);
if (typeof result === 'string') {
return result;
} else {
throw new Error('The function of format does not return a string');
}
}
return value.format(format);
}
2 changes: 1 addition & 1 deletion components/vc-calendar/src/Calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const getMomentObjectIfValid = date => {
const Calendar = {
props: {
locale: PropTypes.object.def(enUs),
format: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
format: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string), PropTypes.func]),
visible: PropTypes.bool.def(true),
prefixCls: PropTypes.string.def('rc-calendar'),
// prefixCls: PropTypes.string,
Expand Down
2 changes: 1 addition & 1 deletion components/vc-calendar/src/FullCalendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import enUs from './locale/en_US';
const FullCalendar = {
props: {
locale: PropTypes.object.def(enUs),
format: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
format: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.func]),
visible: PropTypes.bool.def(true),
prefixCls: PropTypes.string.def('rc-calendar'),
defaultType: PropTypes.string.def('date'),
Expand Down
2 changes: 1 addition & 1 deletion components/vc-calendar/src/Picker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const Picker = {
animation: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
disabled: PropTypes.bool,
transitionName: PropTypes.string,
format: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
format: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.func]),
// onChange: PropTypes.func,
// onOpenChange: PropTypes.func,
children: PropTypes.func,
Expand Down
2 changes: 1 addition & 1 deletion components/vc-calendar/src/RangeCalendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const RangeCalendar = {
// onValueChange: PropTypes.func,
// onHoverChange: PropTypes.func,
// onPanelChange: PropTypes.func,
format: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
format: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string), PropTypes.func]),
// onClear: PropTypes.func,
type: PropTypes.any.def('both'),
disabledDate: PropTypes.func,
Expand Down
2 changes: 1 addition & 1 deletion components/vc-calendar/src/date/DateInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const DateInput = {
timePicker: PropTypes.object,
value: PropTypes.object,
disabledTime: PropTypes.any,
format: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
format: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string), PropTypes.func]),
locale: PropTypes.object,
disabledDate: PropTypes.func,
// onChange: PropTypes.func,
Expand Down
13 changes: 13 additions & 0 deletions components/vc-calendar/src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export function isAllowedDate(value, disabledDate, disabledTime) {
}

export function formatDate(value, format) {
const isFunction = function(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
};

if (!value) {
return '';
}
Expand All @@ -100,5 +104,14 @@ export function formatDate(value, format) {
format = format[0];
}

if (isFunction(format)) {
const result = format(value);
if (typeof result === 'string') {
return result;
} else {
throw new Error('The function of format does not return a string');
}
}

return value.format(format);
}