antd
Version:
An enterprise-class UI design language and React components implementation
55 lines (49 loc) • 2.04 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import * as React from 'react';
import useRCNotification from "rc-notification/es/useNotification";
import { ConfigConsumer } from '../../config-provider';
export default function createUseNotification(getNotificationInstance, getRCNoticeProps) {
var useNotification = function useNotification() {
// We can only get content by render
var getPrefixCls; // We create a proxy to handle delay created instance
var innerInstance = null;
var proxy = {
add: function add(noticeProps, holderCallback) {
innerInstance === null || innerInstance === void 0 ? void 0 : innerInstance.component.add(noticeProps, holderCallback);
}
};
var _useRCNotification = useRCNotification(proxy),
_useRCNotification2 = _slicedToArray(_useRCNotification, 2),
hookNotify = _useRCNotification2[0],
holder = _useRCNotification2[1];
function notify(args) {
var customizePrefixCls = args.prefixCls;
var mergedPrefixCls = getPrefixCls('notification', customizePrefixCls);
getNotificationInstance(_extends(_extends({}, args), {
prefixCls: mergedPrefixCls
}), function (_ref) {
var prefixCls = _ref.prefixCls,
instance = _ref.instance;
innerInstance = instance;
hookNotify(getRCNoticeProps(args, prefixCls));
});
} // Fill functions
var hookApiRef = React.useRef({});
hookApiRef.current.open = notify;
['success', 'info', 'warning', 'error'].forEach(function (type) {
hookApiRef.current[type] = function (args) {
return hookApiRef.current.open(_extends(_extends({}, args), {
type: type
}));
};
});
return [hookApiRef.current, /*#__PURE__*/React.createElement(ConfigConsumer, {
key: "holder"
}, function (context) {
getPrefixCls = context.getPrefixCls;
return holder;
})];
};
return useNotification;
}