Skip to content

Commit 61eeb14

Browse files
authored
feat: date picker custom format (#2276)
* Add function support for format * Add function proptype support for format in date-picker * Handle format not returning a string * Add function support to formatDate in vc-calendar * Add PropType.func to format props Co-authored-by: Herbert Lin <[email protected]>
1 parent 3f66cf4 commit 61eeb14

File tree

8 files changed

+31
-6
lines changed

8 files changed

+31
-6
lines changed

components/date-picker/interface.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const PickerProps = () => ({
77
transitionName: PropTypes.string,
88
prefixCls: PropTypes.string,
99
inputPrefixCls: PropTypes.string,
10-
format: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
10+
format: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.func]),
1111
disabled: PropTypes.bool,
1212
allowClear: PropTypes.bool,
1313
suffixIcon: PropTypes.any,

components/date-picker/utils.js

+12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
export function formatDate(value, format) {
2+
const isFunction = function(obj) {
3+
return !!(obj && obj.constructor && obj.call && obj.apply);
4+
};
5+
26
if (!value) {
37
return '';
48
}
59
if (Array.isArray(format)) {
610
format = format[0];
711
}
12+
if (isFunction(format)) {
13+
const result = format(value);
14+
if (typeof result === 'string') {
15+
return result;
16+
} else {
17+
throw new Error('The function of format does not return a string');
18+
}
19+
}
820
return value.format(format);
921
}

components/vc-calendar/src/Calendar.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const Calendar = {
2525
name: 'Calendar',
2626
props: {
2727
locale: PropTypes.object.def(enUs),
28-
format: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
28+
format: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string), PropTypes.func]),
2929
visible: PropTypes.bool.def(true),
3030
prefixCls: PropTypes.string.def('rc-calendar'),
3131
// prefixCls: PropTypes.string,

components/vc-calendar/src/FullCalendar.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const FullCalendar = {
1212
name: 'FullCalendar',
1313
props: {
1414
locale: PropTypes.object.def(enUs),
15-
format: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
15+
format: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.func]),
1616
visible: PropTypes.bool.def(true),
1717
prefixCls: PropTypes.string.def('rc-calendar'),
1818
defaultType: PropTypes.string.def('date'),

components/vc-calendar/src/Picker.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const Picker = {
2525
animation: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
2626
disabled: PropTypes.bool,
2727
transitionName: PropTypes.string,
28-
format: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
28+
format: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.func]),
2929
// onChange: PropTypes.func,
3030
// onOpenChange: PropTypes.func,
3131
children: PropTypes.func,

components/vc-calendar/src/RangeCalendar.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ const RangeCalendar = {
110110
// onValueChange: PropTypes.func,
111111
// onHoverChange: PropTypes.func,
112112
// onPanelChange: PropTypes.func,
113-
format: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
113+
format: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string), PropTypes.func]),
114114
// onClear: PropTypes.func,
115115
type: PropTypes.any.def('both'),
116116
disabledDate: PropTypes.func,

components/vc-calendar/src/date/DateInput.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const DateInput = {
1616
timePicker: PropTypes.object,
1717
value: PropTypes.object,
1818
disabledTime: PropTypes.any,
19-
format: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
19+
format: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string), PropTypes.func]),
2020
locale: PropTypes.object,
2121
disabledDate: PropTypes.func,
2222
// onChange: PropTypes.func,

components/vc-calendar/src/util/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ export function isAllowedDate(value, disabledDate, disabledTime) {
9292
}
9393

9494
export function formatDate(value, format) {
95+
const isFunction = function(obj) {
96+
return !!(obj && obj.constructor && obj.call && obj.apply);
97+
};
98+
9599
if (!value) {
96100
return '';
97101
}
@@ -100,5 +104,14 @@ export function formatDate(value, format) {
100104
format = format[0];
101105
}
102106

107+
if (isFunction(format)) {
108+
const result = format(value);
109+
if (typeof result === 'string') {
110+
return result;
111+
} else {
112+
throw new Error('The function of format does not return a string');
113+
}
114+
}
115+
103116
return value.format(format);
104117
}

0 commit comments

Comments
 (0)