@alicloud/console-components
Version:
Alibaba Cloud React Components
35 lines (34 loc) • 1.54 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import React, { forwardRef, useMemo } from 'react';
import moment from 'moment';
import hoistNonReactStatics from 'hoist-non-react-statics';
// eslint-disable-next-line @typescript-eslint/naming-convention
export default function hocWithDefaultFormat(WrappedComponent) {
// eslint-disable-next-line react/display-name
var Wrapper = forwardRef(function (props, ref) {
var _a;
var defaultFormat = useMemo(function () {
return moment().localeData().longDateFormat('ll'); // 从全局 moment 对象获取当前的 moment 文案
}, []);
// 从props的moment对象获取当前的moment文案
var value = props.value, defaultValue = props.defaultValue;
var exactValue = value || defaultValue;
if (moment.isMoment(exactValue)) {
defaultFormat = exactValue.localeData().longDateFormat('ll');
}
var format = (_a = props.format) !== null && _a !== void 0 ? _a : defaultFormat;
return React.createElement(WrappedComponent, __assign({}, props, { format: format, ref: ref }));
});
hoistNonReactStatics(Wrapper, WrappedComponent);
return Wrapper;
}