From 831c81551890e73ded5ab3082671a3d74fca9316 Mon Sep 17 00:00:00 2001 From: Herbert Lin Date: Tue, 12 May 2020 23:07:15 +0800 Subject: [PATCH 1/5] Add function support for format --- components/date-picker/utils.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/components/date-picker/utils.js b/components/date-picker/utils.js index b55c28a58d..77f64fa480 100644 --- a/components/date-picker/utils.js +++ b/components/date-picker/utils.js @@ -1,3 +1,7 @@ +const isFunction = function(obj) { + return !!(obj && obj.constructor && obj.call && obj.apply); +}; + export function formatDate(value, format) { if (!value) { return ''; @@ -5,5 +9,8 @@ export function formatDate(value, format) { if (Array.isArray(format)) { format = format[0]; } + if (isFunction(format)) { + return format(value); + } return value.format(format); } From 62e4ee8da1bd37218c2128cd27a26a94a254db31 Mon Sep 17 00:00:00 2001 From: Herbert Lin Date: Tue, 19 May 2020 22:20:11 +0800 Subject: [PATCH 2/5] Add function proptype support for format in date-picker --- components/date-picker/interface.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/date-picker/interface.js b/components/date-picker/interface.js index 4791c2a5a2..c43ae99768 100644 --- a/components/date-picker/interface.js +++ b/components/date-picker/interface.js @@ -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, From 98115f9aa5c93a4dffe5c8a86522d0d118c066d7 Mon Sep 17 00:00:00 2001 From: Herbert Lin Date: Tue, 19 May 2020 22:29:01 +0800 Subject: [PATCH 3/5] Handle format not returning a string --- components/date-picker/utils.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/components/date-picker/utils.js b/components/date-picker/utils.js index 77f64fa480..a47a17e068 100644 --- a/components/date-picker/utils.js +++ b/components/date-picker/utils.js @@ -10,7 +10,12 @@ export function formatDate(value, format) { format = format[0]; } if (isFunction(format)) { - return format(value); + 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); } From 22676dff43e5c5272fc00c6039c434f47994b270 Mon Sep 17 00:00:00 2001 From: Herbert Lin Date: Wed, 20 May 2020 17:29:10 +0800 Subject: [PATCH 4/5] Add function support to formatDate in vc-calendar --- components/date-picker/utils.js | 8 ++++---- components/vc-calendar/src/util/index.js | 13 +++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/components/date-picker/utils.js b/components/date-picker/utils.js index a47a17e068..b2044c3138 100644 --- a/components/date-picker/utils.js +++ b/components/date-picker/utils.js @@ -1,8 +1,8 @@ -const isFunction = function(obj) { - return !!(obj && obj.constructor && obj.call && obj.apply); -}; - export function formatDate(value, format) { + const isFunction = function(obj) { + return !!(obj && obj.constructor && obj.call && obj.apply); + }; + if (!value) { return ''; } diff --git a/components/vc-calendar/src/util/index.js b/components/vc-calendar/src/util/index.js index 59665f68fb..cd7e4d1796 100644 --- a/components/vc-calendar/src/util/index.js +++ b/components/vc-calendar/src/util/index.js @@ -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 ''; } @@ -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); } From 5c0b57221f6970e774004a33de82ba9293859b96 Mon Sep 17 00:00:00 2001 From: Herbert Lin Date: Wed, 10 Jun 2020 18:53:31 +0800 Subject: [PATCH 5/5] Add PropType.func to format props --- components/vc-calendar/src/Calendar.jsx | 2 +- components/vc-calendar/src/FullCalendar.jsx | 2 +- components/vc-calendar/src/Picker.jsx | 2 +- components/vc-calendar/src/RangeCalendar.jsx | 2 +- components/vc-calendar/src/date/DateInput.jsx | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/components/vc-calendar/src/Calendar.jsx b/components/vc-calendar/src/Calendar.jsx index 06a06cfcef..1e6e73b985 100644 --- a/components/vc-calendar/src/Calendar.jsx +++ b/components/vc-calendar/src/Calendar.jsx @@ -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, diff --git a/components/vc-calendar/src/FullCalendar.jsx b/components/vc-calendar/src/FullCalendar.jsx index a1e04649d9..f385d3d54d 100644 --- a/components/vc-calendar/src/FullCalendar.jsx +++ b/components/vc-calendar/src/FullCalendar.jsx @@ -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'), diff --git a/components/vc-calendar/src/Picker.jsx b/components/vc-calendar/src/Picker.jsx index f1f9cc1a6f..1a9b5fdfa8 100644 --- a/components/vc-calendar/src/Picker.jsx +++ b/components/vc-calendar/src/Picker.jsx @@ -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, diff --git a/components/vc-calendar/src/RangeCalendar.jsx b/components/vc-calendar/src/RangeCalendar.jsx index 80ea948f7b..6b91fb6da9 100644 --- a/components/vc-calendar/src/RangeCalendar.jsx +++ b/components/vc-calendar/src/RangeCalendar.jsx @@ -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, diff --git a/components/vc-calendar/src/date/DateInput.jsx b/components/vc-calendar/src/date/DateInput.jsx index 21abf57706..94d8b927ae 100644 --- a/components/vc-calendar/src/date/DateInput.jsx +++ b/components/vc-calendar/src/date/DateInput.jsx @@ -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,