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 <herbert.lin.7@gmail.com>pull/1775/head
parent
3f66cf4044
commit
61eeb14859
|
@ -7,7 +7,7 @@ export const PickerProps = () => ({
|
||||||
transitionName: PropTypes.string,
|
transitionName: PropTypes.string,
|
||||||
prefixCls: PropTypes.string,
|
prefixCls: PropTypes.string,
|
||||||
inputPrefixCls: PropTypes.string,
|
inputPrefixCls: PropTypes.string,
|
||||||
format: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
|
format: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.func]),
|
||||||
disabled: PropTypes.bool,
|
disabled: PropTypes.bool,
|
||||||
allowClear: PropTypes.bool,
|
allowClear: PropTypes.bool,
|
||||||
suffixIcon: PropTypes.any,
|
suffixIcon: PropTypes.any,
|
||||||
|
|
|
@ -1,9 +1,21 @@
|
||||||
export function formatDate(value, format) {
|
export function formatDate(value, format) {
|
||||||
|
const isFunction = function(obj) {
|
||||||
|
return !!(obj && obj.constructor && obj.call && obj.apply);
|
||||||
|
};
|
||||||
|
|
||||||
if (!value) {
|
if (!value) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
if (Array.isArray(format)) {
|
if (Array.isArray(format)) {
|
||||||
format = format[0];
|
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);
|
return value.format(format);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ const Calendar = {
|
||||||
name: 'Calendar',
|
name: 'Calendar',
|
||||||
props: {
|
props: {
|
||||||
locale: PropTypes.object.def(enUs),
|
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),
|
visible: PropTypes.bool.def(true),
|
||||||
prefixCls: PropTypes.string.def('rc-calendar'),
|
prefixCls: PropTypes.string.def('rc-calendar'),
|
||||||
// prefixCls: PropTypes.string,
|
// prefixCls: PropTypes.string,
|
||||||
|
|
|
@ -12,7 +12,7 @@ const FullCalendar = {
|
||||||
name: 'FullCalendar',
|
name: 'FullCalendar',
|
||||||
props: {
|
props: {
|
||||||
locale: PropTypes.object.def(enUs),
|
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),
|
visible: PropTypes.bool.def(true),
|
||||||
prefixCls: PropTypes.string.def('rc-calendar'),
|
prefixCls: PropTypes.string.def('rc-calendar'),
|
||||||
defaultType: PropTypes.string.def('date'),
|
defaultType: PropTypes.string.def('date'),
|
||||||
|
|
|
@ -25,7 +25,7 @@ const Picker = {
|
||||||
animation: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
|
animation: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
|
||||||
disabled: PropTypes.bool,
|
disabled: PropTypes.bool,
|
||||||
transitionName: PropTypes.string,
|
transitionName: PropTypes.string,
|
||||||
format: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
|
format: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.func]),
|
||||||
// onChange: PropTypes.func,
|
// onChange: PropTypes.func,
|
||||||
// onOpenChange: PropTypes.func,
|
// onOpenChange: PropTypes.func,
|
||||||
children: PropTypes.func,
|
children: PropTypes.func,
|
||||||
|
|
|
@ -110,7 +110,7 @@ const RangeCalendar = {
|
||||||
// onValueChange: PropTypes.func,
|
// onValueChange: PropTypes.func,
|
||||||
// onHoverChange: PropTypes.func,
|
// onHoverChange: PropTypes.func,
|
||||||
// onPanelChange: 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,
|
// onClear: PropTypes.func,
|
||||||
type: PropTypes.any.def('both'),
|
type: PropTypes.any.def('both'),
|
||||||
disabledDate: PropTypes.func,
|
disabledDate: PropTypes.func,
|
||||||
|
|
|
@ -16,7 +16,7 @@ const DateInput = {
|
||||||
timePicker: PropTypes.object,
|
timePicker: PropTypes.object,
|
||||||
value: PropTypes.object,
|
value: PropTypes.object,
|
||||||
disabledTime: PropTypes.any,
|
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,
|
locale: PropTypes.object,
|
||||||
disabledDate: PropTypes.func,
|
disabledDate: PropTypes.func,
|
||||||
// onChange: PropTypes.func,
|
// onChange: PropTypes.func,
|
||||||
|
|
|
@ -92,6 +92,10 @@ export function isAllowedDate(value, disabledDate, disabledTime) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatDate(value, format) {
|
export function formatDate(value, format) {
|
||||||
|
const isFunction = function(obj) {
|
||||||
|
return !!(obj && obj.constructor && obj.call && obj.apply);
|
||||||
|
};
|
||||||
|
|
||||||
if (!value) {
|
if (!value) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
@ -100,5 +104,14 @@ export function formatDate(value, format) {
|
||||||
format = format[0];
|
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);
|
return value.format(format);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue